//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;
     }
 }
        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);
            }
        }
        //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;
        }