【C# CE】通过EC属性来获取对应的元素

请问下图中,资源管理器中的所有信息我能有方法获取到吗?

主要我是想不用遍历所有元素来一一获取ec属性看是不是我想要的,我就想获取一个ec属性组,把他所对应的元素给直接找出来

我看资源管理器中就有现成的信息,我有没有办法直接获取到,它应该也是通过ec来找元素的,而不是通过元素找ec的吧?

Parents Reply Children
  • 请问是下列的方法吗?有没有使用的例子啊?它传的几个参数是怎么来的啊?都能通过ECClass得到吗?

    我下面的测试代码用FindInstances方法,获取不到任何instance,

    而FindDgnECInstances()的上面第一个重载的参数我不知道怎么来的,第二个重载的话要传入ElementAgenda,我并没有具体元素传入

    具体怎么才能通过ECClass获取到Instance啊?

    //获取当前文件中的所有写有编码信息的元素  并提取信息
                    var dgnModel = Session.Instance.GetActiveDgnModel();
                    var dgnFile = Session.Instance.GetActiveDgnFile();
                    var list1 = DgnECManager.Manager.DiscoverSchemasForModel(dgnModel, ReferencedModelScopeOption.All, false);
                    foreach (var item in list1)
                    {
                        var strs = item.Split('.');
                        if (strs.Length != 3)
                        {
                            //出错
                            CustomMessageBox.Show("提取ec属性出错,请联系管理员!", "错误", CustomMessageBoxButton.OK, CustomMessageBoxIcon.Error);
                            return;
                        }
                        int majorVersion;
                        int minorVersion;
                        if (int.TryParse(strs[1], out majorVersion) && int.TryParse(strs[2], out minorVersion))
                        {
                            FindInstancesScope scope = FindInstancesScope.CreateScope(dgnModel, new FindInstancesScopeOption());
                            IECSchema schema = DgnECManager.Manager.LocateSchemaInScope(scope, strs[0], majorVersion, minorVersion, SchemaMatchType.Latest);
                            var iecclass1 = schema.GetClass("BMXX");
                            if(iecclass1 != null)
                            {
                                ECQuery eCQuery = new ECQuery(iecclass1);
                                var instancesList = DgnECManager.Manager.FindInstances(scope, eCQuery);
                                foreach (var ins in instancesList)
                                {
                                    CustomMessageBox.Show("获取成功", "提示", CustomMessageBoxButton.OK, CustomMessageBoxIcon.Information);
                                    break;
                                }
                            }
                            var iecclass2 = schema.GetClass("SJSX");
                            if (iecclass2 != null)
                            {
                                ECQuery eCQuery = new ECQuery(iecclass2);
                                var instancesList = DgnECManager.Manager.FindInstances(scope, eCQuery);
                                foreach(var ins in instancesList)
                                {
                                    CustomMessageBox.Show("获取成功", "提示", CustomMessageBoxButton.OK, CustomMessageBoxIcon.Information);
                                    break;
                                }
                            }
    
                        }
                        else
                        {
                            //出错
                            CustomMessageBox.Show("提取ec属性出错,请联系管理员!", "错误", CustomMessageBoxButton.OK, CustomMessageBoxIcon.Error);
                            return;
                        }
    
                    }

  • 请参考如下代码:

    string SchemaName = "TunnelAttributes";
                string ClassName = "TunnelCode";
                FindInstancesScope scope = FindInstancesScope.CreateScope(Session.Instance.GetActiveDgnModel(), new FindInstancesScopeOption(DgnECHostType.Element));
                FindInstancesScope scopeTemp = FindInstancesScope.CreateScope(Session.Instance.GetActiveDgnFile(), new FindInstancesScopeOption());
                int verMaj = 01, verMin = 0;
                IECSchema ecschema = DgnECManager.Manager.LocateSchemaInScope(scopeTemp, SchemaName, verMaj, verMin, SchemaMatchType.Latest);
                scopeTemp = FindInstancesScope.CreateScope(Session.Instance.GetActiveDgnFile(), new FindInstancesScopeOption());
                ecschema = DgnECManager.Manager.LocateSchemaInScope(scopeTemp, SchemaName, verMaj, verMin, SchemaMatchType.Latest);
                IECClass ecClass = ecschema.GetClass(ClassName);
                IECClass[] ecClassArr = new IECClass[1];
                ecClassArr[0] = ecClass;
                ECQuery ecQuery = new ECQuery(ecClassArr);
                DgnECInstanceCollection dgnECInsCol = DgnECManager.Manager.FindInstances(scope, ecQuery);
                foreach(var curVar in dgnECInsCol)
                {
                    MessageCenter.Instance.ShowInfoMessage(curVar.Element.ElementId.ToString(), "", false);
                }

    Answer Verified By: lai xingguang 

  • 成功了,感谢,

    我将之前的FindInstancesScope scope = FindInstancesScope.CreateScope(Session.Instance.GetActiveDgnModel(), new FindInstancesScopeOption());

    替换成FindInstancesScope scope = FindInstancesScope.CreateScope(Session.Instance.GetActiveDgnModel(), new FindInstancesScopeOption(DgnECHostType.Element));

    就可以成功获取了,那就应该是FindInstancesScopeOption的构造不对的问题了