Group

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 dynamic 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.

 Please note that a group containing shapes set to be shown in 3D scene cannot contain shapes with Show in: 2D only option selected, otherwise corresponding error messages are shown in the Problems view.

 To add a group
  1. 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
  1. Select the shape(s) you want to add to a group. You can select several shapes by successively Ctrl-clicking them.
  2. If you want to add shape(s) to an already existing group, right-click (Mac OS: 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).
  3. 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

  1. Select the group by clicking any of its shapes in the graphical editor. 
  2. Click the shape again. The shape will be selected. You can select several shapes by successively Ctrl-clicking them.

Alternatively, Alt+click (Mac OS: Option+click) the required shape. The shape will be selected.

 To remove shape(s) from the group

  1. Select the shape you want to remove from a group.
  2. Right-click (Mac OS: Ctrl+click) the selection and choose Grouping > Remove from group from the context menu.

 To select all shapes contained in a group

  1. Select the group by clicking any of its shapes in the graphical editor.
  2. Right-click (Mac OS: 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.

Properties

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 level – If selected, the group is also visible on the upper level 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 reevaluated 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

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, ScaleY 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 – Code that will be called each time the user clicks on the shape of the group at the model runtime.

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.

Modifying the group contents at runtime

The API of group's class allows you to iterate through the group members, add and remove shapes dynamically. 

Location

Function

Description

double getX()

double getY()

double getZ()

Returns the X (Y, Z) coordinate of the shape (namely, the coordinate of its centre).

void setX(double x)

void setY(double y)

void setZ(double z)

Sets the X (Y, Z) coordinate of the shape. 

Parameters:
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.

Parameters:
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. 

Parameters:
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.

Parameters:
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.

Parameters:
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.

Parameters:
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.

Parameters:
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.

Parameters:
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. 

Parameters:
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.

Parameters:
shape - the shape to remove

Returns:
true if the group contained the shape, false otherwise

boolean remove(Camera3D camera)

Tries to remove a camera from the group.

Parameters:
camera - the camera to remove

Returns:
true if the group contained the camera, false otherwise

boolean remove(Light3D light)

Tries to remove a light from the group.

Parameters:
light - the light to remove

Returns:
true if the group contained the light, false otherwise

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. 

Parameters:
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* 

* 1 = keep original size

void setScale(double sx, double sy)

Sets the scales of the shape along both axes. 

Parameters:
sx - the new value of scale along X axis, 1 = keep original size
sy - the new value of scale along Y axis, 1 = keep original size

void setScale(double s)

Sets the same scale of the shape along both axes. 

Parameter:
s - the new value of scale along both axis, 1 = keep 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.

Parameters:
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.

Parameters:
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.

Parameters:
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.

Parameters:
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.

Parameter:
v - visibility: if true - the shape is set to be visible, if false - not visible.

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.

Parameters:
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 hasn't 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. 
Parameter:
rng - the random number generator.

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() );
    }
}

Related topics

Selecting Presentation Shapes

Moving Presentation Shapes

Resizing Presentation Shapes

Rotating Presentation Shapes

Changing Shape's Position in Stacking Order

 Aligning Shapes