• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Distinguished Contributor
    Posts: 114
    Registered: ‎05-10-2009

    Get the block name by picking it using VB.net

    482 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,     

    Please use plain text.
    *Expert Elite*
    Posts: 683
    Registered: ‎04-27-2009

    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

    }

    Please use plain text.
    *Expert Elite*
    Posts: 683
    Registered: ‎04-27-2009

    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.

    Please use plain text.
    Distinguished Contributor
    Posts: 114
    Registered: ‎05-10-2009

    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,

    Please use plain text.
    Mentor
    Posts: 241
    Registered: ‎05-12-2009

    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
    Please use plain text.
    *Expert Elite*
    Posts: 683
    Registered: ‎04-27-2009

    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;
            }

     

    Please use plain text.
    Distinguished Contributor
    Posts: 114
    Registered: ‎05-10-2009

    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,

    Please use plain text.
    *Expert Elite*
    arcticad
    Posts: 1,251
    Registered: ‎06-21-2004

    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/

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



    “We don’t have a snowball’s chance in a blast furnace of surviving this attack unless every one of our units gets into the fight right now!”
    Please use plain text.
    Distinguished Contributor
    Posts: 114
    Registered: ‎05-10-2009

    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,

    Please use plain text.
    *Expert Elite*
    Keith.Brown
    Posts: 754
    Registered: ‎03-13-2008

    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
    Please use plain text.