Here you go. What you need to supply right now is the Database you want the block inserted into, the Layout Name the block will be inserted into, the Block Name to insert, and the Point to insert. I have a meeting at 1:30, so won't be able to check back for a while in case you have questions, but I will answer them if they are not answered when I check. It will return the ObjectId of the inserted block if successful, it not it will return a Null ObjectId.
public static ObjectId InsertBlock ( Database db, string loName, string blkName, Point3d insPt ) {
ObjectId RtnObjId = ObjectId.Null;
using ( Transaction Trans = db.TransactionManager.StartTransaction( ) ) {
DBDictionary LoDict = Trans.GetObject( db.LayoutDictionaryId, OpenMode.ForRead ) as DBDictionary;
foreach ( DictionaryEntry de in LoDict ) {
if ( string.Compare( ( string )de.Key, loName, true ).Equals( 0 ) ) {
Layout Lo = Trans.GetObject( ( ObjectId )de.Value, OpenMode.ForWrite ) as Layout;
BlockTable BlkTbl = Trans.GetObject( db.BlockTableId, OpenMode.ForRead ) as BlockTable;
BlockTableRecord LoRec = Trans.GetObject( Lo.BlockTableRecordId, OpenMode.ForRead ) as BlockTableRecord;
ObjectId BlkTblRecId = GetNonErasedTableRecordId( BlkTbl.Id, blkName );
if ( BlkTblRecId.IsNull ) {
string BlkPath = HostApplicationServices.Current.FindFile( blkName + ".dwg", db, FindFileHint.Default );
if ( string.IsNullOrEmpty( BlkPath ) )
return RtnObjId;
BlkTbl.UpgradeOpen( );
using ( Database tempDb = new Database( false, true ) ) {
tempDb.ReadDwgFile( BlkPath, FileShare.Read, true, null );
db.Insert( blkName, tempDb, false );
}
BlkTblRecId = GetNonErasedTableRecordId( BlkTbl.Id, blkName );
}
LoRec.UpgradeOpen( );
BlockReference BlkRef = new BlockReference( insPt, BlkTblRecId );
LoRec.AppendEntity( BlkRef );
Trans.AddNewlyCreatedDBObject( BlkRef, true );
BlockTableRecord BlkTblRec = Trans.GetObject( BlkTblRecId, OpenMode.ForRead ) as BlockTableRecord;
if ( BlkTblRec.HasAttributeDefinitions ) {
foreach ( ObjectId objId in BlkTblRec ) {
AttributeDefinition AttDef = Trans.GetObject( objId, OpenMode.ForRead ) as AttributeDefinition;
if ( AttDef != null ) {
AttributeReference AttRef = new AttributeReference( );
AttRef.SetAttributeFromBlock( AttDef, BlkRef.BlockTransform );
BlkRef.AttributeCollection.AppendAttribute( AttRef );
Trans.AddNewlyCreatedDBObject( AttRef, true );
}
}
}
Trans.Commit( );
}
}
}
return RtnObjId;
}
It uses the sub by Tony T. also.
public static ObjectId GetNonErasedTableRecordId( ObjectId TableId, string Name )
// Posted by Tony Tanzillo 01Sept2006
{
ObjectId id = ObjectId.Null;
using( Transaction tr = TableId.Database.TransactionManager.StartTransaction() )
{
SymbolTable table = (SymbolTable) tr.GetObject( TableId, OpenMode.ForRead );
if( table.Has( Name ) )
{
id = table[Name];
if( !id.IsErased )
return id;
foreach( ObjectId recId in table )
{
if( ! recId.IsErased )
{
SymbolTableRecord rec = (SymbolTableRecord) tr.GetObject( recId, OpenMode.ForRead );
if( string.Compare( rec.Name, Name, true ) == 0 )
return recId;
}
}
}
}
return id;
}