Exemplo n.º 1
0
        /// <summary>
        /// Performs the grouping of elements of the model according to the rules defined in the XML pointed by <paramref name="XMLfileName"/>
        /// </summary>
        /// <param name="XMLfileName">Name of the xml file defining the grouping rules.</param>
        /// <returns></returns>
        public bool GroupElements(string XMLfileName, IfcGroup rootGroup = null)
        {
            if (string.IsNullOrEmpty(XMLfileName))
            {
                throw new ArgumentNullException("File name cannot be null or empty.");
            }
            _xmlDoc = new XmlDocument();
            _xmlDoc.Load(XMLfileName);


            using (XbimReadWriteTransaction trans = _model.BeginTransaction())
            {
                if (rootGroup != null)
                {
                    if (((IPersistIfcEntity)rootGroup).ModelOf != _model)
                    {
                        throw new Exception("Model of the group is different than model to be used.");
                    }
                    _rootGroup = rootGroup;
                }
                else
                {
                    _rootGroup = _model.Instances.New <IfcGroup>(g => g.Name = "Root group");
                }

                bool result = PerformGrouping();
                trans.Commit();
                return(result);
            }
        }
Exemplo n.º 2
0
        public bool GroupElements(XmlDocument document, IfcGroup rootGroup)
        {
            if (document == null)
            {
                throw new ArgumentNullException("XML document must be specified");
            }
            _xmlDoc = document;

            if (rootGroup != null)
            {
                if (((IPersistIfcEntity)rootGroup).ModelOf != _model)
                {
                    throw new Exception("Model of the group is different than model to be used.");
                }
                _rootGroup = rootGroup;
            }
            else
            {
                _rootGroup = _model.Instances.New <IfcGroup>(g => g.Name = "Root group");
            }


            using (XbimReadWriteTransaction trans = _model.BeginTransaction("Elements to groups"))
            {
                bool result = PerformGrouping();
                trans.Commit();
                return(result);
            }
        }
Exemplo n.º 3
0
        public IfcGroup GetExistingRootGroup(XmlDocument xmlDocument)
        {
            _xmlDoc = xmlDocument;
            if (_xmlDoc == null)
            {
                errLog.WriteLine("No input XML data.");
                return(null);
            }

            //get all groups nodes
            XmlNodeList groups = _xmlDoc.GetElementsByTagName("my:groups");

            if (groups == null || groups.Count == 0)
            {
                errLog.WriteLine("No groups in the XML document - not even root element.");
                return(null);
            }

            foreach (XmlNode group in groups)
            {
                string   className = (group as XmlElement).GetAttribute("my:classificationName");
                IfcGroup root      = _model.Instances.Where <IfcGroup>(g => g.Name == className).FirstOrDefault();
                if (root != null)
                {
                    return(root);
                }
            }
            return(null);
        }
Exemplo n.º 4
0
        //create group and add it into the group hierarchy (top -> down creation)
        private IfcGroup CreateGroup(string groupName, string classification, IfcGroup parentGroup)
        {
            IfcGroup group = _model.Instances.Where <IfcGroup>(g => g.Name == groupName).FirstOrDefault();

            if (group == null)
            {
                group = _model.Instances.New <IfcGroup>(g => { g.Name = groupName; g.Description = classification; });
            }

            if (parentGroup != null)
            {
                //check if it is not already child group.
                IfcGroup child = parentGroup.GetGroupedObjects <IfcGroup>().Where(g => g.Name == groupName).FirstOrDefault();
                if (child == null)
                {
                    //ad if it is not
                    parentGroup.AddObjectToGroup(group);
                }
            }

            //add to the root groups if this is root (there is no parent group)
            if (parentGroup == null)
            {
                RootGroups.Add(group);
            }

            _numCreated++;
            return(group);
        }
Exemplo n.º 5
0
        public static void AddObjectToGroup(this IfcGroup gr, IfcObjectDefinition obj)
        {
            IModel model = gr.Model;

            IfcRelAssignsToGroup relation = gr.IsGroupedBy ?? model.Instances.New <IfcRelAssignsToGroup>(rel => rel.RelatingGroup = gr);

            relation.RelatedObjects.Add(obj);
        }
