.NET

Reply
Active Member
Danius87
Posts: 6
Registered: ‎07-31-2012
Message 1 of 3 (200 Views)
Accepted Solution

Search block by name

200 Views, 2 Replies
07-31-2012 06:04 AM

Hi at all, my name is Daniele.

I started to develop autocad's application with .Net framework.

But i have some trouble, particularly when i try to find a block by name (not like id).

I would take a block by name, instead to get all block and find one with comparison.

How can resolve this problem?

Thank's very much, and sorry for my bad english

 

Daniele

Try this code, not seriously tested though

This will select all blocks by name on the current layout

 

 [CommandMethod("AllBlockByName","abb", CommandFlags.Modal)]
public void GetAllBlockRefsByName()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
ObjectIdCollection objIds = new ObjectIdCollection();
string ctab = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("ctab").ToString();
string blockname = ed.GetString("\nEnter a block name: ").StringResult;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
string bname = string.Concat( blockname,",*`U*");
TypedValue[] tvs = new TypedValue[] {
new TypedValue(-4, "<and"),
new TypedValue(0, "insert"),
new TypedValue(2, bname),
new TypedValue(410, ctab),
new TypedValue(-4, "and>")
};
SelectionFilter sf = new SelectionFilter(tvs);
PromptSelectionResult psr = ed.SelectAll(sf);
if (psr.Status != PromptStatus.OK)
{
ed.WriteMessage("\nWrong selection\n");
return;
}
ed.WriteMessage("\nCount of selected block: {0}", psr.Value.Count);
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btrec = null;
string brefname= "";
foreach (SelectedObject selobj in psr.Value)
{
DBObject obj = (DBObject)tr.GetObject(selobj.ObjectId, OpenMode.ForRead,false) ;
BlockReference bref = obj as BlockReference;
if (bref == null) continue;
if (!bref.IsDynamicBlock)
{
brefname = bref.Name;
}
else
{
btrec = (BlockTableRecord)tr.GetObject(bref.DynamicBlockTableRecord, OpenMode.ForRead, false, true);
brefname = btrec.Name;
}
if (brefname.CompareTo(blockname) == 0)
{
objIds.Add(bref.ObjectId);
}
for (int n=0;n< objIds.Count;n++)
{
DBObject ent = (DBObject)tr.GetObject(objIds[n], OpenMode.ForRead, false);
BlockReference btref = ent as BlockReference;
ed.WriteMessage("\nPosition: {0}\n", btref.Position);
// [ do your rest work with block instances here ] //
}
}
tr.Commit();
}
}

 Here is better explained article thanks to Stephen Preston:

http://adndevblog.typepad.com/autocad/2012/06/finding-all-block-references-of-a-dynamic-block.html

 

~'J'~

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 3 (182 Views)

Re: Search block by name

07-31-2012 08:26 AM in reply to: Danius87

Try this code, not seriously tested though

This will select all blocks by name on the current layout

 

        [CommandMethod("AllBlockByName","abb", CommandFlags.Modal)]
        public void GetAllBlockRefsByName()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            ObjectIdCollection objIds = new ObjectIdCollection();
            string ctab = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("ctab").ToString();
            string blockname = ed.GetString("\nEnter a block name: ").StringResult;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                string bname = string.Concat( blockname,",*`U*");
                
                TypedValue[] tvs = new TypedValue[] { 
              new TypedValue(-4, "<and"),
              new TypedValue(0, "insert"),
              new TypedValue(2, bname),
              new TypedValue(410, ctab),
              new TypedValue(-4, "and>")
              
            };
                SelectionFilter sf = new SelectionFilter(tvs);

                PromptSelectionResult psr = ed.SelectAll(sf);
                if (psr.Status != PromptStatus.OK)
                {
                    ed.WriteMessage("\nWrong selection\n");

                    return;
                }
                ed.WriteMessage("\nCount of  selected block: {0}", psr.Value.Count);
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord btrec = null;
                string brefname= "";
                foreach (SelectedObject selobj in psr.Value)
                {
                    DBObject obj = (DBObject)tr.GetObject(selobj.ObjectId, OpenMode.ForRead,false) ;
                    BlockReference bref = obj as BlockReference;
                    if (bref == null) continue;
                    if (!bref.IsDynamicBlock)
                    {
                        brefname = bref.Name;
                    }
                    else
                    {
                        btrec = (BlockTableRecord)tr.GetObject(bref.DynamicBlockTableRecord, OpenMode.ForRead, false, true);
                        brefname = btrec.Name;
                    }


                    if (brefname.CompareTo(blockname) == 0)
                    {
                        objIds.Add(bref.ObjectId);
                    }


                        for (int n=0;n< objIds.Count;n++)
                        {
                        DBObject ent = (DBObject)tr.GetObject(objIds[n], OpenMode.ForRead, false);

                        BlockReference btref = ent as BlockReference;
                       
                        ed.WriteMessage("\nPosition: {0}\n", btref.Position);

                        //  [  do your rest work with block instances here  ]  //
                    }
                    
                }

                tr.Commit();
            }
        }

 Here is better explained article thanks to Stephen Preston:

http://adndevblog.typepad.com/autocad/2012/06/finding-all-block-references-of-a-dynamic-block.html

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Valued Mentor
fieldguy
Posts: 371
Registered: ‎03-31-2005
Message 3 of 3 (181 Views)

Re: Search block by name

07-31-2012 08:27 AM in reply to: Danius87

Hi Daniele - welcome aboard!

 

Please use the search tool 1st - this question is very common.  Enter your subject as is and start reading.

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.