[MSCE C++]获取元素属性的问题

老师好:

我想获取 图中所示的信息,包括Raw Data的属性。

Parents
  • /*--------------------------------------------------------
    | getAllInstances
    +-------------------------------------------------------*/
    WCharCP getCategoryName(ECPropertyP ecProp)
    {
    	IECInstancePtr categoryInstance = ecProp->GetCustomAttribute(WString(L"Category"));
    	if (categoryInstance.IsValid())
    	{
    		ECValue ecVal1, ecVal2;
    		categoryInstance->GetValue(ecVal1, L"Standard");
    		categoryInstance->GetValue(ecVal2, L"DisplayLabel");
    		WString result =ecVal1.ToString() + WString(L" | ") + ecVal2.ToString();
    		return result.GetWCharCP();
    	}
    	else
    		return L"No Category";
    }
    void getAllInstances(WCharCP  unparsed)
        {
    	ElementHandle   eh(2013L, 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: 平凡人生 

Reply
  • /*--------------------------------------------------------
    | getAllInstances
    +-------------------------------------------------------*/
    WCharCP getCategoryName(ECPropertyP ecProp)
    {
    	IECInstancePtr categoryInstance = ecProp->GetCustomAttribute(WString(L"Category"));
    	if (categoryInstance.IsValid())
    	{
    		ECValue ecVal1, ecVal2;
    		categoryInstance->GetValue(ecVal1, L"Standard");
    		categoryInstance->GetValue(ecVal2, L"DisplayLabel");
    		WString result =ecVal1.ToString() + WString(L" | ") + ecVal2.ToString();
    		return result.GetWCharCP();
    	}
    	else
    		return L"No Category";
    }
    void getAllInstances(WCharCP  unparsed)
        {
    	ElementHandle   eh(2013L, 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: 平凡人生 

Children