Exemplo n.º 6
0
 public void Load(XmlDocument document)
 {
     if (document == null)
     {
         throw new ArgumentNullException("XML document must be specified");
     }
     _xmlDoc    = document;
     _rootGroup = _model.Instances.New <IfcGroup>(g => g.Name = "Root group");
 }
Exemplo n.º 7
0
 private string GetSystemDescription(IfcGroup ifcGroup)
 {
     if (ifcGroup != null)
     {
         if (!string.IsNullOrEmpty(ifcGroup.Description)) return ifcGroup.Description;
         else if (!string.IsNullOrEmpty(ifcGroup.Name)) return ifcGroup.Name;
     }
     return Constants.DEFAULT_STRING;
 }
Exemplo n.º 8
0
        public static IEnumerable <T> GetGroupedObjects <T>(this IfcGroup gr) where T : IfcObjectDefinition
        {
            IfcRelAssignsToGroup relation = gr.IsGroupedBy;

            if (gr.IsGroupedBy != null)
            {
                return(relation.RelatedObjects.OfType <T>());
            }
            return(new List <T>());
        }
Exemplo n.º 9
0
        public static IEnumerable <IfcObjectDefinition> GetGroupedObjects(this IfcGroup gr)
        {
            IfcRelAssignsToGroup relation = gr.IsGroupedBy;

            if (gr.IsGroupedBy != null)
            {
                return(relation.RelatedObjects);
            }
            return(new List <IfcObjectDefinition>());
        }
Exemplo n.º 10
0
        public static IEnumerable <IfcGroup> GetParentGroups(this IfcGroup gr)
        {
            IModel model = GetModel(gr);

            IEnumerable <IfcRelAssignsToGroup> relations = model.Instances.Where <IfcRelAssignsToGroup>(rel => rel.RelatedObjects.Contains(gr));

            foreach (IfcRelAssignsToGroup rel in relations)
            {
                yield return(rel.RelatingGroup);
            }
        }
Exemplo n.º 11
0
        public static void AddObjectToGroup(this IfcGroup gr, IfcObjectDefinition obj)
        {
            IModel model = GetModel(gr);

            IfcRelAssignsToGroup relation = gr.IsGroupedBy;

            if (gr.IsGroupedBy == null)
            {
                relation = model.Instances.New <IfcRelAssignsToGroup>(rel => rel.RelatingGroup = gr);
            }
            relation.RelatedObjects.Add(obj);
        }
Exemplo n.º 12
0
 private string GetSystemDescription(IfcGroup ifcGroup)
 {
     if (ifcGroup != null)
     {
         if (!string.IsNullOrEmpty(ifcGroup.Description))
         {
             return(ifcGroup.Description);
         }
         else if (!string.IsNullOrEmpty(ifcGroup.Name))
         {
             return(ifcGroup.Name);
         }
     }
     return(Constants.DEFAULT_STRING);
 }
Exemplo n.º 13
0
        public static void AddObjectToGroup(this IfcGroup gr, IEnumerable <IfcObjectDefinition> objects)
        {
            IModel model = GetModel(gr);

            IfcRelAssignsToGroup relation = gr.IsGroupedBy;

            if (gr.IsGroupedBy == null)
            {
                relation = model.Instances.New <IfcRelAssignsToGroup>(rel => rel.RelatingGroup = gr);
            }
            foreach (var item in objects)
            {
                relation.RelatedObjects.Add(item);
            }
        }
 /// <summary>
 /// Sets the script to the predefined pSet. This can be used for a late evaluation of the group members
 /// </summary>
 /// <param name="script">Script to be executed. Set this to NULL to reset the script.
 /// Property will still be defined but will be empty. You can use "ExecuteScript"
 /// to get the entities which belong to this group. These entities are not defined by
 /// IfcRelAssignToGroup relationship.</param>
 public static void SetScript(this IfcGroup group, string script)
 {
     if (String.IsNullOrEmpty(script))
     {
         group.SetPropertySingleValue(Defaults.DefaultPSet, _pScript, typeof(IfcText));
     }
     else
     {
         //check if the script contains '$group' to be used to get elements
         if (!script.Contains("$group"))
         {
             throw new ArgumentException("Script doesn't contain '$group' variable.");
         }
         group.SetPropertySingleValue(Defaults.DefaultPSet, _pScript, new IfcText(script));
     }
 }
        /// <summary>
        /// This function will execute the script if there is any defined.
        /// If no script is defined this will return empty set.
        /// </summary>
        /// <returns>Set of the results or empty set if there are no results or no script defined.</returns>
        public static IEnumerable <IfcObjectDefinition> ExecuteScript(this IfcGroup group)
        {
            var script = group.GetScript();

            if (script == null)
            {
                _lastErrors = new List <string>();
                return(new IfcObjectDefinition[] { });
            }
            var model  = group.ModelOf as XbimModel;
            var parser = new XbimQueryParser(model);

            parser.Parse(script);
            _lastErrors = parser.Errors;

            return(parser.Results.GetEntities("$group").OfType <IfcObjectDefinition>());
        }
