如题,我生成了一个模型用来绘制三维模型的二维平面图,然后通过该模型的名称,获取该模型,最后获取该模型中的所有元素。代码如下:
DgnModelPtr GetModel(string struname) { struname = ReplaceString(struname.c_str(), "[/*?:<>|\"\\\\]", "@"); WString wstrName(struname.c_str()); //StringToWstring(wstrName, this->GetModelName()); //model 已存在 //Gets active dgn file DgnFileP dgnFileP = ISessionMgr::GetActiveDgnFile(); if (nullptr == dgnFileP) return nullptr; ModelId modelId = dgnFileP->FindModelIdByName(wstrName.c_str()); if (INVALID_MODELID != modelId) { //Gets the model DgnModelPtr dgnModelPtr = dgnFileP->LoadModelById(modelId); return dgnModelPtr; } return nullptr; } void Test() { DgnModelPtr modelPtr = GetModel(modelName); if (modelPtr == nullptr) { return; } ReachableElementCollection eleCollection = modelPtr->GetReachableElements(); //获取dgnmodel中的所有元素,包括参考 WString str = modelPtr->GetModelName(); mdlDialog_openMessageBox(DIALOGID_MsgBoxOK, str.data(), MessageBoxIconType::Information); //弹窗 for each (ElementHandle elem in eleCollection) //遍历 { //获取该元素的图层 ElementPropertiesGetterPtr elePropGetter = ElementPropertiesGetter::Create(elem); LevelId levelid = elePropGetter->GetLevel(); .... } }
问题1:每次二维平面图的模型是新建立的,再通过名称去获取模型获取元素可以成功,但若二维平面模型是之前绘制的,即中间经历过PS软件的关闭,则有几率获取元素失败,打印看到的模型名称与二维平面模型名称一致。
问题2:有些元素获取到的图层id为0,这些元素是cell,我使用函数int type = elem.GetElementType();得到的值为66或107,如若打散就会报错,请问我要怎么正确获取到这些元素的图层?
谢谢老师!
焯辉 洪 said:则有几率获取元素失败
您写一个单独的命令函数去试一下,在这个函数中根据名字获取对应Model下的元素看是否还存在您描述的现象,如果仍然存在可将这个单独的命令函数提供给我们。
焯辉 洪 said:有些元素获取到的图层id为0,这些元素是cell
cell本身没有图层属性,图层属性都在其子元素上。但是cell元素的MSElementType属性值为2,并不是66或107,您是怎么判断其是cell的?
DgnModelPtr GetModel(string struname) { struname = ReplaceString(struname.c_str(), "[/*?:<>|\"\\\\]", "@"); WString wstrName(struname.c_str()); //StringToWstring(wstrName, this->GetModelName()); //model 已存在 //Gets active dgn file DgnFileP dgnFileP = ISessionMgr::GetActiveDgnFile(); if (nullptr == dgnFileP) return nullptr; ModelId modelId = dgnFileP->FindModelIdByName(wstrName.c_str()); if (INVALID_MODELID != modelId) { //Gets the model DgnModelPtr dgnModelPtr = dgnFileP->LoadModelById(modelId); return dgnModelPtr; } return nullptr; } void TestFunc(WCharCP * unparsed) { string modelname = "1BDB2003VB_正面配筋图"; DgnModelPtr modelPtr = GetModel(modelname); if (modelPtr == nullptr) { return; } ReachableElementCollection eleCollection = modelPtr->GetReachableElements(); for each (ElementHandle elem in eleCollection) //遍历 { int type = elem.GetElementType(); mdlDialog_openMessageBox(DIALOGID_MsgBoxOK, std::to_wstring(type).c_str(), MessageBoxIconType::Information); //弹窗 } }
...
获取元素前调一下这个:modelPtr->FillSections(DgnModelSections::GraphicElements);
Answer Verified By: 焯辉 洪
可以了 谢谢老师