I am trying to read in a block and then create references of it in model space. This does not work, but if I try out some other code that creates a block in the program itself and then add a reference to modelspace it works. I am guessing this is not working for me because there is something wrong with the way that I am inserting a block and trying to reference it?
public override List<DBObject> CustomTransactionCode()
{
List<DBObject> insertedBlockReferenceToCommit = new List<DBObject>();
if (this.mainBlockTable.Has(this.blockName) == false)
{
using (Database tempDb = new Database(false, true))
{
tempDb.ReadDwgFile(this.blockPath, FileOpenMode.OpenForReadAndAllShare, true, null);
ObjectId newInsertedBlock = this.currentDatabase.Insert(this.blockName, tempDb, true);
if (newInsertedBlock.IsNull)
{
throw new ApplicationException("Failed to read block: " + this.blockPath);
}
}
}
BlockTableRecord existingBlock = this.mainBlockTable[blockName].GetObject(OpenMode.ForRead) as BlockTableRecord;
BlockReference newBlockReferenceToInsert = new BlockReference(this.insertPoint, existingBlock.ObjectId);
if (this.spaceToAddTo == AddToSpaceContext.ModelSpace)
{
ObjectId AppendedEntity = this.modelSpaceBlockTable.AppendEntity(newBlockReferenceToInsert);
}
else if (this.spaceToAddTo == AddToSpaceContext.PaperSpace)
{
ObjectId AppendedEntity = this.paperSpaceBlockTable.AppendEntity(newBlockReferenceToInsert);
}
this.currentTransaction.AddNewlyCreatedDBObject(newBlockReferenceToInsert, true);
foreach (ObjectId ObjectIDInBlockDefinition in existingBlock)
{
Entity EntityInBlock = ObjectIDInBlockDefinition.GetObject(OpenMode.ForRead) as Entity;
if (EntityInBlock.GetType() == typeof(AttributeDefinition))
{
EntityInBlock.UpgradeOpen();
AttributeDefinition attDef = (AttributeDefinition)EntityInBlock;
AttributeReference attRef = new AttributeReference();
attRef.SetAttributeFromBlock(attDef, newBlockReferenceToInsert.BlockTransform);
newBlockReferenceToInsert.AttributeCollection.AppendAttribute(attRef);
this.currentTransaction.AddNewlyCreatedDBObject(attRef, true);
attRef.DowngradeOpen();
EntityInBlock.DowngradeOpen();
}
}
insertedBlockReferenceToCommit.Add(newBlockReferenceToInsert);
return insertedBlockReferenceToCommit;
}