Hello,
I've WBLOCKed a block called "Sensing Unit" from AutoCAD to a file. I'd
like to add it to the model and fill in its attributes.
I can get the block inserted, but with my current code it only gets one
attribute (the last one) instead of the 50 or 60 that it should have.
I feel like I've tried everything here. Anyone know how this is done? My
problem is obviously in the iterator, but I'm very new to this so I'm not
exactly sure how it works.
Thanks so much for any help.
-Carlos
public void InsertIntoAutoCAD()
{
// Inserts the sensing unit into the AutoCAD model and writes the
attributes
try
{
//insert the new block
Document doc = Application.DocumentManager.MdiActiveDocument;
string fname = "C:\\Program Files\\Sensametrics Layout
Interface\\Blocks\\Sensing Unit.dwg";
Database db = new Database(false, false);
db.ReadDwgFile(fname, System.IO.FileShare.Read, true, null);
Transaction t = doc.TransactionManager.StartTransaction();
Autodesk.AutoCAD.ApplicationServices.CommandLinePrompts.Message("\n
Inserting a sensing unit...");
ObjectId idBTR = doc.Database.Insert("Sensing Unit",db,true);
//create a ref to the block
BlockTable bt = (BlockTable)t.GetObject(doc.Database.BlockTableId,
OpenMode.ForWrite);
BlockTableRecord btr =
(BlockTableRecord)t.GetObject(bt[BlockTableRecord.ModelSpace],
OpenMode.ForWrite);
Autodesk.AutoCAD.Geometry.Point3d location = new Point3d(0.0, 0.0,
0.0);
BlockReference bref = new BlockReference(location, idBTR);
//Add the reference to the model space
btr.AppendEntity(bref);
*************** (Works so far, but isn't finding all the
attributes)**************
//Iterate the sensing unit block and find the attributes
BlockTableRecord unitBTR = (BlockTableRecord)t.GetObject(idBTR,
OpenMode.ForWrite);
AttributeReference attRef = new AttributeReference();
foreach(ObjectId id in unitBTR)
{
Entity ent = (Entity)t.GetObject(id, OpenMode.ForWrite, false);
if
(ent.GetType().FullName.Equals("Autodesk.AutoCAD.DatabaseServices.AttributeDefinition"))
{
AttributeDefinition attDef = (AttributeDefinition)ent;
attRef.SetPropertiesFrom(attDef);
attRef.Tag = attDef.Tag;
attRef.TextString = attDef.TextString;
}
}
//Add the attribute reference to the block reference
bref.AppendAttribute(attRef);
//Let the transaction know
t.TransactionManager.AddNewlyCreatedDBObject(attRef, true);
t.TransactionManager.AddNewlyCreatedDBObject(bref, true);
t.Commit();
}
catch
{
}
}