I'd like to use listAdd to create a string that contains geometry information about shapes generated through the comp operation.
The code below separates the shape into its edge components and, in the Edges rule, tries to gather the edge lengths recursively and store them in the EdgeLengths attribute.
Rather than storing separate edge lengths, it produces a string with the same edge length listed multiple times, i.e., the loop is running multiple times on the same edge shape.
Is using listAdd in this way possible in CGA, or is there another way to accomplish this? Is this something I'd need Python integration for?
I'm new to CGA, so any guidance is helpful!
/**
* File: listfunctions.cga
* Created: 16 Jan 2019 20:40:45 GMT
* Author: chlvws
*/version "2018.0"
attr EdgeLengths = ""
#Rules
@StartRule
Lot-->
comp(e) { all : Edges }Edges-->
case listSize(EdgeLengths) < 3:
set(EdgeLengths, AddItem(str(scope.sx)))
Edges
else: print(EdgeLengths)
#Functions
AddItem(Edge) =
listAdd(EdgeLengths, Edge)
In 2019.0, the new comp function (the existing comp operation co-exists with the new comp function) allows you to access attributes for each edge by returning an array, with one element per edge. You can use comp to get an array of edge lengths.
getEdgeLengths = comp(fe) { all: scope.sx }
Lot -->
print(getEdgeLengths)
print(size(getEdgeLengths)) // size of array
print(getEdgeLengths[0]) // element at index 0
print(index(getEdgeLengths, 10, 0.1)) // index of first occurrence of value=10 with eps=0.1
https://doc.arcgis.com/en/cityengine/latest/cga/cga-comp-function.htm
Here are the help pages to get array info like size, item, index:
https://doc.arcgis.com/en/cityengine/latest/cga/cga-size-function.htm
https://doc.arcgis.com/en/cityengine/latest/cga/cga-item-operator-function.htm
https://doc.arcgis.com/en/cityengine/latest/cga/cga-index-function.htm
For more examples, check out how comp is used to when definining dists in the examples on the help page for the setback operation.
https://doc.arcgis.com/en/cityengine/latest/cga/cga-setback.htm
Your above approach doesn't work because shapes created from a comp do not know about each other. Let's say you have 4 edges. The comp will create 4 different shapes in the shape tree, one for each edge, and these shapes do not know about each other. The code in the Edges rule is run 4 times, once for each edge, but these are run independently of each other; they do not build on each other sequentially.