【MSCE C#】放置参数化cell并修改属性,元素外形未改变?

我在一个dgn中批量放置若干个参数化cell,每个cell的参数设置为不一样,但是只有最后一个cell外形尺寸正确,前面放置的cell都和最后放置的外形一致,如下图所示。查看前面放置cell的属性,发现属性已经正确修改。

但是我用鼠标选中cell,稍微移动一下,前面放置的cell就能恢复本身尺寸,移动后的效果如下

调用参数化cell并修改属性的部分代码如下,请问老师这是什么情况?应该如何修改?

ar pcDef = ParametricCellDefinitionElement.FindByName(cellName, dgnFile);
            if (null == pcDef)
            {
                //MessageBox.Show("1");
                var cellModel = LocateCellModel(cellName);
                if (null == cellModel)
                {
                    MessageCenter.Instance.ShowErrorMessage("Not found cell", null, true);
                    return false;
                }
                var hdlr = DgnComponentDefinitionHandler.GetForModel(cellModel);
                var status = hdlr.DefinitionModelHandler.CreateCellDefinition(dgnFile);
                if (ParameterStatus.Success == status)
                    pcDef = ParametricCellDefinitionElement.FindByName(cellName, dgnFile);
                else
                {
                    MessageCenter.Instance.ShowErrorMessage("Error Creating cellDef", null, true);
                    return false;
                }
            }
       
            var pc = ParametricCellElement.Create(pcDef, null, dgnModel);
           
            IDgnECInstance inst = pc.Parameters as IDgnECInstance;
            IECArrayValue arr = inst.GetPropertyValue("ParameterValuesContainer") as IECArrayValue;
            
      
            IECStructValue structVal = arr[0] as IECStructValue;
            structVal.SetValue("Adhoc_Value",dia/1000);

            IECStructValue structVal2 = arr[1] as IECStructValue;  // .Adhoc_Name = _LOCAL_Cabinet_W
            structVal2.SetValue("Adhoc_Value", height/1000);
 
            
            inst.ScheduleChanges(pc);
            
            TransformInfo transInfo2 = new TransformInfo(trans2);
            pc.ApplyTransform(transInfo2);
            pc.AddToModel();

  • 我用如下测试代码,单元使用的是Mstn自带的参数化单元Double Door Cabinet测试,都正常,没有出现您说的现象,您的Mstn版本是多少?

    public static void CreateParametricCell(string pcName,double w=2.0)
            {
                const string setName = "Standard";
                var dgnFile = Session.Instance.GetActiveDgnFile();
                var dgnModel = Session.Instance.GetActiveDgnModel();
                var pcDef = ParametricCellDefinitionElement.FindByName(pcName, dgnFile);
                if (null == pcDef)  
                {
                    var cellModel = LocateCellModel(pcName);
                    if (null == cellModel)
                    {
                        MessageCenter.Instance.ShowErrorMessage("Not found cell", null, true);
                        return;
                    }
                    var hdlr = DgnComponentDefinitionHandler.GetForModel(cellModel);
                    var status = hdlr.DefinitionModelHandler.CreateCellDefinition(dgnFile);
                    if (ParameterStatus.Success == status)
                        pcDef = ParametricCellDefinitionElement.FindByName(pcName, dgnFile);
                    else
                    {
                        MessageCenter.Instance.ShowErrorMessage("Error Creating cellDef", null, true);
                        return;
                    }
                }
                var pc = ParametricCellElement.Create(pcDef, setName, dgnModel);
                IDgnECInstance inst = pc.Parameters as IDgnECInstance;
                IECArrayValue arr = inst.GetPropertyValue("ParameterValuesContainer") as IECArrayValue;
                IECStructValue structVal = arr[1] as IECStructValue;  // .Adhoc_Name = _LOCAL_Cabinet_W
                structVal.SetValue("Adhoc_Value", w);
                inst.ScheduleChanges(pc);
                pc.AddToModel();
                return;
            }

  • 我其实用的OPM U9,新建一个DGN文件又能成功生成

  • 郭老师,我在MS中放置也出现了类似的问题,如下图

     (程序放置出来的cell有两个尺寸未改变)

     (手动移动一下,就能触发改变)

    测试代码如下:

    public static bool CreateParametricCell_Test(double dia,DPoint3d oriPt, double height)
            {
                var dgnFile = Session.Instance.GetActiveDgnFile();
                var dgnModel = Session.Instance.GetActiveDgnModel();
    
                Bentley.MstnPlatformNET.InteropServices.Utilities.ComApp.AttachCellLibrary("...\\GX-FS.cel");
                var pcDef = ParametricCellDefinitionElement.FindByName("GX-FS", dgnFile);
                if (null == pcDef)
                {
                    var cellModel = LocateCellModel("GX-FS");
                    if (null == cellModel)
                    {
                        MessageCenter.Instance.ShowErrorMessage("Not found cell", null, true);
                        return false;
                    }
                    var hdlr = DgnComponentDefinitionHandler.GetForModel(cellModel);
                    var status = hdlr.DefinitionModelHandler.CreateCellDefinition(dgnFile);
                    if (ParameterStatus.Success == status)
                        pcDef = ParametricCellDefinitionElement.FindByName("GX-FS", dgnFile);
                    else
                    {
                        MessageCenter.Instance.ShowErrorMessage("Error Creating cellDef", null, true);
                        return false;
                    }
                }
    
                var pc = ParametricCellElement.Create(pcDef, null, dgnModel);
    
                IDgnECInstance inst = pc.Parameters as IDgnECInstance;
                IECArrayValue arr = inst.GetPropertyValue("ParameterValuesContainer") as IECArrayValue;
                
                //设置直径
                IECStructValue structVal = arr[0] as IECStructValue;  // .Adhoc_Name = _LOCAL_Cabinet_W
                structVal.SetValue("Adhoc_Value", dia / 1000);
    
                //设置高度
                IECStructValue structVal2 = arr[1] as IECStructValue;  // .Adhoc_Name = _LOCAL_Cabinet_W
                structVal2.SetValue("Adhoc_Value", height / 1000);
    
                inst.ScheduleChanges(pc);
                DTransform3d trans = DTransform3d.Identity;
                trans.Translation = new DVector3d(oriPt.X * UorPerMas, oriPt.Y * UorPerMas, oriPt.Z * UorPerMas);  //UOR unit
                TransformInfo transInfo = new TransformInfo(trans);
                pc.ApplyTransform(transInfo);
                pc.AddToModel();
    
                return true;
            }
            
            DPoint3d pt1 = new DPoint3d(0,0,0);
                    DPoint3d pt2 = new DPoint3d(0,2,0);
                    DPoint3d pt3 = new DPoint3d(0,4,0);
                    CreateParametricCell_Test(200, pt1, 200);
                    CreateParametricCell_Test(800, pt2, 700);
                    CreateParametricCell_Test(600, pt3, 400);