This section shows a simple JavaScript interface example, which
is delivered in
.\win_b64\webapps\dymola_interface\examples\dymola_example.xhtml.
For creating a dymola object you can use the following API:
var dymola = new DymolaInterface(port=8082);
Note:
JavaScript interface, similar to Python interface, uses a HTTP
server to receive function calls. The default port is
8082
. For more information, see
Launching HTTP Server.
Some useful methods, available for a dymola object, are demonstrated
in the following example:
xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8"/>
<title>Dymola JavaScript Example</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script type="text/javascript" src="dymola_interface.js"></script>
</head>
<body>
<h1>Dymola JavaScript Example</h1>
<div id="dymola_plot"></div>
<script type="text/javascript">
try {
var dymola = new DymolaInterface();
var result_block = document.getElementById("dymola_plot");
var result = dymola.simulateModel("Modelica.Mechanics.Rotational.Examples.CoupledClutches");
if (result) {
result = dymola.plot(["J1.w", "J2.w", "J3.w", "J4.w"]);
if (result) {
var path = "C:/temp/plot.png";
result = dymola.ExportPlotAsImage(path);
if (result) {
var img = document.createElement("img");
img.src = "file:///" + path;
img.alt = "Dymola Plot";
result_block.appendChild(img);
} else {
result_block.innerHTML = "Failed to export image.";
}
} else {
result_block.innerHTML = "Failed to plot variables.";
}
} else {
result_block.innerHTML = "Simulation failed.";
var log = dymola.getLastErrorLog();
if (log !== null) {
result_block.innerHTML += log;
}
}
} catch (e) {
alert("Exception: " + e);
}
</script>
</body>
</html>
Note:
You have to specify the script source as
dymola_interface.js
in your file, as shown in the
above example.
<script type="text/javascript"
src="dymola_interface.js"></script>
In the example, the location of the
dymola_interface.js
is the same as the example
directory.