If needed, you can create a railway network programmatically. Please note that you can do it only on model startup, and the network cannot be modified later.
In this article we study the following model:
Demo model: Create Rail Yard by Code
In this example we create a railway network that consists of three railway tracks interconnected via the switch. The network includes the Position on track element which sets the initial position for trains. Trains enter the network on the rt1 track and then continue moving either along the rt2 or rt3 track (the choice is regulated by the SelectOutput element of the model flowchart).
To create a rail yard programmatically, provide Java code in the On
startup action of the Main agent (Agent actions properties section). In our demo model the code is the following:
First, we create three linear markup segments. The MarkupSegmentLine constructor accepts six arguments: X, Y, Z coordinates of the line's start point followed by the end point's coordinates.
We create the railway switch using the RailwaySwitch constructor. The constructor arguments are quite the same, but you end with the list of the railway tracks connected by this switch (rt1, rt2, rt3).
We create a Position on track in a similar way. In the PositionOnTrack constructor, provide the reference to the railway track where you put the element (rt1) and specify the offset from the track's start point (200) as the last two arguments.
We now have all the elements that are needed to create a railway network. In the RailwayNetwork constructor, provide the reference to the agent the network will be added to (this), the name of the network ("myRailwayNetwork"), drawing mode (2D, 3D or both), and the network's Z-coordinate (which is zero in our case).
Next, add elements to the network by calling the network's addAll() function and passing the elements that we created earlier (railway tracks, railway switch and position on track) as its arguments.
Initialize the railway network by calling the initialize() function on the
previously created rn
Finally, we add the created elements into the presentation group to make them visible at runtime. To do this, call the add() function of the presentation group for each railway network element.