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 theAddBlocksToDwg(...)
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!