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

inserting block with attributes

2 REPLIES 2
Reply
Message 1 of 3
poklembaroland
386 Views, 2 Replies

inserting block with attributes

I am trying to insert block with attributes from external drawing. When I perform the same operation using insert command block is inserter correctly and I can access attributes using enhanced attribute editor. When I perform the same task using .net code attributes are not in attribute collection of a newly inserted block. I can still see them by accessing BlockTableRecord but behavior inside the drawing is different. Any idea what am I missing? Here is the code:

Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
PromptPointOptions prPoint = new PromptPointOptions("Enter Attachment Point:");
PromptPointOptions prPoint2 = new PromptPointOptions("Enter BOM Tag Location:");
PromptPointResult prRes = ed.GetPoint(prPoint);
prPoint2.BasePoint = prRes.Value;
prPoint2.UseBasePoint = true;
PromptPointResult prRes2 = ed.GetPoint(prPoint2);

if (prRes.Status == PromptStatus.OK && prRes2.Status == PromptStatus.OK)
{
using (Transaction trans = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

Database newDb = new Database(false, true);
newDb.ReadDwgFile(@"d:\Projects\RandD\ACAD\BOM_R.dwg", FileShare.Read, false, string.Empty);

ObjectId newBlockID = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database.Insert(BlockTableRecord.ModelSpace, "My Block", newDb, true);

BlockReference br = new BlockReference(prRes2.Value, newBlockID);
br.ScaleFactors = new Scale3d(5, 5, 5);

btr.AppendEntity(br);
trans.AddNewlyCreatedDBObject(br, true);

Leader ld = new Leader();

ld.AppendVertex(prRes.Value);
ld.AppendVertex(prRes2.Value);

btr.AppendEntity(ld);
trans.AddNewlyCreatedDBObject(ld, true);

trans.Commit();

br.Dispose();
ld.Dispose();
btr.Dispose();
bt.Dispose();
trans.Dispose();
}

}

And to access them:


string spacing = " ";

Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
using (Transaction trans = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
foreach (ObjectId obj in btr)
{
DBObject brO = trans.GetObject(obj, OpenMode.ForRead);
ed.WriteMessage("\n" + spacing + brO.GetType());

if (brO.GetType().ToString() == "Autodesk.AutoCAD.DatabaseServices.BlockReference")
{
BlockReference br = (BlockReference)brO;

//br.AttributeCollection is empty

BlockTableRecord inBtr = (BlockTableRecord)trans.GetObject(br.BlockTableRecord, OpenMode.ForRead);

ed.WriteMessage("\n " + spacing + br.Name);
foreach (ObjectId inObj in inBtr)
{
DBObject inBrO = trans.GetObject(inObj, OpenMode.ForWrite);
ed.WriteMessage("\n " + spacing + inBrO.GetType());

if (inBrO.GetType().ToString() == "Autodesk.AutoCAD.DatabaseServices.AttributeDefinition")
{
ed.WriteMessage("\n " + spacing + ((AttributeDefinition)inBrO).Tag);

((AttributeDefinition)inBrO).TextString = "Test";
}
}
}
}

trans.Dispose();
}
2 REPLIES 2
Message 2 of 3
Anonymous
in reply to: poklembaroland

http://discussion.autodesk.com/thread.jspa?messageID=5023173
Message 3 of 3

Thanks!

One missing lane was causing troubles.

trans.AddNewlyCreatedDBObject(ar, true);

right after appending attribute reference to to block reference collection. Just in case anybody else is interested 🙂 .

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