.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Get the block name by picking it using VB.net

9 REPLIES 9
Reply
Message 1 of 10
muckmailer
3046 Views, 9 Replies

Get the block name by picking it using VB.net

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

drawing using vb.net?   

 

Thank you,     

9 REPLIES 9
Message 2 of 10
norman.yuan
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

}

Message 3 of 10
norman.yuan
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.

Message 4 of 10
muckmailer
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,

Message 5 of 10
jeff
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
Message 6 of 10
norman.yuan
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;
        }

 

Message 7 of 10
muckmailer
in reply to: muckmailer

This code looks like it is in c:

What about vb.net ?

Thank you,

Message 8 of 10
arcticad
in reply to: muckmailer

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

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



(defun botsbuildbots() (botsbuildbots))
Message 9 of 10
muckmailer
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,

Message 10 of 10
Keith.Brown
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.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost