Community
i have problem in (view port.viewcenter=center of (rectangle from model space)
the center in view port view is not the same in model space
here the code
PromptEntityOptions opt = new PromptEntityOptions("\nSelect a polyline ");
opt.SetRejectMessage("\nObject must be a polyline.");
opt.AddAllowedClass(typeof(Autodesk.AutoCAD.DatabaseServices.Polyline), false);
PromptEntityResult res = ed.GetEntity(opt);
Autodesk.AutoCAD.DatabaseServices.ObjectId polyid = res.ObjectId;
Polyline poly = ts.GetObject(polyid, OpenMode.ForWrite) as Polyline;
string layername = poly.Layer;
TypedValue[] filter = new TypedValue[1];
filter.SetValue(new TypedValue((int)DxfCode.LayerName, layername), 0);
SelectionFilter selectionFilter = new SelectionFilter(filter);
PromptSelectionResult res0 = ed.SelectAll(selectionFilter);
SelectionSet selectedpolys = res0.Value;
if (res0.Status != PromptStatus.OK)
{
return;
}
List<Polyline> shapes = new List<Polyline>();
foreach (SelectedObject X in selectedpolys)
{
Polyline polyline = ts.GetObject(X.ObjectId, OpenMode.ForWrite) as Polyline;
if (polyline != null && polyline.NumberOfVertices == 4 && polyline.Closed)
{
List<double> angles = new List<double>();
for (int i = 0; i < 3; i++)
{
Vector3d v1 = polyline.GetLineSegmentAt(i).Direction;
Vector3d v2 = polyline.GetLineSegmentAt((i + 1)).Direction;
double angle = v1.GetAngleTo(v2);
angles.Add(Math.Abs(angle - Math.PI / 2));
}
List<double> angles1 = angles.Distinct().ToList();
if (angles1.Count == 1 && angles1[0] == 0)
{
shapes.Add(polyline);
}
}
}
int counter = lays.Count;
foreach (Polyline x in shapes)
{
LayoutManager.Current.CloneLayout("Layout1", "Layout1" + " " + "(" + counter.ToString() + ")", counter);
LayoutManager.Current.CurrentLayout = "Layout1" + " " + "(" + counter.ToString() + ")";
double angle= x.GetLineSegment2dAt(0).Direction.Angle;
Point2d starpoint= x.GetLineSegment2dAt(0).StartPoint;
Point2d endpoint= x.GetLineSegment2dAt(1).EndPoint;
Point2d center = new Point2d((starpoint.X + endpoint.X) / 2, (starpoint.Y + endpoint.Y) / 2);
Point3d centerpoint = new Point3d((starpoint.X + endpoint.X) / 2, (starpoint.Y + endpoint.Y) / 2,0);
List<Layout> lays = new List<Layout>();
DBDictionary layouts =
ts.GetObject(db.LayoutDictionaryId,
OpenMode.ForRead) as DBDictionary;
foreach (DBDictionaryEntry x1 in layouts)
{
Layout layout = ts.GetObject(x1.Value, OpenMode.ForRead) as Layout;
lays.Add(layout);
}
Layout layout1 = lays.Where(l => l.LayoutName == "Layout1" + " " + "(" + counter.ToString() + ")").ToList()[0];
Autodesk.AutoCAD.DatabaseServices.ObjectId vpid = layout1.GetViewports()[1];
Viewport vp = ts.GetObject(vpid, OpenMode.ForWrite) as Viewport;
vp.On = true;
vp.ViewCenter = center;
vp.ViewDirection = Vector3d.ZAxis;
vp.UpdateDisplay();
counter++;
}
Solved! Go to Solution.
Solved by _gile. Go to Solution.
Hi,
You have to transform points coordinates from WCS (world Coordinate System) into the viewport DCS (Display Coordinate System). you can use the Viewport extension methods from the GeometryExtensions library.
You can also read this topic.
Can't find what you're looking for? Ask the community or share your knowledge.