[MSCE C#]您好老师我想请问下,我修改了cell中的子元素然后replaceinmodel文件中的cell元素就出错请问是什么原因呢?有没有案例代码可供参考呢?

 ElementPropertiesSetter elePropSet = new ElementPropertiesSetter();
RgbColorDef rgbColor = new RgbColorDef((byte)AutoDimFormatInfo.rgbline.R, (byte)AutoDimFormatInfo.rgbline.G, (byte)AutoDimFormatInfo.rgbline.B);
uint colormap = DgnColorMap.CreateElementColor(rgbColor, null, null, dgnfile);
elePropSet.SetColor(colormap);
for (int i = 0; i < listline.Count; i++)
{
 elePropSet.Apply(listline[i]);
 headcellElement.ReplaceInModel(headcellElement);//报错
 //headcellElement.ReplaceInModel(listline[i]);//报错
} 

您好老师,我修改完cell的子元素,ReplaceInModel其中的cell元素仍然会报错,其他代码没有问题,请问老师能否提供下案例代码怎么修改cell子元素呢?

Parents
  • 你如果仅仅是修改Cell的颜色的话,我测试了不需要遍历到Cell中的每个子元素呀。如下代码可以:

            public static void ChangeCellProp(string unparsed)
            {
                DgnModel myModel = Session.Instance.GetActiveDgnModel();
                CellHeaderElement myCell = myModel.FindElementById((ElementId)3288L) as CellHeaderElement;
                Element oldCell = Element.GetFromElementRef(myCell.GetNativeElementRef());
                ElementPropertiesSetter setter = new ElementPropertiesSetter();
                setter.SetColor(3);
                setter.Apply(myCell);
                myCell.ReplaceInModel(oldCell);
            }



  • 您好 符老师,我这边的需求是分别去修改Cell中的子元素,例如:有字体,有直线(线串),分别修改字体的颜色,字体样式,直线的颜色,线性之类的。通过ElementPropertySetter修改,修改完之后想通过ReplaceInModel刷新Cell元素,然后就出错了。

  • 有可能是.NET封装的问题。用C#写的如下代码确实会导致Mstn崩溃:

            public static void ChangeCellProp(string unparsed)
            {
                DgnModel myModel = Session.Instance.GetActiveDgnModel();
                CellHeaderElement myCell = myModel.FindElementById((ElementId)3288L) as CellHeaderElement;
                Element oldCell = Element.GetFromElementRef(myCell.GetNativeElementRef());
                ElementPropertiesSetter setter = new ElementPropertiesSetter();
                ChildElementEnumerator children = new ChildElementEnumerator(myCell);
                while (children.MoveNext())
                {
                    switch (children.Current.ElementType)
                    {
                        case MSElementType.Shape:
                            setter.SetColor(3);
                            setter.Apply(children.Current);
                            break;
                        case MSElementType.LineString:
                            setter.SetColor(4);
                            setter.Apply(children.Current);
                            break;
                        default:
                            break;
                    }
                }
                myCell.ReplaceInModel(oldCell);
            }

    但如下C++代码就能正确工作:

    void changeCellProp()
    {
    	EditElementHandle myCell(3288L, ACTIVEMODEL);
    	ElementRefP oldRef = myCell.GetElementRef();
    	ElementPropertiesSetterPtr setter = ElementPropertiesSetter::Create();
    	for (ChildEditElemIter child(myCell); child.IsValid(); child = child.ToNext())
    	{
    		switch (child.GetElementType())
    		{
    		case SHAPE_ELM:
    			setter->SetColor(3);
    			setter->Apply(child);
    			break;
    		case LINE_STRING_ELM:
    			setter->SetColor(4);
    			setter->Apply(child);
    			break;
    		default:
    			break;
    		}
    	}
    	myCell.ReplaceInModel(oldRef);
    }



    Answer Verified By: Shaopu.Cao 

  • 好的,谢谢符老师,Grin

    我通过如下方法解决了,

    参考自:https://communities.bentley.com/products/programming/microstation_programming/f/microstation-programming---forum/132050/connect-up-4-net-api-modify-delete-cell-subelement

    如下贴部分代码(参考思路是这样的),el为子元素:

    private List<Element> listEle = new List<Element>();
    ElementPropertiesSetter elePropSet = new ElementPropertiesSetter();
    RgbColorDef rgbColor = new RgbColorDef((byte)AutoDimFormatInfo.rgbline.R, (byte)AutoDimFormatInfo.rgbline.G, (byte)AutoDimFormatInfo.rgbline.B);
    uint colormap = DgnColorMap.CreateElementColor(rgbColor, null, null, dgnfile);
    elePropSet.SetColor(colormap);
    elePropSet.Apply(el);
    listEle.Add(el);     
    
     DPoint3d origin;
     headcellElement.GetTransformOrigin(out origin);
     CellHeaderElement newCell = new CellHeaderElement(dgnmodel, "AutoWaterLevel", origin, DMatrix3d.Identity, listEle);
     newCell.ReplaceInModel(headcellElement);
     listEle.Clear();

    Answer Verified By: Shaopu.Cao 

Reply Children