Creating a function for loading libraries

April 8th, 2016 by Mahdieh Mehrabi

There is the ability within Dymola to create a function to load a customised list of libraries, rather than manually loading each library. Using this approach, you can create multiple functions to load the sets of libraries required in different projects.  These functions can then be made easily accessible as a toolbar menu item or by having them appear by default in the package browser.  This is done by modifying a Dymola startup script to automatically open the appropriate package and the method can be found here: The Dymola Startup Configuration

First a package needs to be created to hold the functions (e.g. MyTools).  Within the new package create a new function; to make it more recognisable as a function you can extend “Modelica.Icon.Function” which will carry over the function icon:

Image 1

Figure 1: New function box to open a Claytex library

The Dymola built-in function that opens a Modelica file is:

DymolaCommands.SimulatorAPI.openModel

This function can be used to open a specific model file or a complete package if the package.mo (or package.moe for an encrypted library) within the library directory is specified

An example for the path to the Claytex library package.mo could therefore be:

“C:\Claytex\Modelica\Claytex\package.mo”

This function requires the path to be within quotation marks.  A second input to the function controls whether the working directory should be changed to the folder that contains the file being loaded or if the directory should not be changed. To keep the default working directory the following modification needs to be implemented:

changeDirectory=false

This function can be repeated as many times with any number of different libraries as long as none of the libraries conflict.

In the following example, a final step is to change the working directory to a specific location using the cd command.

 

function ClaytexLibrariesSuite "Opens some Claytex libraries"
  extends Modelica.Icons.Function;

  // SCRIPT TO OPEN SOME CLAYTEX LIBRARIES
algorithm

  //CLAYTEX LIBRARY
    DymolaCommands.SimulatorAPI.openModel(
      "C:\Claytex\Modelica\Claytex\package.mo", changeDirectory=false);
  
  //VDL MOTORSPORTS LIBRARY
    DymolaCommands.SimulatorAPI.openModel(
      "C:\Claytex\Modelica\VDLMotorsports\package.mo", changeDirectory=false);

  //POWERTRAIN DYNAMICS LIBRARY
    DymolaCommands.SimulatorAPI.openModel(
      "C:\Claytex\Modelica\PTDynamics\package.mo", changeDirectory=false);

  //FLEX BODY LIBRARY
    DymolaCommands.SimulatorAPI.openModel(
      "C:\Claytex\Modelica\FlexBody\package.mo", changeDirectory=false);

  //SIMULATOR LIBRARY
    DymolaCommands.SimulatorAPI.openModel(
      "C:\Claytex\Modelica\Simulator\package.mo", changeDirectory=false);

  //ENSURE THE CORRECT WORKING DIRECTORY IS VALID
    DymolaCommands.System.cd("C:\Users\Public\Documents\Dymola");

  annotation ();
end ClaytexLibrariesSuite;

 

By: David Briant – Project Engineer

Archives

2017

2016

2015

2013

2012

2011

2010