[MSCE C++]创建动态的textblock,使其内容和某一元素的参数相关联

请教一个问题,在创建出textblock以后,在textblock中输入的文本,是否有办法将输入的文本与某一元素的属性相关联,比如说画了一条10cm的直线,然后创建一个textblock,textblock的文本内容为直线长度10,当改变直线长度为15cm的时候,文本内容自动更新为15。这个功能和标注的关联标注有点类似。

Parents Reply Children
  • 符老师,我学习了一下TextField的使用,能满足我需要的功能,麻烦再问一下,这个功能是如何通过代码实现的,因为我想做一个批量添加text的工具,麻烦符老师了

  • 该编程较为复杂,需要用到EC的编程技术。目前C++可实现。如下示例演示了放置一个与弧元素的长度相关的TextField。

    /*--------------------------------------------------------
    | createText
    +-------------------------------------------------------*/
    TextFieldPtr createField(DgnModelR model)
        {
        // "DgnElementSchema" and "ArcElement" are found from ...\MicroStation\ECSchemas\Dgn\DgnElementSchema.01.00.ecschema.xml
        DgnFileP   pDgnFile = ISessionMgr::GetActiveDgnFile();
        SchemaInfo schemaInfo(ECN::SchemaKey(L"DgnElementSchema", 1, 0), *pDgnFile);
    	DgnECManagerR ecMan = DgnECManager::GetManager();
    	ECN::ECSchemaPtr pSchema = ecMan.LocateSchemaInDgnFile(schemaInfo, ECN::SchemaMatchType::SCHEMAMATCHTYPE_LatestCompatible);
        if (pSchema == NULL)
            return NULL;
        ECN::ECClassCR   ecClass = *pSchema->GetClassCP(L"ArcElement");
    
        ElementId  elemId = 6173;  // An arc element id
        ElementHandle elem(elemId, &model);
        if (!elem.IsValid())
            return NULL;
        DgnElementECInstancePtr pElemInst = ecMan.FindInstanceOnElement(elem, ecClass);
    
        return TextField::CreateForElement(*pElemInst, L"Length", nullptr, model);
        }
    
    void createText(WCharCP  unparsed)
        {
        DgnModelP       pActiveModel = ISessionMgr::GetActiveDgnModelP();
        TextBlockPropertiesPtr  pTBProp = TextBlockProperties::Create(*pActiveModel);
        pTBProp->SetIsViewIndependent(true);
        ParagraphPropertiesPtr  pParaProp = ParagraphProperties::Create(*pActiveModel);
        DgnTextStylePtr         pTextStyle = DgnTextStyle::GetActive();
        RunPropertiesPtr        pRunProp = RunProperties::Create(*pTextStyle, *pActiveModel);
    
        TextBlockPtr pTextBlock = TextBlock::Create(*pTBProp, *pParaProp, *pRunProp, *pActiveModel);
        pTextBlock->AppendText(L"This is a ViewIndependent text");
    
        TextFieldPtr pField = createField(*pActiveModel);
        if (pField != NULL)
            pTextBlock->AppendField(*pField);
    
        EditElementHandle  eeh;
        if (TEXTBLOCK_TO_ELEMENT_RESULT_Success ==
            TextHandlerBase::CreateElement(eeh, nullptr, *pTextBlock))
            {
            eeh.AddToModel();
            }
        }



    Answer Verified By: 杨 昊霖 

  • 符老师,麻烦再请教一下,关于这行代码ECN::ECClassCR ecClass = *pSchema->GetClassCP(L"ArcElement");编译的时候提示错误, ,请问一下是缺少哪个库文件导致的