Message 1 of 4
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi:
I have a problem.
There are multiple viewports in a layout.
Now I want to copy the elements in the viewport to the current layout by selecting the viewport.
The effect is like the command chspace.
However, for some reason, I cannot use the chspace command.
I also tried to use the GetPaperToModelTransform method to convert elements,
but the elements were only scaled and did not come to the current layout.
[CommandMethod("TIV")]
public void TIV()
{
Document doc = null;
Database db = null;
Editor ed = null;
try
{
doc = AcadApp.DocumentManager.MdiActiveDocument;
db = doc.Database;
ed = doc.Editor;
LayoutManager layoutManager = LayoutManager.Current;
string layoutName = layoutManager.CurrentLayout;
ObjectId layoutId = layoutManager.GetLayoutId(layoutName);
ObjectId vpId = ObjectId.Null;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Layout layout = (Layout)tr.GetObject(layoutId, OpenMode.ForRead);
var vpIds = layout.GetViewports();
if (vpIds.Count > 0)
vpId = vpIds[1]; // First Viewport is Paperspace itself. Only one Viewport will be evaluated.
tr.Commit();
}
if (vpId.IsNull)
throw new System.Exception("No Viewport found in: " + layoutName);
List<Point3d> vpOutlinePntsInMs = null;
Viewport vp = null;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
vp = (Viewport)tr.GetObject(vpId, OpenMode.ForRead);
//For now w'll assume the Viewport outline is a Polyline.
Polyline vpOutlineInMs = null;
if (vp.NonRectClipOn)
{
ObjectId vpClipId = vp.NonRectClipEntityId;
Entity vpBoundary = (Entity)tr.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-2012 Tony Tanzillo
Point3d center = new Point3d(vp.ViewCenter.X, vp.ViewCenter.Y, 0.0);
Matrix3d msToPs = Matrix3d.Displacement(new Vector3d(vp.CenterPoint.X - center.X, vp.CenterPoint.Y - center.Y, 0.0))
* Matrix3d.Scaling(vp.CustomScale, center)
* Matrix3d.Rotation(vp.TwistAngle, Vector3d.ZAxis, Point3d.Origin)
* Matrix3d.WorldToPlane(new Plane(vp.ViewTarget, vp.ViewDirection));
vpOutlineInMs.TransformBy(msToPs.Inverse());
vpOutlinePntsInMs = new List<Point3d>();
for (int i = 0; i < vpOutlineInMs.NumberOfVertices; i++)
vpOutlinePntsInMs.Add(vpOutlineInMs.GetPoint3dAt(i));
List<Entity> lst = new List<Entity>();
var ms = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
foreach (ObjectId entId in ms)
{
Entity ent = (Entity)tr.GetObject(entId, OpenMode.ForRead);
var entExtents = ent.GeometricExtents;
//For now the entity is inside when all Extent points are.
var pt1 = ent.GeometricExtents.MinPoint;
var pt2 = new Point3d(ent.GeometricExtents.MaxPoint.X, ent.GeometricExtents.MinPoint.Y, 0);
var pt3 = ent.GeometricExtents.MaxPoint;
var pt4 = new Point3d(ent.GeometricExtents.MinPoint.X, ent.GeometricExtents.MaxPoint.Y, 0);
if (IsInside2D(vpOutlinePntsInMs.ToArray(), pt1) && IsInside2D(vpOutlinePntsInMs.ToArray(), pt2) &&
IsInside2D(vpOutlinePntsInMs.ToArray(), pt2) && IsInside2D(vpOutlinePntsInMs.ToArray(), pt4))
{
ed.WriteMessage("\n Entity in Viewport: {0} ({1})", entId.Handle, entId.ObjectClass.DxfName);
lst.Add(ent);
}
}
vp.UpgradeOpen();
lst.ModelToPaper(vp);
vp.DowngradeOpen();
tr.Commit();
}
}
catch (System.Exception ex)
{
if (ed != null)
ed.WriteMessage("\n Error: " + ex.Message);
}
}
public static void ModelToPaper(this List<Entity> src, Viewport viewport)
{
Matrix3d xform = GetModelToPaperTransform(viewport);
foreach (Entity ent in src)
{
ent.UpgradeOpen();
ent.TransformBy(xform);
ent.DowngradeOpen();
}
}
public static Matrix3d GetModelToPaperTransform(this Viewport vport)
{
if (vport.PerspectiveOn)
throw new NotSupportedException("不支持透视图");
Point3d center = new Point3d(vport.ViewCenter.X, vport.ViewCenter.Y, 0.0);
Vector3d v = new Vector3d(vport.CenterPoint.X - center.X, vport.CenterPoint.Y - center.Y, 0.0);
return Matrix3d.Displacement(v)
* Matrix3d.Scaling(vport.CustomScale, center)
* Matrix3d.Rotation(vport.TwistAngle, Vector3d.ZAxis, Point3d.Origin)
* Matrix3d.WorldToPlane(new Plane(vport.ViewTarget, vport.ViewDirection));
}
Solved! Go to Solution.