Hello Gentlemen,
I am getting Fatal error at the explode method in the following highlighed (red) code.
Can you please help me?
public class ExplodeDynamicBlocks
{
[CommandMethod("EDB")]
public static void ExplodeDymanicBlockReference()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt["JDuct-SP"], OpenMode.ForWrite) as BlockTableRecord;
ObjectIdCollection oidc1 = btr.GetAnonymousBlockIds();
ObjectIdCollection oidc2 = btr.GetBlockReferenceIds(true, false);
DBObjectCollection dboc = new DBObjectCollection();
foreach (ObjectId id in oidc1)
{
BlockReference br = tr.GetObject(id, OpenMode.ForWrite) as BlockReference;
br.ExplodeToOwnerSpace(); // I am getting Fatal Error at this point
}
}
}
}
Also, if possible, how to combine two objectidcollection into one?
Solved! Go to Solution.
Solved by Alfred.NESWADBA. Go to Solution.
Hi,
Two questions:
- alfred -
BTW: you have not commited the transaction.
Hi,
>> I don't know how to verify owner?
Every object has a property .OwnerID that gives you the ObjectID to the owner object.
As there are some differences in the usage of dynamic blocks and the resulting anonymous blocks can you upload a sample drawing that shows the problem when running your code?
- alfred -
Here I found the problem when I debugged.
In my code, there are two objectid collection.
1. ObjectIdCollection oidc1 = btr.GetAnonymousBlockIds();
2. ObjectIdCollection oidc2 = btr.GetBlockReferenceIds(true, false);
when I tried with the second object "oidc2" it works fine.
foreach (ObjectId id in oidc2) { BlockReference br = tr.GetObject(id, OpenMode.ForWrite) as BlockReference; br.ExplodeToOwnerSpace(); }
The problem is with the first object "oidc1".
foreach (ObjectId id in oidc1) { BlockReference br = tr.GetObject(id, OpenMode.ForWrite) as BlockReference; br.ExplodeToOwnerSpace(); // Problem }
In the above code, the "br" object is null. That is why the autocad is crashing when trying to explode null object.
Can you help me now how to explode annonumous blocks?
BlockReference br = tr.GetObject(id, OpenMode.ForWrite) as BlockReference;
This is code, we have to change.
(Thanks for your immediate response)
Hi,
>> In the above code, the "br" object is null
And when you check the following two properties (before accessing the blockreference):
id.isValid
id.isErased
how are they set?
- alfred -
Hi,
can you upload a drawing that shows that problem?
And let us know what type of AutoCAD (vertical product?), what release and what servicepack you have.
- alfred -
Hi,
well, got it to run (and learned something 😉 😞
When you look to that object:
tr.GetObject(id, OpenMode.ForWrite)
AutoCAD returns a BlockTableRecord, not a BlockReference!
So I tried then to get the Blockreferences for that BlockTableRecord and exploded them ... and that worked:
For Each id As ObjectId In oidc1
Dim btr2 As BlockTableRecord = TryCast(tr.GetObject(id, OpenMode.ForWrite), BlockTableRecord)
For Each id2 As ObjectId In btr2.GetBlockReferenceIds(True, False)
Dim br2 As BlockReference = TryCast(tr.GetObject(id2, OpenMode.ForWrite), BlockReference)
br2.ExplodeToOwnerSpace()
Next
Next
(no cast checking, no error handling, no disposes, just shows the workflow!)
HTH, - alfred -
glad I could help, you are welcome, - alfred -
Can't find what you're looking for? Ask the community or share your knowledge.