Using SaveToFile causes Exception

Hi
I am using Delphi XE8 with TMS Diagram Studio V4.24.01

Using the built in editor TDiagramEditor (editor) I can save and load a file using the file open and save buttons on the editor.

When programmatically saving a file using editor.Diagram.SavetoFile() which essentially executes the same code as the editor buttons. I get an exception '$C0000005 ACCESS VIOLATION'.

Any help would be appreciated.

AndyC

Please provide more details, specially because you said the built-in code works, and your custom code doesn't not. We cannot guess what's going on at your side. Code you are using, offending line, call stack, etc.

In the supplied Demos\Simple\DiagramDemo a made the following changes

unit fMain;

In TForrm1 class..

private

procedure EditorCreate(Sender: TObject);

  • procedure EditorSave(Sender: TObject);

In TForm1.FormCreate..

DiagramEditor1.OnCreateDesigner := EditorCreate;

  • DiagramEditor1.OnSaveDiagram := EditorSave;

DiagramEditor1.Execute;

  • // ADDED THIS PROCEDURE TO FORCE SAVING TO SINGLE FILE

  • //

  • procedure TForm1.EditorSave(Sender: TObject);

  • begin

And what is the content of EditorSave procedure? Can you please properly format the message and provide the correct information?

The following was sent via email. Looks like the forum messed it up a bit lol.

In the supplied Demos\Simple\DiagramDemo a made the following changes

unit fMain;
…

In TForrm1 class..

  private
    procedure EditorCreate(Sender: TObject);
+   procedure EditorSave(Sender: TObject);


In TForm1.FormCreate..

  DiagramEditor1.OnCreateDesigner := EditorCreate;
+ DiagramEditor1.OnSaveDiagram := EditorSave;    
  DiagramEditor1.Execute;


+  // ADDED THIS PROCEDURE TO FORCE SAVING TO SINGLE FILE
+  //
+  procedure TForm1.EditorSave(Sender: TObject);
+  begin
+
+   TfmDiagramEditor(Sender).Diagram.SaveToFile('tempfille.dgr');
+   
+    // ALSO TRIED BELOW
+    // diagramEditor1.Diagram.SaveToFile('tempfile.dgr');
+

When I the save the file I get the exception.
This break’s here

procedure TatDiagram.SaveToFile(const AFileName: string);
var
  MemStream: TMemoryStream;
  FileStream: TFileStream;
begin
  MemStream := TMemoryStream.Create;
  try
    FileStream := TFileStream.Create(AFileName, fmCreate);
    try
>>    SaveToStream(MemStream);
      MemStream.Seek(0, soFromBeginning);
      ObjectBinaryToText(MemStream, FileStream);
    finally
      FileStream.Free;
    end;
  finally
    MemStream.Free
  end;
end;

Please let me know if you need more information.
Thanks
AndyC

You can't cast Sender to TfmDiagramEditor, as it's not such class, but just a TAction descendant.

Ok. Then how can i use the OnSaveDiagram event to save the Diagram?

From OnEditorCreate event you can save a reference to the TatDiagram object, and then reuse it in the OnSaveDiagram event. The diagram reference is never modified until you close the editor.

Sorted now :slight_smile:
Thanks for that.

1 Like

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.