You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Cameo中访问Block自定义属性doctype并分类排序

在Cameo MagicReport中访问Block自定义枚举属性并生成分类列表

问题背景

需要在Cameo的MagicReport模板里,访问为Block创建的枚举类型自定义属性doctype,最终将图中所有Block按doctype分类生成对应列表。当前尝试的代码出现属性不存在错误,或返回空值;尝试getExpandoBridge()、report.getProperty()等方法均失败,仅能正常访问$b.name等通用属性,当前图中仅一个Block带有该属性。

解决方案

1. 构造型自定义属性的正确访问方式

Cameo中自定义属性通常通过**Stereotype(构造型)**扩展元模型,需先获取应用的构造型实例,再读取属性值:

#set($diagramElements=$report.getDiagramElements($docDiagram)) 
#set($blocks=$report.filterElement($diagramElements,["Block"])) 

#foreach($b in $blocks)
    #set($docStereotype = $b.getAppliedStereotype("<<DocTypeStereo>>"))
    #if($docStereotype)
        #set($doctypeVal = $report.getProperty($b, $docStereotype, "doctype"))
        #if($doctypeVal)
            $b.name : $doctypeVal
        #else
            $b.name : 无doctype属性
        #end
    #else
        $b.name : 未应用文档类型构造型
    #end
#end

替换<<DocTypeStereo>>为你定义doctype属性的实际构造型名称(包含<< >>符号)

2. 直接扩展属性的访问方式(非构造型)

如果doctype是直接添加到Block的扩展属性,用isPropertyExist()判断后取值:

#set($diagramElements=$report.getDiagramElements($docDiagram)) 
#set($blocks=$report.filterElement($diagramElements,["Block"])) 

#foreach($b in $blocks)
    #if($b.isPropertyExist("doctype"))
        #set($doctypeVal = $b.getPropertyValue("doctype"))
        $b.name : $doctypeVal
    #else
        $b.name : 无doctype属性
    #end
#end

3. 按doctype分类生成列表

先收集所有Block的分类信息,再分组输出:

#set($diagramElements=$report.getDiagramElements($docDiagram)) 
#set($blocks=$report.filterElement($diagramElements,["Block"])) 

#set($typeGroups = {})

#foreach($b in $blocks)
    #set($currentType = "未分类")
    #set($docStereotype = $b.getAppliedStereotype("<<DocTypeStereo>>"))
    
    #if($docStereotype)
        #set($doctypeVal = $report.getProperty($b, $docStereotype, "doctype"))
        #if($doctypeVal)
            #set($currentType = $doctypeVal)
        #end
    #end

    #if(!$typeGroups.containsKey($currentType))
        #set($void = $typeGroups.put($currentType, []))
    #end
    #set($void = $typeGroups.get($currentType).add($b))
#end

#foreach($docType in $typeGroups.keySet())
    ### $docType
    #foreach($block in $typeGroups.get($docType))
        - $block.name
    #end
#end

注意事项

  • 确保属性名称doctype拼写完全一致(大小写敏感)
  • 枚举类型属性返回的是枚举实例,如需显示标签值,可尝试$doctypeVal.getLabel()或$doctypeVal.getName()
  • 若仍报错,检查构造型是否正确应用到目标Block上,或属性是否设置了可见性权限

内容的提问来源于stack exchange,提问作者Julia Sullivan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 22:55:20