I'm trying to figure out how dynamic block works and i see it very complicated. At the moment I'm trying to insert dynamic block with attributes and at every step of adding it I had problems. First with simply add it to modelspace, later with changing dynamic properties and with correct display, now I'm struggling with adding attributes and I have added them BUT! When I add those attributes they are not in correct place and my dynamic bloc behaves weird - see on screens. Moreover attributes are show with a delay some time it is short and some it's quite long.
This is my code
[CommandMethod("iblock")]
public void iblock()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Transaction tr = db.TransactionManager.StartTransaction();
ObjectId blockReferenceId = ObjectId.Null;
string NazwaBloku = "Bore_Hole";
using (tr)
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
if (!bt.Has(NazwaBloku))
{
ed.WriteMessage("\n Blok o nazwie Bore_HOle nie istnieje");
}
else
{
bt.UpgradeOpen();
BlockTableRecord ms = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
ObjectId blockId = bt[NazwaBloku];
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(blockId, OpenMode.ForRead);
ed.WriteMessage("\nblockId" + blockId.ToString());
Point3d pt = new Point3d(0, 0, 0);
BlockReference br = new BlockReference(pt, blockId);
blockReferenceId = ms.AppendEntity(br);
tr.AddNewlyCreatedDBObject(br, true);
BlockTableRecord bd = (BlockTableRecord)tr.GetObject(br.BlockTableRecord, OpenMode.ForRead);
foreach (ObjectId attId in bd)
{
Entity ent = (Entity)tr.GetObject(attId, OpenMode.ForRead);
if (ent is AttributeDefinition)
{
AttributeDefinition ad = (AttributeDefinition)ent;
AttributeReference ar = new AttributeReference();
ar.SetAttributeFromBlock(ad, br.BlockTransform);
ar.Position = ad.Position.TransformBy(br.BlockTransform);
br.AttributeCollection.AppendAttribute(ar);
}
}
doc.TransactionManager.QueueForGraphicsFlush();
}
tr.Commit();
ed.Regen();
}
using (Transaction trx = db.TransactionManager.StartTransaction())
{
BlockReference br = (BlockReference)trx.GetObject(blockReferenceId, OpenMode.ForWrite);
DynamicBlockReferencePropertyCollection pc = br.DynamicBlockReferencePropertyCollection;
BlockTableRecord bd = (BlockTableRecord)trx.GetObject(br.BlockTableRecord, OpenMode.ForRead);
foreach (DynamicBlockReferenceProperty prop in pc)
{
if (prop.PropertyName.ToUpper() == "VISIBILITY1")
{
ed.WriteMessage("\nIstnieje taka wlasciwosc");
prop.Value = "2";
}
if (prop.PropertyName.ToUpper() == "D1")
{
double length = 2.5;
prop.Value = length;
}
if (prop.PropertyName.ToUpper() == "D1")
{
double length = 2.5;
prop.Value = length;
}
ed.WriteMessage("\nProperty:\"{0}\":{1}", prop.PropertyName, prop.UnitsType);
if (prop.Description != "")
{
ed.WriteMessage("\nDescription: {0}", prop.Description);
}
if (prop.ReadOnly)
{
ed.WriteMessage("read only");
}
bool first = true;
foreach (object value in prop.GetAllowedValues())
{
ed.WriteMessage(first ? "\n Allowed values: [" : ", ");
ed.WriteMessage("\"{0}\"", value);
first = false;
}
if (!first) ed.WriteMessage("]");
ed.WriteMessage("\n Current value: \"{0}\"\n", prop.Value);
}
doc.TransactionManager.QueueForGraphicsFlush();
trx.Commit();
ed.Regen();
}
}