Beispiel #1
0
        public void InitializeAdditionalProperties()
        {
            // default value
            this.IsLandmarked       = false;
            this.YearOfConstruction = "";
            this.NumberOfStoreys    = 0;
            this.OccupancyType      = "";

            Ifc4.IfcPropertySet ifcPropertySet = this.GetIfcPropertySetFromRelatingPropertyDefinition();

            IEnumerable <IfcProperty> ifcPropertyCollection = this.Document.IfcXmlDocument.Items.OfType <IfcProperty>().ToList();

            if (ifcPropertySet == null)
            {
                ifcPropertySet = new IfcPropertySet();
            }

            if (ifcPropertySet.HasProperties == null)
            {
                ifcPropertySet.HasProperties = new IfcPropertySetHasProperties();
            }

            m_IfcPropertySingleValueCollection = new List <IfcPropertySingleValue>();
            foreach (IfcProperty ifcPropertyTmp in ifcPropertySet.HasProperties.Items)
            {
                if (ifcPropertyTmp.IsRef)
                {
                    IfcProperty ifcProperty = ifcPropertyCollection.FirstOrDefault(item => item.Id == ifcPropertyTmp.Ref);
                    if (ifcProperty != null && ifcProperty is IfcPropertySingleValue)
                    {
                        m_IfcPropertySingleValueCollection.Add((IfcPropertySingleValue)ifcProperty);
                    }
                }
            }

            IfcPropertySingleValue ifcPropertySingleValue = null;

            ifcPropertySingleValue = m_IfcPropertySingleValueCollection.FirstOrDefault(item => item.Name.Equals("IsLandmarked", StringComparison.OrdinalIgnoreCase));
            if (ifcPropertySingleValue == null)
            {
                ifcPropertySingleValue = new Ifc4.IfcPropertySingleValue()
                {
                    Id           = this.Document.GetNextSid(),
                    Name         = "IsLandmarked",
                    NominalValue = new Ifc4.IfcPropertySingleValueNominalValue(),
                };

                ifcPropertySingleValue.NominalValue.Item = new Ifc4.IfcLogicalwrapper()
                {
                    Value = IsLandmarked ? Ifc4.IfcLogical.True : Ifc4.IfcLogical.False
                };

                m_IfcPropertySingleValueCollection.Add(ifcPropertySingleValue);
                this.Document.IfcXmlDocument.Items.Add(ifcPropertySingleValue);
                ifcPropertySet.HasProperties.Items.Add(new Ifc4.IfcPropertySingleValue()
                {
                    Ref = ifcPropertySingleValue.Id
                });
            }
            else
            {
                // read
                Ifc4.IfcLogicalwrapper ifcLogicalwrapper = ifcPropertySingleValue.NominalValue.Item as Ifc4.IfcLogicalwrapper;
                if (ifcLogicalwrapper != null)
                {
                    this.IsLandmarked = ifcLogicalwrapper.Value == IfcLogical.True ? true : false;
                }
            }

            ifcPropertySingleValue = m_IfcPropertySingleValueCollection.FirstOrDefault(item => item.Name.Equals("YearOfConstruction", StringComparison.OrdinalIgnoreCase));
            if (ifcPropertySingleValue == null)
            {
                ifcPropertySingleValue = new Ifc4.IfcPropertySingleValue()
                {
                    Id           = this.Document.GetNextSid(),
                    Name         = "YearOfConstruction",
                    NominalValue = new Ifc4.IfcPropertySingleValueNominalValue(),
                };

                ifcPropertySingleValue.NominalValue.Item = new Ifc4.IfcLabelwrapper()
                {
                    Value = this.YearOfConstruction
                };

                m_IfcPropertySingleValueCollection.Add(ifcPropertySingleValue);
                this.Document.IfcXmlDocument.Items.Add(ifcPropertySingleValue);
                ifcPropertySet.HasProperties.Items.Add(new Ifc4.IfcPropertySingleValue()
                {
                    Ref = ifcPropertySingleValue.Id
                });
            }
            else
            {
                // read
                Ifc4.IfcLabelwrapper ifcLabelwrapper = ifcPropertySingleValue.NominalValue.Item as Ifc4.IfcLabelwrapper;
                if (ifcLabelwrapper != null)
                {
                    this.YearOfConstruction = ifcLabelwrapper.Value;
                }
            }

            ifcPropertySingleValue = m_IfcPropertySingleValueCollection.FirstOrDefault(item => item.Name.Equals("NumberOfStoreys", StringComparison.OrdinalIgnoreCase));
            if (ifcPropertySingleValue == null)
            {
                ifcPropertySingleValue = new Ifc4.IfcPropertySingleValue()
                {
                    Id           = this.Document.GetNextSid(),
                    Name         = "NumberOfStoreys",
                    NominalValue = new Ifc4.IfcPropertySingleValueNominalValue(),
                };

                ifcPropertySingleValue.NominalValue.Item = new Ifc4.IfcIntegerwrapper()
                {
                    Value = this.NumberOfStoreys
                };


                m_IfcPropertySingleValueCollection.Add(ifcPropertySingleValue);
                this.Document.IfcXmlDocument.Items.Add(ifcPropertySingleValue);
                ifcPropertySet.HasProperties.Items.Add(new Ifc4.IfcPropertySingleValue()
                {
                    Ref = ifcPropertySingleValue.Id
                });
            }
            else
            {
                // read
                Ifc4.IfcIntegerwrapper ifcIntegerwrapper = ifcPropertySingleValue.NominalValue.Item as Ifc4.IfcIntegerwrapper;
                if (ifcIntegerwrapper != null)
                {
                    this.NumberOfStoreys = ifcIntegerwrapper.Value;
                }
            }

            ifcPropertySingleValue = m_IfcPropertySingleValueCollection.FirstOrDefault(item => item.Name.Equals("OccupancyType", StringComparison.OrdinalIgnoreCase));
            if (ifcPropertySingleValue == null)
            {
                ifcPropertySingleValue = new Ifc4.IfcPropertySingleValue()
                {
                    Id           = this.Document.GetNextSid(),
                    Name         = "OccupancyType",
                    NominalValue = new Ifc4.IfcPropertySingleValueNominalValue(),
                };

                ifcPropertySingleValue.NominalValue.Item = new Ifc4.IfcLabelwrapper()
                {
                    Value = this.OccupancyType
                };

                m_IfcPropertySingleValueCollection.Add(ifcPropertySingleValue);
                this.Document.IfcXmlDocument.Items.Add(ifcPropertySingleValue);
                ifcPropertySet.HasProperties.Items.Add(new Ifc4.IfcPropertySingleValue()
                {
                    Ref = ifcPropertySingleValue.Id
                });
            }
            else
            {
                // read
                Ifc4.IfcLabelwrapper ifcLabelwrapper = ifcPropertySingleValue.NominalValue.Item as Ifc4.IfcLabelwrapper;
                if (ifcLabelwrapper != null)
                {
                    this.OccupancyType = ifcLabelwrapper.Value;
                }
            }


            if (ifcPropertySet.Id == null)
            {
                ifcPropertySet.Id   = this.Document.GetNextSid();
                ifcPropertySet.Name = "Pset_BuildingCommon";
                this.Document.IfcXmlDocument.Items.Add(ifcPropertySet);

                Ifc4.IfcRelDefinesByProperties ifcRelDefinesByProperties = new Ifc4.IfcRelDefinesByProperties();
                ifcRelDefinesByProperties.Id = this.Document.GetNextSid();
                //ifcRelDefinesByProperties.RelatedObjects = new Ifc4.IfcBuilding() { Ref = this.Id };
                ifcRelDefinesByProperties.RelatedObjects                  = this.RefInstance();
                ifcRelDefinesByProperties.RelatingPropertyDefinition      = new Ifc4.IfcRelDefinesByPropertiesRelatingPropertyDefinition();
                ifcRelDefinesByProperties.RelatingPropertyDefinition.Item = new Ifc4.IfcPropertySet()
                {
                    Ref = ifcPropertySet.Id
                };
                this.Document.IfcXmlDocument.Items.Add(ifcRelDefinesByProperties);
            }

            // -----------------------------------------
            Ifc4.IfcElementQuantity ifcElementQuantity = this.GetIfcElementQuantityFromRelatingPropertyDefinition();
            if (ifcElementQuantity != null && ifcElementQuantity.IsRef)
            {
                ifcElementQuantity = this.Document.IfcXmlDocument.Items.OfType <IfcElementQuantity>().FirstOrDefault(item => item.Id == ifcElementQuantity.Ref);
            }

            IEnumerable <IfcQuantityArea> ifcQuantityAreaCollection = this.Document.IfcXmlDocument.Items.OfType <IfcQuantityArea>();

            if (ifcElementQuantity == null)
            {
                ifcElementQuantity = new IfcElementQuantity();
            }

            if (ifcElementQuantity.Quantities == null)
            {
                ifcElementQuantity.Quantities = new IfcElementQuantityQuantities();
            }


            m_IfcQuantityAreaCollection = new List <IfcQuantityArea>();
            foreach (IfcQuantityArea ifcQuantityAreaTmp in ifcElementQuantity.Quantities.Items)
            {
                if (ifcQuantityAreaTmp.IsRef)
                {
                    IfcQuantityArea existingIfcQuantityArea = ifcQuantityAreaCollection.FirstOrDefault(item => item.Id == ifcQuantityAreaTmp.Ref);
                    if (existingIfcQuantityArea != null)
                    {
                        m_IfcQuantityAreaCollection.Add(existingIfcQuantityArea);
                    }
                }
            }

            IfcQuantityArea ifcQuantityArea = null;

            ifcQuantityArea = m_IfcQuantityAreaCollection.FirstOrDefault(item => item.Name.Equals("GrossFloorArea", StringComparison.OrdinalIgnoreCase));
            if (ifcQuantityArea == null)
            {
                ifcQuantityArea = new IfcQuantityArea()
                {
                    Id        = this.Document.GetNextSid(),
                    Name      = "GrossFloorArea",
                    AreaValue = GrossFloorArea
                };

                m_IfcQuantityAreaCollection.Add(ifcQuantityArea);
                this.Document.IfcXmlDocument.Items.Add(ifcQuantityArea);
                ifcElementQuantity.Quantities.Items.Add(new Ifc4.IfcQuantityArea()
                {
                    Ref = ifcQuantityArea.Id
                });
            }
            else
            {
                // read
                this.GrossFloorArea = ifcQuantityArea.AreaValue;
            }

            ifcQuantityArea = m_IfcQuantityAreaCollection.FirstOrDefault(item => item.Name.Equals("NetFloorArea", StringComparison.OrdinalIgnoreCase));
            if (ifcQuantityArea == null)
            {
                ifcQuantityArea = new IfcQuantityArea()
                {
                    Id        = this.Document.GetNextSid(),
                    Name      = "NetFloorArea",
                    AreaValue = NetFloorArea
                };

                m_IfcQuantityAreaCollection.Add(ifcQuantityArea);
                this.Document.IfcXmlDocument.Items.Add(ifcQuantityArea);
                ifcElementQuantity.Quantities.Items.Add(new Ifc4.IfcQuantityArea()
                {
                    Ref = ifcQuantityArea.Id
                });
            }
            else
            {
                // read
                this.NetFloorArea = ifcQuantityArea.AreaValue;
            }

            if (ifcElementQuantity.Id == null)
            {
                ifcElementQuantity.Id   = this.Document.GetNextSid();
                ifcElementQuantity.Name = "Qto_BuildingBaseQuantities";
                this.Document.IfcXmlDocument.Items.Add(ifcElementQuantity);

                Ifc4.IfcRelDefinesByProperties ifcRelDefinesByProperties = new Ifc4.IfcRelDefinesByProperties();
                ifcRelDefinesByProperties.Id                         = this.Document.GetNextSid();
                ifcRelDefinesByProperties.RelatedObjects             = this.RefInstance();
                ifcRelDefinesByProperties.RelatingPropertyDefinition = new Ifc4.IfcRelDefinesByPropertiesRelatingPropertyDefinition();


                // old
                // ifcRelDefinesByProperties.RelatingPropertyDefinition.Item = new Ifc4.IfcElementQuantity() { Ref = ifcElementQuantity.Id };

                // new
                ifcRelDefinesByProperties.RelatingPropertyDefinition.Item = ((IfcPropertySetDefinition)(new Ifc4.IfcElementQuantity()
                {
                    Ref = ifcElementQuantity.Id
                }));

                this.Document.IfcXmlDocument.Items.Add(ifcRelDefinesByProperties);
            }
        }
        private List <CustomModel.CustomPropertyDescriptor> GetRuntimeProperties(Ifc4.BaseObject baseObject)
        {
            CustomModel.CustomPropertyDescriptor        cpd;
            CustomModel.CustomProperty                  customProperty;
            List <CustomModel.CustomPropertyDescriptor> propertyDescriptorCollection = new List <CustomModel.CustomPropertyDescriptor>();

            Ifc4.CcFacility facility = baseObject as Ifc4.CcFacility;
            if (facility == null || String.IsNullOrEmpty(facility.ObjectTypeId))
            {
                return(propertyDescriptorCollection);
            }

            Ifc4.Document document = baseObject.GetParent <Ifc4.Document>();

            IEnumerable <Ifc4.IfcPropertySetTemplate> ifcPropertySetTemplateCollection = document.GetIfcPropertySetTemplateCollection(facility);

            ////string objectTypeId = null;
            ////if (facility != null && !String.IsNullOrEmpty(facility.ObjectTypeId))
            ////{
            ////    objectTypeId = facility.ObjectTypeId;
            ////    IEnumerable<Ifc4.IfcRelAssociatesClassification> ifcRelAssociatesClassificationCollection = document.IfcXmlDocument.Items.OfType<Ifc4.IfcRelAssociatesClassification>();
            ////    IEnumerable<string> relatedObjectsRefs = from ifcRelAssociatesClassification in ifcRelAssociatesClassificationCollection
            ////                                             from relatedObject in ifcRelAssociatesClassification.RelatedObjects.Items
            ////                                             where ifcRelAssociatesClassification.RelatingClassification.Item != null && ifcRelAssociatesClassification.RelatingClassification.Item.Ref == objectTypeId
            ////                                             select relatedObject.Ref;

            ////    ifcPropertySetTemplateCollection = document.IfcXmlDocument.Items.OfType<Ifc4.IfcPropertySetTemplate>().Where(item => relatedObjectsRefs.Contains(item.Id));
            ////}

            if (ifcPropertySetTemplateCollection == null)
            {
                return(propertyDescriptorCollection);
            }


            foreach (var ifcPropertySetTemplate in ifcPropertySetTemplateCollection)
            {
                foreach (var propertyTemplate in ifcPropertySetTemplate.HasPropertyTemplates.Items)
                {
                    Ifc4.IfcSimplePropertyTemplate simplePropertyTemplate = propertyTemplate as Ifc4.IfcSimplePropertyTemplate;

                    if (simplePropertyTemplate == null)
                    {
                        continue;
                    }

                    //string displayName = String.IsNullOrEmpty(simplePropertyTemplate.PrimaryMeasureType) ? simplePropertyTemplate.Name : String.Format("{0} [{1}]", simplePropertyTemplate.Name, simplePropertyTemplate.PrimaryMeasureType);
                    string displayName = simplePropertyTemplate.Name;

                    string unit = null;
                    if (simplePropertyTemplate.PrimaryUnit != null)
                    {
                        if (simplePropertyTemplate.PrimaryUnit.Item is Ifc4.IfcSIUnit)
                        {
                            Ifc4.IfcSIUnit ifcSIUnit = GetUnit((Ifc4.IfcSIUnit)simplePropertyTemplate.PrimaryUnit.Item);
                            if (ifcSIUnit.NameSpecified)
                            {
                                if (ifcSIUnit.PrefixSpecified)
                                {
                                    if (ifcSIUnit.Prefix == IfcSIPrefix.Centi && ifcSIUnit.Name == IfcSIUnitName.Metre)
                                    {
                                        unit = "[cm]";
                                    }
                                    else if (ifcSIUnit.Prefix == IfcSIPrefix.Milli && ifcSIUnit.Name == IfcSIUnitName.Metre)
                                    {
                                        unit = "[mm]";
                                    }
                                    else if (ifcSIUnit.Prefix == IfcSIPrefix.Kilo && ifcSIUnit.Name == IfcSIUnitName.Gram)
                                    {
                                        unit = "[kg]";
                                    }
                                    else if (ifcSIUnit.Prefix == IfcSIPrefix.Kilo && ifcSIUnit.Name == IfcSIUnitName.Watt)
                                    {
                                        unit = "[kW]";
                                    }
                                }
                                else
                                {
                                    if (ifcSIUnit.Name == IfcSIUnitName.Metre)
                                    {
                                        unit = "[m]";
                                    }
                                    else if (ifcSIUnit.Name == IfcSIUnitName.SquareMetre)
                                    {
                                        unit = "[m²]";
                                    }
                                    else if (ifcSIUnit.Name == IfcSIUnitName.CubicMetre)
                                    {
                                        unit = "[m³]";
                                    }
                                    else if (ifcSIUnit.Name == IfcSIUnitName.Volt)
                                    {
                                        unit = "[V]";
                                    }
                                    else if (ifcSIUnit.Name == IfcSIUnitName.DegreeCelsius)
                                    {
                                        unit = "[°C]";
                                    }
                                    else if (ifcSIUnit.Name == IfcSIUnitName.Ampere)
                                    {
                                        unit = "[A]";
                                    }
                                    else if (ifcSIUnit.Name == IfcSIUnitName.Hertz)
                                    {
                                        unit = "[Hz]";
                                    }
                                    else if (ifcSIUnit.Name == IfcSIUnitName.Watt)
                                    {
                                        unit = "[W]";
                                    }
                                    else if (ifcSIUnit.Name == IfcSIUnitName.Pascal)
                                    {
                                        unit = "[Pa]";
                                    }
                                }
                            }
                        }
                        else if (simplePropertyTemplate.PrimaryUnit.Item is Ifc4.IfcDerivedUnit)
                        {
                            Ifc4.IfcDerivedUnit ifcDerivedUnit = GetUnit <Ifc4.IfcDerivedUnit>((Ifc4.IfcDerivedUnit)simplePropertyTemplate.PrimaryUnit.Item);
                            if (ifcDerivedUnit.UnitTypeSpecified)
                            {
                                if (ifcDerivedUnit.UnitType == IfcDerivedUnitEnum.Heatfluxdensityunit)
                                {
                                    unit = "[Ah]";
                                }
                                else if (ifcDerivedUnit.UnitType == IfcDerivedUnitEnum.Volumetricflowrateunit)
                                {
                                    unit = "[m³/h]";
                                }
                                else if (ifcDerivedUnit.UnitType == IfcDerivedUnitEnum.Massdensityunit)
                                {
                                    unit = "[kg/m³]";
                                }
                                else if (ifcDerivedUnit.UnitType == IfcDerivedUnitEnum.Soundpowerlevelunit)
                                {
                                    unit = "[db]";
                                }
                            }
                        }
                        if (!String.IsNullOrEmpty(unit))
                        {
                            displayName = String.Concat(displayName, " ", unit);
                        }
                    }

                    List <Attribute> attributes = new List <Attribute>()
                    {
                        new System.ComponentModel.BrowsableAttribute(true),
                        new System.ComponentModel.CategoryAttribute("Runtime Object"),
                        new System.ComponentModel.DisplayNameAttribute(displayName)
                    };


                    CustomPropertyStandardValues customPropertyStandardValues = null;
                    Type customPropertyType;

                    string primaryMeasureType = simplePropertyTemplate.PrimaryMeasureType;

                    if (simplePropertyTemplate.TemplateType == Ifc4.IfcSimplePropertyTemplateTypeEnum.PSinglevalue)
                    {
                        // default
                        customPropertyType = typeof(System.String);

                        // overwrite
                        if (!String.IsNullOrEmpty(primaryMeasureType))
                        {
                            if (primaryMeasureType.Equals("IfcText", StringComparison.OrdinalIgnoreCase))
                            {
                                customPropertyType = typeof(System.String);
                            }
                            else if (primaryMeasureType.Equals("IfcDate", StringComparison.OrdinalIgnoreCase))
                            {
                                customPropertyType = typeof(System.Nullable <DateTime>);
                                attributes.Add(new System.ComponentModel.TypeConverterAttribute(typeof(CustomNullableTypeConverter <DateTime?>)));
                            }
                            else if (primaryMeasureType.Equals("IfcReal", StringComparison.OrdinalIgnoreCase))
                            {
                                customPropertyType = typeof(System.Nullable <double>);
                                attributes.Add(new System.ComponentModel.TypeConverterAttribute(typeof(CustomNullableTypeConverter <double?>)));
                            }
                            else if (primaryMeasureType.Equals("IfcBoolean", StringComparison.OrdinalIgnoreCase))
                            {
                                customPropertyType = typeof(System.String);
                                attributes.Add(new System.ComponentModel.TypeConverterAttribute(typeof(CustomBooleanNullableTypeConverter)));
                            }
                        }
                    }
                    else if (simplePropertyTemplate.TemplateType == Ifc4.IfcSimplePropertyTemplateTypeEnum.PEnumeratedvalue)
                    {
                        customPropertyStandardValues = new CustomPropertyStandardValues();
                        attributes.Add(new System.ComponentModel.TypeConverterAttribute(typeof(CustomEnumTypeConverter)));

                        int i = 0;
                        // add empty enum value
                        if (simplePropertyTemplate.Enumerators.EnumerationValues.Items.Any())
                        {
                            // empty string not possible - use a string with length > 0
                            customPropertyStandardValues.Add(new CustomPropertyStandardValue("E" + (i++), " "));
                        }

                        foreach (var enumItem in simplePropertyTemplate.Enumerators.EnumerationValues.Items)
                        {
                            string enumValue;
                            if (enumItem is Ifc4.IfcLabelwrapper)
                            {
                                Ifc4.IfcLabelwrapper labelWrapper = enumItem as Ifc4.IfcLabelwrapper;
                                enumValue = labelWrapper.Value;
                            }
                            else
                            {
                                enumValue = enumItem.ToString();
                            }

                            customPropertyStandardValues.Add(new CustomPropertyStandardValue("E" + (i++), enumValue.Trim()));
                        }

                        customPropertyType = typeof(System.Enum);
                    }
                    else
                    {
                        customPropertyType = typeof(System.String);
                    }

                    // ------------------------------------------------------------------
                    StringBuilder sbAttributeDescription = new StringBuilder();
                    sbAttributeDescription.AppendLine("");
                    if (String.IsNullOrEmpty(primaryMeasureType))
                    {
                        sbAttributeDescription.AppendLine(String.Format("IFC4 Type: {0}", "unknown"));
                    }
                    else
                    {
                        sbAttributeDescription.AppendLine(String.Format("IFC4 Type: {0}", primaryMeasureType));
                    }

                    if (customPropertyType.IsGenericType && customPropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                    {
                        sbAttributeDescription.AppendLine(String.Format(".NET Type: Nullable {0}", (customPropertyType.GetGenericArguments()[0]).Name));
                    }
                    else
                    {
                        sbAttributeDescription.AppendLine(String.Format(".NET Type: {0}", customPropertyType.Name));
                    }


                    if (!String.IsNullOrEmpty(simplePropertyTemplate.Description))
                    {
                        sbAttributeDescription.AppendLine("");
                        sbAttributeDescription.AppendLine(simplePropertyTemplate.Description);
                    }

                    if (sbAttributeDescription.Length > 0)
                    {
                        attributes.Add(new System.ComponentModel.DescriptionAttribute(sbAttributeDescription.ToString()));
                    }
                    // ------------------------------------------------------------------

                    customProperty = new CustomModel.CustomProperty
                                     (
                        null,
                        simplePropertyTemplate.TempId.ToString("N"),
                        simplePropertyTemplate.TempId.ToString("N"),
                        customPropertyType,
                        simplePropertyTemplate.Name,
                        simplePropertyTemplate.Description,
                        attributes,
                        customPropertyStandardValues,
                        null
                                     );

                    customProperty.IfcPropertyName     = simplePropertyTemplate.Name;
                    customProperty.IfcPropertyGlobalId = simplePropertyTemplate.GlobalId;

                    CustomModel.CustomAssembly customAssembly = new CustomAssembly();
                    Type type = customAssembly.CreateDynamicEnum(customProperty);
                    if (type != null)
                    {
                        customProperty.PropertyType = type;
                    }

                    cpd = new CustomModel.CustomPropertyDescriptor(customProperty, this);
                    propertyDescriptorCollection.Add(cpd);
                }
            }

            return(propertyDescriptorCollection);
        }