[021] Import External CGA Files for Modular Rule Design in CityEngine
Learn how to use the import command to split complex CGA rules into manageable, reusable modules.
When CGA code becomes too long, it can be separated by function into multiple files. This modular approach makes code easier to edit and reusable across different projects. This tutorial explains the basic concept of importing files and demonstrates how imported attributes are organized within the CityEngine Inspector.
Single-file vs Modular CGA Structure
When CGA code becomes too long, you can separate rules by function into modules and reuse them as needed.
This is extremely useful because it is easy to modify and can be reused in other code.
This is a very familiar concept for those who do programming.
For example,
Suppose there are mixed parcels with different uses, and the regulations require changes in building form according to the use, such as through district unit plans.
Writing all the rules for different types of buildings within a single CGA file would result in a massive amount of code, and even with good comments, it remains inconvenient.
This becomes even more inefficient when the file includes building rules that may never be used in certain scenarios.
In such cases, you can save the rules constituting buildings for each use in separate CGA files and then call them for use.
Today, we will look at how import is used through a very simple example.
First, let's look at an example where all rules are included in one CGA.
@Range
attr BD_1_Height = 20
# Height of Building 1
@Color
attr BD_1_color = "#FF0000"
# Color of Building 1
@Range
attr BD_2_Height = 20
# Height of Building 2
@Color
attr BD_2_color = "#00FF00"
# Color of Building 2
Lot -->
case geometry.area() > 1000:
extrude(BD_1_Height) color(BD_1_color) X.
else:
extrude(BD_2_Height) color(BD_2_color) X.
# If the building area is greater than 1000, attributes of BD_1 are applied; otherwise, attributes of BD_2 are applied.
The above example sets a condition on the building area so that buildings are represented in red if the area is greater than 1000, and green otherwise.
This is the result of the code above. Take note of the Inspector panel shown on the right.
Import-Based Modular Workflow in CityEngine
Now, let's look at an example of separating BD_1 and BD_2 into separate files and importing them.
1st_BD.cga
@Range
attr BD_1_Height = 20
# Height of Building 1
@Color
attr BD_1_color = "#FF0000"
# Color of Building 1
Lot --> extrude(BD_1_Height) color(BD_1_color) X.
2nd_BD.cga
@Range
attr BD_2_Height = 20
# Height of Building 2
@Color
attr BD_2_color = "#00FF00"
# Color of Building 2
Lot --> extrude(BD_2_Height) color(BD_2_color) X.
Once you have saved the rules you want to use as separate CGA files, you create a CGA file that will call these modules.
main.cga
import BD_1 : "1st_BD.cga"
import BD_2 : "2nd_BD.cga"
Lot -->
case geometry.area() > 1000:
BD_1.Lot
else:
BD_2.Lot
Now, let's look at the result after applying main.cga to the shape.
img 2 : Attributes of imported CGA files appearing separately in the Inspector
The attributes of the CGA files called as modules appear separately in the Inspector window.(You can achieve a similar grouping by putting all rules in one file and using annotation functions.)
Although import is not directly related to the interface itself, it is an important concept to understand before diving into CGA coding.
For more detailed information, please refer to the CGA reference regarding import.
Import supports a wide range of advanced syntax options, including style inheritance, independent attribute usage, and rule or attribute overriding.
As the coding content progresses and we reach the point of using import, I plan to cover it by syntax as well.
The time to cover CGA coding is approaching.
Whenever you have time, it will be helpful to look ahead at the code needed for the beginner stage informed in Lesson 17.
Next is the SketchUp extension that allows CGA files to be used in SketchUp.
I intended to cover it, but when I installed the SketchUp trial and checked today, the provided RPK works well, but the RPK I created myself causes a forced termination.
Although I do not actively use SketchUp, I hope the extension continues to receive updates.
I will replace the content about the extension with a link to GitHub.
https://github.com/highered-esricanada/CityEngine-Sketchup-Extension/tree/master
- If you want to visualize CGA report data with charts, see [020] How to Create Dashboard Charts from Report Data in CityEngine
- If you want to use CityEngine procedural rules inside SketchUp, see [022] Export CGA Rules to SketchUp Using RPK Packages
Comments
Post a Comment
Feel free to leave a comment if you have any questions about Global Mapper or CityEngine. I will get back to you with a sincere response as soon as possible. (Please note that all comments are moderated and manually approved to maintain a high-quality community. Promotional or spam content will not be published.)