Jump to content

DGN Import, Block References are "Line" Type and I can't explode or delete any of them


Recommended Posts

Posted

Hey all,

 

I am running into an issue with working with an DGN imported DWG file.

 

I import the DGN and run my script and none of the blocks are erased.

 

I have found through debugging that the blocks are type "Line" for some reason and because of that, I can't interact with them in any way (see attached screenshot).

 

Has anyone else had this issue before? What am I missing?

Here is the code:

 

private static List<string> blockPrefixes = new List<string> { "CP", "CL", "GRATE", "GRID" };

[CommandMethod("CleanDGN")]
public void ImportAndCleanDwg()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;

    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        // Remove blocks
        BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
        foreach (ObjectId btrId in bt)
        {
            BlockTableRecord btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
            if (blockPrefixes.Any(prefix => btr.Name.StartsWith(prefix)))
            {
                // delete each reference in the drawing
                foreach (ObjectId id in btr)
                {
                    DBObject obj = tr.GetObject(id, OpenMode.ForWrite);

                  	// this is where I checked the type to see why none of the blocks are erased and this is where i found out it is type "Line".
                    Type type = obj.GetType();

                    // Check if the object is a BlockReference
                    if (obj is BlockReference)
                    {
                        obj.Erase();
                    }
                }

                btr.UpgradeOpen();
                btr.Erase();
            }
        }

        tr.Commit();
    }
}

 

Type is Line.png

Posted

I figured it out, I was overcomplicating it.

I wanted to find specific blocks, but after looking at the drawing further, I don't want any of the blocks in it, just the subcomponents and I greatly simplified what I was doing.

 

this is what got me there.

[CommandMethod("CleanDGN")]
public void ImportAndCleanDwg()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;

    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        // Get the current space (ModelSpace or PaperSpace)
        BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
        BlockTableRecord currentSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

        // Iterate over each object in the current space
        foreach (ObjectId id in currentSpace)
        {
            DBObject obj = tr.GetObject(id, OpenMode.ForWrite);

            // Check if the object is a BlockReference
            if (obj is BlockReference blockRef)
            {
                // Explode the block reference
                DBObjectCollection dbObjCollection = new DBObjectCollection();
                blockRef.Explode(dbObjCollection);

                // Add the exploded entities to the current space
                foreach (DBObject dbObj in dbObjCollection)
                {
                    Entity ent = dbObj as Entity;
                    if (ent != null)
                    {
                        currentSpace.AppendEntity(ent);
                        tr.AddNewlyCreatedDBObject(ent, true);
                    }
                }

                // Erase the block reference
                blockRef.Erase();
            }
        }

        tr.Commit();
    }
}

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...