TsgDXFImage.GetExtents - Error
Moderators: SDS, support, admin
TsgDXFImage.GetExtents - Error
I load from base DXF:
If comment a line: FreeAndNil(vCAD); // all works
Why there is an error if I release object vCAD?
Code: Select all
<b>var</b> Img: TsgDXFImage;
<b>procedure</b> TMainForm.DrawCAD(ID: String; PRX, PRY:Real);
<b>var</b> vInsert: TsgDXFInsert;
vCAD: TsgDXFImage;
Stream:TStream;
vPoint:TFPoint;
<b>begin</b>
vCAD:=TsgDXFImage.Create;
<b>try</b>
vCAD.IsWithoutBorder:=True;
with pFIBDataSet1 do Stream:=CreateBlobStream(FieldByName('PIC'), bmRead);
<b>try</b>
vCAD.LoadFromStream(Stream);
<b>finally</b>
FreeAndNil(Stream);
<b>end</b>;
<b>with</b> vCAD <b>do</b> CurrentLayout:=Converter.Layouts[Converter.DefaultLayoutIndex];
vInsert:=TsgDXFInsert.Create;
vInsert.AssignEntity(vCAD.Converter.Sections[csEntities].Entities[0]);
vInsert.Block.Name:=ID;
Img.Converter.Sections[csEntities].AddEntity(vInsert);
vPoint.X:=PRX;
vPoint.Y:=PRY;
vInsert.Point:=vPoint;
Img.Converter.Loads(vInsert);
<b>finally</b>
FreeAndNil(vCAD);
<b>end</b>;
Img.GetExtents; // Error Access Violation
<b>end</b>;
Why there is an error if I release object vCAD?
Hello Yura,
1. It is necessary to use <b>Img.Converter.OnCreate(vInsert);</b> after <b>vInsert.AssignEntity(vCAD.Converter.Sections[csEntities].Entities[0]);</b>. It makes it visible.
2. Entity <b>Insert</b> which you copy to <b>vInsert</b> contains such fields as <b>Layer</b>, <b>Block</b> etc. When you call <b>FreeAndNil(vCAD);</b> you destroy <b>TsgDXFImage</b> class object which contains these objects (Layer, Block etc). So <b>vInsert</b> refers to unexisting objects. That is why <b>Error Access Violation</b> arises.
It is necessary to set these fields for <b>vInsert</b> before calling <b>Img.Converter.Loads(vInsert);</b>.
Sergey.
please post questions to the forum or write to support@cadsofttools.com
1. It is necessary to use <b>Img.Converter.OnCreate(vInsert);</b> after <b>vInsert.AssignEntity(vCAD.Converter.Sections[csEntities].Entities[0]);</b>. It makes it visible.
2. Entity <b>Insert</b> which you copy to <b>vInsert</b> contains such fields as <b>Layer</b>, <b>Block</b> etc. When you call <b>FreeAndNil(vCAD);</b> you destroy <b>TsgDXFImage</b> class object which contains these objects (Layer, Block etc). So <b>vInsert</b> refers to unexisting objects. That is why <b>Error Access Violation</b> arises.
It is necessary to set these fields for <b>vInsert</b> before calling <b>Img.Converter.Loads(vInsert);</b>.
Sergey.
please post questions to the forum or write to support@cadsofttools.com
<blockquote id="quote"><font size="2" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote">
It is necessary to set these fields for <b>vInsert</b> before calling <b>Img.Converter.Loads(vInsert);</b>
<hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote">
If it is possible an example of a code please.
It is necessary to set these fields for <b>vInsert</b> before calling <b>Img.Converter.Loads(vInsert);</b>
<hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote">
If it is possible an example of a code please.
The following code works:
Sergey.
please post questions to the forum or write to support@cadsofttools.com
Code: Select all
<b>type</b>
TForm1 = <b>class</b>(TForm)
sgImage1: TsgImage;
...
<b>procedure</b> TForm1.Button2Click(Sender: TObject);
<b>type</b>
TsgClassOfEntity = <b>class of</b> TsgDXFEntity;
<b>var</b>
I: Integer;
vInsert: TsgDXFInsert;
vLayer: TsgDXFLayer;
vBlock: TsgDXFBlock;
vCAD: TsgDXFImage;
vEntity: TsgDXFEntity;
Converter: TsgDXFConverter;
<b>begin</b>
sgImage1.Picture.Graphic := <b>nil</b>;
Img := TsgDXFImage.Create;
Converter := Img.Converter;
vLayer := <b>nil</b>;
vCAD := TsgDXFImage.Create;
<b>try</b>
vCAD.LoadFromFile('C:\Test.dxf'); //simple DXF file with two entities: line and inserted line
vCAD.IsWithoutBorder := True;
I := 0;
<b>while</b> (I < vCAD.Converter.Counts[csEntities]) <b>and not</b> (vCAD.Converter.Entities[I] <b>is</b> TsgDXFInsert) <b>do</b>
Inc(I);
<b>if</b> I >= vCAD.Converter.Counts[csEntities] <b>then</b> Exit;
vInsert := TsgDXFInsert(vCAD.Converter.Entities[I]);
vBlock := TsgDXFBlock.Create;
Converter.Sections[csBlocks].AddEntity(vBlock);
vBlock.Name := vInsert.Block.EntName;
vBlock.Offset := vInsert.Block.Offset;
vBlock.Color := vInsert.Block.Color;
vBlock.Layer := vLayer;
<b>for</b> I := 0 <b>to</b> vInsert.Block.Count - 1 <b>do
begin</b>
vEntity := TsgClassOfEntity(vInsert.Block.Entities[I].ClassType).Create;
vBlock.AddEntity(vEntity);
vEntity.AssignEntity(vInsert.Block.Entities[I]);
vEntity.Layer := vLayer;
vEntity.SetLType(<b>nil</b>);
Converter.Loads(vEntity);
Converter.OnCreate(vEntity);
<b>end</b>;
Img.Converter.Loads(vBlock);
vInsert := TsgDXFInsert.Create;
Converter.Sections[csEntities].AddEntity(vInsert);
vInsert.AssignEntity(vCAD.Converter.Entities[I]);
vInsert.Point := MakeFPoint(100, 100, 0);
vInsert.Block := vBlock;
vInsert.Layer := vLayer;
Converter.Loads(vInsert);
Converter.OnCreate(vInsert);
<b>finally</b>
FreeAndNil(vCAD);
<b>end</b>;
Img.GetExtents;
sgImage1.Picture.Graphic := Img;
<b>end</b>;
please post questions to the forum or write to support@cadsofttools.com