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()); }
Solved! Go to Solution.
Solved by _gile. Go to Solution.
The below link shows how to transform from one coordinate system to another.
(In your case you will have to go from the WCS to UCS.)
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()); }
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}");
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
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(); }
Can't find what you're looking for? Ask the community or share your knowledge.