老师,我用下面代码增加EC属性,在文件测试后,后续想在DrawingName Param增加一条属性,但是此时采用AddItemType去增加属性,在为后面增加的属性设置值的时候,报错找不到该属性ECOBJECTS_STATUS_PropertyNotFound ,请问这是什么原因,是不是一个DGN文件ItemTypeLibrary和ItemType的格式一次固定后,后续就不能修改或者增加了
void CreateItemTypeLibraryItemTypeAndProperty() { DgnFileP dgnfilePtr = Bentley::MstnPlatform::ISessionMgr::GetActiveDgnFile();//获取DGN文件 ItemTypeLibraryPtr libPtr = ItemTypeLibrary::Create(L"DrawingName Arttribute", *dgnfilePtr, false);//为DGN文件创建ItemTypeLibrary ItemTypeP sofaItemTypePtr = libPtr->AddItemType(L"DrawingName Param", false);//增加ItemType CustomPropertyP typeProperty = sofaItemTypePtr->AddProperty(L"OrignallPoint", false);//在ItemType下增加属性当前点的坐标 typeProperty->SetType(Bentley::DgnPlatform::CustomProperty::Type::Point);//设置属性的格式为D3dpoint DPoint3d defaultPoint = DPoint3d::From(0, 0, 0); typeProperty->SetDefaultValue(ECValue(defaultPoint));//设置默认坐标为原点 CustomPropertyP typePropertyRatio = sofaItemTypePtr->AddProperty(L"IsRatio", false); typePropertyRatio->SetType(Bentley::DgnPlatform::CustomProperty::Type::Boolean); typePropertyRatio->SetDefaultValue(ECValue(false)); libPtr->Write();//写进去 }
您上面的代码执行完以后,在dgn文件里边打开ItemType的对话框能看到您添加的ItemType定义么?
可以,是否要把之前的删调,后面新增的才能有效果
应该是先获取当前dgn文件中是否已经存在指定名字的ItemType,如果存在就获取已有的,再判断是否已经有要添加的属性,如果有的话就不用再增加什么。如果没有ItemType就新创建,如果有ItemType没有属性就获取已有的ItemType再去添加属性。总之就是创建之前要先判断是否已经有了。
Answer Verified By: 陈玄 陈
好的谢谢老师,之前是由于没有判断有没有,直接创建,导致第二次创建同名的library时,又创建了一个,不过名字后面加上了数字!代码修改如下
void CreateItemTypeLibraryItemTypeAndProperty() { DgnFileP dgnfilePtr = Bentley::MstnPlatform::ISessionMgr::GetActiveDgnFile();//获取DGN文件 ItemTypeLibraryPtr libPtr = ItemTypeLibrary::FindByName(L"DrawingName Arttribute", *dgnfilePtr); if (libPtr == NULL) { libPtr = ItemTypeLibrary::Create(L"DrawingName Arttribute", *dgnfilePtr, false);//为DGN文件创建ItemTypeLibrary } ItemTypeP sofaItemTypePtr = libPtr->GetItemTypeByName(L"DrawingName Param"); if (sofaItemTypePtr == NULL) { sofaItemTypePtr = libPtr->AddItemType(L"DrawingName Param", false);//增加ItemType } CustomPropertyP typeProperty = sofaItemTypePtr->GetPropertyByName(L"OrignallPoint"); if (typeProperty == NULL) { typeProperty = sofaItemTypePtr->AddProperty(L"OrignallPoint", false);//在ItemType下增加属性当前点的坐标 typeProperty->SetType(Bentley::DgnPlatform::CustomProperty::Type::Point);//设置属性的格式为D3dpoint DPoint3d defaultPoint = DPoint3d::From(0, 0, 0); typeProperty->SetDefaultValue(ECValue(defaultPoint));//设置默认坐标为原点 } CustomPropertyP typePropertyRatio = sofaItemTypePtr->GetPropertyByName(L"IsRatio"); if (typePropertyRatio == NULL) { typePropertyRatio = sofaItemTypePtr->AddProperty(L"IsRatio", false); typePropertyRatio->SetType(Bentley::DgnPlatform::CustomProperty::Type::Boolean); typePropertyRatio->SetDefaultValue(ECValue(false)); } CustomPropertyP typePropertyLen = sofaItemTypePtr->GetPropertyByName(L"WordsLenth"); if (typePropertyLen == NULL) { typePropertyLen = sofaItemTypePtr->AddProperty(L"WordsLenth", false); typePropertyLen->SetType(Bentley::DgnPlatform::CustomProperty::Type::Double); typePropertyLen->SetDefaultValue(ECValue(0.0)); } libPtr->Write();//写进去 }