private Type GetEnumType(ref string enumName, CustomModel.CustomProperty customProperty) { object enumValue; string enumDesc; Type enumType; bool defined; if (!m_EnumTypeHashtable.ContainsKey(enumName)) { return(null); } System.Collections.IEnumerator enumer; System.Collections.DictionaryEntry entry; enumer = m_EnumTypeHashtable.GetEnumerator(); while (enumer.MoveNext()) { entry = (System.Collections.DictionaryEntry)enumer.Current; enumType = entry.Value as Type; if (enumType == null) { continue; } if (entry.Key.ToString().StartsWith(enumName) == false) { continue; } //Array enumArray = System.Enum.GetValues(enumType); //defined = false; //for(int i=0; i<enumArray.Length; i++) //{ // defined = false; // enumValue = enumArray.GetValue(i); // foreach (CustomModel.CustomPropertyStandardValue pei in customProperty.ComboboxValues) // { // //if(pei.Browsable == false) // // continue; // //enumDesc = pei.DisplayText; // //enumDesc = VerifyEnumDesc(enumDesc, enumValue); // if ((int)enumValue == pei.Index && enumValue.ToString() == enumDesc) // { // defined = true; // break; // } // } // if(!defined) // break; //} //if(defined) //{ // //Console.WriteLine(enumType.Name); // return enumType; //} return(enumType); } enumName += "_" + m_EnumCounter++; return(null); }
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); }
public Type CreateDynamicEnum(CustomModel.CustomProperty customProperty) { try { ModuleBuilder module; object enumValue; string enumDesc; FieldBuilder fbe; Type tEnum; EnumBuilder eb; string enumName; tEnum = null; fbe = null; if (customProperty.ComboboxValues == null) { return(tEnum); } module = GetDynamicModule(); enumName = String.Format("enum{0}", customProperty.Key); tEnum = this.GetEnumType(ref enumName, customProperty); if (tEnum != null) { return(tEnum); } eb = module.DefineEnum(enumName, TypeAttributes.Public, typeof(int)); int index = 0; foreach (CustomModel.CustomPropertyStandardValue pei in customProperty.ComboboxValues) { try { enumValue = index++; enumDesc = pei.DisplayText; enumDesc = VerifyEnumDesc(enumDesc, enumValue); pei.DisplayText = enumDesc; fbe = eb.DefineLiteral(enumDesc, enumValue); } catch (Exception exc) { continue; } finally { } } tEnum = eb.CreateType(); if (!this.m_EnumTypeHashtable.ContainsKey(enumName)) { this.m_EnumTypeHashtable.Add(enumName, tEnum); } return(tEnum); } catch (Exception exc) { return(null); } }