怎么将一个dgn文件中的model拷贝到另一个dgn文件中的model中去

求助

  • 这个没办法直接复制,只能是获取源Model中的元素,然后拷贝到目标Model中去,跨文件或者跨Model拷贝元素需要用到ElementCopyContext类,您可以搜一下以前的贴子看看,有讨论过这个类型,而且也有示例代码的。

  • 郭老师,我按照之前的帖子试了,但是不知道怎么才能将model参考进来,这个是我的代码

    void Copy()
    {
    	WString fileName(L"RebarAddCoupler.dgn");
    	//从文件名字读取模型生成指针
    	DgnDocumentMonikerPtr moniker = Bentley::DgnPlatform::DgnDocumentMoniker::CreateFromFileName(fileName.c_str(), NULL);
    	//从一个model到另一个model的引用指针
    	DgnAttachmentP attachment;
    	WString ModelName(L"");
    	if (0 != ACTIVEMODEL->CreateDgnAttachment(attachment, *moniker, ModelName.c_str()))
    	{
    		mdlDialog_dmsgsPrint(L"参考模板文件失败");
    		//return;
    	}
    	DgnModelP pActiveModel = ISessionMgr::GetActiveDgnModelP();
    	DgnAttachmentArrayP pAttachArray = pActiveModel->GetDgnAttachmentsP();
    	if (NULL == pAttachArray)
    	{
    		mdlDialog_dmsgsPrint(L"No Attached Reference");
    		return;
    	}
    	for (DgnAttachmentP pAttach : *pAttachArray)
    	{
    		//DgnModelP newModel = pAttach->GetDgnModelP();
    
    		EditElementHandle eeh(1438L, pAttach);
    		if (!eeh.IsValid())
    			continue;
    
    		Bentley::DgnPlatform::ElementCopyContext copier(ACTIVEMODEL);
    		//确定副本来源的模型 原来可以是参考附件
    		copier.SetSourceModelRef(eeh.GetModelRef());
    		//定义复制的元素是否应按照比例缩放以匹配目标模型中的单位
    		copier.SetTransformToDestination(true);
    		//指定元素变成原始元素的副本
    		copier.DoCopy(eeh);
    	}
    }
    

  • 不用参考的,您直接使用DgnFile::Create打开外部dgn文件,然后读取里边的元素,使用ElementCopyContext将元素拷贝到当前文件即可。