[MSCE C#]请问是否有办法获取到ItemType添加的表达式类型的属性?

通过原来遍历ECInstance中每一个属性值的方法,无法获得表达式类型的属性,查了一下接口,感觉应该使用IECCalculatedPropertyValue来获得,但是这个新接口没有地方找到它的用法。

下面是可以正确获得普通ItemType属性的代码,但是遍历不到表达式类型的属性值;

Dictionary<string, object> dict = new Dictionary<string, object>();
foreach (IECPropertyValue ecPropertyValue in instance)
{
var property = ecPropertyValue.Property;
dict.Add(property.DisplayLabel, ecPropertyValue.GetValue());
}

请指教,谢谢!

  • 抱歉,您是想获得一个ItemType Property中定义的表达式吗?从内部代码中能搜索到如下用法:

    IECCalculatedPropertyValue^ propVal0 = (IECCalculatedPropertyValue^)instance[accessor];
    if (ECTypeHelper::IsString(propVal0->Property->Type) || ECTypeHelper::IsStringArray(propVal0->Property->Type))
        ((IECCalculatedStringPropertyValue^)propVal0)->SetInternalStringValue((String^)customValue);
    else
        propVal0->NativeValue = customValue;



  • 谢谢符老师回复,我用C#在做,仿照您的代码测试发现还是不能获取到,下面是我的代码,以及ItemType的截图。

    IECPropertyValue pv1 = instance["总钢筋长度"];
    IECPropertyValue pv2 = instance["总钢筋长度".EncodeToValidName()];
    if (pv1 != null || pv2 != null)
    {
    Console.WriteLine("aaa");
    }

    我的代码是测试代码,两种获得元素方式也只是在试验,辛苦您看看我的写法是否有错误?

  • 下面是我的文件,这是用PS做的钢筋,辛苦符老师。

    EHE-CP-AP0501-V-14_钢筋_V2.0.dgn

  • 抱歉,我是用C++代码测试的,没有问题能提取出你的这个钢筋长度的值。测试代码如下:

    void getAllInstances(WCharCP  unparsed)
        {
    	ElementHandle   eh(57948L, ACTIVEMODEL);
    
        DgnECManagerR ecMgr = DgnECManager::GetManager();
    	FindInstancesScopePtr scope = FindInstancesScope::CreateScope(eh, FindInstancesScopeOption(DgnECHostType::Element));
        ECQueryPtr            ecQuery = ECQuery::CreateQuery (ECQUERY_PROCESS_SearchAllClasses); 
    										//ECQUERY_PROCESS_SearchAllExtrinsic will only search ECXAttr
        ecQuery->SetSelectProperties(true);
    
        for (DgnECInstancePtr instance : ecMgr.FindInstances(*scope, *ecQuery))
            {
            DgnElementECInstanceP elemInst = instance->GetAsElementInstance();
            WPrintfString outStr(L"--------- className = %s, instanceId = %s ---------------",
                                 elemInst->GetClass().GetName().GetWCharCP(),
                                 elemInst->GetInstanceId().GetWCharCP());
            mdlDialog_dmsgsPrint(outStr);
            for (ECPropertyP ecProp : elemInst->GetClass().GetProperties())
                {
    			WString valStr;
    			ECValue ecVal;
    			elemInst->GetValue(ecVal, ecProp->GetName().GetWCharCP());
    			IDgnECTypeAdapterR typeAdapter = IDgnECTypeAdapter::GetForProperty(*ecProp);
    			IDgnECTypeAdapterContextPtr typeContext = IDgnECTypeAdapterContext::Create(*ecProp, *elemInst, ecProp->GetName().GetWCharCP());
    			typeAdapter.ConvertToString(valStr, ecVal, *typeContext);
    
    			WPrintfString outStr2(L"\t%s[%s] = %s (categoryName=%s)",
    				ecProp->GetDisplayLabel().GetWCharCP(), ecProp->GetTypeName().GetWCharCP(), valStr.GetWCharCP(), getCategoryName(ecProp));
                mdlDialog_dmsgsPrint(outStr2);
                }
            }
        }

    输出结果如下:



    Answer Verified By: ZhangFan