Page 1 of 1

Several blocks inside one block

Posted: 21 Jun 2018, 16:31
by FieldConsult
HI,

I have several blocks, in a in memory CADImage, and I need to include these blocks in a single resulting block. I need the blocks to remain as independent blocks within the new block and not copy the entities to a new block.

Can yo show the necessary code to iterate between all the entities in the in memory CADImage and recognize the blocks to be included in the new block. Each source block is associated with its respective TsgDXFInsert and since the TsgDXFInsert only accept one block at a time I have not been able to associate several blocks to a single TsgDXFInsert in a correct way.

Any Idea, thanks!!!

Re: Several blocks inside one block

Posted: 21 Jun 2018, 22:35
by support
Hello,

If you want to combine several blocks in a new single block, you need to create a reference (TsgDXFInsert) for each source block, and then add this block reference to the new block as an entity. Below you will find a code which demonstrates this approach.

Code: Select all

procedure LoadBlockToConverter(ACADImage: TsgCADImage; ABlock: TsgDXFBlock);
var
  I: Integer;
begin
  for I := 0 to ABlock.Count - 1 do
  begin
    ACADImage.Converter.OnCreate(ABlock.Entities[I]);
    ACADImage.Converter.Loads(ABlock.Entities[I]);
  end;
  // If the block doesn't exist, add it to the BLOCKS section
  if ACADImage.Converter.BlockByName(ABlock.Name) = nil then
    ACADImage.Converter.Sections[csBlocks].AddEntity(ABlock);
  // Reload the block in the TsgDXFConverter
  ACADImage.Converter.Loads(ABlock);
end;

procedure CombineBlocksInNewBlock(ATargetImage: TsgCADImage; ANewBlockName: string);
var
  I: Integer;
  vSrcBlock, vNewBlock: TsgDXFBlock;
  vSrcBlockInsert: TsgDXFInsert;
begin
  // Create a definition of the new block
  vNewBlock := TsgDXFBlock.Create;
  vNewBlock.Name := ANewBlockName;
  // Iterate through the source blocks in CADImage
  for I := 0 to ATargetImage.Converter.Sections[csBlocks].Count - 1 do
  begin
    // Read a source block
    vSrcBlock := ATargetImage.Converter.Blocks[I];
    // Create a reference (INSERT) for the source block
    vSrcBlockInsert := TsgDXFInsert.Create;
    vSrcBlockInsert.Block := vSrcBlock;
    vSrcBlockInsert.Point := MakeFPoint(0, 0, 0); // specify coordinates of the insertion point according to your needs
    vSrcBlockInsert.Scale := MakeFPoint(1, 1, 1);
    vSrcBlockInsert.Angle := 0;
    // Add the block reference to the new block
    vNewBlock.AddEntity(vSrcBlockInsert);
  end;
  LoadBlockToConverter(ATargetImage, vNewBlock);
end;
Mikhail

Re: Several blocks inside one block

Posted: 22 Jun 2018, 01:10
by FieldConsult
Hi,

I followed the code as you indicated it but it causes an error in the resulting file can not be opened in autocad. As I have indicated each source block is already belonging to its own Insert, when associating each block with the new block, I think that a conflict of ownership (Owner) of the entities is generated and therefore the code does not work as expected.

I have also tried to create a new CADImage and place the blocks there but the result is also wrong.