private void Init() { _children = new List<SimpleGroup>(); _buildingElementTypes = new List<SimpleBuildingElementType>(); IEnumerable<IfcRelAssignsToGroup> rels = _model.Instances.Where<IfcRelAssignsToGroup>(r => r.RelatingGroup == _group).ToList(); if (rels.FirstOrDefault() == null) { using (XbimReadWriteTransaction trans = _model.BeginTransaction("Group relation creation")) { var rel = _model.Instances.New<IfcRelAssignsToGroup>(r => r.RelatingGroup = _group); trans.Commit(); (rel as INotifyPropertyChanged).PropertyChanged += new PropertyChangedEventHandler(RelationChanged); (rel.RelatedObjects as INotifyCollectionChanged).CollectionChanged -= new NotifyCollectionChangedEventHandler(ElementCollectionChanged); (rel.RelatedObjects as INotifyCollectionChanged).CollectionChanged += new NotifyCollectionChangedEventHandler(ElementCollectionChanged); } } else { foreach (var rel in rels) { (rel as INotifyPropertyChanged).PropertyChanged += new PropertyChangedEventHandler(RelationChanged); (rel.RelatedObjects as INotifyCollectionChanged).CollectionChanged -= new NotifyCollectionChangedEventHandler(ElementCollectionChanged); (rel.RelatedObjects as INotifyCollectionChanged).CollectionChanged += new NotifyCollectionChangedEventHandler(ElementCollectionChanged); IEnumerable<IfcGroup> groups = rel.RelatedObjects.OfType<IfcGroup>(); foreach (IfcGroup group in groups) { SimpleGroup child = new SimpleGroup(group, this); child.PropertyChanged += new PropertyChangedEventHandler(ChildChanged); _children.Add(child); } IEnumerable<IfcTypeProduct> types = rel.RelatedObjects.OfType<IfcTypeProduct>(); foreach (var type in types) { SimpleBuildingElementType simpleType = new SimpleBuildingElementType(type); simpleType.PropertyChanged -= new PropertyChangedEventHandler(TypeChanged); simpleType.PropertyChanged += new PropertyChangedEventHandler(TypeChanged); _buildingElementTypes.Add(simpleType); } } } }
void ElementCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Add: foreach (var item in e.NewItems) { IfcTypeProduct type = item as IfcTypeProduct; if (type == null) continue; //find existing in the structure SimpleBuildingElementType simpleType = GetTypeFromHierarchy(type); //if it does not exist than create new one if (simpleType == null) simpleType = new SimpleBuildingElementType(type); _buildingElementTypes.Add(simpleType); } break; case NotifyCollectionChangedAction.Remove: foreach (var item in e.OldItems) { IfcTypeProduct type = item as IfcTypeProduct; if (type == null) continue; SimpleBuildingElementType simpleType = _buildingElementTypes.Where(st => st.IfcType == type).FirstOrDefault(); if (simpleType != null) _buildingElementTypes.Remove(simpleType); } break; case NotifyCollectionChangedAction.Replace: throw new NotSupportedException(); case NotifyCollectionChangedAction.Reset: Init(); break; default: break; } }