How does someone get the block name in a drawing by picking the block reference in the
drawing using vb.net?
Thank you,
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
}
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.
Norman Yuan
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,
Can't you just use BlockReference.DynamicBlockTableRecord Property for both as DynamicBlockTableRecord & BlockTableRecord property will always be the same for non-dynamic blocks?
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; }
Norman Yuan
http://www.developerfusion.com/tools/convert/csharp-to-vb/
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,
@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.