【ORD C++】XAttribute问题

My_elementRef 是当前model中选中的参考文件的元素,写XAttribute失败了,返回值是-105。但是如果将参考文件合并到主文件,就会写入成功。请问是什么原因呢?

ElementRefP My_elementRef = mdlDisplayPath_getElem(path, mdlDisplayPath_getCursorIndex((DisplayPathP)path));
ElementID elID = My_elementRef->GetElementId();
EditElementHandle eeh(elID, ACTIVEMODEL); //添加attribute的元素
...
...
...
XAttributeHandlerId xAttHandlerId(234, 567);

UInt32 xAttId = 101;

XAttributeHandle xAttHandle(eeh.GetElementRef(), xAttHandlerId, xAttId);
StatusInt iRet = -1;
if (xAttHandle.m_node.m_xattr == NULL)
{
//直接存
iRet = ITxnManager::GetCurrentTxn().AddXAttribute(eeh.GetElementRef(), xAttHandlerId, xAttId, pdata, size);
// eeh.ScheduleWriteXAttribute(xAttHandlerId, xAttId, size, pdata);
}

Parents Reply
  • 多谢张老师的指导,根据张老师提供的代码和其他帖子做的了以下总结:

    写参考文件元素属性时,不能直接通过当前DGN中所选中的参考文件中元素的elementRefP来构造XAttributeHandle,这样写不成功,现已成功的一种方式如下,需记录选中的元素ID即ElementID,并重新打开参考文件进行编辑并加载选中元素所在model,且通过ElementRefP elRefP = pModel->FindElementByID(elmID) 来获取elRefP,通过该elRefP来构造XAttributeHandle,这样才能写成功,代码如下:

    ElementRefP elRefP = mdlDisplayPath_getElem(path, mdlDisplayPath_getCursorIndex((DisplayPathP)path));
    ElementId elmID = elRefP->GetElementId();
    WString filepath = elRefP->GetDgnModelP()->GetDgnFileP()->GetDocumentPtr()->GetFileName();
    DgnDocumentPtr pDgnDoc = DgnDocument::CreateForLocalFile(filepath.GetWCharCP());
    DgnFilePtr file = DgnFile::Create(*pDgnDoc, DgnFileOpenMode::ReadWrite);
    StatusInt openForWriteStatus;
    file->LoadDgnFile(&openForWriteStatus);
    if (!file->IsOpen())
    {
        mdlDialog_dmsgsPrint(L"The file can't be written");
        return;
    }
    
    file->FillDictionaryModel();
    
    DgnModelPtr pModel = file->LoadModelById(elRefP->GetDgnModelP()->GetModelId());
    file->FillSectionsInModel(pModel.get());
    
    if (pModel != NULL)
    {
        // 获取参考文件中的元素对象
        elRefP = pModel->FindElementByID(elmID);
    }
    
    //存储的key值
    XAttributeHandlerId xAttHandlerId(234, 567);
    UInt32 xAttId = 101;
    XAttributeHandle xAttHandle(elRefP, xAttHandlerId, xAttId);
    StatusInt iRet = -1;
    if (xAttHandle.m_node.m_xattr == NULL)
    {
        //直接存
        iRet = ITxnManager::GetCurrentTxn().AddXAttribute(elRefP, xAttHandlerId, xAttId, pdata, size);
    }
    else
    {
        //删除再存
        ITxnManager::GetCurrentTxn().DeleteXAttribute(xAttHandle);
        ITxnManager::GetCurrentTxn().AddXAttribute(elRefP, xAttHandlerId, xAttId, pdata, size);
    }
    file->ProcessChanges(DgnSaveReason::FileClose);

    参考帖子:

    https://communities.bentley.com/communities/other_communities/chinafirst/f/microstation-projectwise/188733/ms-v8i-c

    Answer Verified By: Xiang Liu 

Children