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

Region Bounding Box In 3D Coordinate

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
thannaingoo.api
1650 Views, 7 Replies

Region Bounding Box In 3D Coordinate

Dear All,

 

How to get region bounding box in 3D coordinate?

I used below code to extract the bounding box of a region.

The result is not same as massprop value.

How should I get the correct value of region bounding box base on the current coordinate system? 

 

Thanks,

Naing Oo

 

var region = (Region)tr.GetObject(id, OpenMode.ForWrite, false, true);

                                if (region.Area > 0.0)
                                {                   
                                    Point3d max = region.Bounds.Value.MaxPoint;
                                    Point3d min = region.Bounds.Value.MinPoint;                                
                                    Application.ShowAlertDialog(min.ToString() + "\n" + max.ToString());
                                }

RegionBoundingBox.png

7 REPLIES 7
Message 2 of 8

The below link shows how to transform from one coordinate system to another.

 

https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2015/ENU/AutoCAD-NET/files/GUI...

 

 

(In your case you will have to go from the WCS to UCS.)

Message 3 of 8

 

I still can't get the right value. Below image green color is region and the red color is a bounding box from the result.

How should I fix it? Below is the latest code.

 

Thanks,

Naing Oo

 

 var region = (Region)tr.GetObject(id, OpenMode.ForWrite, false, true);

                                if (region.Area > 0.0)
                                {
                                    Point3d MaxPoint = region.Bounds.Value.MaxPoint;
                                    Point3d MinPoint = region.Bounds.Value.MinPoint;
                                    Point3d max = MaxPoint.TransformBy(Matrix3d.PlaneToWorld(region.Normal));
                                    Point3d min = MinPoint.TransformBy(Matrix3d.PlaneToWorld(region.Normal));
                                    Application.ShowAlertDialog(max.ToString() + "\n" + min.ToString());
                                }

Region.JPG

 

Message 4 of 8
_gile
in reply to: thannaingoo.api

Hi,

 

You have to transform the region object from current UCS to WCS, get the extents, and transform back the region to UCS.

 

var region = (Region)tr.GetObject(id, OpenMode.ForWrite);
                region.TransformBy(ed.CurrentUserCoordinateSystem.Inverse());
                var extents = region.GeometricExtents;
                region.TransformBy(ed.CurrentUserCoordinateSystem);
                ed.WriteMessage($"\nBoundingBox about UCS: {extents.MinPoint} {extents.MaxPoint}");


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 8
thannaingoo.bim.mep
in reply to: _gile

Dear @_gile,

 

Thank you for the reply.

I got the wrong value for 45degree rotated region object along Zaxis.

Please, Let me know the way how to solve it.

 

Regards,

Naing Oo

Message 6 of 8
thannaingoo.api
in reply to: _gile

I am trying to create a rectangle of region object bounding box in 3D coordinate.

Let me know the way to create a bounding box rectangle. 

 

Regards,

Naing Oo

Message 7 of 8
_gile
in reply to: thannaingoo.api

If I do not misuderstand, what you want to achieve is not related to the current UCS. it is related to the region OCS.

 

Try like this:

 

        [CommandMethod("RegionToBoundingBoxRectangle")]
        public static void RegionToBoundingBoxRectangle()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            var peo = new PromptEntityOptions("\nSelect region: ");
            peo.SetRejectMessage("\nSelected object is not a Region.");
            peo.AddAllowedClass(typeof(Region), true);
            var per = ed.GetEntity(peo);
            if (per.Status != PromptStatus.OK)
                return;
            var id = per.ObjectId;
            using (var tr = db.TransactionManager.StartTransaction())
            {
                var region = (Region)tr.GetObject(id, OpenMode.ForWrite);
                // get the region plane normal
                var normal = region.Normal;
                var plane = new Plane(Point3d.Origin, normal);
                // transform the region from WCS to OCS
                region.TransformBy(Matrix3d.WorldToPlane(plane));
                // get the 'bounding box'
                var extents = region.GeometricExtents;
                // transform bak the region from OCS to WCS
                region.TransformBy(Matrix3d.PlaneToWorld(plane));
                // get the plane elevation
                var elevation = extents.MaxPoint.Z;
                // get the bounding box coorinates (OCS)
                var minX = extents.MinPoint.X;
                var minY = extents.MinPoint.Y;
                var maxX = extents.MaxPoint.X;
                var maxY = extents.MaxPoint.Y;
                // create a rectangle (Polyline)
                var pline = new Polyline(4);
                pline.AddVertexAt(0, new Point2d(minX, minY), 0.0, 0.0, 0.0);
                pline.AddVertexAt(1, new Point2d(maxX, minY), 0.0, 0.0, 0.0);
                pline.AddVertexAt(2, new Point2d(maxX, maxY), 0.0, 0.0, 0.0);
                pline.AddVertexAt(3, new Point2d(minX, maxY), 0.0, 0.0, 0.0);
                pline.Closed = true;
                pline.Normal = normal;
                pline.Elevation = elevation;
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                curSpace.AppendEntity(pline);
                tr.AddNewlyCreatedDBObject(pline, true);
                tr.Commit();
            }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 8 of 8
thannaingoo.api
in reply to: _gile

Dear @_gile,

 

I want to thank you for taking the time out to answer my question. I am sure that you are busy, and I appreciate you taking the time to respond. Thank you again.

 

Regards,

Naing Oo

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

Post to forums  

Forma Design Contest


AutoCAD Beta