.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Reply
Message 1 of 3
Anonymous
224 Views, 2 Replies

Block name

I actually work with AutoCAD 2006 and Visual C# 2002.

Here is the description of my problem:
The last object that I insert in a drawing on the layer < 0 > is a block namely < Test >. This drawing has the layer name < Test > with the color yellow.

With the following code I can move the block from layer < 0 > to the layer < Test >. However I don't know how to recuperate the name of the block in the variable < info >. The name of the block that I received is < *Model_Space > rather than < Test >.

Anyone has the solution to my problem?

Thank you in advance.

[CommandMethod("TestBlk")]
static public void TestBlk()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
PromptSelectionResult result = ed.SelectLast();

Transaction trans = db.TransactionManager.StartTransaction();
try
{
ObjectId[]ids = result.Value.GetObjectIds();
Entity ent = (Entity)trans.GetObject(ids[0], OpenMode.ForWrite, false);

ent.Layer = "Test";
string info = GetBlockName(ent.ObjectId);
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(info);

trans.Commit();
}
finally
{
trans.Dispose();
}
}

static public string GetBlockName(ObjectId idRef)
{
using (Transaction t = idRef.Database.TransactionManager.StartTransaction())
{
BlockReference bref = (BlockReference)t.GetObject(idRef, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)t.GetObject(bref.BlockId,OpenMode.ForRead);
return btr.Name;
}
}
2 REPLIES 2
Message 2 of 3
Anonymous
in reply to: Anonymous

you need to edit your code to use the blocktablerecord property of the blockreference object, instead of the blockId property.

modify your code to the following:

static public string GetBlockName(ObjectId idRef)
{
using (Transaction t = idRef.Database.TransactionManager.StartTransaction())
{
BlockReference bref = (BlockReference)t.GetObject(idRef, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)t.GetObject(bref.BlockTableRecord,OpenMode.ForRead);
return btr.Name;
}
}

Notice all i changed was (bref.blockid, ....... to (bref.blocktablerecord......

Let me know if that works for you.

-Mark P
Message 3 of 3
Anonymous
in reply to: Anonymous

Thank you very much.
Exactly what I need.

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