[MSCE C#]元素个数获取与操作

我想查找出一个Model中Element的个数,然后通过循环的方式(其他方式也可以),遍历每一个Element,然后执行相关的函数。

 class Convert : ElementGraphicsProcessor
    {
        public static void Material2(string unparsed)
        {
            Bentley.DgnPlatformNET.DgnModel dgnModel = Session.Instance.GetActiveDgnModel();
            Bentley.DgnPlatformNET.DgnFile dgnFile = Session.Instance.GetActiveDgnFile();
            //读取model

            if (0 == dgnModel.GetElementCount(DgnModelSections.Model))
            {
                MessageCenter.Instance.ShowInfoMessage("未找到可读取元素!", null, true);
                return;
            }
            
            uint i,j;

            Element ele = null;
            j = dgnModel.GetElementCount(DgnModelSections.Model);
            for (i = 0; i < j; i++)
            {
                // 返回数组的第i的个元素
                ele =;//不知如何获取
                
                Material3(dgnModel,dgnFile,ele);
            }

现在的问题是我不知道用什么函数来调用模型的Element中的element顺序

Parents
  • DgnModel下有GetElements、GetControlElements、GetGraphicElements可以获取到所有元素、控制元素、图形元素的集合,迭代返回的这个集合就行了。

  • 谢谢老师解答,不过我通过这种方法调试,读取element始终显示为空。

    public static void Material2(string unparsed)
            {
                
                Bentley.DgnPlatformNET.DgnModel dgnModel = Session.Instance.GetActiveDgnModel();
                Bentley.DgnPlatformNET.DgnFile dgnFile = Session.Instance.GetActiveDgnFile();
                //读取model
    
                if (0 == dgnModel.GetElementCount(DgnModelSections.Model))
                {
                    MessageCenter.Instance.ShowInfoMessage("未找到可读取元素!", null, true);
                    return;
                }
    
                uint j;
                Element ele = null;
                
                j = dgnModel.GetElementCount(DgnModelSections.Model);
                ModelElementsCollection eles =dgnModel.GetControlElements();
                
                eles.GetEnumerator().Reset();
                eles.GetEnumerator().MoveNext();
                eles.GetEnumerator().MoveNext();
                var i=0;
                
                while (i < j)
                {
                    // 返回数组的第i的个元素
                    ele = eles.GetEnumerator().Current;
                    
                    
                    Material3(dgnModel,dgnFile, ele);
                    eles.GetEnumerator().MoveNext();
                }
                  
            }

    就是后面那个ele

Reply
  • 谢谢老师解答,不过我通过这种方法调试,读取element始终显示为空。

    public static void Material2(string unparsed)
            {
                
                Bentley.DgnPlatformNET.DgnModel dgnModel = Session.Instance.GetActiveDgnModel();
                Bentley.DgnPlatformNET.DgnFile dgnFile = Session.Instance.GetActiveDgnFile();
                //读取model
    
                if (0 == dgnModel.GetElementCount(DgnModelSections.Model))
                {
                    MessageCenter.Instance.ShowInfoMessage("未找到可读取元素!", null, true);
                    return;
                }
    
                uint j;
                Element ele = null;
                
                j = dgnModel.GetElementCount(DgnModelSections.Model);
                ModelElementsCollection eles =dgnModel.GetControlElements();
                
                eles.GetEnumerator().Reset();
                eles.GetEnumerator().MoveNext();
                eles.GetEnumerator().MoveNext();
                var i=0;
                
                while (i < j)
                {
                    // 返回数组的第i的个元素
                    ele = eles.GetEnumerator().Current;
                    
                    
                    Material3(dgnModel,dgnFile, ele);
                    eles.GetEnumerator().MoveNext();
                }
                  
            }

    就是后面那个ele

Children