Page 1 of 1

Remove block and its entities from drawing

Posted: 13 Sep 2018, 08:43
by StefanChrist
Hello Support,

I'd like to remove an insert from the drawing. When using

Drawing.RemoveEntity

on the insert it is removed.
There is a block within the insert, this block seems to be left. How can I delete the block and all its entities?

Drawing.RemoveEntity doesn't seem to work with blocks and returns false. The same happens with the entities within the block.

Re: Remove block and its entities from drawing

Posted: 13 Sep 2018, 19:25
by support
Hello Stefan,

AutoCAD drawing files use different sections to store blocks and entities: BLOCKS and ENTITIES, respectively. To remove a block from drawing, you need to access the BLOCKS section and remove the CADBlock object from there. If you know the block name, it can be done by a single line of code:

Code: Select all

drawing.Converter.GetSection(FaceModule.ConvSection.Blocks).RemoveEntityByName("Block1");
Please note that CADInsert entities refer to block(s), so they should be removed before deleting a block.

Mikhail

Re: Remove block and its entities from drawing

Posted: 14 Sep 2018, 08:50
by StefanChrist
Hello Makhail,

thank you for your help. I guess the other entities within a block (in my case a ployline) should also be removed before removing the block. Trying to remove them always returns false:

Code: Select all

    If Not DeleteEntity(Insert) Then
      Stop
    End If
    For Each Entity As rthCadEntity In Insert.Block.Entities
      If Not DeleteEntity(Entity) Then
        Stop
      End If
    Next
    If Not DeleteBlock(Insert.Block) Then
      Stop
    End If
When should those entities be removed?

Thank you!

Re: Remove block and its entities from drawing

Posted: 18 Sep 2018, 22:50
by support
Hello Stefan,

The following call removes a CADBlock and all child entities from the BLOCKS section:

Code: Select all

drawing.Converter.GetSection(FaceModule.ConvSection.Blocks).RemoveEntityByName("Block1");
Please try the following code:

Code: Select all

using System.Collections.Generic;
using CADImport;
...

public static void RemoveBlockFromDrawing(CADImage drawing, string blockName)
{
    CADBlock block = drawing.Converter.BlockByName(blockName);
    if (block != null)
    {
        // Find and remove all inserts of the given block
        List<CADEntity> blockInserts = drawing.CurrentLayout.Entities.FindAll(ent => (ent.EntType == EntityType.Insert & (ent as CADInsert).Block.Name == blockName));
        foreach (CADEntity ins in blockInserts)
            drawing.RemoveEntity(ins);

        // Remove the block from the BLOCKS section
        drawing.Converter.GetSection(FaceModule.ConvSection.Blocks).RemoveEntityByName(blockName);
    }
}
Mikhail