Insert DXF/DWG drawing inside DXF

How to insert DXF/DWG drawing inside DXF

Issue: How to insert DXF/DWG drawing inside DXF.

Tips: To do this, you must first create a CadInsertObject with the necessary values, then get all the blocks in the CadBlockDictionary, create a new CadBlockEntity block and add it to the CadBlockDictionary, add the CadBlockDictionary to the BlockEntities of the drawing, add a CadInsertObject to the entities of the drawing for the array.

Example:

/ this part is not required but detects the first available free ID for handle as each object should have unique one
// assuming that we only make export, but not saving to dxf/dwg, you may assign any unique value
string newObjectID = "FFFFFF";
if (cadImage.Header.HeaderProperties.ContainsKey(CadHeaderAttribute.HANDSEED))
{
newObjectID = ((CadStringParameter)cadImage.Header.HeaderProperties[CadHeaderAttribute.HANDSEED][0]).Value;
int nextAvailableID = int.Parse(newObjectID, System.Globalization.NumberStyles.HexNumber) + 1;
((CadStringParameter)cadImage.Header.HeaderProperties[CadHeaderAttribute.HANDSEED][0]).Value = nextAvailableID.ToString("X");
}
CadInsertObject newInsert = new CadInsertObject();
newInsert.Name = "foreground";
newInsert.SoftOwner.Value = cadImage.Layouts["Model"].BlockTableRecordHandle;
newInsert.ObjectHandle = newObjectID;
newInsert.LayerName = "0";
newInsert.InsertionPoint.X = 200;
newInsert.InsertionPoint.Y = -1300;
CadBlockDictionary allBlocks = cadImage.BlockEntities;
CadBlockEntity newBlock = new CadBlockEntity();
newBlock.XRefPathName.Value = "foreground.dwg";
allBlocks.Add(newInsert.Name, newBlock);
cadImage.BlockEntities = allBlocks;
List entities = new List(cadImage.Entities);
entities.Add(newInsert);
cadImage.Entities = entities.ToArray();
cadImage.Save(...)