3

I am using Dymola, and I want a Modelica script that enables to capture the diagram of my model after simulation: example

I know how to do it manually in Dymola, so if I click "Tools" - " Image", then I can export the image. That's why I think there would be a way to do it with a Modelica script.

I tried to look into the "Dymola User Manual","commands window" in Dymola and google it, but I couldn't find out the solution so far.

Does anyone know whether it is possible or not? If your answer is yes, then could you help me with this topic?

Thanks in advance!

*Maybe previous picture was confusing, so I updated the picture. My goal is to take screenshots with updated number. (eg. 5K of subcooling and 6000W of condenser performance)

Markus A.
  • 6,300
  • 10
  • 26
y.min
  • 33
  • 5

2 Answers2

3

Seems it should be documented in a better way, but there is:

function exportDiagram "Export the diagram layer to file"
  input  String path  "File path. Supported file formats are PNG and SVG.";
  input  Integer width  := 400 "Width";
  input  Integer height  := 400 "Height";
  input  Boolean trim  := true "Remove unnecessary space around the image.";
  input  String modelToExport  := "" "Model path. Empty means model active one.";
  input  Boolean evaluate  := false "Evaluate default values of parameters.";
  output  Boolean result  "true if successful";
end exportDiagram;

e.g., exportDiagram("Test.png", modelToExport="Modelica.Mechanics.Rotational.Examples.CoupledClutches")

In this specific case there are some rests of disabled parts, you can reduce it with the argument evaluate=true (not to be confused with other Evaluate-flags).

Hans Olsson
  • 11,123
  • 15
  • 38
  • 1
    @Olsson If I use this one, then I can see the diagram, but not the result of simulation. Do you know how to display the screenshot with the simulation result? – y.min Dec 14 '22 at 12:02
  • 1
    Well, you asked for the diagram layer. Your screenshot does not show much more. If you want to export a plot you can use `DymolaCommands.Plot.ExportPlotAsImage`. And if you want to know if the simulation succeeded, use the return value of `DymolaCommands.SimulatorAPI.simulateModel`. – marco Dec 16 '22 at 08:05
2

The answer by Hans Olsson was fine originally, but meanwhile the question got more precise and exportDiagram is of no use any more. Apparently the exported model diagram is created from the Modelica code, so animations and hence simulation results are not visible.

I guess your only solution is to use an external program which takes screenshots. Windows lacks support for that, but there are multiple tools. See the answers to this question for details. I picked nircmd.exe, which allows us to create a screenshot after a wait time.

With the Modelica function below you can simulate a model and it will automatically capture a screenshot of your whole screen when the simulation has finished. The screenshot should include Dymola and your model in the diagram layer. I had to trick a bit to make sure that no black cmd window is on the screenshot. Check the code for details.

function simshot "Simulate given model and take a screenshot of the diagram layer"

  input String mdl="MyExample" "Simulation model";
  input Real t_stop=10 "Simulation stop time";
  input String out_dir = "." "Output directory for screenshot. Default is working directory.";

protected 
  final parameter String png = out_dir + "/" + mdl + ".png";
  Boolean _ "Dummy variable for return values of no interest";
  Boolean ok;

algorithm 
  _ :=instantiateModel(mdl);                  // Make sure that the model is the currently displayed model
  ok := simulateModel(mdl, stopTime=t_stop);  // simulate
  if not ok then
    Modelica.Utilities.Streams.error("Simulation failed");
  end if;

  _ :=switchToModelingMode();                 // Go to modeling moode
  _ :=switchLayer(2);                         // and to the diagram layer of the model
  _ :=animationTime(t_stop);                  // make sure the animation displays the value of simulation end

  // The "command" call spawns a cmd window, which is visible on the screenshot
  // Hence we use start to launch a separate process and wait a little, to make sure that
  // the cmd window is closed.
  Modelica.Utilities.System.command("start nircmd.exe cmdwait 500 savescreenshot " + png);

   annotation(__Dymola_interactive=true);
end simshot ;
marco
  • 5,944
  • 10
  • 22