各位老师好!
在学习MSTN ce版本时 ,通过DgnECExample 这个例子可以获取EC属性...但是在获取ElementID属性 发现出现多个ElementID值...请问为什么会出现这些?该如何筛选出我们要的某一种呢?public void SearchInstance() { DgnECManager manager = DgnECManager.Manager; //作用域指定要搜索实例的元素集 FindInstancesScope scope = FindInstancesScope.CreateScope(Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModel(), new FindInstancesScopeOption(DgnECHostType.Element, false)); //指定要返回的实例 ECQuery query = new ECQuery(SearchClasses()); query.SelectClause.SelectAllProperties = true; DgnECInstanceCollection ecInstances = manager.FindInstances(scope, query); LstInstances.Items.Clear(); //遍历获取到的实例ecInstacne foreach (IDgnECInstance ecInstance in ecInstances) { try { ListViewItem itemClass = new ListViewItem(ecInstance.ClassDefinition.Name); itemClass.SubItems.Add(""); itemClass.SubItems.Add(""); itemClass.SubItems.Add(""); LstInstances.Items.Add(itemClass); //获取到实例ecInstance下的类名 string className = ecInstance.ClassDefinition.Name; //MessageBox.Show(ecInstance.GetPropertyValue("all").Property.ToString()); //遍历类名下的所有属性名称及属性名称 foreach (IECProperty prop in ecInstance.ClassDefinition.Properties(true)) { ListViewItem itemProp = new ListViewItem(); itemProp.SubItems.Add(prop.Name); itemProp.SubItems.Add(prop.Type.Name); string propName = prop.Name; IECPropertyValue propValue = ecInstance.GetPropertyValue(prop.Name); if (propName == "ElementID") { //MessageBox.Show(propValue.StringValue); itemProp.SubItems.Add(propValue.StringValue); } if (propName == "ElementDescription") { itemProp.SubItems.Add(propValue.StringValue); } //MessageBox.Show(propName +":"+ propValue); LstInstances.Items.Add(itemProp); } } catch (Exception ex) { continue; } } } public static IECClass[] SearchClasses() { List<IECClass> classes = new List<IECClass>(); DgnECManager manager = DgnECManager.Manager; List<string> schemas = (List<string>)manager.DiscoverSchemasForModel(Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModel(), ReferencedModelScopeOption.All, true); //MessageBox.Show(schemas.Count().ToString()); foreach (string schemaName in schemas) { //MessageBox.Show(schemaName); FindInstancesScope scope = FindInstancesScope.CreateScope(Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModel(), new FindInstancesScopeOption()); string new_scehmaName = schemaName.Substring(0, schemaName.IndexOf(".")); IECSchema schema = DgnECManager.Manager.LocateSchemaInScope(scope, new_scehmaName, 1, 0, SchemaMatchType.Exact); if (schema == null) { continue; } foreach (IECClass ecClass in schema.GetClasses()) { //MessageBox.Show(ecClass.ToString()); classes.Add(ecClass); } } return classes.ToArray(); }
public void SearchInstance() { DgnECManager manager = DgnECManager.Manager; //作用域指定要搜索实例的元素集 FindInstancesScope scope = FindInstancesScope.CreateScope(Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModel(), new FindInstancesScopeOption(DgnECHostType.Element, false)); //指定要返回的实例 ECQuery query = new ECQuery(SearchClasses()); query.SelectClause.SelectAllProperties = true; DgnECInstanceCollection ecInstances = manager.FindInstances(scope, query); LstInstances.Items.Clear(); //遍历获取到的实例ecInstacne foreach (IDgnECInstance ecInstance in ecInstances) { try { ListViewItem itemClass = new ListViewItem(ecInstance.ClassDefinition.Name); itemClass.SubItems.Add(""); itemClass.SubItems.Add(""); itemClass.SubItems.Add(""); LstInstances.Items.Add(itemClass); //获取到实例ecInstance下的类名 string className = ecInstance.ClassDefinition.Name; //MessageBox.Show(ecInstance.GetPropertyValue("all").Property.ToString()); //遍历类名下的所有属性名称及属性名称 foreach (IECProperty prop in ecInstance.ClassDefinition.Properties(true)) { ListViewItem itemProp = new ListViewItem(); itemProp.SubItems.Add(prop.Name); itemProp.SubItems.Add(prop.Type.Name); string propName = prop.Name; IECPropertyValue propValue = ecInstance.GetPropertyValue(prop.Name); if (propName == "ElementID") { //MessageBox.Show(propValue.StringValue); itemProp.SubItems.Add(propValue.StringValue); } if (propName == "ElementDescription") { itemProp.SubItems.Add(propValue.StringValue); } //MessageBox.Show(propName +":"+ propValue); LstInstances.Items.Add(itemProp); } } catch (Exception ex) { continue; } } } public static IECClass[] SearchClasses() { List<IECClass> classes = new List<IECClass>(); DgnECManager manager = DgnECManager.Manager; List<string> schemas = (List<string>)manager.DiscoverSchemasForModel(Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModel(), ReferencedModelScopeOption.All, true); //MessageBox.Show(schemas.Count().ToString()); foreach (string schemaName in schemas) { //MessageBox.Show(schemaName); FindInstancesScope scope = FindInstancesScope.CreateScope(Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModel(), new FindInstancesScopeOption()); string new_scehmaName = schemaName.Substring(0, schemaName.IndexOf(".")); IECSchema schema = DgnECManager.Manager.LocateSchemaInScope(scope, new_scehmaName, 1, 0, SchemaMatchType.Exact); if (schema == null) { continue; } foreach (IECClass ecClass in schema.GetClasses()) { //MessageBox.Show(ecClass.ToString()); classes.Add(ecClass); } } return classes.ToArray(); }
通过EC机制往元素上添加属性,首先要定义数据类型,就好比编程里边要先定义一个结构体(struct或者说是class)。数据类型定义好了之后要构造一个对象实例(在EC中叫ECInstace)才能添加到元素上去。您这里搜索的不是特定的某个元素上的ECInstace,而是整个文件中特定类型的所有ECInstace,所以会出现很多了。
一个元素只能对应一个实例吗?
一个实例只能对应一个元素,但是一个元素上可以添加多个ECClass的实例。
那郭工 那我该用什么函数去搜索特定元素的ECinstance
CreateScope有一个重载函数接收一个Element参数,用这个重载函数查找指定元素上的EC属性。
Answer Verified By: 超 上官
采用ScanCriteria这个扫描当前当前文件中的图形元素,然后遍历传入是否也可以?
但是不知 new ScanDelegate()这有何用?
StatusInt si = sc.Scan(new ScanDelegate());
ScanDelegate是一个委托,您需要定义一个跟这个委托签名一致的函数,然后传递给Scan。这样Scan内部会把所有满足扫描要求的元素传递给您定义的函数。