After adding BlockRefernce there are not attributes to edit

After adding BlockRefernce there are not attributes to edit

Anonymous
Not applicable
439 Views
1 Reply
Message 1 of 2

After adding BlockRefernce there are not attributes to edit

Anonymous
Not applicable
In a drawing I have a block with editable attributes.
the following code adds it to any layout required.

private static void ProcessSpaceForRevBlockAttributeUpdate(Layout acLayout, Transaction acTrans, string blockName, List attributes)
{

BlockTable acBlkTbl = acTrans.GetObject(acLayout.Database.BlockTableId,
OpenMode.ForRead) as BlockTable;

BlockTableRecord acLayoutBlkTblRec = acTrans.GetObject(acLayout.BlockTableRecordId, OpenMode.ForWrite) as BlockTableRecord;

//Get any offset values from Configuration.
double yValue = 0;
double initialOffset = Convert.ToDouble(cs.GetSetting("//RevisionBlocks/RevisionBlock[@name='" + blockName + "']", "initialOffset"));
double offset = Convert.ToDouble(cs.GetSetting("//RevisionBlocks/RevisionBlock[@name='" + blockName + "']", "offset")); ;


foreach (ObjectId acObjId in acLayoutBlkTblRec)
{
DBObject acObject = acTrans.GetObject(acObjId, OpenMode.ForWrite);

//get the highest of the existing blocks

if (acObject.GetType().ToString() == "Autodesk.AutoCAD.DatabaseServices.BlockReference")
{
BlockReference acBlockRef = acObject as BlockReference;
if (acBlockRef.Name == blockName)
if (acBlockRef.Position.Y > yValue) yValue = acBlockRef.Position.Y;
}

}
//if no current blocks, get a new insertion
if (yValue == 0) yValue = initialOffset;
//increment for new block
yValue = yValue + offset;

//Get a point for insertion
Autodesk.AutoCAD.Geometry.Point3d p3d = new Autodesk.AutoCAD.Geometry.Point3d(0, yValue, 0);
BlockReference acRevBlockRef = new BlockReference(p3d, acBlkTbl[blockName]);

acRevBlockRef.ScaleFactors = new Autodesk.AutoCAD.Geometry.Scale3d(1);
acRevBlockRef.SetDatabaseDefaults();

acLayoutBlkTblRec.AppendEntity(acRevBlockRef);
acTrans.AddNewlyCreatedDBObject(acRevBlockRef, true);

AttributeCollection attColl = acRevBlockRef.AttributeCollection;
foreach (ObjectId attObjectId in attColl)
{
AttributeReference attRef = (AttributeReference)acTrans.GetObject(attObjectId, OpenMode.ForWrite);
foreach (ACADInterface.Attribute at in attributes)
if (at.Name == attRef.Tag)
attRef.TextString = at.Value;
}
return;
}

The block does get added to all layouts and model space, but in the code it doesnt find any attributes.
And in Autocad if I try attedit on the block, it also has not editable attributes.
But if I add the block in Acad via Insert, attributes edit fine, so I'm reasonably certian its not an issue with the block definition.
I think its something pretty simple I'm missing, but cant see it.
0 Likes
440 Views
1 Reply
Reply (1)
Message 2 of 2

norman.yuan
Mentor
Mentor
Unlike Acad COM API's counterpart (ModelSpace.InsertBlock()), which when inserting an Acadblockreference, it also inserts AcadAttributeReference according to the AcadBlock definition, in .NET API, you create a BlockRefernce based on a BlockTableRecord (as you did correctly), but the AttributeReferences are not automatically created in the BlockRefernce according to the BlockTableRecord. You need to create the AttributeReferences and append them into the BlockReference's AttributeCollection. That is, even a block definition defines a set of attributes, a block reference based on a block definition may or may not have all the attribute references corresponding to the attribute definition, depending on whether your code creates them or not.

Here is some quick psuedo code (not tested):

{code}

//Start a transaction here...
//Create your new blockreference and add to database as you needed
BlockReference bref=new BlockReference(pt,theBlockTableRecord);
...
if (theBlockTableRecord.HasAttributeDefinitions)
{
foreach (ObjecyId id in theBlockTableRecord)
{
//Get the Entity in the block definition with the id
....
//If the Entity is AttributeDefinition
if (ent is AttributeDefinition)
{
AttributeReference aref=new AttributeReference()
aref.SetAttributeFromBlock(theAttrDefinition,theBlockTableRecord.BlockTransform)
aref.TextString="XXXXXX"

//Add to the BlockReference
bref.AttributeCollection.AppendAttribute(aref)
tran.AddNewlyCreatedDBObject(aref,true)
}
}
}

{code}

Edited by: norman.yuan on Jan 19, 2010 3:08 PM

Edited by: norman.yuan on Jan 19, 2010 3:09 PM Edited by: norman.yuan on Jan 19, 2010 3:10 PM

Norman Yuan

Drive CAD With Code

EESignature

0 Likes