.NET

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

Search block by name

207 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 (189 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: 379
Registered: ‎03-31-2005
Message 3 of 3 (188 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.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!