How to correctly copy entities (e.g. CadCircle) from one CadImage(template) to another DwgImage(new)?

Goal

I’m trying to reuse block definitions from a DWG template (CadImage) and build a new drawing (DwgImage) by copying its contents — particularly the entities inside CadBlockEntity.Entities.

What I’m doing

I load the block definition from the template and manually clone each entity like this:

 foreach (var entity in templateBlockEntity.Entities)
 {
        entity.ObjectHandle = NextAvailableHandle(dwgImage);
        entity.SoftOwner = cadBlockTable.ObjectHandle;
        cadBlockEntity.Entities.Add(entity);
}

This works for some entity types (e.g. CadLwPolyline) but causes issues for others.

Problem

When I open the generated DWG file in AutoCAD (or FME), I get errors like:

typescript

KopiërenBewerken

Reading handle 2C3 object type AcDbCircle  
  Error 34 (eWrongObjectType) → Object discarded

Only certain entity types (such as CadCircle, CadInsertObject, or CadArc) seem to be affected.
AutoCAD discards them, even though I assign new handles and owners.

What I’m asking

What is the correct way to copy or clone an entity from one CadImage to another DwgImage in Aspose.CAD for .NET, without causing DWG structural errors like eWrongObjectType?

  • Is it safe to reuse entities from another image at all?
  • Is there a built-in method for deep-cloning entities that resets all required metadata?
  • Do entities from a CadImage carry hidden internal state that makes them incompatible when reused?

Environment

  • Aspose.CAD for .NET version: 8
  • Output format: DWG 2018
  • Validation tools: AutoCAD
    See full code from the AddBlocksToDwg(...) function below:
var cadBlockTable = new CadBlockTableObject()
{
    ObjectHandle = NextAvailableHandle(dwgImage),
    SoftOwner = dwgImage.BlocksTables.CadSymbolTableGroupCodes.ObjectHandle,
    BlockName = blockName,
    IsAnonymous = false,
    BlockExplodability = 2
};
var cadBlockNameEntity = new CadBlockNameEntity()
{
    ObjectHandle = NextAvailableHandle(dwgImage),
    LayerName = "0",
    XDirMissingFlag = true,
};
var cadEndBlockNameEntity = new CadBlockNameEntity()
{
    ObjectHandle = NextAvailableHandle(dwgImage),
    SoftOwner = cadBlockTable.ObjectHandle,
    LayerName = "0",
    XDirMissingFlag = true,
};
var cadBlockEntity = new CadBlockEntity()
{
    BlockHandle = cadBlockNameEntity.ObjectHandle,
    EndBlockHandle = cadEndBlockNameEntity.ObjectHandle,
    EndBlockLayerName = layerName,
    LayerName = "0",
    Name = blockName,
    SoftOwner = cadBlockTable.ObjectHandle,
    CadBlockNameEntity = cadBlockNameEntity,
    CadEndBlockNameEntity = cadEndBlockNameEntity,
    CadBlockTableObject = cadBlockTable
};

// Add a block to the drawing
var blockEntities = dwgImage.BlockEntities;
blockEntities.Add(cadBlockEntity.Name, cadBlockEntity);
dwgImage.BlockEntities = blockEntities;
dwgImage.BlocksTables.Add(cadBlockTable);

// Create entities and add them to the block
var templateBlockEntities = templateImage.BlockEntities;
if (templateBlockEntities.ContainsKey(blockName))
{
    var templateBlockEntity = templateBlockEntities[blockName];
    if (templateBlockEntity == null)
    {
        //log
        Log.Warning("Block {BlockName} not found in template image.", blockName);
    }
    if (templateBlockEntity != null)
    {

        foreach (var entity in templateBlockEntity.Entities)
        {
            //entity.ObjectHandle = NextAvailableHandle(dwgImage);
            //entity.SoftOwner = cadBlockTable.ObjectHandle;

            var nextAvailableHandle = NextAvailableHandle(dwgImage);
            var softOwner = cadBlockTable.ObjectHandle;
            var newEntity = CloneEntity(entity, nextAvailableHandle, softOwner);
            cadBlockEntity.Entities.Add(newEntity);
        }
        //Log.Information("Block {BlockName} added to new blocks.", blockName);


    }
}

Thank you for your help!

@Abne

To correctly copy entities such as CadCircle from one CadImage (template) to another DwgImage (new), you need to ensure that you are deep-cloning the entities properly. This is crucial because certain entities may carry internal states or metadata that can lead to errors like eWrongObjectType when reused directly.

Steps to Clone Entities Correctly

  1. Deep Clone Entities: Instead of directly assigning the entity to the new block, create a new instance of the entity and copy the necessary properties. This ensures that any internal state or references are reset.

  2. Reset Object Handles and Owners: When cloning, make sure to assign new object handles and set the soft owner to the new block table’s object handle.

  3. Use a Cloning Method: Implement a method that handles the cloning of different entity types. This method should account for the specific properties that need to be copied for each entity type.

Example Code

Here’s an example of how you might implement the cloning process:

private CadEntityBase CloneEntity(CadEntityBase entity, long newHandle, long softOwner)
{
    CadEntityBase newEntity = (CadEntityBase)entity.Clone(); // Assuming Clone method is available
    newEntity.ObjectHandle = newHandle;
    newEntity.SoftOwner = softOwner;

    // Additional properties may need to be reset based on entity type
    // For example, if the entity has specific properties that need to be cleared or reset
    // newEntity.SomeProperty = defaultValue;

    return newEntity;
}

// Usage in your existing code
foreach (var entity in templateBlockEntity.Entities)
{
    var nextAvailableHandle = NextAvailableHandle(dwgImage);
    var softOwner = cadBlockTable.ObjectHandle;
    var newEntity = CloneEntity(entity, nextAvailableHandle, softOwner);
    cadBlockEntity.Entities.Add(newEntity);
}

Important Considerations

  • Entity-Specific Properties: Some entities may have properties that are specific to their type (e.g., CadCircle may have radius, center point, etc.). Ensure that these properties are also copied or reset appropriately during the cloning process.

  • Testing in AutoCAD: After generating the DWG file, always validate it in AutoCAD or similar tools to catch any structural errors early.

By following these guidelines, you should be able to copy entities from one CadImage to another without encountering compatibility issues. If you continue to face problems, consider checking the specific properties of the entities that are causing errors and ensure they are being handled correctly during the cloning process.

@Abne,
Hi.
There is no built-in way to copy entity from one file to another, and all errors appearing could be entity-related. Theoretically reusing is possible. But it depends on a lot of circumstances in practice. For instance, it cound be handles conflict, the entity may use some style/layer that is missing in another drawing, some entities could be related to different ones (for example hatch and its border), and some entities store additional information in related objects inside a drawing.

Please create and share the example about the particular cases you can observe the issues with (the initial file, the code you are using to copy data from it and the destination file if it is required), so we can run it, reproduce and analyze exactly.

Hi all,

Thanks for your insights and help.

:white_check_mark: I’ve resolved the issue by creating a completely new instance of the entity (e.g., CadCircle) and then manually copying its relevant properties instead of trying to reuse the original object.

This approach avoids internal conflicts related to handles, styles, and drawing-specific dependencies that were causing errors:

What worked:

  • Creating a new CadCircle()
  • Assigning a new ObjectHandle and SoftOwner
  • Copying required fields like CenterPoint, Radius, ColorId, etc.

By doing this, the generated DWG file loads without errors in AutoCAD.

Thanks again,

1 Like

@Abne,
thank you for all details, happy to hear problem is resolved :slight_smile: