.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Redefine dynamic block

1 REPLY 1
Reply
Message 1 of 2
Ertqwa
433 Views, 1 Reply

Redefine dynamic block

Hello Forum,

 

I use the following code to redefine a block:

 

objBlockTable = (BlockTable)objTransactionManager.GetObject(objDatabaseSource.BlockTableId, OpenMode.ForRead, false);
structObjectId = objBlockTable[strBlockName];
objBlockTableRecord = (BlockTableRecord)objTransactionManager.GetObject(structObjectId, OpenMode.ForRead, false);
objObjectIdCollection.Add(structObjectId);
objIdMapping = new IdMapping();
objDatabaseSource.WblockCloneObjects(objObjectIdCollection, objDatabase.BlockTableId, objIdMapping, DuplicateRecordCloning.Replace, false);

 

It works for normal blocks but it does not redefine dymanic blocks. Is there a way to redefine dynamic blocks?

 

Thank you.

1 REPLY 1
Message 2 of 2
Ertqwa
in reply to: Ertqwa

Hello again,

 

I've come up with the following code for redefining dynamic blocks:
- Redefine dynamic block (not in code).
- Store dynamic property values of each block reference.
- Reset each block reference to 'apply' the newly redefined block.
- Set the stored dynamic property values.
* Calling ResetBlock and Setting dynamic property values in a single transaction results in strange behavior, so multiple transactions are required.

 

If anyone knows a better/more efficient way, please let me know. Thank you,

 

Test code:

 

[CommandMethod("AnalyzeDyn", CommandFlags.Session)]
public void AnalyzeDyn()
{
    Document objDocument;
    Database objDatabase;
    DocumentLock objDocumentLock;
    Transaction objTransaction;
    BlockTable objBlockTable;
    BlockTableRecord objBlockTableRecord;
    string strMsg = "";
    BlockReference objBlockReference;
    Dictionary<string, object> objDictionary = new Dictionary<string, object>();
    List<ObjectId> oidList = new List<ObjectId>();
    double dblValue;

    try
    {
        objDocument = CADS.Application.DocumentManager.MdiActiveDocument;
        objDatabase = objDocument.Database;
        using (objDocumentLock = objDocument.LockDocument())
        {
            using (objTransaction = objDocument.TransactionManager.StartTransaction())
            {
                // Get the blocktable of the database.
                objBlockTable = objTransaction.GetObject(objDatabase.BlockTableId, OpenMode.ForRead) as BlockTable;
                // 1: Add all blocktablerecord id's to a list.
                //      Then loop this list.
                //      You can't loop 'objBlockTable' directly because it will be altered inside the loop.
                //      (I think by 'ResetBlock') That will result in an endless loop.
                //      Reset will create a new anonymous block.
                // 2: Calling 'ResetBlock' and editing dynamic properties in the same transaction causes strange results.
                oidList.Clear();
                foreach (ObjectId oidBlockTableRecord in objBlockTable)
                {
                    oidList.Add(oidBlockTableRecord);
                }
            }
            // Get each ObjectID in the blocktable.
            foreach (ObjectId oidBlockTableRecord in oidList)
            {
                // Check if the ObjectID is anonymous.
                using (objTransaction = objDocument.TransactionManager.StartTransaction())
                {
                    objBlockTableRecord = objTransaction.GetObject(oidBlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
                }
                if (objBlockTableRecord.IsAnonymous)
                {
                    // Get all block references.
                    foreach (ObjectId oidBlockReference in objBlockTableRecord.GetBlockReferenceIds(false, false))
                    {
                        objDictionary.Clear();
                        if (strMsg.Length > 0) { strMsg += System.Environment.NewLine; }
                        using (objTransaction = objDocument.TransactionManager.StartTransaction())
                        {
                            objBlockReference = objTransaction.GetObject(oidBlockReference, OpenMode.ForWrite) as BlockReference;
                            // Get all properties.
                            foreach (DynamicBlockReferenceProperty objItem in objBlockReference.DynamicBlockReferencePropertyCollection)
                            {
                                strMsg += objItem.PropertyName.ToString();
                                strMsg += " = " + objItem.Value.ToString();
                                strMsg += " (" + objItem.Value.GetType().ToString() + ").";
                                // Only add items that are not readonly.
                                if (!objItem.ReadOnly && !objDictionary.ContainsKey(objItem.PropertyName))
                                { objDictionary.Add(objItem.PropertyName, objItem.Value); }
                            }
                            // Reset.
                            objBlockReference.ResetBlock();
                            // Commit.
                            objTransaction.Commit();
                        }
                        using (objTransaction = objDocument.TransactionManager.StartTransaction())
                        {
                            objBlockReference = objTransaction.GetObject(oidBlockReference, OpenMode.ForWrite) as BlockReference;
                            foreach (DynamicBlockReferenceProperty objItem in objBlockReference.DynamicBlockReferencePropertyCollection)
                            {
                                if (!objItem.ReadOnly && objDictionary.ContainsKey(objItem.PropertyName))
                                {
                                    dblValue = (double)objDictionary[objItem.PropertyName];
                                    dblValue += 100;
                                    objItem.Value = dblValue;
                                }
                            }
                            // Commit.
                            objTransaction.Commit();
                        }
                    }
                }
            }
        }
        //MessageBox.Show(strMsg, "Dynamic block properties.");
    }//try
    catch (CADR.Exception Ex)
    {
        MessageBox.Show("Error analyzing dynamic blocks: " + Ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost