DgnModelRefP modelRef; CString strDgn = L"E:\\MainDesign.dgn"; WCharCP inFileName = strDgn; if (SUCCESS == mdlWorkDgn_openFile(&modelRef, NULL, NULL, inFileName, NULL, TRUE)) { if (!modelRef) return; DPoint3d pts[2] = { { 0 },{ 0,0,10000 } }; MSElement line; mdlLine_create(&line, NULL, pts); EditElementHandle eeh(&line, ACTIVEMODEL); eeh.AddToModel(); // 返回值是始终为0 //UInt32 nStatus = mdlElmdscr_addByModelRef(edP1, modelRef); UInt32 nStatus = mdlWorkDgn_write(eeh.GetElementDescrP(), -1, modelRef); mdlWorkDgn_saveChanges(modelRef); mdlWorkDgn_closeFile(modelRef); }
以上是我的代码,但总是失败,
我参考的是这个帖子:https://communities.bentley.com/communities/other_communities/chinafirst/f/microstation-projectwise/119458/dgn/368601#368601
请问我的代码问题出在哪?万分感谢
我用CE中新的API重写了这个功能如下(所有对象几乎都是来自于CE SDK,尽量不用C++本身的和老的MDL C函数):
void writeElementToExternalDgn() { WString strDgn("c:\\111\\test.dgn"); DgnDocumentPtr pDgnDoc = DgnDocument::CreateForLocalFile(strDgn.GetWCharCP()); DgnFilePtr pDgn = DgnFile::Create(*pDgnDoc, DgnFileOpenMode::ReadWrite); StatusInt openForWriteStatus; pDgn->LoadDgnFile(&openForWriteStatus); if (!pDgn->IsOpen()) { mdlDialog_dmsgsPrint(L"The file can't be written"); return; } pDgn->FillDictionaryModel(); DgnModelPtr pModel = pDgn->LoadModelById(pDgn->GetDefaultModelId()); pDgn->FillSectionsInModel(pModel.get()); DSegment3d segment = DSegment3d::From(0, 0, 0, 0, 0, 10000); EditElementHandle eeh; LineHandler::CreateLineElement(eeh, nullptr, segment, true, *pModel); eeh.AddToModel(); pDgn->ProcessChanges(DgnSaveReason::FileClose); }
在我本机成功实现,非常感谢符老师,顺便问一下,如何将我的当前dgn的Model拷贝到外部dgn的Model中。我用的是mdlModelRef_copyModel,但是拷贝失败,代码如下:
void writeElementToExternalDgn() { WString strDgn("c:\\111\\test.dgn"); DgnDocumentPtr pDgnDoc = DgnDocument::CreateForLocalFile(strDgn.GetWCharCP()); DgnFilePtr pDgn = DgnFile::Create(*pDgnDoc, DgnFileOpenMode::ReadWrite); StatusInt openForWriteStatus; pDgn->LoadDgnFile(&openForWriteStatus); if (!pDgn->IsOpen()) { mdlDialog_dmsgsPrint(L"The file can't be written"); return; } pDgn->FillDictionaryModel(); DgnModelPtr pModel = pDgn->LoadModelById(pDgn->GetDefaultModelId()); pDgn->FillSectionsInModel(pModel.get()); // 以下是拷贝代码 返回失败 DgnModelRefP returnDestModelRef; DgnModelRefP activeModelRefP = ISessionMgr::GetActiveDgnModelRefP(); StatusInt sInt = mdlModelRef_copyModel(&returnDestModelRef, activeModelRefP, pDgn.get(), "3D Metric Design", "no_delete"); pDgn->ProcessChanges(DgnSaveReason::FileClose); }
通过这个方法实现:
pDgn->CopyModelContents(*pModel.get(), *ptrModel, nullptr)
不过在SDK的例子中,也看过很多CE中新的API和老的MDL C函数)混用,
也没有问题,所以带来的困惑就是:哪些老的MDL C函数)不可用,因为毕竟还有很多接口都是老的MDL C函数)?
Answer Verified By: Yongan.Fu
//用来测试同一个文件多次打开 for(int i=0;i<2; i++) { string fullPath = @"C:\Users\Administrator\Desktop\111.dgn"; DgnDocument pDgnDoc = DgnDocument.CreateForLocalFile(fullPath); DgnFile pDgn = DgnFile.Create(pDgnDoc, DgnFileOpenMode.ReadWrite).DgnFile; StatusInt openForWriteStatus; //第二次就不行了 感觉第一次没关闭 pDgn.LoadDgnFile(out openForWriteStatus); if (!pDgn.IsOpen) { return; } pDgn.FillDictionaryModel(); //感觉没关闭(因为第二次LoadDgnFile会出现问题) StatusInt stat = pDgn.ProcessChanges(DgnSaveReason.FileClose); }
符老师 我依葫芦画瓢(C#) 但发现 我第二次再加载时 就会有问题了 是不是第一次没关闭呢?这种怎么处理?
每次迭代最后调一下DgnFIle的Release