Exploding Civil3D Objects resulting in multiple copies of Block References.

Anthony_PungitoreJFMRZ
Contributor

Exploding Civil3D Objects resulting in multiple copies of Block References.

Anthony_PungitoreJFMRZ
Contributor
Contributor

I am having an issue where after appending the exploded Civil objects to modelspace, I am left with multiple copies of block references, instead of just 1, like when you explode natively using the user interface.

 

namespace Civil3D_ExportTools
{
    public class Civil3D_Exporter
    {
        [CommandMethod("ExportCivil3DToCAD")]
        public void ExportCivil3DToCAD()
        {            
            ExplodeCivilObjectsToAutoCAD();
        }
        
        private void ExplodeCivilObjectsToAutoCAD()
        {
            
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            try
            {
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    Autodesk.Civil.ApplicationServices.CivilDocument civilDoc = Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument;
                    BlockTable blockTable = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                    BlockTableRecord modelSpace = (BlockTableRecord)trans.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

                    foreach (ObjectId entityId in modelSpace)
                    {
                        Autodesk.AutoCAD.DatabaseServices.Entity entity = (Autodesk.AutoCAD.DatabaseServices.Entity)trans.GetObject(entityId, OpenMode.ForWrite);

                        
                        if (entity is Autodesk.Civil.DatabaseServices.Entity || entity is Autodesk.Civil.DatabaseServices.CogoPoint)
                        {
                            // Explode the Civil 3D object
                            ExplodeEntity(entity, trans, db);
                        }
                    }
                    trans.Commit();
                    ed.WriteMessage("\nSuccessfully exploded Civil 3D objects to AutoCAD entities.");
                }                
            }
            catch (System.Exception ex)
            {
                doc.Editor.WriteMessage($"\nError during explosion: {ex.Message}");
            }            
        }

        // Method to explode a Civil 3D object into AutoCAD entities
        private void ExplodeEntity(Autodesk.AutoCAD.DatabaseServices.Entity entity, Transaction t, Database db)
        {
            // Explode the entity by creating a collection of exploded entities
            DBObjectCollection explodedEntities = new DBObjectCollection();
            entity.Explode(explodedEntities);
                        
            foreach (Autodesk.AutoCAD.DatabaseServices.DBObject explodedEntity in explodedEntities)
            {
                if (explodedEntity is Autodesk.AutoCAD.DatabaseServices.Entity explodedEnt)
                {
                    BlockTable bt = t.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    BlockTableRecord modelSpace = t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                    modelSpace.AppendEntity(explodedEnt);
                    t.AddNewlyCreatedDBObject(explodedEnt, true);
                }
            }            
        }        
    }
}

Thank you so much to anyone who cares enough to look at this. 

0 Likes
Reply
273 Views
7 Replies
Replies (7)

ActivistInvestor
Advisor
Advisor

Are the BlockReferences produced by exploding the Civil3d objects identical, or are there variations?  

0 Likes

norman.yuan
Mentor
Mentor

Well, being exploded into one or more BlockReferences is expected behaviour of AEC/AECC objects. Also, the exploding-resulted blockreferences could be a blockreference of a nested block, or nested inside nested block.

 

Not sure what is/are the objective(s) of why you want to explode them, simply exploding them and adding the resulted AutoCAD entities into the database may not make much sense at all. For example, to retrieve C3D label text, you may need to recursively explode the label a few times and the combine the resulted text entities into a meaningful text string.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes

Anthony_PungitoreJFMRZ
Contributor
Contributor

They are identical, but have different anonymous names.

0 Likes

Anthony_PungitoreJFMRZ
Contributor
Contributor

I am trying to replicate the functionality of the native command "aexexporttoautocad" so that I can extend it.  

0 Likes

ActivistInvestor
Advisor
Advisor

If duplicate objects are being created by Explode() it could be intentional or it could be a bug. For example multi-view blocks might be exploded into multiple block references. Without knowing more about the Civil3d entities you're dealing with I can't really tell you much. What happens if you try to explode one of those objects using the explode command? Does it also produce multiple objects?

0 Likes

Anthony_PungitoreJFMRZ
Contributor
Contributor

Exploding Civil3D objects typically results in a single block reference, which may or may not have additional nested block references but never duplicates.  Exploding Civil3D entities typically factors in your current annotation scale to determine the size of mtexts/lines and other annotative aspects of Civil3D elements, ie Alignments, Profile Views, Profiles, Sample Lines, Section Views, etc.  This is how the native Explode has always worked.  Thank you again.

0 Likes

ActivistInvestor
Advisor
Advisor

I imagine that's the problem, as the Civil3d version of the EXPLODE command seems to be doing more than what the Explode() method does.

 

You can try to implement your custom behavior using a TransformOverrule

 

This code, shows how custom explode behavior is implemented for REGION objects using a TransformOverrule. Rather than requiring a custom command to implement the custom behavior, it affects the result of the EXPLODE command.

 

You can also use a TransformOverrule to examine/analyze what the default Explode() implementation for each type of Civil3d entity is producing.

 

0 Likes

Type a product name