CADImage and Stream

Discuss and ask questions about CAD VCL (Delphi and C++ Builder).

Moderators: SDS, support, admin

Post Reply
FieldConsult
Posts: 54
Joined: 14 Mar 2015, 22:33

CADImage and Stream

Post by FieldConsult » 24 Jan 2017, 01:23

How to save a CADImage to stream (file or memory) and restore from and, two CADImage in the same stream ex:

Code: Select all

CADImage1.SaveToStream(ms);
CADImage2.SaveToStream(ms);
and later

Code: Select all

CADImage1.LoadFromStream(ms); 
CADImage2.LoadFromStream(ms);
I try with one CADImage
ms is TMemoryStream to a TFileStream.

Code: Select all

CADImage.SaveToStream(ms);
ms.SaveToFile(fileName);
later

Code: Select all

ms.LoadFromFile(fileName);
CADImage.LoadFromStream(ms);
and the result stream is empty, no entities on the CADImage????

Thanks!!

support
Posts: 3253
Joined: 30 Mar 2005, 11:36
Contact:

Re: CADImage and Stream

Post by support » 24 Jan 2017, 17:39

Hello,

TsgCADImage.SaveToStream results in an empty stream if you save a drawing which was created programmatically. To get a non-empty stream, you should load a drawing from file, setting a value of KeepOriginal variable to True beforehand. For example:

Code: Select all

uses
   CADImage, DXF, DWG, sgConsts;
...

implementation

{$R *.dfm}

function CreateImageByFileFormat(AFileName: string): TsgCADImage;
var
  vFileExt: string;
begin
  Result := nil;
  vFileExt := AnsiLowerCase(ExtractFileExt(AFileName));
  if (vFileExt = '.dxf') then
    Result := TsgCADdxfImage.Create;
  if (vFileExt = '.dwg') then
    Result := TsgDWGImage.Create;
end;
...

var
  CADImage: TsgCADImage;
  ms: TMemoryStream;
...

ms := TMemoryStream.Create;
KeepOriginal := True;
CADImage := CreateImageByFileFormat(fileName);
CADImage.LoadFromFile(fileName);
...
CADImage.SaveToStream(ms);
Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

FieldConsult
Posts: 54
Joined: 14 Mar 2015, 22:33

Re: CADImage and Stream

Post by FieldConsult » 24 Jan 2017, 21:01

Thanks for the answer but the result is the same.
I am working with a very complex drawing, which contains a series of mesh, and one of which is composed of almost 10,000 faces of an origin of 20,000 points.

Each mesh is loaded from dwg file and its points are analysed in clockwise order and then sorted by angle to get the convex hull of the points cloud and the minimum rectangle between points, among other things. Each mesh has a complex data structure associated so I need to store all the information in a single file and not have to perform the analysis every time I read the file with the mesh.

In short, I load a serie of dwg files, each one containing a mesh and group them into a single CADImage, then I perform a series of complex geometric calculations, which require a certain amount of time to complete, so I want to record all the information in a series of streams that are grouped into a single stream file (multi stream file) for later use.

The idea is to load the file containing the multiple stream and get the calculation information immediately without having to recalculate everything one more time, which would be the case that you should read the files with mesh each time.

The only problem I have is when I group the different streams with the one originated by a CADImage, this stream is the first to be added to the file and the first to be read, the stream structure has an index position for each stream which allows to locate the stream to read it, but when I read the stream originated by CADImage I always get an empty image

I tried to write the contents of the CADImage to a temporary dwg file, then I load it on the CADImage itself (using KeepOriginal on True) and later I recorded that CADImage in a stream but, the result is the same, I get an empty CADImage.

Thanks!!

support
Posts: 3253
Joined: 30 Mar 2005, 11:36
Contact:

Re: CADImage and Stream

Post by support » 24 Jan 2017, 22:18

In short, I load a serie of dwg files, each one containing a mesh and group them into a single CADImage
When you group objects loaded from different CAD files in a single CADImage, you actually create a CAD drawing programmatically. In this case TsgCADImage.SaveToStream results in an empty stream.

Generally, TsgCADImage.SaveToStream can be used in development of database related software, when a very popular task is to load a CAD drawing file and save it in some BLOB field of the database table by using a TBlobStream object.


Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

FieldConsult
Posts: 54
Joined: 14 Mar 2015, 22:33

Re: CADImage and Stream

Post by FieldConsult » 24 Jan 2017, 22:32

When you group objects loaded from different CAD files in a single CADImage, you actually create a CAD drawing programmatically. In this case TsgCADImage.SaveToStream results in an empty stream.
That's why I first save the contents of the resulting CADImage to a normal dwg file! And then load it again on the same CADImage as if it were a normal file with the option KeepOriginal on True, as you indicated earlier.
I tried to write the contents of the CADImage to a temporary dwg file, then I load it on the CADImage itself (using KeepOriginal on True) and later I recorded that CADImage in a stream but, the result is the same, I get an empty CADImage.
To get a non-empty stream, you should load a drawing from file, setting a value of KeepOriginal variable to True beforehand.
That is, if I write all the contents of a CADImage to a dwg file, it is a normal procedure and I get a normal file. I then load the new file into a CADImage to get the non-empty stream, but I get an empty stream again. (In your example you use a TMemoryStream and I use the same).

Thanks!

support
Posts: 3253
Joined: 30 Mar 2005, 11:36
Contact:

Re: CADImage and Stream

Post by support » 24 Jan 2017, 22:41

if I write all the contents of a CADImage to a dwg file, it is a normal procedure and I get a normal file. I then load the new file into a CADImage to get the non-empty stream, but I get an empty stream again. (In your example you use a TMemoryStream and I use the same).
Please send this .dwg file to the Technical Support e-mail, I'll test my code example with your file and try to clear up this point.


Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

Post Reply