cart: empty [ Login ]

Inserting an external block creates two entries in block editor

1 
Sandeep Bhat
3/28/2022 8:47 AM
Hi, I have been trying to insert an external dwg into my existing dwg. I looked up into the samples of DxfBlock, DxfInsert and CloneContext and came up with this code. Unfortunately when I look into the block editor, I see two entries. Is there a way I can achieve this only having one entry? I know I can achieve this using the example in CloneContext, however I want to place this external dwg at a particular position which is where DxfInsert comes into the game but creates two blocks :(. I also tried naming the block same as what is being inserted but then it would throw an stack overflow exception. Any help is appreciated. Thanks in advance! Below is my code:
C# Code:
private static void InsertBlock(DxfModel targetModel) { var sourceModel = CadReader.Read("C:/ExternalDwgs/A0.dwg"); DxfBlock block = new(Path.GetFileName(sourceModel.Filename)); targetModel.Blocks.Add(block); CloneContext cloneContext = new(sourceModel, targetModel, ReferenceResolutionType.CloneMissing); foreach (DxfEntity entity in sourceModel.Entities) { DxfEntity clonedEntity = (DxfEntity)entity.Clone(cloneContext); block.Entities.Add(clonedEntity); } cloneContext.ResolveReferences(); targetModel.Entities.Add(new DxfInsert(block, new Point3D(50,30, 0))); DwgWriter.Write("Sample.dwg", targetModel); }
Two blocks.png
Wout
3/28/2022 9:04 AM
Hi, When you are cloning the entities, this potentially clones a number of inserts, which in turn may clone a number of blocks. There is no way of not cloning these blocks, as that would be incorrect to do. - Wout
Sandeep Bhat
3/28/2022 11:28 AM
Is there a way about without cloning? Basically copy all the entities of a source model into a block and then insert / add into the target model but at a specific position ? I tried the code below but I get an error opening the dwg file in autocad.
C# Code:
private static void InsertBlock(DxfModel targetModel) { var sourceModel = CadReader.Read("C:/ExternalDwgs/A0.dwg"); DxfBlock block = new(Path.GetFileNameWithoutExtension(sourceModel.Filename)); targetModel.Blocks.Add(block); foreach (DxfEntity entity in sourceModel.Entities) { block.Entities.Add(entity); } var dxfInsert = new DxfInsert(block, new Point3D(50, 30, 0)); targetModel.Entities.Add(dxfInsert); DwgWriter.Write("Sample.dwg", targetModel); }
Wout
3/28/2022 11:40 AM
Hi, That code will result in a corrupt target model. You can't mix objects from 2 different models. The normal way of doing this is creating a DxfBlock and setting property ExternalReference to the drawing you want to insert. - Wout
Sandeep Bhat
3/28/2022 12:42 PM
Yes, with this I can load an external reference and can view it under external references. But I need this to display under blocks. The use case is like I open an existing drawing in Autocad, insert a block and then it is displayed under the block editor. I am trying to replicate this use case programmatically using cadlib :)
Wout
3/28/2022 1:15 PM
I don't think I can provide you any further assistance with this. Good luck.
Sandeep Bhat
3/28/2022 1:27 PM
Hi Wout, I found the problem. Basically if I insert an external dwg which consists of multiple entities it works fine. But if I insert an external dwg which itself contains only a block then the insertion fails. Thanks for your support!
1