.NET

Reply
Distinguished Contributor
muckmailer
Posts: 117
Registered: ‎05-10-2009
Message 1 of 10 (740 Views)

Get the block name by picking it using VB.net

740 Views, 9 Replies
01-31-2012 06:40 AM

How does someone get the block name in a drawing by picking the block reference in the

drawing using vb.net?   

 

Thank you,     

*Expert Elite*
norman.yuan
Posts: 961
Registered: ‎04-27-2009
Message 2 of 10 (737 Views)

Re: Get the block name by picking it using VB.net

01-31-2012 07:05 AM in reply to: muckmailer

psuedo code:

 

//Create a PromptEntityOptions object

PromptEntityOptions opt=new PromptEntityOptions(....);

 

//Set PromptEntityOptions' property to allow only Blockreference to be picked

 

//Pick entity

PromptEntityresult res=theEditor.GetEntity(opt);

 

if (res.Status=PromptStatus.OK)

{

    //Get ObjectId from PromptEntityResult

 

    //Open a transaction to obtain the BlockReference from the ObjectId

 

    //Block name equals to BlcokReference.Name

}

*Expert Elite*
norman.yuan
Posts: 961
Registered: ‎04-27-2009
Message 3 of 10 (733 Views)

Re: Get the block name by picking it using VB.net

01-31-2012 07:17 AM in reply to: norman.yuan

Oh, addtional info.

 

If the block refernece is an dynamic block, its name property might be like "*Uxx", which probably is not what you want. So, after getting a BlockRefernce object, you may want to test its IsDynamicBlock property within the transaction. If it is, you need to use its DynamicBlockTableRecord property to get the corresponding BlockTableRecord and then its name.

Distinguished Contributor
muckmailer
Posts: 117
Registered: ‎05-10-2009
Message 4 of 10 (729 Views)

Re: Get the block name by picking it using VB.net

01-31-2012 07:37 AM in reply to: muckmailer

1. I want to understand how to get the block name by picking it.

2. The next step would be to get block attributes of that choosen picked block 

 and placing its attributes in a MultiText box.

3. That will get me ready to make a program to edit attributes in a choosen block.

Thats my objective now now.

I want to keep the code simple as possable.

Thank you,

Valued Mentor
jeff
Posts: 322
Registered: ‎05-12-2009
Message 5 of 10 (728 Views)

Re: Get the block name by picking it using VB.net

01-31-2012 07:37 AM in reply to: norman.yuan

Can't you just use BlockReference.DynamicBlockTableRecord Property for both as DynamicBlockTableRecord  & BlockTableRecord property will always be the same for non-dynamic blocks?

You can also find your answers @ TheSwamp
*Expert Elite*
norman.yuan
Posts: 961
Registered: ‎04-27-2009
Message 6 of 10 (719 Views)

Re: Get the block name by picking it using VB.net

01-31-2012 08:03 AM in reply to: jeff

Yes, we can use DynamicBlockTableRecord property to get the blockreference's BlockTableRecord regardless the blockreference being regular block or being dynamic block. However, when doing so, we need to open one more object (BlockTableRecord) to get block name inside the transaction, which is not necessary if the block is not a dynamic block. So, I'd still prefer to test IsDynamicBlock property first.

 

