How to calculate the length and width of a block definition?

How to calculate the length and width of a block definition?

zhengyunyang2019
Advocate Advocate
586 Views
2 Replies
Message 1 of 3

How to calculate the length and width of a block definition?

zhengyunyang2019
Advocate
Advocate

I am using the following code to calculate the dimensions of a block definition, but 'bounds' is always null. What could be the issue?

 

The blockReference object is obtained through Editor.GetSelection()

 

double width = 0;
double height = 0;
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
    BlockReference blockRef = blockReference.ObjectId.GetObject(AcDb.OpenMode.ForRead) as BlockReference;
    if (blockRef != null)
    {
        string blockName = blockRef.Name;
        BlockTable blockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
        if (blockTable.Has(blockName))
        {
            ObjectId blockDefId = blockTable[blockName];
            BlockTableRecord blockDef = tr.GetObject(blockDefId, OpenMode.ForRead) as BlockTableRecord;

            if (blockDef != null)
            {
                Extents3d? bounds = blockDef.Bounds;
                if (bounds != null)
                {
                    width = bounds.Value.MaxPoint.X - bounds.Value.MinPoint.X;
                    height = bounds.Value.MaxPoint.Y - bounds.Value.MinPoint.Y;
                }
                else
                {
                     AcAp.ShowAlertDialog("Block Bounds is null.\n");
                }
            }
       }
       tr.Commit();
    }
}

 

 

0 Likes
Accepted solutions (2)
587 Views
2 Replies
Replies (2)
Message 2 of 3

daniel_cadext
Advisor
Advisor
Accepted solution

maybe you're looking for BlockReference.GeometryExtentsBestFit()  ?

that applies a transformation, otherwise you can iterate the block and get the extent of each entity.

Python for AutoCAD, Python wrappers for ARX https://github.com/CEXT-Dan/PyRx
0 Likes
Message 3 of 3

cuongtk2
Advocate
Advocate
Accepted solution
var br = new Block reference(blockdefId, Point3d.Origin);
var bound = br.bound.value;
0 Likes