Search code examples
modelicadymola

mos code to take screenshot automatically after simulation in dymola


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)


Solution

  • 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 ;