Peyep CGA 函数参考属性操作 · 2025.1
版本首页查看 demo →
← 返回 2025.1 分类导航

属性操作 Attributes · 6 个函数

颜色、材质设置与打印 / 报表类操作语句

color native ✓ cpp-fast ✓ 已验证 715 条 · gold 官方文档 ↗ #
设置颜色
签名(3 种重载)
color(hexValue)
color(red, green, blue)
color(red, green, blue, opacity)
参数说明
参数类型说明
hexValuestringColor to set, in the format "#rrggbb" or "#rrggbboo" (hex).
redfloat (range[0.0, 1.0])Color to set in red components.
greenfloat (range[0.0, 1.0])Color to set in green components.
bluefloat (range[0.0, 1.0])Color to set in blue components.
opacityfloat (range[0.0, 1.0])Color to set in opacity components.
官方说明(英文原文)

The color operation sets the color of the current shape's material. Input parameter values for red, green, blue, or opacity that are outside the range [0,1] are clamped to [0,1].

In CGA, colors are always in (non-linear) sRGB.

Note:

This command has the same effect as set(material.color.{ r | g | b }, val), it is a shortcut for convenience.

官方示例 · 点击展开
Lot-->
   extrude(20)
   split(x) { 2  : color("#ff0000") 
                   X
            | 2  : color(1, 0, 0) 
                   X 
            | 2  : set(material.color.r, 1.0) 
                   set(material.color.g, 0) 
                   set(material.color.b, 0) 
                   X }
我方已验证示例:基础挤出 + 颜色(verified_gold)· 点击展开代码 · 在 demo 中运行 →
@Range(min=0, max=20, step=0.5)
attr height = 10
attr roofColor = "#333333"
@StartRule
Lot -->
primitiveQuad(7, 15)
extrude(height)
color(1,0.5,0.2)
comp(f){side:Facade|top:Roof}
Facade --> split(y) { ~1 : Floor }*
Floor --> color(0.7,0.75,0.8)
Roof --> roofHip(30) color(roofColor)
print native ✓ cpp-fast ✓ 已验证 66 条 · silver 官方文档 ↗ #
输出调试信息到日志
签名(1 种重载)
print(value)
参数说明
参数类型说明
value—Value (or variable / shape attribute) to print
官方说明(英文原文)

The print operation prints the passed variable to the console.

官方示例 · 点击展开
Lot-->
  extrude(16)
  split(y) { 2 : r(scopeCenter, 0, rand(90), 0)
                 print("split idx " + split.index + ", 
                        tx   = " + scope.tx + 
                        " ry = " + scope.ry)
                 X  }*
我方已验证示例:learning case #124(verified_silver)· 点击展开代码 · 在 demo 中运行 →
inArray(value, array) = findFirst(array, value) >= 0
getByKey(key, keys, values) = values[findFirst(keys, key)]
getByKey(key, dict) with(idx := findFirst(dict, key))  = case idx < 0: dict[-1] else: dict[idx + 1] # TODO (MB): Doesn't work if there exists a value that is also a key.
getKeys(dict) = dict[0:2:size(dict) - 2]
getRandom(values) = values[rand(0, size(values))]

