Solved! Go to Solution.
Solved by Balaji_Ram. Go to Solution.
Well, I'm guessing there was an error introduced by the copy/paste operation, as this needed correcting:
pl.AddVertexAt(i, pts.TransformBy(mat2d), 0, 0, 0);
to this:
pl.AddVertexAt(i, pts[j].TransformBy(mat2d), 0, 0, 0);
But once that was done, it appears to work fine as far as the rotation goes. It's not centering the view on the polyline or the blockreference, but I'm not sure if it's supposed to be.
In this image, the dashed line is the edge of the drawing canvas when the created view is is current. But, the UCS and code generated polyline appear to match the rotation of the water valve block which was inserted with a rotation of 33 degrees.
Hope that helps you track down what you are looking for.
Hi thanks for the reply not sure how that happened? however part I need help with is how to center the view within the polyline. this is what happens when I edit the boundaries of the named view create "100 m"
the Yellow line is the block, the white is the polyline created on the VM command and the black rectangle is the boundaries of "100 m" view?
I want this to be centered within the polyline?
If I remove the vtr.viewtwist line the view is centered wwithin the polyline but the rotations out....if I uncomment this line I get the above result.
Any ideas?
Hi,
The CenterPoint of the ViewtableRecord is to be provided in DCS. In the below sample code, I set the view width and height for the current view. I then perform the transformation of the center point from UCS to DCS and set it as the centerpoint of the viewtable record.
[CommandMethod("VM")] public static void NamedView() { // Test Values for Named View string viewname = "100 m"; double width = 420; double height = 297; double angle = 0; Document doc = Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Database db = doc.Database; using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead); BlockTableRecord ms = (BlockTableRecord)tr.GetObject( bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); // Catch last entity (MVF Block) and align UCS ObjectId ent = Autodesk.AutoCAD.Internal.Utils.EntLast(); if (ent.ObjectClass.DxfName == "INSERT") { BlockReference blk = (BlockReference)tr.GetObject(ent, OpenMode.ForRead); ed.CurrentUserCoordinateSystem = blk.BlockTransform; angle = blk.Rotation; } else { ed.WriteMessage("Last entity was not a block reference"); return; } // Get Current coordinate system CoordinateSystem3d cs = ed.CurrentUserCoordinateSystem.CoordinateSystem3d; Point3d ucsOrigin = cs.Origin; // Used later for View Origin creation //================================================================== // Create New User UCS //================================================================== UcsTable ut = (UcsTable)tr.GetObject(db.UcsTableId, OpenMode.ForRead); UcsTableRecord utr; if (ut.Has(viewname)) { utr = (UcsTableRecord)tr.GetObject( ut[viewname], OpenMode.ForWrite); } else { utr = new UcsTableRecord(); utr.Name = viewname; ut.UpgradeOpen(); ut.Add(utr); tr.AddNewlyCreatedDBObject(utr, true); } utr.Origin = ucsOrigin; utr.XAxis = cs.Xaxis; utr.YAxis = cs.Yaxis; ViewportTableRecord vptr = tr.GetObject(doc.Editor.ActiveViewportId, OpenMode.ForWrite) as ViewportTableRecord; vptr.IconAtOrigin = true; vptr.IconEnabled = true; vptr.SetUcs(utr.ObjectId); doc.Editor.UpdateTiledViewportsFromDatabase(); //================================================================== // Create View //================================================================== ViewTable vt = (ViewTable)tr.GetObject(db.ViewTableId, OpenMode.ForRead); ViewTableRecord vtr; // Check if View Name exists if (vt.Has(viewname)) { vtr = tr.GetObject(vt[viewname], OpenMode.ForWrite) as ViewTableRecord; } else { vtr = new ViewTableRecord(); vtr.Name = viewname; vt.UpgradeOpen(); ObjectId vtId = vt.Add(vtr); tr.AddNewlyCreatedDBObject(vtr, true); } // Calculate center point if view double centerX = width / 2; double centerY = height / 2; Point2d ctr = new Point2d(centerX, centerY); //Convert to ucs origin to Point2d Point2d viewOrigin = new Point2d(ucsOrigin.X, ucsOrigin.Y); //Create a rotation Matrix for X and Y Axis Matrix2d mat = Matrix2d.Rotation(angle, Point2d.Origin); Matrix2d mat2d = new Matrix2d(); mat2d = Matrix2d.AlignCoordinateSystem( Point2d.Origin, Vector2d.XAxis, Vector2d.YAxis, viewOrigin, new Vector2d(1, 0).TransformBy(mat), new Vector2d(0, 1).TransformBy(mat) ); vtr.ViewTwist = -angle; vtr.IsPaperspaceView = false; vtr.SetUcs(utr.ObjectId); vtr.Height = height; vtr.Width = width; // Set new view to current ed.SetCurrentView(vtr); ////////////////////////////////////////////////////////////////////////////////////////////////////////////// //// Set the CenterPoint of the VTR in DCS ResultBuffer ucs = new ResultBuffer(new TypedValue(5003, 1)); // UCS ResultBuffer dcs = new ResultBuffer(new TypedValue(5003, 2)); // DCS Point3d viewCenter3d_ucs = new Point3d(ctr.X, ctr.Y, 0.0); double[] fromPt = viewCenter3d_ucs.ToArray(); double[] toPt = new double[] { 0, 0, 0 }; acedTrans(fromPt, ucs.UnmanagedObject, dcs.UnmanagedObject, 0, toPt); Point3d viewCenter3d_dcs = new Point3d(toPt[0], toPt[1], toPt[2]); vtr.CenterPoint = new Point2d(viewCenter3d_dcs.X, viewCenter3d_dcs.Y); ////////////////////////////////////////////////////////////////////////////////////////////////////////////// //================================================================== // Create Polyline test geometry //================================================================== // Defined bouding box to new View Point2d pt1 = new Point2d(0, 0); Point2d pt2 = new Point2d(width, 0); Point2d pt3 = new Point2d(width, height); Point2d pt4 = new Point2d(0, height); Point2d[] pts = { pt1, pt2, pt3, pt4 }; // Define Polyline Vertices Polyline pl = new Polyline(4); int max = pts.GetUpperBound(0); for (int i = 0; i <= max; i++) { int j = (i == max ? 0 : i + 1); pl.AddVertexAt(i, pts[j].TransformBy(mat2d), 0, 0, 0); } pl.Closed = true; // Add the boundary to modelspace ObjectId id = ms.AppendEntity(pl); tr.AddNewlyCreatedDBObject(pl, true); tr.Commit(); } }
Same idea, slightly different approach for anyone interested
Matrix3d WCS2DCS =
Matrix3d
.PlaneToWorld(vtr.ViewDirection)
.PreMultiplyBy(Matrix3d.Displacement(vtr.Target - Point3d.Origin))
.PreMultiplyBy(Matrix3d.Rotation(-vtr.ViewTwist, vtr.ViewDirection, vtr.Target))
.Inverse();
Point3d viewCenter = blk.Position.TransformBy(WCS2DCS);
vtr.CenterPoint = new Point2d(viewCenter.X + width / 2.0, viewCenter.Y + height / 2.0);
vtr.SetUcs(utr.ObjectId);
Can't find what you're looking for? Ask the community or share your knowledge.