Esempio n. 1
0
        /// <summary>
        /// Creates property sets for Revit groups and parameters, if export options is set.
        /// </summary>
        /// <param name="exporterIFC">
        /// The ExporterIFC.
        /// </param>
        /// <param name="element">
        /// The Element.
        /// </param>
        /// <param name="productWrapper">
        /// The product wrapper.
        /// </param>
        public static void CreateInternalRevitPropertySets(ExporterIFC exporterIFC, Element element, IFCProductWrapper productWrapper)
        {
            if (exporterIFC == null || element == null || productWrapper == null ||
                !ExporterCacheManager.ExportOptionsCache.ExportInternalRevitPropertySets)
                return;

            IFCFile file = exporterIFC.GetFile();

            ICollection<IFCAnyHandle> elements = productWrapper.GetElements();
            if (elements.Count == 0)
            {
                elements = productWrapper.GetProducts();
                if (elements.Count == 0)
                    return;
            }

            HashSet<IFCAnyHandle> elementSets = new HashSet<IFCAnyHandle>(elements);

            double lengthScale = exporterIFC.LinearScale;
            double angleScale = 180.0 / Math.PI;

            ElementId typeId = element.GetTypeId();
            Element elementType = element.Document.GetElement(typeId);
            int whichStart = elementType != null ? 0 : (element is ElementType ? 1 : 0);
            if (whichStart == 1)
                elementType = element as ElementType;

            Dictionary<BuiltInParameterGroup, int>[] propertyArrIdxMap;
            propertyArrIdxMap = new Dictionary<BuiltInParameterGroup, int>[2]; // one for instance, one for type.
            propertyArrIdxMap[0] = new Dictionary<BuiltInParameterGroup, int>();
            propertyArrIdxMap[1] = new Dictionary<BuiltInParameterGroup, int>();
            List<HashSet<IFCAnyHandle>>[] propertyArr;
            propertyArr = new List<HashSet<IFCAnyHandle>>[2];
            propertyArr[0] = new List<HashSet<IFCAnyHandle>>();
            propertyArr[1] = new List<HashSet<IFCAnyHandle>>();
            List<string>[] propertySetNames;
            propertySetNames = new List<string>[2];
            propertySetNames[0] = new List<string>();
            propertySetNames[1] = new List<string>();

            // pass through: element and element type.  If the element is a ElementType, there will only be one pass.
            for (int which = whichStart; which < 2; which++)
            {
                Element whichElement = (which == 0) ? element : elementType;
                if (whichElement == null)
                    continue;

                bool createType = (which == 1);
                if (createType)
                {
                    if (ExporterCacheManager.TypePropertyInfoCache.HasTypeProperties(typeId))
                    {
                        ExporterCacheManager.TypePropertyInfoCache.AddTypeProperties(typeId, elementSets);
                        continue;
                    }
                }

                bool canUseElementType = ((which == 1) && elementType != null);

                ParameterSet parameters = whichElement.Parameters;
                foreach (Parameter parameter in parameters)
                {
                    Definition parameterDefinition = parameter.Definition;
                    if (parameterDefinition == null)
                        continue;

                    // IFC parameters dealt with separately.
                    // TODO: also sort out parameters passed in Common Psets.
                    BuiltInParameterGroup parameterGroup = parameterDefinition.ParameterGroup;
                    if (parameterGroup == BuiltInParameterGroup.PG_IFC)
                        continue;

                    int idx = -1;
                    if (!propertyArrIdxMap[which].TryGetValue(parameterGroup, out idx))
                    {
                        idx = propertyArr[which].Count;
                        propertyArrIdxMap[which][parameterGroup] = idx;
                        propertyArr[which].Add(new HashSet<IFCAnyHandle>());

                        string groupName = LabelUtils.GetLabelFor(parameterGroup);
                        propertySetNames[which].Add(groupName);
                    }

                    string parameterCaption = parameterDefinition.Name;
                    switch (parameter.StorageType)
                    {
                        case StorageType.None:
                            break;
                        case StorageType.Integer:
                            {
                                int value;
                                if (parameter.HasValue)
                                    value = parameter.AsInteger();
                                else
                                {
                                    if (!canUseElementType)
                                        continue;

                                    Parameter elementTypeParameter = elementType.get_Parameter(parameterDefinition);
                                    if (elementTypeParameter != null && elementTypeParameter.HasValue &&
                                        elementTypeParameter.StorageType == StorageType.Integer)
                                        value = elementTypeParameter.AsInteger();
                                    else
                                        continue;
                                }

                                // YesNo or actual integer?
                                if (parameterDefinition.ParameterType == ParameterType.YesNo)
                                {
                                    propertyArr[which][idx].Add(CreateBooleanPropertyFromCache(file, parameterCaption, value != 0, PropertyValueType.SingleValue));
                                }
                                else
                                {
                                    propertyArr[which][idx].Add(CreateIntegerPropertyFromCache(file, parameterCaption, value, PropertyValueType.SingleValue));
                                }
                                break;
                            }
                        case StorageType.Double:
                            {
                                double value = -1.0;
                                if (parameter.HasValue)
                                    value = parameter.AsDouble();
                                else
                                {
                                    if (!canUseElementType)
                                        continue;

                                    Parameter elementTypeParameter = elementType.get_Parameter(parameterDefinition);
                                    if (elementTypeParameter != null && elementTypeParameter.HasValue &&
                                        elementTypeParameter.StorageType == StorageType.Double)
                                        value = elementTypeParameter.AsDouble();
                                    else
                                        continue;
                                }

                                bool assigned = false;
                                switch (parameterDefinition.ParameterType)
                                {
                                    case ParameterType.Length:
                                        {
                                            propertyArr[which][idx].Add(CreateLengthMeasurePropertyFromCache(file, lengthScale, parameterCaption,
                                                value * lengthScale, PropertyValueType.SingleValue));
                                            assigned = true;
                                            break;
                                        }
                                    case ParameterType.Angle:
                                        {
                                            propertyArr[which][idx].Add(CreatePlaneAngleMeasurePropertyFromCache(file, parameterCaption,
                                                value * angleScale, PropertyValueType.SingleValue));
                                            assigned = true;
                                            break;
                                        }
                                    case ParameterType.Area:
                                        {
                                            propertyArr[which][idx].Add(CreateAreaMeasureProperty(file, parameterCaption,
                                                value * lengthScale * lengthScale, PropertyValueType.SingleValue));
                                            assigned = true;
                                            break;
                                        }
                                    case ParameterType.Volume:
                                        {
                                            propertyArr[which][idx].Add(CreateVolumeMeasureProperty(file, parameterCaption,
                                                value * lengthScale * lengthScale * lengthScale, PropertyValueType.SingleValue));
                                            assigned = true;
                                            break;
                                        }
                                }

                                if (!assigned)
                                {
                                    propertyArr[which][idx].Add(CreateRealPropertyFromCache(file, lengthScale, parameterCaption, value, PropertyValueType.SingleValue));
                                }
                                break;
                            }
                        case StorageType.String:
                            {
                                string value;
                                if (parameter.HasValue)
                                    value = parameter.AsString();
                                else
                                {
                                    if (!canUseElementType)
                                        continue;

                                    Parameter elementTypeParameter = elementType.get_Parameter(parameterDefinition);
                                    if (elementTypeParameter != null && elementTypeParameter.HasValue &&
                                        elementTypeParameter.StorageType == StorageType.String)
                                        value = elementTypeParameter.AsString();
                                    else
                                        continue;
                                }
                                propertyArr[which][idx].Add(CreateLabelPropertyFromCache(file, parameterCaption, value, PropertyValueType.SingleValue, false));
                                break;
                            }
                        case StorageType.ElementId:
                            {
                                ElementId value = ElementId.InvalidElementId;
                                if (parameter.HasValue)
                                    value = parameter.AsElementId();

                                if (value == ElementId.InvalidElementId)
                                {
                                    if (!canUseElementType)
                                        continue;

                                    Parameter elementTypeParameter = elementType.get_Parameter(parameterDefinition);
                                    if (elementTypeParameter != null && elementTypeParameter.HasValue &&
                                        elementTypeParameter.StorageType == StorageType.ElementId)
                                        value = elementTypeParameter.AsElementId();
                                    else
                                        continue;

                                    if (value == ElementId.InvalidElementId)
                                        continue;
                                }

                                Element paramElement = element.Document.GetElement(value);
                                string valueString;
                                if (paramElement != null && !string.IsNullOrEmpty(paramElement.Name))
                                {
                                    valueString = paramElement.Name;
                                    ElementType paramElementType = paramElement is ElementType ? paramElement as ElementType :
                                        element.Document.GetElement(paramElement.GetTypeId()) as ElementType;
                                    if (paramElementType != null && !string.IsNullOrEmpty(ExporterIFCUtils.GetFamilyName(paramElementType)))
                                        valueString = ExporterIFCUtils.GetFamilyName(paramElementType) + ": " + valueString;
                                }
                                else
                                    valueString = value.ToString();

                                propertyArr[which][idx].Add(CreateLabelPropertyFromCache(file, parameterCaption, valueString, PropertyValueType.SingleValue, true));
                                break;
                            }

                    }
                }
            }

            for (int which = whichStart; which < 2; which++)
            {
                HashSet<IFCAnyHandle> propertySets = new HashSet<IFCAnyHandle>();

                int size = propertyArr[which].Count;
                if (size == 0)
                    continue;

                for (int ii = 0; ii < size; ii++)
                {
                    if (propertyArr[which][ii].Count == 0)
                        continue;

                    IFCAnyHandle propertySet = IFCInstanceExporter.CreatePropertySet(file, ExporterIFCUtils.CreateGUID(), exporterIFC.GetOwnerHistoryHandle(),
                        propertySetNames[which][ii], null, propertyArr[which][ii]);

                    if (which == 1)
                        propertySets.Add(propertySet);
                    else
                        IFCInstanceExporter.CreateRelDefinesByProperties(file, ExporterIFCUtils.CreateGUID(), exporterIFC.GetOwnerHistoryHandle(),
                            null, null, elementSets, propertySet);
                }

                if (which == 1)
                    ExporterCacheManager.TypePropertyInfoCache.AddNewTypeProperties(typeId, propertySets, elementSets);
            }
        }