.NET

Reply
Valued Contributor
99 Posts
4 Kudos
Registered: ‎10-03-2011
Post 1 of 5
Accepted Solution

Block size

369 Views, 4 Replies
03-24-2013 05:01 AM

Hello,

 

I want to determine the size of a (non-dynamic non-annotative) block definition (BlockTableRecord). Does a block have a boundry or limits?
For example, if the block would contain a simple square, the size of the block would be equal to the size of the square.

 

Thank you.

You can check bound property of block as well:

 [CommandMethod("bsz")]
static public void BlockRecordSize()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = HostApplicationServices.WorkingDatabase;
PromptStringOptions pso = new PromptStringOptions("\nEnter block name:");
pso.AllowSpaces = true;
PromptResult res;
res = ed.GetString(pso);
if (res.Status != PromptStatus.OK) return;
string blockname = res.StringResult;
ed.WriteMessage("\nBock name entered\t{0}\n", blockname);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt= tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
if (!bt.Has(blockname))
{
ed.WriteMessage("\nBock \"{0}\" does not exists.\n", blockname);
return;
}
BlockTableRecord btrec = (BlockTableRecord)tr.GetObject(bt[blockname], OpenMode.ForRead,false);
Extents3d? bounds;
bounds = btrec.Bounds;
if (bounds.HasValue)
{
Extents3d ext = bounds.Value;
double width = ext.MaxPoint.X - ext.MinPoint.X;
double height = ext.MaxPoint.Y - ext.MinPoint.Y;
ed.WriteMessage("\nBock {0} hase width= {1:f3}; height = {2:f3}\n", blockname, width, height);
}
else
{
BlockReference bref = new BlockReference(Point3d.Origin, bt[blockname]);
bounds =bref.Bounds;
Extents3d ext = bounds.Value;
double width = ext.MaxPoint.X - ext.MinPoint.X;
double height = ext.MaxPoint.Y - ext.MinPoint.Y;
ed.WriteMessage("\nBock {0} hase width= {1:f3}; height = {2:f3}\n", blockname, width, height);
bref.Dispose();
}
tr.Commit();
}
}

 

 

*Expert Elite*
9,817 Posts
1,211 Kudos
Registered: ‎06-29-2007
Post 2 of 5

Re: Block size

03-24-2013 05:06 AM in reply to: Ertqwa

Hi,

 

do you look for that?

Public Overridable ReadOnly Property GeometricExtents As Autodesk.AutoCAD.DatabaseServices.Extents3d
     Member von Autodesk.AutoCAD.DatabaseServices.Entity

 

[EDIT] sorry, I saw to late that you wrote "block-definition", not block-reference.

So you can either scan through the elements within the blockdefinition or you create one reference virtually and take that reference for .GeometricExtents. [/EDIT]

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
*Expert Elite*
1,569 Posts
171 Kudos
Registered: ‎10-08-2008
Post 3 of 5

Re: Block size

03-24-2013 09:19 AM in reply to: Ertqwa

You can check bound property of block as well:

        [CommandMethod("bsz")]
        static public void BlockRecordSize()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = HostApplicationServices.WorkingDatabase;

            PromptStringOptions pso = new PromptStringOptions("\nEnter block name:");
            pso.AllowSpaces = true;
            PromptResult res;
            res = ed.GetString(pso);
            if (res.Status != PromptStatus.OK)  return;

            string blockname = res.StringResult;

            ed.WriteMessage("\nBock name entered\t{0}\n",  blockname);
   
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt= tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                if (!bt.Has(blockname)) 
                { 
                    ed.WriteMessage("\nBock \"{0}\" does not exists.\n", blockname); 
                    return;
                }
                BlockTableRecord btrec = (BlockTableRecord)tr.GetObject(bt[blockname], OpenMode.ForRead,false);
                Extents3d? bounds;
                 bounds = btrec.Bounds;
                if (bounds.HasValue)
                {
                    Extents3d ext = bounds.Value;
                    double width = ext.MaxPoint.X - ext.MinPoint.X;
                    double height = ext.MaxPoint.Y - ext.MinPoint.Y;
                    ed.WriteMessage("\nBock {0} hase width= {1:f3}; height = {2:f3}\n", blockname, width, height);

                }
                else
                {
                    BlockReference bref = new BlockReference(Point3d.Origin, bt[blockname]);
                    bounds =bref.Bounds;
                    Extents3d ext = bounds.Value;
                    double width = ext.MaxPoint.X - ext.MinPoint.X;
                    double height = ext.MaxPoint.Y - ext.MinPoint.Y;
                    ed.WriteMessage("\nBock {0} hase width= {1:f3}; height = {2:f3}\n", blockname, width, height);
                    bref.Dispose();
                }
                tr.Commit();
            }            
        }

 

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Valued Contributor
99 Posts
4 Kudos
Registered: ‎10-03-2011
Post 4 of 5

Re: Block size

03-25-2013 01:35 PM in reply to: Hallex

Great, both replies are what I am looking for. Ty!

*Expert Elite*
1,569 Posts
171 Kudos
Registered: ‎10-08-2008
Post 5 of 5

Re: Block size

03-25-2013 02:16 PM in reply to: Ertqwa

Glad you got it working

Cheers :smileyhappy:

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!