Dbg(label, value) --> case print(label + " = " + str(value))== "": NIL else: NIL
dbg(label, value) = case print(label + " = " …
report native ✓ cpp-fast ✓ 已验证 79 条 · silver 官方文档 ↗ #
报表统计:把数值汇入报告
签名(1 种重载)
report(key, value)
参数说明
参数类型说明
keystringA string which defines a key for a report collection. Keys can be grouped with the name separator ''.''
value—Value (or variable / shape attribute) to add to the collection.
官方说明(英文原文)

The report operation permits collection of arbitrary data during model generation. The operation takes two parameters: a key of type string and a value of any type. The key and value type define a collection to which the value is added on every invocation. After generation, the collections are assessed statistically and displayed in the Reports shelf in the Inspector.

Reports can be manually exported from the Inspector via the copy-paste clipboard (select the rows you want to export with Shift and the cursor keys, press Ctrl+ C and paste the data to a text editor or a spreadsheet). Python scripting provides another, more powerful way to export the reports; scripting allows for accessing all values in the collections, see Script-based export.

官方示例 · 点击展开
Lot --> 
    extrude(30)
    comp(f) { side : Facade 
            | top  : Roof }
        
Facade --> 
   report("facades", 1) 
   split(y) { ~5   : Floor  
            | ~0.5 : Ledge }*
        
Floor --> 
   split(x) { ~1 : Tile 
            | 2  : Window 
            | ~1 : Tile}*
        
Window --> 
   40%: 
      report("windowarea", geometry.area())
      report("windows.open", 1)
      NIL
   else:
      report("windowarea", geometry.area())
      report("windows.closed", 1)
      color("#aaffaa")
我方已验证示例:learning case #4(verified_silver)· 点击展开代码 · 示例过大,请复制源码到 demo 运行
/**
 * File:    Building_From_OpenStreetMap.cga
 * Created: 26 Jul 2016 13:24:57 GMT
 * Author:  Esri R&D Center Zurich
 */

@Hidden(Representation, Transparency, CleanupTolerance, Unit, Reporting)
import LOD1_Parameters: "/ESRI.lib/rules/Buildings/Building_From_Footprint.cga"
	(Unit="Meters", Reporting="None")
@Hidden(Eave_Ht, Ridge_Ht, Usage, Building_Form, Roof_Form, Roof_Orientation, Roof_Direction, Floor_Ht, Underground_Ht, Min_Ht, OverwriteColor, RoofColor, Representation, Transparency, Cl…
resetMaterial native ✓ cpp-fast ✓ 已验证 18 条 · silver 官方文档 ↗ #
重置材质到默认
签名(2 种重载)
resetMaterial()
resetMaterial(target)
参数说明
参数类型说明
targetselector
官方说明(英文原文)

The resetMaterial operation resets the targeted material to the CGA default material (i.e. resets all material attributes). Using resetMaterial() without a parameter resets only the material of the shape. If the target is geometry or all and the geometry consists of multiple materials all of them are reset.

官方示例 · 点击展开
Lot --> primitiveQuad() 
        texture("stones.jpg")
        set(material.reflectivity, 0.75)
        set(material.specular.r, 1)
        set(material.specular.g, 1)
        set(material.specular.b, 1)
        Stones.
          
        i("plant.glb")
        resetMaterial()
        Flowers.
Init-->
	i("car.glb")
	Matler(0)
	comp(m) { 0 : Matler(0.75) | 1 : Matler(1.25) }
	
Matler(tx)-->
	alignScopeToAxes()
	t(tx,0,0) 
	X.
	t(0,0.5,0)
	resetMaterial(geometry)
	X.
我方已验证示例:learning case #189(verified_silver)· 点击展开代码 · 在 demo 中运行 →
/* resetMaterial operation example */
Lot -->
  extrude(10)
  color("#ff0000")
  resetMaterial()
set native ✓ cpp-fast ✓ 已验证 319 条 · silver 官方文档 ↗ #
设置属性 / 材质参数
签名(1 种重载)
set(attribute, value)
参数说明
参数类型说明
attributeselectorName of shape attribute to set.
value—Value to assign to shape attribute.Note:Not all shape attributes are writable.
官方说明(英文原文)

The set operation assigns a value to a shape attribute of the current shape. There are two kinds of shape attributes: builtin attributes such as material.color or scope.tx and generic attributes, which are declared as rule attributes (see CGA attributes).

官方示例 · 点击展开
attr floorNumber = -1

const floorHeight = 4

Init -->    extrude(30)
            comp(f)  { side : Side }
   
Side -->    split(y) { ~floorHeight : Floor }*
   
Floor -->   set(floorNumber, split.index)
            split(x) { ~1   : Wall 
                     | {~3  : Tile }* 
                     | ~1   : Wall } 
Tile -->    split(x) { 0.5  : Wall 
                     | ~1   : MidTile 
                     | 0.5  : Wall }
MidTile --> split(y) { 0.5  : Wall 
                     | ~1   : Window 
                     | 0.5  : Wall }
			
Window -->  case floorNumber % 3 == 0 :
                color(1.0, 0.0, 1.0) X.
            else : X.
我方已验证示例:learning case #78(verified_silver)· 点击展开代码 · 示例过大,请复制源码到 demo 运行
/**
 * File:    Fence_On_Graph.cga
 * Created: 18 Nov 2015 17:11:40 GMT
 * Author:  Esri R&D Center Zurich
 */

/*

Input shape:  street shape (shape with UVs), street width >= base_depth

Fences are created along the u direction of the street shape.

Notes on Occlusion:

* Occlusion Labels:  The fence parts (post, middle, base post, base middle, upper post, upper middle)
  are each labeled with the given occlusionLabel.  These labeled parts are meant to be occluders which
  can be found when te…
setMaterial native ✓ cpp-fast ✓ 已验证 12 条 · silver 官方文档 ↗ #
设置材质
签名(1 种重载)
setMaterial(material)
参数说明
参数类型说明
materialstring[]A string array containing key-value pairs in the form of [ attr-name0, value0, attr-name1, value1, ... ]. The names of the material attributes are without the "material." prefix.
官方说明(英文原文)

The setMaterial operation sets the material attributes of the current shape's material according to material. This operation allows for setting of multiple (all) material attributes in one operation.

The result will be the same as using set() to set each material attribute individually.

官方示例 · 点击展开
myMaterial = [ "color.r",    "1.0", 
               "color.g",    "0.0", 
               "color.b",    "0.0", 
               "opacitymap", "builtin:default" ]

Lot --> primitiveCube() 
        scaleUV(0,0.25,0.25)
        setMaterial(myMaterial)
Lot --> 
    primitiveSphere()
    setMaterial(readMaterial(
                "materials/Metal2/Metal2.cgamat"))
RefExampleCSV-->
     primitiveCube()
     setMaterial(readStringTable(
                 "exampleMaterial.csv"))    
    
    
exampleMaterial.csv:    
color.r,1
color.g,0.5
color.b,0
colormap,builtin:default
我方已验证示例:learning case #188(verified_silver)· 点击展开代码 · 在 demo 中运行 →
/* setMaterial operation example */
Lot -->
  extrude(10)
  setMaterial("brick")