Message 1 of 13
Block with Multiple Attribute References
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello,
I tried to create a block composed of a polyline and multiple attributes.
Here is the part of the program. Please note that "ScalledPline" is the polyline on which I will create the block, and "_nameofbl" is the name of the block. The variables "insertPoint (point3d), _lstofposition (List<Pointe3d>), _lstoftextstring (List<string>), _lstofrotation (List<double>)" are used to store data related to the attributes (texts, positions, rotations).
// ScalledPline (polyline), insertPoint(point3d), _lstOfPosition (List<Point3d>), _lstOfTextString (List<string>)
//_nameOfBl (string), _lstOfRotation (List<double>), insertPoint : given
// Get the current database and start a transaction
Database db = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction acTrans = db.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable bt;
bt = acTrans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
ObjectId blkRecId = ObjectId.Null;
if (!bt.Has(_nameOfBl))
{
using (BlockTableRecord acBlkTblRec = new BlockTableRecord())
{
acBlkTblRec.Name = _nameOfBl;
// Set the insertion point for the block
acBlkTblRec.Origin = insertPoint;
// Add pline with attribute onto block
using (ScalledPline)
{
acBlkTblRec.AppendEntity(ScalledPline);
// Add an attribute definition to the block
for (int i = 0; i < _lstOfDBText.Count; i++)
{
using (AttributeDefinition acAttDef = new AttributeDefinition())
{
acAttDef.Position = _lstOfPosition[i];
acAttDef.Prompt = "Txt N°" + (i + 1).ToString();
acAttDef.Tag = "Txt " + (i + 1).ToString();
acAttDef.TextString = _lstOfTextString[i];
acAttDef.Rotation = _lstOfRotation[i];
acAttDef.Height = 1;
acAttDef.Justify = AttachmentPoint.MiddleCenter;
acBlkTblRec.AppendEntity(acAttDef);
}
}
bt.UpgradeOpen();
bt.Add(acBlkTblRec); // error in this line
acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);
}
blkRecId = acBlkTblRec.Id;
}
}
else
{
blkRecId = bt[_nameOfBl];
}
//========== 3 - Insert the block into the current space
if (blkRecId != ObjectId.Null)
{
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord;
// Create and insert the new block reference
using (BlockReference acBlkRef = new BlockReference(insertPoint, blkRecId))
{
BlockTableRecord acCurSpaceBlkTblRec;
acCurSpaceBlkTblRec = acTrans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
acCurSpaceBlkTblRec.AppendEntity(acBlkRef);
acTrans.AddNewlyCreatedDBObject(acBlkRef, true);
// Verify block table record has attribute definitions associated with it
if (acBlkTblRec.HasAttributeDefinitions)
{
// Add attributes from the block table record
foreach (ObjectId objID in acBlkTblRec)
{
DBObject dbObj = acTrans.GetObject(objID, OpenMode.ForRead) as DBObject;
if (dbObj is AttributeDefinition)
{
AttributeDefinition acAtt = dbObj as AttributeDefinition;
if (!acAtt.Constant)
{
using (AttributeReference acAttRef = new AttributeReference())
{
acAttRef.SetAttributeFromBlock(acAtt, acBlkRef.BlockTransform);
acAttRef.Position = acAtt.Position.TransformBy(acBlkRef.BlockTransform);
acAttRef.TextString = acAtt.TextString;
acBlkRef.AttributeCollection.AppendAttribute(acAttRef);
acTrans.AddNewlyCreatedDBObject(acAttRef, true);
}
}
}
}
}
}
}
// Save the new object to the database
acTrans.Commit();
}
Unfortunately, I encountered an issue with a function I'm using : Unhandled Access Violation Reading
Thank you.