【msce c++】如何把参考文件的元素复制到当前model中?需要保留参考的缩放参数。

我这边可能需求更多一点,我想要的是把参考的图形复制一份保存在 当前model,这些元素需要有参考的旋转,缩放等信息。

如下代码。我做了测试,只是把原始参考文件的元素复制过来了, 这些元素不具有参考的参数,比如旋转和缩放等。

DgnDocumentMonikerPtr moniker = Bentley::DgnPlatform::DgnDocumentMoniker::CreateFromFileName(CCommonOperate::CStringToBentleyWString(frameInfo.sFilePath).c_str(),NULL);

	DgnAttachmentP attachment;
	if (0 != ACTIVEMODEL->CreateDgnAttachment(attachment, *moniker, CCommonOperate::CStringToBentleyWString(frameInfo.sModelName).c_str())) {
		MessageBox::Show("参考模版文件失败");
	}
	attachment->SetStoredScale(dScale);
	attachment->SetMasterOrigin(insertPoint);
	Bentley::DgnModelP dgnModelP = attachment->GetDgnModelP();
	for (PersistentElementRefP const& elemRef : *dgnModelP->GetGraphicElementsP())
	{
		if (CELL_HEADER_ELM != elemRef->GetElementType()) {
			continue;
		}

		Bentley::DgnPlatform::ElementId elmID = elemRef->GetElementId();
		EditElementHandle eehCell(elmID, dgnModelP->AsDgnModelP());
		if (!eehCell.IsValid()) {
			continue;
		}

		Bentley::DgnPlatform::ElementCopyContext copier(ACTIVEMODEL);
		if (SUCCESS != copier.DoCopy(eehCell))
		{
			MessageBox::Show("有一个没成功");
		}
		
	}
	ACTIVEMODEL->DeleteDgnAttachment(attachment);

Parents
  • 使用老的mdl函数应该可以。mdlElmdscr_readToMaster能获取到变换过的元素

  • 我使用这一套老函数成功了。

    int scanCallback(MSElementDescr* pDes, void* pArg, Bentley::DgnPlatform::ScanCriteria* pScan)
    {
    if (mdlElement_getType(&pDes->el) != CELL_HEADER_ELM) {
    return SUCCESS;
    }
    
    ElementCopyContextP ccP = mdlCopyContext_create();
    mdlCopyContext_setScaleToDestination(ccP, true);
    mdlCopyContext_setModelFromElmdscr(ccP, true);
    if (SUCCESS == mdlElmdscr_copy(&pDes, mdlScanCriteria_getModel(pScan), ACTIVEMODEL, ccP)) {
    Bentley::DgnPlatform::ElementId id = mdlElement_getID(&pDes->el);
    TempScanElmId.push_back(id);
    }
    mdlCopyContext_free(&ccP);
    return SUCCESS;
    }
    
    
    
    mdlRefFile_attach();
    
    Bentley::DgnPlatform::ScanCriteria *scP = mdlScanCriteria_create();
    mdlScanCriteria_setReturnType(scP, MSSCANCRIT_ITERATE_ELMDSCR, FALSE, FALSE) ;
    mdlScanCriteria_setElmDscrCallback(scP, (PFScanElemDscrCallback)scanCallback, NULL) ;
    mdlScanCriteria_setDrawnElements(scP) ;
    mdlScanCriteria_setModel(scP, dgnModelRef) ;
    mdlScanCriteria_scan(scP, NULL, NULL, NULL);
    mdlScanCriteria_free(scP);

  • element没有类似用attach作为model的构造。SetSourceModelRef设置为attach也没效果。

  • 请参考我改写的代码如下:

    public static void DoCopyTest()
            {
                DgnModel activeModel = Session.Instance.GetActiveDgnModel();
                foreach (DgnAttachment dgnAttach in activeModel.GetDgnAttachments())
                {
                    using (ElementCopyContext copier = new ElementCopyContext(activeModel))
                    {
                        Element elemInRef = dgnAttach.GetDgnModel().FindElementById((ElementId)1438L);
                        if (!elemInRef.IsValid)
                            continue;
                        copier.SetSourceModelRef(dgnAttach);
                        copier.DoCopy(elemInRef);
                    }
                }
            }



  • 符老师你好,我的拷贝代码如下,并不能保留参考的缩放和移动参数。

    public List<Element> ScanAttach( DgnAttachment atach)
            {
                List<Element> rtnElem = new List<Element>();
                ElementPropertiesSetter transSet = new ElementPropertiesSetter();
                transSet.SetTransparency(0.9);
                using (ElementCopyContext copyContext = new ElementCopyContext(workModel))
                {
                    copyContext.SetSourceModelRef(atach);
                    copyContext.WriteElements = false;
                    ModelElementsCollection copyElems = atach.GetDgnModel().GetGraphicElements();
                    foreach (Element ele in copyElems)
                    {
                        Element copyEle = copyContext.DoCopy(ele);
                        transSet.Apply(copyEle);
                        rtnElem.Add(copyEle);
                    }
                }
                return rtnElem;
            }

  •         public List<Element> ScanAttach( DgnAttachment atach)
            {
                List<Element> rtnElem = new List<Element>();
                ElementPropertiesSetter transSet = new ElementPropertiesSetter();
                transSet.SetTransparency(0.9);
                atach.GetTransformToParent(out DTransform3d tranceTo, true);
                TransformInfo tranceInfo2 = new TransformInfo(tranceTo);
                using (ElementCopyContext copyContext = new ElementCopyContext(workModel))
                {
                    copyContext.SetSourceModelRef(atach);
                    copyContext.WriteElements = false;
                    ModelElementsCollection copyElems = atach.GetDgnModel().GetGraphicElements();
                    foreach (Element ele in copyElems)
                    {
                        Element copyEle = copyContext.DoCopy(ele);
                        copyEle.ApplyTransform(tranceInfo2);
                        transSet.Apply(copyEle);
                        rtnElem.Add(copyEle);
                    }
                }
                return rtnElem;
            }

    用添加Transform的方式实现了。。。

  • 是的。如果参考到主模型有变换的话,需要应用变换。



Reply Children
No Data