安排模型空间,如纸空间问题
我编写了一个方法,可以在模型空间中移动一些外部参照,复制它们在图纸空间布局中的排列。大多数情况下,它都运行良好。但是,脚本下面的示例文件会失败。你可以看到下面的预期结果和冗杂的结果。
public void tecFixMs()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
LayoutManager layoutMan = LayoutManager.Current;
using (Transaction Tx = db.TransactionManager.StartTransaction())
{
DBDictionary layoutDict = (DBDictionary)db.LayoutDictionaryId.GetObject(OpenMode.ForWrite);
BlockTableRecord ms = (BlockTableRecord)Tx.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
foreach (DBDictionaryEntry entry in layoutDict)
{
string layoutName = entry.Key;
if (layoutName == "Layout1")
{
ed.WriteMessage("---------------");
Point3d basept = new Point3d(0, 0, 0);
//Point3d lastbasept = new Point3d(0, 0, 0);
Layout layoutObj = (Layout)layoutMan.GetLayoutId(layoutName).GetObject(OpenMode.ForWrite);
ed.WriteMessage("{0} - Viewports: {1}\n", layoutObj.LayoutName, layoutObj.GetViewports().Count);
ObjectIdCollection initObj = layoutObj.GetViewports();
Vector3d oshift = new Vector3d(0, 0, 0);
Point3d lastvpcpt = new Point3d(0, 0, 0);
int counter = 0;
Point3d psbasept = new Point3d(0, 0, 0);
Point3d msbasept = new Point3d(0, 0, 0);
foreach (ObjectId vpId in initObj)
{
Viewport vp = (Viewport)vpId.GetObject(OpenMode.ForWrite);
ed.WriteMessage("VP # - {0} - center: {1} view center: {2}\n", vp.Number, vp.CenterPoint, vp.ViewCenter);
Polyline vpOutlineInMs = null;
if (vp.NonRectClipOn)
{
ObjectId vpClipId = vp.NonRectClipEntityId;
Entity vpBoundary = (Entity)Tx.GetObject(vpClipId, OpenMode.ForRead);
vpOutlineInMs = (Polyline)vpBoundary.Clone();
}
else
{
Extents3d vpExt = vp.GeometricExtents;
vpOutlineInMs = new Polyline(4);
vpOutlineInMs.AddVertexAt(0, new Point2d(vpExt.MinPoint.X, vpExt.MinPoint.Y), 0, 0, 0);
vpOutlineInMs.AddVertexAt(1, new Point2d(vpExt.MaxPoint.X, vpExt.MinPoint.Y), 0, 0, 0);
vpOutlineInMs.AddVertexAt(2, new Point2d(vpExt.MaxPoint.X, vpExt.MaxPoint.Y), 0, 0, 0);
vpOutlineInMs.AddVertexAt(3, new Point2d(vpExt.MinPoint.X, vpExt.MaxPoint.Y), 0, 0, 0);
vpOutlineInMs.Closed = true;
}
// ViewportExtensionMethods.cs(c) 2007-2012Tony Tanzillo
Point3d vpMScpt = new Point3d(vp.ViewCenter.X, vp.ViewCenter.Y, 0.0);
Point3d vpPScpt = vp.CenterPoint;
Matrix3d msToPs = Matrix3d.Displacement(new Vector3d(vp.CenterPoint.X - vpMScpt.X, vp.CenterPoint.Y - vpMScpt.Y, 0.0))
* Matrix3d.Scaling(vp.CustomScale, vpMScpt)
* Matrix3d.Rotation(vp.TwistAngle, Vector3d.ZAxis, Point3d.Origin)
* Matrix3d.WorldToPlane(new Plane(vp.ViewTarget, vp.ViewDirection));
vpOutlineInMs.TransformBy(msToPs.Inverse());
foreach (ObjectId entId in ms)
{
Entity ent = (Entity)Tx.GetObject(entId, OpenMode.ForWrite);
try
{
Extents3d entExtents = ent.GeometricExtents;
Point3d cpt = new Point3d(entExtents.MinPoint.X + (entExtents.MaxPoint.X - entExtents.MinPoint.X) / 2, entExtents.MinPoint.Y + (entExtents.MaxPoint.Y - entExtents.MinPoint.Y) / 2, 0);
if (IsInside2D(vpOutlineInMs, cpt))
{
Matrix3d vptargettransform = new Matrix3d();
if (counter == 0)
{
counter = 1;
vptargettransform = Matrix3d.Displacement(vpMScpt.GetVectorTo(msbasept));
}
else
{
Matrix3d bpmat = Matrix3d.Displacement(psbasept.GetVectorTo(vpPScpt).TransformBy(Matrix3d.Scaling(1 / vp.CustomScale, vpPScpt)));
msbasept = msbasept.TransformBy(bpmat);
vptargettransform = Matrix3d.Displacement(vpMScpt.GetVectorTo(msbasept));
}
ent.TransformBy(vptargettransform);
vp.ViewTarget = vp.ViewTarget.TransformBy(vptargettransform); ;
//msbasept = vpMScpt;
psbasept = vpPScpt;
}
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
}
}
}
}
}
Tx.Commit();
}
}
**** Hidden Message *****
页:
[1]