A Group is a set of graphics represented as a single graphic. Groups can also contain other groups. They are a useful way of enabling manual positioning and editing of a set of graphics.
cnvs = document.windows[0].selection.canvas
groups = new Array()
cnvs.graphics.forEach(function(graphic){
if(graphic instanceof Group){groups.push(graphic)}
})
Get All Groups on Current Canvas
01
cnvs = document.windows[0].selection.canvas
02
groups = newArray()
03
cnvs.graphics.forEach(function(graphic){
04
if(graphicinstanceofGroup){groups.push(graphic)}
05
})
var groups = new Array()
canvases.forEach(function(cnvs){
cnvs.graphics.forEach(function(graphic){
if(graphic instanceof Group){groups.push(graphic)}
})
})
Get All Groups in Document
01
vargroups = newArray()
02
canvases.forEach(function(cnvs){
03
cnvs.graphics.forEach(function(graphic){
04
if(graphicinstanceofGroup){groups.push(graphic)}
05
})
06
})
Creating a Group
A new instance of the Group class is created by passing an array of graphic references, to the Group allocation method: new Group()
new Group(Array of Graphics) • Construct a new group containing the given graphics and replace those graphics on their original canvas with the group graphic.
var canvas = document.windows[0].selection.canvas
var g1 = canvas.newShape()
var g2 = canvas.newShape()
var g3 = canvas.newShape()
var g4 = canvas.newShape()
var g5 = canvas.newShape()
g1.textUnitRect = new Rect(0.33, 0.33, 0.34, 0.34)
g1.shape = "AdjustableStar"
g1.geometry = new Rect(83.00, 81.00, 104.50, 104.50)
g1.shadowColor = null
g1.fillColor = Color.RGB(0.25, 0.25, 1.0)
g2.textUnitRect = new Rect(0.10, 0.15, 0.80, 0.70)
g2.shape = "Circle"
g2.geometry = new Rect(187.50, 129.00, 111.00, 111.00)
g2.shadowColor = null
g2.fillColor = Color.RGB(0.0, 1.0, 0.0)
g3.textUnitRect = new Rect(0.33, 0.33, 0.34, 0.34)
g3.shape = "AdjustableStar"
g3.geometry = new Rect(298.50, 81.00, 104.50, 104.50)
g3.shadowColor = null
g3.fillColor = Color.RGB(1.0, 1.0, 0.0)
g4.textUnitRect = new Rect(0.33, 0.33, 0.34, 0.34)
g4.shape = "AdjustableStar"
g4.geometry = new Rect(83.00, 204.50, 104.50, 104.50)
g4.shadowColor = null
g4.fillColor = Color.RGB(1.0, 0.5, 0.5)
g5.textUnitRect = new Rect(0.33, 0.33, 0.34, 0.34)
g5.shape = "AdjustableStar"
g5.geometry = new Rect(298.50, 204.50, 104.50, 104.50)
g5.shadowColor = null
g5.fillColor = Color.RGB(0.0, 1.0, 1.0)
group = new Group([g1,g2,g3,g4,g5])
document.windows[0].selection.view.select([group])
Even though the properties of the individual elements of group can still be edited, a group as an entity has its own properties as well.
connectToGroupOnly (boolean) • Whether lines may connect to individual constituents or must connect to the group as a whole.
graphics (array of Graphics r/o) • An array of graphics contained inside the group.
magnets (array of arrays) • Connection magnets on the group.
rotation (Number) • Rotation of the group, in degrees.
An array of references to a group’s elements can be ascertained by using the graphics property of the group. This array can then be used to manipulate the individual group elements without changing their status as belonging to the host group. Here’s a script example demonstrating how to change the value of each element of a group:
if (document.windows[0].selection.graphics.length != 1){
title = "SELECTION ERROR"
message = "Please select a single group."
new Alert(title, message).show(function(result){})
} else {
obj = document.windows[0].selection.graphics[0]
if(obj instanceof Group){
obj.graphics.forEach(function(item){
item.strokeColor = Color.red
item.strokeThickness = 4
item.strokeType = StrokeType.Single
})
} else {
title = "SELECTION ERROR"
message = "The selected object is not a group."
new Alert(title, message).show(function(result){})
}
}
if (document.windows[0].selection.graphics.length != 1){
02
title = "SELECTION ERROR"
03
message = "Please select a single group."
04
newAlert(title, message).show(function(result){})
05
} else {
06
obj = document.windows[0].selection.graphics[0]
07
if(objinstanceofGroup){
08
obj.graphics.forEach(function(item){
09
item.strokeColor = Color.red
10
item.strokeThickness = 4
11
item.strokeType = StrokeType.Single
12
})
13
} else {
14
title = "SELECTION ERROR"
15
message = "The selected object is not a group."
16
newAlert(title, message).show(function(result){})
17
}
18
}
Instance Functions
The Group class offers a single instance function, which can be applied to a group in order to dissolve it back into its individual elements.
ungroup() (result: Array of Graphics) • Remove this group, putting all of the graphics inside back on the canvas the group was previously upon. The group itself is deleted.
if (document.windows[0].selection.graphics.length != 1){
title = "SELECTION ERROR"
message = "Please select a single group."
new Alert(title, message).show(function(result){})
} else {
obj = document.windows[0].selection.graphics[0]
if(obj instanceof Group){
objects = obj.ungroup()
document.windows[0].selection.view.select(objects)
} else {
title = "SELECTION ERROR"
message = "The selected object is not a group."
new Alert(title, message).show(function(result){})
}
}