To the OP: the code is really very simple, that was why I wrote psuedo code above. But since you ask, here you go:

 

        [CommandMethod("MyCmd")]
        public static void MyCmd()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;

            string blkName = PickBlock(ed);

            ed.WriteMessage("\nPicked block is {{0}\n", blkName);
        }

        private static string PickBlock(Editor ed)
        {
            string blkName = "";

            PromptEntityOptions opt = new PromptEntityOptions("\nPick a block:");
            opt.SetRejectMessage("Bad pick: must be a block");
            opt.AddAllowedClass(typeof(BlockReference), true);
            PromptEntityResult res = ed.GetEntity(opt);

            if (res.Status == PromptStatus.OK)
            {
                using (Transaction tran = res.ObjectId.Database.TransactionManager.StartTransaction())
                {
                    BlockReference blk = tran.GetObject(res.ObjectId, OpenMode.ForRead) as BlockReference;

                    if (blk != null)
                    {
                        if (blk.IsDynamicBlock)
                        {
                            BlockTableRecord br = tran.GetObject(blk.DynamicBlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
                            blkName = br.Name;
                        }
                        else
                        {
                            blkName = blk.Name;
                        }
                    }

                    tran.Commit();
                }
            }

            return blkName;
        }

 

Distinguished Contributor
muckmailer
Posts: 117
Registered: ‎05-10-2009
Message 7 of 10 (710 Views)

Re: Get the block name by picking it using VB.net

01-31-2012 09:52 AM in reply to: muckmailer

This code looks like it is in c:

What about vb.net ?

Thank you,

*Expert Elite*
arcticad
Posts: 1,285
Registered: ‎06-21-2004
Message 8 of 10 (708 Views)

Re: Get the block name by picking it using VB.net

01-31-2012 09:54 AM in reply to: muckmailer

http://www.developerfusion.com/tools/convert/csharp-to-vb/

---------------------------



(defun botsbuildbots() (botsbuildbots))
Distinguished Contributor
muckmailer
Posts: 117
Registered: ‎05-10-2009
Message 9 of 10 (707 Views)

Re: Get the block name by picking it using VB.net

01-31-2012 09:54 AM in reply to: muckmailer

For now All I want to do is go get the block name in vb.net and understand that little

snippet of code first.

Thank you,

*Expert Elite*
Keith.Brown
Posts: 1,091
Registered: ‎03-13-2008
Message 10 of 10 (683 Views)

Re: Get the block name by picking it using VB.net

01-31-2012 01:19 PM in reply to: norman.yuan

norman.yuan wrote:

Yes, we can use DynamicBlockTableRecord property to get the blockreference's BlockTableRecord regardless the blockreference being regular block or being dynamic block. However, when doing so, we need to open one more object (BlockTableRecord) to get block name inside the transaction, which is not necessary if the block is not a dynamic block. So, I'd still prefer to test IsDynamicBlock property first.

 

To the OP: the code is really very simple, that was why I wrote psuedo code above. But since you ask, here you go:

 

        [CommandMethod("MyCmd")]
        public static void MyCmd()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;

            string blkName = PickBlock(ed);

            ed.WriteMessage("\nPicked block is {{0}\n", blkName);
        }

        private static string PickBlock(Editor ed)
        {
            string blkName = "";

            PromptEntityOptions opt = new PromptEntityOptions("\nPick a block:");
            opt.SetRejectMessage("Bad pick: must be a block");
            opt.AddAllowedClass(typeof(BlockReference), true);
            PromptEntityResult res = ed.GetEntity(opt);

            if (res.Status == PromptStatus.OK)
            {
                using (Transaction tran = res.ObjectId.Database.TransactionManager.StartTransaction())
                {
                    BlockReference blk = tran.GetObject(res.ObjectId, OpenMode.ForRead) as BlockReference;

                    if (blk != null)
                    {
                        if (blk.IsDynamicBlock)
                        {
                            BlockTableRecord br = tran.GetObject(blk.DynamicBlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
                            blkName = br.Name;
                        }
                        else
                        {
                            blkName = blk.Name;
                        }
                    }

                    tran.Commit();
                }
            }

            return blkName;
        }

 



This code shown above by norman.yuan does exactly what you asked for.  It is just in c#.  Copy the code to your clipboard and head to the website listed by arcticad.  That site has a convertor that will convert the code from c# to vb.net for you.  The code is simple enough it should convert just fine.

 

Most people on the boards on happy enough to answer your questions and give help whenever possible.  Some know c# extremely well and others know vb.net extremely well.  If someone gives the code in a format different than what you want, it is simple enough to convert it.


Keith Brown AutoCAD MEP BLOG | RSS Feed
AutoCAD MEP 2014 | Revit 2014 | EastCoast CAD/CAM V6.1 | Visual Studio 2013
────────────────────────────────────
⁞|⁞ Please use Mark Solutions!.Accept as Solution and Give Kudos!Give Kudos as appropriate. Thank you!

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

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 to get help installing your software.

Ask the Community