[MSCE C++ U6]如何打开一个dgn文件并向其添加元素?

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

请问我的代码问题出在哪?万分感谢

Parents
  • 我用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);
    }



  • //用来测试同一个文件多次打开
                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#)  但发现  我第二次再加载时   就会有问题了   是不是第一次没关闭呢?这种怎么处理?

Reply
  • //用来测试同一个文件多次打开
                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#)  但发现  我第二次再加载时   就会有问题了   是不是第一次没关闭呢?这种怎么处理?

Children