Exemplo n.º 16
0
        private void ClearGroups(IfcGroup root)
        {
            List <IfcElement> elements = root.GetGroupedObjects <IfcElement>().ToList();
            int count = elements.Count;

            //must use for cycle instead of foreach because enumeration would collapse
            for (int i = 0; i < count; i++)
            {
                root.RemoveObjectFromGroup(elements[i]);
            }

            //recursive call for children
            IEnumerable <IfcGroup> children = root.GetGroupedObjects <IfcGroup>();

            foreach (IfcGroup group in children)
            {
                ClearGroups(group);
            }
        }
Exemplo n.º 17
0
 //recursive function to find group with specified name in the scope of the root
 private IfcGroup GetGroup(string name, IfcGroup root)
 {
     if (root.Name == name)
     {
         return(root);
     }
     else
     {
         IEnumerable <IfcGroup> children = root.GetGroupedObjects <IfcGroup>();
         foreach (IfcGroup group in children)
         {
             IfcGroup result = GetGroup(name, group);
             if (result != null)
             {
                 return(result);
             }
         }
         return(null);
     }
 }
Exemplo n.º 18
0
        public static bool RemoveObjectFromGroup(this IfcGroup gr, IfcObjectDefinition obj)
        {
            if (gr == null || obj == null)
            {
                return(false);
            }
            IModel model = GetModel(gr);
            IfcRelAssignsToGroup relation = gr.IsGroupedBy;

            if (gr.IsGroupedBy == null)
            {
                return(false);
            }
            if (!relation.RelatedObjects.Contains(obj))
            {
                return(false);
            }
            relation.RelatedObjects.Remove(obj);
            return(true);
        }
Exemplo n.º 19
0
        public override void Parse(int propIndex, IPropertyValue value, int[] nestedIndex)
        {
            switch (propIndex)
            {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                base.Parse(propIndex, value, nestedIndex);
                return;

            case 6:
                _relatingGroup = (IfcGroup)(value.EntityVal);
                return;

            default:
                throw new XbimParserException(string.Format("Attribute index {0} is out of range for {1}", propIndex + 1, GetType().Name.ToUpper()));
            }
        }
Exemplo n.º 20
0
        private void Process()
        {
            if (_xmlDoc == null)
            {
                errLog.WriteLine("No input XML data.");
                return;
            }

            //get all groups nodes
            XmlNodeList groups = _xmlDoc.GetElementsByTagName("my:groups");

            if (groups == null || groups.Count == 0)
            {
                errLog.WriteLine("No groups in the XML document - not even root element.");
                return;
            }

            //start xbim transaction
            using (XbimReadWriteTransaction transaction = _model.BeginTransaction())
            {
                foreach (XmlNode group in groups)
                {
                    string   className = (group as XmlElement).GetAttribute("my:classificationName");
                    IfcGroup root      = CreateGroup(className, "", null);

                    //create group for all non-grouped elements
                    CreateGroup("No group", "", root);

                    XmlNodeList groupList = group.ChildNodes;
                    if (groupList.Count == 0)
                    {
                        errLog.WriteLine("No groups in the XML document");
                        continue;
                    }
                    ProcessGroups(groupList, root);
                }

                transaction.Commit();
            }
        }
