- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm trying to add a block with an attribute to a drawing. The command works if there is only one drawing open, but when there are other open drawings then the block doesn't appear. Strangely if I select the block in the block editor and then close the block editor the block suddenly appears. Do you know what is causing the block to not appear when other drawings are open?
I included the code below. "s1" works but "s2" has the strange behavior of the block not being visible.
Thanks for any help,
Casey Joyce
[CommandMethod("s1")]
public void AddBlockWithAttributeToDestination_WithNoOtherDrawingsOpen() {
var attributeValue = "1234";
var destination = Application.DocumentManager.MdiActiveDocument;
using (var transaction = destination.TransactionManager.StartTransaction()) {
addBlock(transaction, destination.Database, attributeValue);
transaction.Commit();
}
}
[CommandMethod("s2")]
public void AddBlockWithAttributeToDestination_WhileSourceDrawingIsOpen() {
// pretend user selects info from opened source drawing
var attributeValue = "1234";
// pretend user selects destination drawing from popup list
var destinationDrawing = @"c:\destination.dwg";
// add block to destination drawing
var destination = Application.DocumentManager.Open(destinationDrawing, false);
using (var transaction = destination.TransactionManager.StartTransaction()) {
using (var key = destination.LockDocument()) {
addBlock(transaction, destination.Database, attributeValue);
}
transaction.Commit();
}
}
void addBlock(Transaction transaction, Database database, string attributeValue) {
// add block
var block = new BlockTableRecord() { Name = "SampleBlock" };
var table = transaction.GetObject(database.BlockTableId, OpenMode.ForWrite) as BlockTable;
var blockId = table.Add(block);
transaction.AddNewlyCreatedDBObject(block, true);
// add block reference
var blockReference = new BlockReference(Point3d.Origin, block.ObjectId);
var modelSpace = transaction.GetObject(table[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
modelSpace.AppendEntity(blockReference);
transaction.AddNewlyCreatedDBObject(blockReference, true);
// set attribute definition
var definition = new AttributeDefinition(
new Point3d(1, 1, 0),
attributeValue,
"SampleAttribute",
"",
database.Textstyle);
block.AppendEntity(definition);
transaction.AddNewlyCreatedDBObject(definition, true);
// add attribute reference
var reference = new AttributeReference();
reference.SetAttributeFromBlock(definition, blockReference.BlockTransform);
blockReference.AttributeCollection.AppendAttribute(reference);
transaction.AddNewlyCreatedDBObject(reference, true);
}
Solved! Go to Solution.