Page 1 of 1
How to convert two metafile to one dxf with two layers
Posted: 24 Jun 2022, 17:44
by pierpaolo.degrandis
Hi,
I need to create a DXF file with two layers, then load two metafile and draw it one for layer.
How can i do?
Re: How to convert two metafile to one dxf with two layers
Posted: 27 Jun 2022, 15:17
by support
pierpaolo.degrandis wrote: ↑24 Jun 2022, 17:44
Hi,
I need to create a DXF file with two layers, then load two metafile and draw it one for layer.
How can i do?
Hi, you should create a drawing, add layers and IMAGEENT entities to the drawing, then set the entities to the corresponding layers:
Code: Select all
function AddImageEnt(ACADImage: TsgCADImage; const AFileName: string;
APosition: TFPoint; const ALayerName: string): TsgDXFImageEnt;
var
vImageDef: TsgDXFImageDef;
vImageDefName: string;
begin
Result := TsgDXFImageEnt.Create;
vImageDefName := ChangeFileExt(ExtractFileName(AFileName), '');
vImageDef := TsgDXFImageDef(ACADImage.Converter.Sections[csImageDefs].FindEntByName(vImageDefName));
if not Assigned(vImageDef) then
begin
vImageDef := TsgDXFImageDef.Create;
vImageDef.Name := vImageDefName;
vImageDef.FileName := AFileName;
ACADImage.Converter.Sections[csImageDefs].AddEntity(vImageDef);
ACADImage.Converter.Loads(vImageDef);
end;
Result.ImageDef := vImageDef;
Result.Point := APosition;
Result.Layer := ACADImage.Converter.LayerByName(ALayerName);
ACADImage.CurrentLayout.AddEntity(Result);
ACADImage.Converter.Loads(Result);
end;
Code: Select all
...
var
vImageEnt: TsgDXFImageEnt;
begin
vImageEnt := AddImageEnt(FImg, 'd:\New1.emf', MakeFPoint(0, 0, 0), 'my_layer1');
AddImageEnt(FImg, 'd:\New2.emf', MakeFPoint(vImageEnt.Width, 0, 0), 'my_layer2');
FImg.GetExtents;
btnFitToScreenClick(nil);
end;
Best regards,
Catherine.
Re: How to convert two metafile to one dxf with two layers
Posted: 28 Jul 2022, 13:33
by pierpaolo.degrandis
Hi Catherine,
thanks for your reply.
I create this procedure to create a DXF file from two metafiles on two different layers:
Code: Select all
class procedure TImageConvertion.AddMetafileToCADImage(ACADFile: TsgCADImage; AFileName: string; AWidth: Integer; ALayerName: string);
var
vImageDef: TsgDXFImageDef;
DXFImageEnt: TsgDXFImageEnt;
vLayout: TsgDXFLayout;
begin
vImageDef := TsgDXFImageDef.create;
vImageDef.FileName := AFileName;
ACADFile.Converter.Loads(vImageDef);
if vImageDef.Picture.Graphic <> nil then
begin
ACADFile.Converter.Sections[csImageDefs].AddEntity(vImageDef);
DXFImageEnt := TsgDXFImageEnt.create;
DXFImageEnt.ImageDef := vImageDef;
DXFImageEnt.Point := MakeFPoint(0, 0, 0);
DXFImageEnt.UVector := cnstXOrtAxis;
DXFImageEnt.VVector := cnstYOrtAxis;
DXFImageEnt.Size := DXFImageEnt.ImageDef.Size;
DXFImageEnt.Width := AWidth;
if ALayerName <> '' then
DXFImageEnt.Layer := ACADFile.Converter.LayerByName(ALayerName);
vLayout := ACADFile.Converter.Layouts[0];
if vLayout <> nil then
begin
if Assigned(ACADFile.Converter.OnCreate) then
ACADFile.Converter.OnCreate(DXFImageEnt);
ACADFile.Converter.Loads(DXFImageEnt);
vLayout.AddEntity(DXFImageEnt);
end;
end;
end;
class procedure TImageConvertion.MetafileToDxfFile(ExportFilename: string; ImportFilename1, ImportFilename2: string)overload;
var
vDimStyle: TsgDXFDimensionStyle;
FCADFile1: TsgCADImage;
FDXFLayer: TsgDXFLayer;
vExp: TsgCADExport;
ext: string;
begin
FCADFile1 := TsgCADImage.create;
FCADFile1.Converter.InitializeSectionsBegin;
FCADFile1.Converter.InitializeSectionsEnd;
// vDimStyle := TsgDXFDimensionStyle(FCADFile1.Converter.DimensionStyleByName(FCADFile1.Converter.HeadVarStruct.DimStyle));
// vDimStyle.DIMBLK1T := datOpen;
// FCADFile1.Converter.Loads(vDimStyle);
AddMetafileToCADImage(FCADFile1, ImportFilename1, 5000, '');
FCADFile1.GetExtents;
AddMetafileToCADImage(FCADFile1, ImportFilename2, 5000, 'graphics');
ext := LowerCase(ExtractFileExt(ExportFilename));
if (ext = '.dxf') or (ext = '.dwg') then
begin
if ext = '.dxf' then
vExp := TsgCADtoDXF.create(FCADFile1)
else
vExp := TsgCADtoDWG.create(FCADFile1);
vExp.SaveToFile(ExportFilename);
vExp.Free;
end;
FCADFile1.Free;
end;
but...
I have this problem:
If I delete the metafiles then the DXF will be empty.
It seems that metafiles are linked in DXF not realy imported.
What I wrong?
Re: How to convert two metafile to one dxf with two layers
Posted: 01 Aug 2022, 10:31
by support
Hello,
Please use the code example below:
type
TsgDXFExportAccess = class(TsgDXFExport);
procedure TfmDXFExporter.btnEmf2dwgClick(Sender: TObject);
var
vExport: TsgDXFExport;
I: Integer;
vCADImage: TsgVectorImage;
vLayers: TStringList;
vMetafile: TMetafile;
vImageInInClip: Boolean;
vBox: TFRect;
begin
OpenPictureDialog.Options := OpenPictureDialog.Options +
[ofAllowMultiSelect];
if not OpenPictureDialog.Execute then Exit;
if cbUseOneFile.Checked then
begin
vCADImage := TsgVectorImage.Create;
vCADImage.Converter.InitializeSectionsBegin;
vCADImage.Converter.InitializeSectionsEnd;
vCADImage.CurrentLayout := vCADImage.Layouts[0];
vExport := TsgDXFExport.Create;
try
TsgDXFExportAccess(vExport).SetCADImage(vCADImage);
SetDXFParams(vExport);
vLayers := TStringList.Create;
try
vLayers.OwnsObjects := True;
vBox := MakeFRectByPoints(cnstFPointZero, cnstFPointZero);
for I := 0 to OpenPictureDialog.Files.Count - 1 do
begin
vMetafile := TMetafile.Create;
vMetafile.LoadFromFile(OpenPictureDialog.Files.Strings);
ExpandFRect(vBox, MakeFPointFrom2D(GetMetafileSize(vMetafile)));
vLayers.AddObject(ExtractFileName(OpenPictureDialog.Files.Strings),
vMetafile);
end;
ConvertToCad(nil, vLayers, vCADImage, vBox, nil, '', nil, nil,
vImageInInClip);
finally
vLayers.Free;
end;
vExport.SaveToFile(ChangeFileExt(OpenPictureDialog.Files.Strings[0],
'.dwg'));
finally
vExport.Free;
end;
end
else
for I := 0 to OpenPictureDialog.Files.Count - 1 do
begin
vExport := TsgDXFExport.Create;
try
SetDXFParams(vExport);
vExport.LoadFromFile(OpenPictureDialog.Files.Strings);
vExport.SaveToFile(ChangeFileExt(OpenPictureDialog.Files.Strings,
'.dwg'));
finally
vExport.Free;
end;
end;
end;
Kind regards,
Suraya
Re: How to convert two metafile to one dxf with two layers
Posted: 08 Nov 2022, 14:08
by pierpaolo.degrandis
Hi Suraya,
starting from DXFExporter demo example, I've replaced TfmDXFExporter.btnEmf2dwgClick method with your one.
I have some compilation errors:
Cannot find:
TsgDXFExportAccess(vExport).SetCADImage(vCADImage);
Cannot find:
ConvertToCad(nil, vLayers, vCADImage, vBox, nil, '', nil, nil, vImageInInClip);
Can you help me?
(I suppose your snippet start from an older version than 15)
Re: How to convert two metafile to one dxf with two layers
Posted: 09 Nov 2022, 16:14
by support
Hello,
Thank you for the information.
It was forwarded to our department of software developers.
We will contact you by e-mail to get more details.
Kind regards,
Suraya Mamedova
Re: How to convert two metafile to one dxf with two layers
Posted: 10 Nov 2022, 13:48
by pierpaolo.degrandis
Hi Suraya
thank you for your support.
I finally solved:
I've add a method to class TsgDXFExportDirect to export a list of metafile in more layers:
Code: Select all
procedure TsgDXFExportDirect.LoadLayersFromMetafiles(MetafileList: TStringList);
var
vMetafileLoader: TsgConvertMetafileToCad;
vBox: TFRect;
LastMetafile: TMetafile;
begin
try
FreeAndNil(FCADImage);
except
{$IFDEF _FIXINSIGHT_} on E: Exception do
EmptyExceptionHandler(E); {$ENDIF}
end;
FCADImage := TsgVectorImage.Create;
try
LastMetafile := TMetafile(MetafileList.Objects[MetafileList.Count - 1]);
FCADImage.Converter.InitializeSectionsBegin;
FCADImage.Converter.InitializeSectionsEnd;
FCADImage.CurrentLayout := FCADImage.Layouts[0];
if IsBadRect(FOwnerBox) then
vBox := MakeFRectByPoints(cnstFPointZero, MakeFPointFrom2D(GetMetafileSize(LastMetafile)))
else
vBox := FOwnerBox;
vMetafileLoader := TsgConvertMetafileToCad.Create(FCADImage, nil, vBox, '', FExtData);
try
vMetafileLoader.AlternateBlack := AlternateBlack;
vMetafileLoader.Use01MM := Use01MM;
vMetafileLoader.Scale := Scale;
vMetafileLoader.UnitSize := UnitSize;
vMetafileLoader.OffsetX := Offset.X;
vMetafileLoader.OffsetY := Offset.Y;
vMetafileLoader.LineWeightScale := FLineWeightScale;
vMetafileLoader.Convert(MetafileList, nil);
finally
vMetafileLoader.Free;
end;
except
FreeAndNil(FCADImage);
end;
end;
I call this method like this:
Code: Select all
class procedure TImageConvertion.MetafileToDxfFile(ExportFilename: string; Metafile1, Metafile2: TMetafile)overload;
var
vExport: TsgDXFExport;
AList: TStringList;
Begin
vExport := TsgDXFExport.create;
try
vExport.AlternateBlack := false;
vExport.Use01MM := false;
vExport.Version := acR2004;
vExport.Millimetres := True;
vExport.Scale := 0.01;
AList := TStringList.Create;
AList.AddObject('Second', Metafile2);
AList.AddObject('', Metafile1);
vExport.LoadLayersFromMetafiles(AList);
vExport.SaveToFile(ExportFilename);
finally
vExport.Free;
end;
end;
I've inverted the order of metafile (and layers) because I need the first layers to be in front of the second one (as you noted me).
Many thanks.
Re: How to convert two metafile to one dxf with two layers
Posted: 10 Nov 2022, 14:14
by support
Hello,
You are welcome!
Feel free to contact us if any questions arise.
Kind regards,
Suraya Mamedova