.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Draw a border around all entities in the drawing

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
dynamicscope
1406 Views, 9 Replies

Draw a border around all entities in the drawing

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. 🙂

9 REPLIES 9
Message 2 of 10
_gile
in reply to: dynamicscope

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();
            }
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 10
mzakiralam
in reply to: _gile

@ _gile: this is great. I am looking for this one and got the answer without asking anybody :D. Thanks a lot.
Message 4 of 10
_gile
in reply to: mzakiralam

You're welcome.

The same can be done with Database.Extmin and Extmax.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 10
Hallex
in reply to: dynamicscope

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);
            }

        }

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 6 of 10
dynamicscope
in reply to: Hallex

Awesome!!!! This is exactly what I wanted.

Thank you very much guys~!!

 

I have a little curious question. 🙂

 

 

SymbolUtilityServices.GetBlockModelSpaceId(db)
db.CurrentSpaceId

 

  • The above two are always the same ID?
Message 7 of 10
_gile
in reply to: dynamicscope

SymbolUtilityServices.GetModelSpaceId(db) always returns the specified Database model space ID

Database.CurrentSpaceId returns the currently active space for the Database (model or paper space).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 8 of 10
mzakiralam
in reply to: _gile

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

 

Capture.JPG

Message 9 of 10
mzakiralam
in reply to: mzakiralam

Please ignore my mail. I think there is something wrong with this drawing. I have transferred this to a new file and now working well

Regards
Zakir
Message 10 of 10
dynamicscope
in reply to: _gile

Would

Database db = HostApplicationServices.WorkingDatabase;
db.Extmin;
db Extmax;

be the same as

Extents3d ext = new Extents(model);
ext.AddBlockExtents(model);
ext.MinPoint;
ext.MaxPoint;

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost