V. Generating Graphics and
Graphs in MODSIM III:
The use of SIMGRAPHICS II.
Objective
-
Learn ways of introducing graphics and graphs in MODSIM III
codes.
-
Get familiar with SIMGRAPHICS II.
5.1. Graphics Objects in MODSIM III
-
GraphicVObj—It provides the capabilities of drawing,
positioning, selection, and the ability to contain sets of other objects.
-
WindowObj—Standard system window which can be moved,
resized, etc. Acts as a container for all graphical objects.
-
ImageObj—Basic object used for static icons and backgrounds.
-
DynImageObj—Basic graphic object used for animation.
-
DialogBoxObj—Receives various types of input from
the user. Controls, such as buttons, check boxes, list boxes, radio boxes,
value boxes and text boxes can be part of a dialog box.
-
MenuBarObj—Receives simple menu selections.
-
PaletteObj—Receives input from two-state palette buttons.
5.2. Ways to add graphics:
-
To add an image to a window:
ASK MyWindow TO AddGraphic(MyImage);
-
To make an object visible on the screen you must ask it to
draw:
ASK MyWindow TO Draw;
ASK MyImage TO Draw;
-
To erase an object:
-
To use an image, graph or form created by the editor you
must do the following:
1. Create a window:
2. Create a GraphicLib object:
3. Ask it to read the file containing the objects created
in the editor:
ASK GraphicLib TO ReadFromFile("graphics.sg2");
4. Create instances of objects you want:
5. Ask the instances to customize their appearance by copying
an
object in the library:
ASK TruckImage TO LoadFromLibrary(GraphicLib, "truck");
6. Add the instances to the window:
ASK window TO AddGraphic(TruckImage);
7. Draw either the window or object:
5.3. A Simple Program:
MAIN MODULE Example1;
{ This program gets a "truck" created
in the editor and
displays it for 10 seconds. }
FROM OSMod IMPORT Delay;
FROM Graphic IMPORT GraphicLibObj;
FROM Window IMPORT WindowObj;
FROM Animate IMPORT DynImageObj;
VAR
GraphicLib : GraphicLibObj;
window : WindowObj;
truck : DynImageObj;
BEGIN
NEW(window);
NEW(GraphicLib);
ASK GraphicLib TO ReadFromFile("graphics.sg2");
NEW(truck);
ASK truck TO LoadFromLibrary(GraphicLib,
"truck");
ASK window TO AddGraphic(truck);
ASK window TO Draw;
Delay(10);
END MODULE.
5.4. More examples:
{ Assume file "SolarSys.sg2" was
created by
SIMDRAW and contains the objects
named
"MERCURY", "VENUS", and "EARTH"
}
VAR library : GraphicLibObj;
mercury, venus, earth: ImageObj;
...
NEW(library);
NEW(mercury);
NEW(venus);
NEW(earth);
ASK library TO ReadFromFile("SolarSys.sg2");
ASK mercury TO LoadFromLibrary(library,
"MERCURY");
ASK venus TO LoadFromLibrary(library,
"VENUS");
ASK earth TO LoadFromLibrary(library,
"EARTH");
...
Change the position of a graphical
object:
...
ASK graphic TO SetTranslation(x,y);
{ sets the position }
ASK graphic TO Draw( );
{ updates object display }
...
Or:
...
ASK graphic TO DisplayAt(x,y);
...
Set window size:
...
VAR window : WindowObj;
...
NEW(window);
ASK window TO SetSize(50.0, 50.0);
ASK window TO SetTranslation(25.0,
25.0);
ASK window TO Draw();
...
Background Color:
...
ASK window TO SetColor(Blue);
ASK window TO Draw();
...
Adding Graphical Objects to a Window:
...
VAR window : WindowObj;
dialogBox : DialogBoxObj;
menuBar : MenuBarObj;
rootImage : ImageObj;
...
ASK window TO AddGraphic(dialogBox);
ASK window TO AddGraphic(menuBar);
ASK window TO AddGraphic(rootImage);
ASK window TO Draw;
...
Moving image:
ASK dynimage TO SetSpeed(1000.0);
ASK dynimage TO SetTranslation(0.0,
0.0);
ASK dynimage TO MoveTo(32767.0,
0.0);
Printing the Contents of a Window
ASK METHOD PrintGraphic (IN usedialog:
BOOLEAN)
: BOOLEAN ;
5.5 Program of a Small Graphical
Simulation:
The following example creates a clock
to display simulation time, and moves a truck image to the center of the
window using the MoveTo method:
MAIN MODULE Example6;
FROM Animate IMPORT DynImageObj,
DynAClockObj;
FROM Window IMPORT WindowObj;
FROM Graphic IMPORT GraphicLibObj;
FROM SimMod IMPORT StartSimulation,
Timescale;
VAR
truck : DynImageObj;
clock : DynAClockObj;
window : WindowObj;
lib : GraphicLibObj;
BEGIN
{ create objects }
NEW(window);
NEW(lib);
NEW(truck);
NEW(clock);
{ load in defs. of objects }
ASK lib TO ReadFromFile("Example6.sg2");
ASK truck TO LoadFromLibrary(lib,
"Truck");
ASK clock TO LoadFromLibrary(lib,
"Clock");
{ add objects to the window }
ASK window TO AddGraphic(truck);
ASK window TO AddGraphic(clock);
ASK window TO Draw;
{ start the clock's motion }
ASK clock TO StartMotion;
{ set speed of truck }
ASK truck TO SetSpeed(1000.0);
{ move truck to window center }
TELL truck TO MoveTo(16384.0, 16384.0);
{ two seconds for every time unit
}
Timescale := 2.0;
{ start animation }
StartSimulation;
END MODULE.
5.6. Graphs in MODSIM III.
-
GraphVObj — Generic object meaning
any graph type.
-
ChartObj — A chart containing
a data plot, legends, and titles.
-
ClockVObj — A generic object
describing a clock.
-
AnalogClockObj — A graphical
display of an analog clock.
-
DigitalClockObj — A graphical
display of a digital clock.
-
PiechartObj — A piechart containing
slices, legends, and titling.
-
MeterVObj — A generic object
for any graphical display of a single numerical value.
-
DialObj — A graphical display
of an analog dial.
-
DigitalDisplayObj — A simple
display of a single value.
-
LevelMeterObj — A graphical
display of a level meter.
-
TextDisplayObj — A graphical
display of a text string.
Simiple example:
VAR library : GraphicLibObj;
fuelGauge : DialObj;
...
NEW(library);
ASK library TO ReadFromFile("graphs.sg2");
NEW(fuelGauge);
ASK fuelGauge TO LoadFromLibrary(library,
"Fuel Gauge");
...
The following code monitors "queueLength" with a histogram:
VAR
queueLength : LMONITORED INTEGER
BY IDataPtMObj;
histogram : ChartObj;
...
NEW(histogram);
ASK histogram TO LoadFromLibrary(graphicLib,
"Queue Length");
...
ASK GETMONITOR(queueLength, IDataPtMObj)
TO
SetGraph(histogram);
ASK GETMONITOR(queueLength, IDataPtMObj)
TO
SetHistMode(TRUE);
...
queueLength := 12;
5.7. A program for using a chart:
MAIN MODULE Example5;
{ This small program loads in a
chart from a library, and plots a curve within it. We assume that the editor
has created a chart named "Chart". This graph should have it's XMin at
-2.0, XMax at 2.0, YMin at -8.0, and YMax at 8.0, and have 2 data sets.
These data sets should be of the 'simple plot' representation. It should
be saved in "Example5.sg2" }
FROM Window IMPORT WindowObj;
FROM Graphic IMPORT GraphicLibObj;
FROM Chart IMPORT ChartObj;
FROM GProcs IMPORT HandleEvents;
VAR
window : WindowObj
library : GraphicLibObj;
chart : ChartObj;
x : REAL;
BEGIN
{ create a window and open it up
}
NEW(window);
ASK window TO Draw();
{ read a "Example5.sg2" into a
library, and get a chart called "Chart" }
NEW(library);
ASK library TO ReadFromFile("Example5.sg2");
NEW(chart);
ASK chart TO LoadFromLibrary(library,
"Chart");
{ Add the chart to the window }
ASK window TO AddGraphic(chart);
{ Plot the curve y = x^2 in dataset
1 and y =x^3 in data set 2 }
x := -2.0;
WHILE x <= 2.0
ASK chart TO SetCoordinate(1, x,
x*x);
ASK chart TO SetCoordinate(2, x,
x*x*x);
x := x + 0.1;
END WHILE;
{ Now 'see' the chart and the results
of the plotting }
ASK chart TO Draw();
{ wait }
LOOP
HandleEvents(TRUE);
END LOOP;
END MODULE.
5.8. Creating graphics and graphs
using SIMDRAW.
SIMDRAW is an interactive menu based
program for creating and editing SIMGRAPHICS II objects. These objects
can be used for animation, presentation graphics, and interactive graphical
input. Types of objects include images, dialog boxes, menu bars, palettes,
and various charts and graphs. These objects are saved to and loaded from
SIMGRAPHICS II ".sg2"
files that can be accessed by a MODSIM III program.