Author Topic: pass command an argument  (Read 5757 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: pass command an argument
« Reply #15 on: April 28, 2008, 04:16:33 PM »
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.

Code: [Select]
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.

Code: [Select]
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;
}
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Glenn R

  • Guest
Re: pass command an argument
« Reply #16 on: April 29, 2008, 04:44:29 AM »
Duh,

Can't you specify the layer in the properties for the tool on the palette directly? From memory you can set the layer and it will create it if necessary and place the block on that layer...at least you could last time I played with palettes.

Cheers,

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: pass command an argument
« Reply #17 on: April 29, 2008, 10:07:59 AM »
Glenn, at this point we are not using palettes.  You are correct though, you can specify the layer in the props of the pallette.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)