MSCE C# 共享单元

下面代码中 CellHeaderElement cellHeaderEle是有元素的,但是创建的SharedCellElement sharedCellElement没有元素,不知道哪里出什么问题了?

            DPoint3d ptOri = new DPoint3d(0,0,0);
            DMatrix3d rMatrix = DMatrix3d.Identity;
            DPoint3d ptScale = new DPoint3d(1, 1, 1);
            CellHeaderElement cellHeaderEle = new CellHeaderElement(Session.Instance.GetActiveDgnModel(), 
                "TunnelSegment", ptOri, rMatrix, listEle);
            cellHeaderEle.AddToModel();

            SharedCellDefinitionElement sharedCellDefinitionElement = new SharedCellDefinitionElement(Session.Instance.GetActiveDgnModel(), 
                "共享单元");
            foreach (Element elem in listEle)
            {
                sharedCellDefinitionElement.AddChildElement(elem);
            }
            sharedCellDefinitionElement.AddChildComplete();
            sharedCellDefinitionElement.AddToModel();

            DPoint3d scale = new DPoint3d(1, 1, 1);
            SharedCellElement sharedCellElement = new SharedCellElement(Session.Instance.GetActiveDgnModel(), null, 
                "共享单元", ptOri, rMatrix, scale);
            sharedCellElement.AddToModel();

Parents
  • sharedCellDefinitionElement中不应该含有你cellHeaderEle中的第一个单元头元素。请参考如下C++代码来创建共享单元。

    bool createCircleShapeElement(EditElementHandleR out, DPoint3dCR center, double radius, DPoint3dCR normal)
    {
    	DEllipse3d ellipse = DEllipse3d::FromCenterNormalRadius(center, (DVec3dR)normal, radius);
    	ICurvePrimitivePtr arcPtr = ICurvePrimitive::CreateArc(ellipse);
    	CurveVectorPtr arcShape;
    	arcShape = CurveVector::Create(CurveVector::BoundaryType::BOUNDARY_TYPE_Outer);
    	arcShape->Add(arcPtr);
    	BentleyStatus status = DraftingElementSchema::ToElement(out, *arcShape, nullptr, true, *ACTIVEMODEL);
    	return status == SUCCESS && out.IsValid();
    }
    void createSharedCell(WCharCP)
    {
    	//Create SharedCell element
    	EditElementHandle sharedCellEeh;
    	DPoint3d origin{ 0, 0, 0 };
    	RotMatrix rMatrix = RotMatrix::FromIdentity();
    	DPoint3d scale{ 1.0, 1.0, 1.0 };
    	SharedCellHandler::CreateSharedCellElement(sharedCellEeh, NULL, L"sharedCell", &origin, &rMatrix, &scale, true, *ACTIVEMODEL);
    	if (!sharedCellEeh.IsValid())
    		return;
    	DgnFileP pDgnFile = ISessionMgr::GetActiveDgnFile();
    	auto sharedCellQuery = dynamic_cast<ISharedCellQuery *>(&sharedCellEeh.GetHandler());
    	if (NULL == sharedCellQuery->GetDefinition(sharedCellEeh, *pDgnFile))
    	{
    		//Create a line
    		DSegment3d segment = DSegment3d::From({ 0,0,0 }, { 0,0, 30000});
    		EditElementHandle lineEeh;
    		LineHandler::CreateLineElement(lineEeh, NULL, segment, true, *ACTIVEMODEL);
    
    		//Create an ellipse
    		EditElementHandle circleEeh;
    		if (!createCircleShapeElement(circleEeh, {0,0,0}, 10000, {0,0,1}))
    			return;
    
    		//Create SharedCell Definition
    		EditElementHandle celldefEeh;
    		SharedCellDefHandler::CreateSharedCellDefElement(celldefEeh, L"sharedCell", true, *ACTIVEMODEL);
    		SharedCellDefHandler::AddChildElement(celldefEeh, lineEeh);
    		SharedCellDefHandler::AddChildElement(celldefEeh, circleEeh);
    		SharedCellDefHandler::AddChildComplete(celldefEeh);
    		if (celldefEeh.IsValid())
    		{
    			celldefEeh.AddToModel();
    		}
    	}
    	if (SharedCellHandler::CreateSharedCellComplete(sharedCellEeh) != SUCCESS)
    	{
    		mdlDialog_dmsgsPrint(L"CreateSharedCellComplete Error.");
    		return;
    	}
    	sharedCellEeh.AddToModel();
    }



  • 符老师,参照您之前的帖子,最终上面的代码创建的共享单元里有元素了。

    但是我添加共享单元至模型,在模型中却找不到该单元。

                DPoint3d scale = new DPoint3d(1, 1, 1);
                SharedCellElement sharedCellElement = new SharedCellElement(m_dgnFile.GetDictionaryModel(), null,
                    "盾构管片", new DPoint3d(0, 0, 0), DMatrix3d.Identity, scale);
                if (sharedCellElement.IsValid)
                {
                    sharedCellElement.AddToModel();
                }


    而我尝试删除该共享单元的时候,提示模型中已经有该单元的实例。不知道我那个地方又出错了。

  • 同问,在MDL中测试了您贴出的例子,可以在模型中添加共享单元定义,但是将其生成共享元素放到模型中时,代码中返回Success,模型中也可以通过返回的ID查询到共享元素,但是模型中显示不出来。

    如图中的File中的Shared Cells定义,右边是在Cell Library中通过手动放置到模型中的共享单元元素,但是通过例子中的代码,只能创建出定义以及共享单元元素,但模型中显示不出模型。

  • 我用上边符老师发的代码测试都正常,是否是dgn文件的问题?您新建个dgn文件试试。

  • 新建一个dgn再测试也不行。能加定义,模型里加进去不显示。

  • 您上边截图里树状图里边圈中的是共享单元定义不是共享单元实例元素,共享单元实例元素要到Model里边找:

Reply Children
No Data