I am currently using CityEngine for a project and need to import a CSV file into CGA rules for application. However, I have encountered issues with CGA rules in CityEngine and have been unable to resolve them. I am reaching out for your assistance.
Below are the requirements for the project:
Currently, no matter how I modify the CGA rules, different parts of the code keep displaying an "unexpected token" error, as shown in the attached image. I am unsure of the source of the issue, so I would like to ask if there are any errors in my CGA rule code or if there is a recommended way to write it.
Here is my current CGA rule code:
const filePath = "75sampling_results.csv"
const data = readStringTable(filePath)
const numSamples = 75 // Our sample range is from row 2 to row 76
// Define attributes
attr Buildingoffset = 0
attr Height = 0
attr ArcadeSetback = 0
attr Streetoffset = 0
attr GreenCoverPercentage = 0
// Read the CSV file and randomly select a sample
@StartRule
Lot -->
randomSelectSample()
// Randomly select a row from the CSV file and set attributes
randomSelectSample -->
setAttributes(rand(1, numSamples) + 1) // Randomly select rows 2 to 76
// Set attributes
setAttributes(index) -->
Buildingoffset = toFloat(data[index][0])
Height = toFloat(data[index][1])
ArcadeSetback = toFloat(data[index][2])
Streetoffset = toFloat(data[index][3])
GreenCoverPercentage = toFloat(data[index][4])
generateBuilding()
// Generate the building using attributes
generateBuilding -->
offset(Buildingoffset)
extrude(Height)
applyArcadeSetback()
applyStreetoffset()
applyGreenCover()
// Apply ArcadeSetback
applyArcadeSetback -->
setback(ArcadeSetback)
// Apply Streetoffset
applyStreetoffset -->
setback(Streetoffset)
// Apply GreenCoverPercentage
applyGreenCover -->
case GreenCoverPercentage > 0:
color(0, GreenCoverPercentage / 100, 0)
extrude(0.1)
else:
nil
No matter how many times I modify and rewrite the rules, I continue to encounter similar issues. I apologize for the inconvenience and would be grateful if you could advise me on where the error might be or how I should structure this CGA rule. Thank you!
Hello @Yu_Tung_Lin0
Thank you for your question. I looked into the CGA code you provided and found several issues:
The unexpected token errors are resolved in this version of the setAttributes(index) rule above:
// Set attributes
setAttributes(i) -->
set(Buildingoffset, float(data[rint(i),0]))
set(Height, float(data[rint(i),1]))
set(ArcadeSetback, float(data[rint(i),2]))
set(Streetoffset, float(data[rint(i),3]))
set(GreenCoverPercentage, float(data[rint(i),4]))
generateBuilding()
I hope this helps you completing your project successfully 😊
Thank you very much for your prompt response. After rewriting the rules according to your suggestions, the issues with setAttributes have been resolved. However, new unexpected token errors have appeared in the applyStreetoffset and applyGreenCover sections (as shown in the attached image). Could you please advise how these parts of the CGA rules should be modified?
Here are the CGA rules code that have errors:
// Apply Streetoffset
applyStreetoffset -->
setback(Streetoffset)
// Apply GreenCoverPercentage
applyGreenCover -->
case GreenCoverPercentage > 0:
color(0, GreenCoverPercentage / 100, 0)
extrude(0.1)
else:
nil
I apologize for asking again, and thank you so much for your help!
I have modified the code you provided, but I believe it might not produce the intended result. The current outcome is due to applying setbacks simultaneously to a single parcel. It is better to use the 'selector' to distinguish areas for the offset and setback commands, and apply setbacks step by step while progressing through the rule for each area.
const data = readStringTable("data/75sampling_results.csv")
const numSamples = 75 // Our sample range is from row 2 to row 76
// Define attributes
attr Buildingoffset = 0
attr Height = 0
attr ArcadeSetback = 0
attr Streetoffset = 0
attr GreenCoverPercentage = 0
// Read the CSV file and randomly select a sample
@StartRule
Lot -->
randomSelectSample()
// Randomly select a row from the CSV file and set attributes
randomSelectSample -->
setAttributes(rand(1, numSamples) + 1) // Randomly select rows 2 to 76
// Set attributes
setAttributes(i) -->
set(Buildingoffset,float(data[rint(i),0]))
set(Height, float(data[rint(i),1]))
set(ArcadeSetback, float(data[rint(i),2]))
set(Streetoffset, float(data[rint(i),3]))
set(GreenCoverPercentage, float(data[rint(i),4]))
generateBuilding
// Generate the building using attributes
generateBuilding -->
offset(Buildingoffset)
extrude(Height)
print(Height)
applyArcadeSetback
applyStreetoffset
applyGreenCover
// Apply ArcadeSetback
applyArcadeSetback -->
setback(ArcadeSetback){all = ARC_offset. | remainder : X.}
applyStreetoffset -->
setback(Streetoffset){all = ST_offset. | remainder : X.}
// Apply GreenCoverPercentage
applyGreenCover -->
case GreenCoverPercentage > 0:
color(0, GreenCoverPercentage / 100, 0)
extrude(0.1)
else: NIL
I hope this helps.
Thanks @desert for providing more help.
At this point it also needs to be mentioned, that those "unexpected token" errors generally occur when using a wrong syntax in a previous code statement. It is always a good idea to consult the online help. Usually there are also code snippets provided that show common usage.
Also the code completion feature (hit <ctrl + space> while typing) can help finding the right syntax.
setback operation—ArcGIS CityEngine Resources | Documentation