Group is used to group presentation shapes so that you can control them (move, rotate, resize, etc.) all at once by controlling the group. By specifying dynamic properties of a group (X, Y, Rotation, etc.), you can move a group of shapes and rotate it around the pivot.
When a shape is added to a group, the group becomes the origin of its relative coordinates, instead of the presentation origin point (0,0). This can be used to shift the coordinate system for a part of a presentation.
AnyLogic enables creating groups of 3D shapes. If a group contains shapes with Show in: 2D and 3D, or 3D only option selected, the same option must be selected in the group. When grouping those shapes, the option is automatically selected.
To add a group
- Drag the Group element from the Presentation palette to the graphical editor.
The group icon is shown in the graphical editor only if this group does not contain any shapes.
To add shape(s) to the group
- Select the shape(s) you want to add to a group. You can select several shapes by holding Ctrl and clicking them successively.
- If you want to add shape(s) to an already existing group, right-click (macOS: Ctrl + click) the selection, then choose Grouping > Add to existing group from the context menu and finally click the group icon where you want to add your shape(s).
- Otherwise, if you want to create a new group containing these shapes, choose Grouping > Create a group.
To select a shape contained in a group
- Select the group by clicking any of its shapes in the graphical editor.
- Click the shape again. The shape will be selected. You can select several shapes by holding Ctrl and clicking them successively.
Alternatively, Alt + click (macOS: Option + click) the required shape. The shape will be selected.
To remove shape(s) from the group
- Select the shape you want to remove from a group.
- Right-click (macOS: Ctrl + click) the selection and choose Grouping > Remove from group from the context menu.
To select all shapes contained in a group
- Select the group by clicking any of its shapes in the graphical editor.
- Right-click (macOS: Ctrl + click) the selection and choose Select Group Contents from the context menu.
You can dynamically modify contents of a group at model runtime using the API of the corresponding ShapeGroup class.
- General
-
Name — The name of the group. The name is used to identify and access the group from code.
Ignore — If selected, the group is excluded from the model.
Visible on upper agent — If selected, the group is also visible on the upper agent where this agent lives.
Icon — If selected, the shape is considered as a part of the agent’s icon. Please note that icon shapes are not shown in 3D animation.
Lock — If selected, the group of shapes is locked. Locked shapes do not react to mouse clicks — it is impossible to select them in the graphical editor until you unlock them. It is frequently needed when you use your shape as a background image for your animation and you want to prevent editing this shape while drawing other shapes over it.
Visible — Here you specify whether the group is visible on the animation at the model runtime, or not. Using the control, choose yes or no. If you expect visibility to change dynamically or to depend on some conditions, you may specify here the expression defining the group’s visibility. This expression will be dynamically re-evaluated at the model runtime. It should return a boolean value. The group is visible when the specified expression evaluates to true, and not visible otherwise.
- Position and size
-
Level — Level to which this element belongs.
X — X-coordinate of the group’s origin.
Y — Y-coordinate of the group’s origin.
Z — Z-coordinate of the group’s origin.
Rotation — The rotation angle around Z-axis (in XY-plane), in radians or degrees.
Rotation X — The rotation angle around X-axis (in YZ-plane), in radians.
Rotation Y — The rotation angle around Y-axis (in XZ-plane), in radians.
Scale X — Here you can specify the expression returning the X-direction scale factor for the shapes of the group. *
Scale Y — Here you can specify the expression returning the Y-direction scale factor for the shapes of the group. *
Scale Z — Here you can specify the expression returning the Z-direction scale factor for the shapes of the group. *
* You can dynamically scale the shapes of the group at the model runtime by specifying expressions in the Scale X, Scale Y and optionally Scale Z. - Advanced
-
Show in — Here you can choose whether you want the group of shapes to be shown both In 2D and 3D animation, or in 2D only, or in 3D only.
Replication — The replication factor of the group. Here you specify how many copies of the group will be created (the integer number). If you leave this field empty, a single group will be created.
On click — Here you can type Java code that will be called each time a user clicks on the shape at the model runtime. If there are several shapes overlapping in the click point, the click will interact with the topmost shape only.
Local variables:
self — the element itself.
clickx — x-coordinate of the click relative to the shape coordinates.
clicky — y-coordinate of the click relative to the shape coordinates.On draw — Code that will be called each time the group is about to be redrawn.
Show name — If selected, the group's name is displayed on a presentation diagram.
- Location
-
Function Description double getX()
double getY()
double getZ()Returns the X (Y, Z) coordinate of the shape (namely, the coordinate of its center). void setX(double x)
void setY(double y)
void setZ(double z)Sets the X (Y, Z) coordinate of the shape.
x — the new value of X coordinate.
y — the new value of Y coordinate.
z — the new value of Z coordinate.void setPos(double x, double y) Sets new coordinates for the shape. Z coordinate is not changed.
x — the new value of x coordinate.
y — the new value of y coordinate.void setPos(double x, double y, double z) Sets new coordinates for the shape.
x — the new value of X coordinate.
y — the new value of Y coordinate.
z — the new value of Z coordinate. - Working with group elements
-
Function Description int size() Returns the number of shapes in the group. Object get(int i) Returns the shape with the given index. The result may be a shape (object of class Shape), replicated shape, 3D light or camera.
i — the index of the shape.int indexOf(Object shape) Returns the index of the specified shape (object of class Shape, or object of class ReplicatedShape) in this group, or -1 if this group does not contain the shape.
shape — the shape to find index of.java.util.List<Object> getShapes() Returns the collection of shapes in the group (not a copy). The collection can contain objects of class Shape or ReplicatedShape (for replicated shapes). It may also contain 3D lights and cameras. ShapeGroup getGroup() Returns the group containing this shape. - Adding shapes
-
Function Description void add(Shape shape) Adds the shape to the group. You need to make sure that this shape does not belong to any other group.
shape — the shape to add.void add(Camera3D camera) Adds a camera to the group. You need to make sure that this camera does not belong to any other group.
camera — the camera to add.void add(Light3D light) Adds a light to the group. You need to make sure that this light does not belong to any other group.
light — the light to add. - Inserting shapes
-
Function Description void insert(int index, Shape shape) Adds the shape to the group at the specified index (which defines z-order of shape in 2D animation). You need to make sure that this shape does not belong to any other group.
index — index at which the specified shape is to be inserted (from 0 to size(), inclusive).
shape — the shape to add. - Removing shapes
-
Function Description boolean remove(Shape shape) Tries to remove the shape from the group. Returns true if the group contained the shape, false otherwise.
shape — the shape to remove.boolean remove(Camera3D camera) Tries to remove a camera from the group. Returns true if the group contained the camera, false otherwise.
camera — the camera to remove.boolean remove(Light3D light) Tries to remove a light from the group. Returns true if the group contained the light, false otherwise.
light — the light to remove.void clear() Removes all shapes from the group. - Scaling
-
Function Description double getScaleX()
double getScaleY()
double getScaleZ()Returns the scale of the shape along X (Y, Z) axis. void setScaleX(double sx)
void setScaleY(double sy)
void setScaleZ(double sz)Sets the scale of the shape along X (Y, Z) axis.
sx — the new value of scale along X axis. *
sy — the new value of scale along Y axis. *
sz — the new value of scale along Z axis. *void setScale(double sx, double sy) Sets the scales of the shape along both axes.
sx — the new value of scale along X axis. *
sy — the new value of scale along Y axis.void setScale(double s) Sets the same scale of the shape along both axes.
s — the new value of scale along both axis.* Set to 1 to keep the original size. - Rotation
-
Function Description double getRotation() Returns the horizontal rotation of the shape, in radians, clockwise. double getRotationX() Returns the rotation of the shape around X axis, in radians (CW from Y to Z). double getRotationY() Returns the rotation of the shape around Y axis, in radians (CW from Z to X). double getRotationZ() Returns the rotation of the shape around Z axis, in radians (CW from X to Y). void setRotation(double rotation) Sets the horizontal rotation of the shape.
rotation — the new value of horizontal rotation in radians.void setRotationX(double rotationX) Sets rotation around X axis (CW from Y to Z) of the shape.
rotationX — the new value of rotation around X axis (CW from Y to Z), in radians.void setRotationY(double rotationY) Sets rotation around Y axis (CW from Z to X) of the shape.
rotationY — the new value of rotation around Y axis (CW from Z to X), in radians.void setRotationZ(double rotationZ) Sets the rotation of the shape around Z-axis.
rotationZ — the new value of rotation around Z-axis in radians. - Visibility
-
Function Description boolean isVisible() Checks the visibility of the shape. If the shape is visible, returns true, otherwise returns false. void setVisible(boolean v) Sets the visibility of the shape.
v — visibility: if true — the shape is set to be visible, if false — not visible. - Draw mode (2D / 3D)
-
Function Description ShapeDrawMode getDrawMode() Returns the drawing mode of the group (it defines where this group is drawn: in 2D, 3D or 2D+3D animation).
Valid values:
SHAPE_DRAW_2D3D — show in 2D and 3D animation
SHAPE_DRAW_2D — show in 2D animation only
SHAPE_DRAW_3D — show in 3D animation onlyvoid setDrawMode(ShapeDrawMode drawMode) Sets the drawing mode of the group (where to draw this group: 2D, 3D or 2D+3D animation). This method may be called only once and only for the groups created using the constructor without arguments. The call, which changes the set draw mode, will throw the error.drawMode — the new draw mode of the group.
Valid values:
SHAPE_DRAW_2D3D — show in 2D and 3D animation
SHAPE_DRAW_2D — show in 2D animation only
SHAPE_DRAW_3D — show in 3D animation only - Group
-
Function Description ShapeGroup getGroup() Returns the group containing this group. - Level
-
Function Description Level getLevel() Returns the level, where this group is located. - Points inside the shape
-
Function Description boolean contains(double px, double py) Test if the shape contains the point with the given coordinates (relative to this shape’s container, i.e. in the same system with the coordinates of this shape, x and y). Returns true if the shape contains the point with the given coordinates.
px — the x coordinate relative to this shape’s container.
py — the y coordinate relative to this shape’s container.Point randomPointInside() Returns the randomly chosen point inside the shape area.
This function utilizes Random Number Generator of the Presentable object containing this shape. (Will throw an exception if the shape has been created from code and has not been added to any group, — in this case use randomPointInside(Random rng)).Point randomPointInside(java.util.Random rng) Returns the randomly chosen point inside the shape area. This function utilizes the given Random Number Generator.
rng — the random number generator.
The API of group’s class allows you to iterate through the group members, as well as add and remove shapes dynamically.
Suppose you have a group with name group1. The following code iterates through the contents of the group, finds all ovals and outputs their sizes to the model log:
for( int i = 0; i < group1.size(); i++ ) {
Object member=group1.get(i);
if( member instanceof ShapeOval ) {
ShapeOval oval=(ShapeOval)member;
traceln( "Oval Rx: " + oval.getRadiusX() + " Ry: " + oval.getRadiusX() );
}
}
-
How can we improve this article?
-