.NET

Reply
Valued Contributor
dynamicscope
Posts: 95
Registered: ‎04-04-2012
Message 1 of 10 (496 Views)
Accepted Solution

Draw a border around all entities in the drawing

496 Views, 9 Replies
06-21-2013 02:59 AM

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. :smileyhappy:

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

 

*Expert Elite*
_gile
Posts: 2,114
Registered: ‎04-29-2006
Message 2 of 10 (494 Views)

Re : Draw a border around all entities in the drawing

06-21-2013 03:42 AM 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
Mentor
mzakiralam
Posts: 231
Registered: ‎11-09-2012
Message 3 of 10 (481 Views)

Re : Draw a border around all entities in the drawing

06-21-2013 06:36 AM in reply to: _gile
@ _gile: this is great. I am looking for this one and got the answer without asking anybody :smileyvery-happy:. Thanks a lot.
*Expert Elite*
_gile
Posts: 2,114
Registered: ‎04-29-2006
Message 4 of 10 (468 Views)

Re : Draw a border around all entities in the drawing

06-21-2013 09:06 AM in reply to: mzakiralam

You're welcome.

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

Gilles Chanteau
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 5 of 10 (465 Views)

Re: Draw a border around all entities in the drawing

06-21-2013 09:26 AM 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
Valued Contributor
dynamicscope
Posts: 95
Registered: ‎04-04-2012
Message 6 of 10 (442 Views)

Re: Draw a border around all entities in the drawing

06-23-2013 04:40 AM in reply to: Hallex

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

Thank you very much guys~!!

 

I have a little curious question. :smileyhappy:

 

 

SymbolUtilityServices.GetBlockModelSpaceId(db)
db.CurrentSpaceId

 

  • The above two are always the same ID?
*Expert Elite*
_gile
Posts: 2,114
Registered: ‎04-29-2006
Message 7 of 10 (437 Views)

Re: Draw a border around all entities in the drawing

06-23-2013 04:50 AM 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
Mentor
mzakiralam
Posts: 231
Registered: ‎11-09-2012
Message 8 of 10 (385 Views)

Re: Draw a border around all entities in the drawing

06-27-2013 09:06 AM 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

Mentor
mzakiralam
Posts: 231
Registered: ‎11-09-2012
Message 9 of 10 (379 Views)

Re: Draw a border around all entities in the drawing

06-27-2013 09:29 AM 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
Valued Contributor
dynamicscope
Posts: 95
Registered: ‎04-04-2012
Message 10 of 10 (334 Views)

Re : Draw a border around all entities in the drawing

07-03-2013 06:14 PM 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;
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!