使用Custom Attributes,就是以Schema文件为基础,在其中附加预定义的XML节点。由于需要引用额外的Schema类型的XML,所以,但凡需要使用CustomAttribute时,必须在当前Schema中添加对特定Schema的引用节点。例如: <ECSchemaReference name="EditorCustomAttributes" version="01.00" prefix="beca" /> 节点将添加对EditorCustomAttributes.01.00.ecschema.xml文件的引用。添加了此种引用后,我们就可以在当前Schema中以<命名空间前缀>:<类名>的方式来使用所引用Schema中的类定义了,例如: beca:OtherECClassName。
通常,我们定义或使用CustomAttribute的目的在于指示系统的属性面板以特定的形式或方式来显示和处理我们的EC属性,比如: 将EC属性显示成带有下拉选择框的形式、将EC属性显示成带有文件或路径浏览能力的形式、将EC属性显示成带有按钮的形式等等。由于这些显示形式的控制都是针对属性面板而言的,所以相关的CustomAttribute通常都定义在EditorCustomAttributes中,我们需要引用它。此外,还有一些非面向属性面板的CustomAttribute,它们定义在Bentley_Standard_CustomAttributes中,需要时也可以引用它。
这些CustomAttribute可以大致分为3类,也就是: 面向ECSchema的,用于控制系统处理Schema本身以及Schema范围内的一些特性;面向ECClass的,用于控制特定EC类的某些行为;面向ECProperty的,用于控制系统解释特定ECProperty的行为,下面我们将逐一介绍这3类CustomAttribute的使用方式。
<ECCustomAttributes>
<StoresUnitsAsUors xmlns="EditorCustomAttributes.01.00" />
</ECCustomAttributes>
用以指示此Schema中所有表示长度、面积、体积等量的数值,在保存到DGN中时都将会自动换算成以UOR为单位的值,而且 StoresUnitsAsUors 这个CustomAttribute来自于 EditorCustomAttributes 这个Schema文件;
<ECClass typeName="BSplineBoundaryCurveData" isStruct="True" isDomainClass="True"> <ECCustomAttributes> <DisplayOptions xmlns="Bentley_Standard_CustomAttributes.01.10"> <Hidden>True</Hidden> </DisplayOptions> <DontShowNullProperties xmlns="EditorCustomAttributes.01.00" /> </ECCustomAttributes> </ECClass>
这个ECClass中包含两个类级别的CustomAttribute: DisplayOptions和DontShowNullProperties,其中,前者用于指示在属性面板中不要显示(因为Hidden节点的值为True)此类中的任何EC属性,并且它来源于Bentley_Standard_CustomAttributes.01.10.ecschema.xml文件;后者用于指示不要显示此类中的任何空值EC属性,并且此属性来源于EditorCustomAttributes.01.00.ecschema.xml文件。
<ECClass typeName="BSplineBoundaryCurveData" isStruct="True" isDomainClass="True"> <ECProperty propertyName="Order" typeName="int" readOnly="True" displayLabel="Order"> <ECCustomAttributes> <Category xmlns="EditorCustomAttributes.01.00"> <Standard>8</Standard> </Category> <PropertyPriority xmlns="EditorCustomAttributes.01.00"> <Priority>113000</Priority> </PropertyPriority> </ECCustomAttributes> </ECProperty> </ECClass>
这个ECProperty中包含两个Property级别的CustomAttribute: Category和PropertyPriority,其中,前者用于说明此EC属性应被分类为值为8的类别(基本几何信息类),它来自于EditorCustomAttributes.01.00.ecschema.xml文件;后者用于说明此EC属性在属性面板中显示时的排序优先级(影响其在属性面板中的显示顺序)为113000(介于低优先级和中优先级之间),它来自于同一Schema文件。