Exemplo n.º 21
0
        //recursive function for groups creation
        private void ProcessGroups(XmlNodeList groupNodeList, IfcGroup parentGroup)
        {
            foreach (XmlNode groupNode in groupNodeList)
            {
                //check node type
                if (groupNode.Name != "my:group")
                {
                    errLog.WriteLine("Unexpected node: " + groupNode.Name);
                    continue;
                }

                XmlElement groupElement = groupNode as XmlElement;
                string     name         = groupElement.GetAttribute("my:name");
                if (string.IsNullOrEmpty(name))
                {
                    errLog.WriteLine("Group with no name identified.");
                    continue;
                }

                string classification = groupElement.GetAttribute("my:classification");
                if (string.IsNullOrEmpty(classification))
                {
                    errLog.WriteLine("Group '" + name + "' does not have any classification code assigned.");
                }

                //create group
                IfcGroup group = CreateGroup(name, classification, parentGroup);

                //recursive call for the child nodes
                XmlNodeList children = groupNode.ChildNodes;
                if (children.Count != 0)
                {
                    ProcessGroups(children, group);
                }
            }
        }
Exemplo n.º 22
0
        /// <summary>
        /// Performs grouping based on the XML data provided.
        /// </summary>
        /// <param name="errLog">Stream for err output</param>
        /// <returns>True if grouping was successful. False otherwise.</returns>
        public bool PerformGrouping()
        {
            errLog = new StringWriter();

            if (_xmlDoc == null)
            {
                errLog.WriteLine("No input XML data.");
                return(false);
            }

            //get all group nodes
            XmlNodeList groups = _xmlDoc.GetElementsByTagName("group");

            if (groups == null || groups.Count == 0)
            {
                errLog.WriteLine("No group rules in the XML document");
                return(false);
            }

            //clear groups from elements
            ClearGroups(_rootGroup);


            //create group for all non-grouped elements
            IfcGroup noGroup = GetGroup("No group");

            if (noGroup == null)
            {
                noGroup = _model.Instances.New <IfcGroup>(gr => { gr.Name = "No group"; gr.Description = ""; });
                _rootGroup.AddObjectToGroup(noGroup);
            }
            List <IfcElement>    allElements = _model.Instances.OfType <IfcElement>().ToList();
            List <IfcTypeObject> allTypes    = _model.Instances.OfType <IfcTypeObject>().ToList();

            foreach (XmlNode group in groups)
            {
                grpName = GetName(group);
                if (String.IsNullOrEmpty(grpName))
                {
                    errLog.WriteLine("Group without name detected. All information in this group specification will be skipped.");
                    continue;
                }

                //search for the existing group with the same name (convert to upper case for the comparison) and create new group if none exists.
                IfcGroup iGroup = GetGroup(grpName);
                if (iGroup == null)
                {
                    iGroup = _model.Instances.New <IfcGroup>(gr => gr.Name = grpName);
                    _rootGroup.AddObjectToGroup(iGroup);
                }


                //process all ifc objects specified for the group
                XmlNodeList elements = ((XmlElement)group).GetElementsByTagName("element");
                foreach (XmlNode element in group)
                {
                    eName = GetName(element);
                    if (String.IsNullOrEmpty(eName))
                    {
                        errLog.WriteLine("Element without name detected in group '" + grpName + "'. This element will NOT be included in the");
                        continue;
                    }

                    //get elemType for reflection of attributes
                    IfcType ifcType;
                    if (!IfcMetaData.TryGetIfcType(eName, out ifcType))
                    {
                        continue;
                    }
                    else
                    {
                        elemType = ifcType.Type;
                    }
                    XbimQueryBuilder qBuilder = new XbimQueryBuilder(elemType);

                    //process all element attributes
                    XmlNodeList attributeGroups = ((XmlElement)element).GetElementsByTagName("attributes");
                    //there could be different grouping rules for attribute rules inside (all, none, oneOf, any)
                    foreach (XmlNode attrGroup in attributeGroups)
                    {
                        CreateAttributeCondition(qBuilder, attrGroup);
                    }

                    //process element properties
                    XmlNodeList propertyGroups = ((XmlElement)element).GetElementsByTagName("properties");
                    //there could be different grouping rules for attribute rules inside (all, none, oneOf, any)
                    foreach (XmlNode propGroup in propertyGroups)
                    {
                        CreatePropertyCondition(qBuilder, propGroup);
                    }


                    //element type processing
                    IEnumerable <IPersistIfcEntity> types = null;
                    XmlNodeList elTypeNodeList            = (element as XmlElement).GetElementsByTagName("elementType");
                    foreach (XmlNode elTypeNode in elTypeNodeList) //there should be just one 'elTypeNode'
                    {
                        XbimQueryBuilder typeQueryBuilder = null;
                        elTypeName = elTypeNode.InnerText;
                        if (string.IsNullOrEmpty(elTypeName))
                        {
                            errLog.WriteLine("Name of the element type is not specified for element of type '" + eName + "'. Element type conditions will not be applied. ");
                            continue;
                        }

                        IfcType ifcTypeLookup;
                        if (!IfcMetaData.TryGetIfcType(eName, out ifcTypeLookup))
                        {
                            continue;
                        }
                        else
                        {
                            elemType = ifcTypeLookup.Type;
                        }
                        if (!typeof(IfcTypeObject).IsAssignableFrom(elemType))
                        {
                            errLog.WriteLine("'" + elTypeName + "' is not type object.");
                            continue;
                        }

                        typeQueryBuilder = new XbimQueryBuilder(elTypeName);

                        //type attributes
                        XmlNodeList typeAttrGroups = ((XmlElement)element).GetElementsByTagName("typeAttributes");
                        foreach (XmlNode typeAttrGrpNode in typeAttrGroups)
                        {
                            CreateAttributeCondition(typeQueryBuilder, typeAttrGrpNode);
                        }

                        //process element type properties
                        XmlNodeList typePropGroups = ((XmlElement)element).GetElementsByTagName("typeProperties");
                        //there could be different grouping rules for attribute rules inside (all, none, oneOf, any)
                        foreach (XmlNode propGroup in typePropGroups)
                        {
                            CreatePropertyCondition(typeQueryBuilder, propGroup);
                        }


                        types = _model.Instances.Where <IPersistIfcEntity>(typeQueryBuilder.BuildQuery());
                        break;
                    }

                    //get elements and element type
                    IEnumerable <IPersistIfcEntity> instances = allElements.Where(qBuilder.BuildQuery().Compile()).ToList();

                    //check elements against element type (if defined)
                    if (types != null)
                    {
                        instances = FilterElementsByType(instances, types);
                    }

                    //add result to the group
                    foreach (var inst in instances)
                    {
                        IfcElement el = inst as IfcElement;
                        if (el != null && allElements.Remove(el))
                        {
                            IfcTypeObject type = el.GetDefiningType();
                            if (allTypes.Remove(type)) //ensure that it is not added twice
                            {
                                iGroup.AddObjectToGroup(type);
                            }

                            //get all elements of this type
                            IEnumerable <IfcRelDefinesByType> rels = _model.Instances.Where <IfcRelDefinesByType>(r => r.RelatingType == type);
                            foreach (var rel in rels)
                            {
                                IEnumerable <IfcElement> elemOfType = rel.RelatedObjects.OfType <IfcElement>();
                                foreach (var item in elemOfType)
                                {
                                    allElements.Remove(item);
                                }
                            }
                            //IfcObjectDefinition objDef = inst as IfcObjectDefinition;
                            //if (objDef != null) iGroup.AddObjectToGroup(objDef);
                        }
                    }
                }
            }

            //fill the no-group group with elements which are not in any group
            foreach (IfcTypeObject element in allTypes)
            {
                noGroup.AddObjectToGroup(element);
            }
            return(true);
        }
 /// <summary>
 /// Gets the script from predefined pSet and property.
 /// </summary>
 /// <returns>Script saved in the property</returns>
 public static string GetScript(this IfcGroup group)
 {
     return(group.GetPropertySingleValue <IfcText>(Defaults.DefaultPSet, _pScript));
 }
        /// <summary>
        /// Indicates whether the script is defined for this group
        /// </summary>
        /// <returns>True if the script is defined, false otherwise.</returns>
        public static bool HasScript(this IfcGroup group)
        {
            string script = group.GetPropertySingleValue <IfcText>(Defaults.DefaultPSet, _pScript);

            return(!String.IsNullOrEmpty(script));
        }
Exemplo n.º 25
0
 public GroupViewModel(IfcGroup gr, IXbimViewModel parent)
 {
     this.group     = gr;
     CreatingParent = parent;
 }