I am trying to make a button such that if the user clicks it, AutoCAD will automatically draw a border that just includes all entities in the drawing. (Something like the view port border in the paperspace)
Approach #1
The first approach could be zoom extent and draw a border around the zoomed view. But for this, I know how to zoom extent and draw a polyline, but can't figure out the exact size of zoomed view.
Approach #2 (optimal)
If the entity is a right circle and my AutoCAD window is not in square shape (for example, the AutoCAD window is maximized and my monitor is wide-scrren monitor), Approach #1 will create redundant spaces at the right and left side of the circle entity. So if I could get the grid coordinate of far left, far right, top, and bottom entities, I can draw the optimal border around all entities in the drawing.
If anyone knows the trick and can teach me, it would be very appreciated. 🙂
Solved! Go to Solution.
Solved by _gile. Go to Solution.
Hi,
You can use the Extents3d structure with the Extents3d.AddBlockExtents() method.
[CommandMethod("Test", CommandFlags.Modal)] public void Test() { Database db = HostApplicationServices.WorkingDatabase; using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTableRecord model = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite); Extents3d ext = new Extents3d(); ext.AddBlockExtents(model); using (Polyline pline = new Polyline()) { pline.AddVertexAt(0, new Point2d(ext.MinPoint.X, ext.MinPoint.Y), 0.0, 0.0, 0.0); pline.AddVertexAt(0, new Point2d(ext.MaxPoint.X, ext.MinPoint.Y), 0.0, 0.0, 0.0); pline.AddVertexAt(0, new Point2d(ext.MaxPoint.X, ext.MaxPoint.Y), 0.0, 0.0, 0.0); pline.AddVertexAt(0, new Point2d(ext.MinPoint.X, ext.MaxPoint.Y), 0.0, 0.0, 0.0); pline.Closed = true; model.AppendEntity(pline); tr.AddNewlyCreatedDBObject(pline, true); } tr.Commit(); } }
You can try use the same as _gile posted
just with selection a piece of drawing screen,
something like this (thanks _gile)
[CommandMethod("DPB")]// by fixo public void testDrawSelectionBounds() { Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Database db = doc.Database; try { // select some objects SelectionSet sset = ed.GetSelection().Value; if (sset == null) return; using (Transaction tr = doc.TransactionManager.StartTransaction()) { ed.WriteMessage("\nSelection Count: ", sset.Count); Extents3d ext = new Extents3d(); // iterate through selected objects foreach (ObjectId id in sset.GetObjectIds()) { Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead, false); Extents3d entxt = ent.GeometricExtents; if (entxt != null) { ext.AddExtents(entxt); } } // get current space for write BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); using (Polyline poly = new Polyline()) { poly.AddVertexAt(0, new Point2d(ext.MinPoint.X, ext.MinPoint.Y), 0.0, 0.0, 0.0); poly.AddVertexAt(0, new Point2d(ext.MaxPoint.X, ext.MinPoint.Y), 0.0, 0.0, 0.0); poly.AddVertexAt(0, new Point2d(ext.MaxPoint.X, ext.MaxPoint.Y), 0.0, 0.0, 0.0); poly.AddVertexAt(0, new Point2d(ext.MinPoint.X, ext.MaxPoint.Y), 0.0, 0.0, 0.0); poly.Closed = true; btr.AppendEntity(poly); tr.AddNewlyCreatedDBObject(poly, true); } tr.Commit(); } } catch (Autodesk.AutoCAD.Runtime.Exception ex) { ed.WriteMessage("\n" + ex.Message + "\n" + ex.StackTrace); } }
Awesome!!!! This is exactly what I wanted.
Thank you very much guys~!!
I have a little curious question. 🙂
SymbolUtilityServices.GetBlockModelSpaceId(db)
db.CurrentSpaceId
Hi Gile,
I need a help from you if you have time. For few examples draw of border line does not work well in my case. I can not find out the reason. Below is my code and you can see resut in the screen shot. I have also attached .dwg file. If you have time please help me in this regard. Thanks in advance
<CommandMethod("dba")> PublicSubDrawBorderLine()
Dim doc AsDocument = Application.DocumentManager.MdiActiveDocument
Dim db AsDatabase= doc.Database
Using tx AsTransaction= db.TransactionManager.StartTransaction
Dim bt AsBlockTable = tx.GetObject(db.BlockTableId, OpenMode.ForRead)
Dim btr AsBlockTableRecord = tx.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
Dim lyr AsString = "PKG-OUT"
Dim ext AsExtents3d = NewExtents3d()
ext.AddBlockExtents(btr)
Dim pline AsPolyline = NewPolyline()
pline.AddVertexAt(0, NewPoint2d(ext.MinPoint.X, ext.MinPoint.Y), 0, 0, 0)
pline.AddVertexAt(1, NewPoint2d(ext.MaxPoint.X, ext.MinPoint.Y), 0, 0, 0)
pline.AddVertexAt(2, NewPoint2d(ext.MaxPoint.X, ext.MaxPoint.Y), 0, 0, 0)
pline.AddVertexAt(3, NewPoint2d(ext.MinPoint.X, ext.MaxPoint.Y), 0, 0, 0)
pline.Closed = True
pline.Layer = lyr
btr.AppendEntity(pline)
tx.AddNewlyCreatedDBObject(pline, True)
tx.Commit()
EndUsing
doc.Editor.WriteMessage(vbLf + "Finish")
EndSub