/// <summary>
        /// Gets material handle from material id or creates one if there is none.
        /// </summary>
        /// <param name="exporterIFC">The ExporterIFC object.</param>
        /// <param name="materialId">The material id.</param>
        /// <returns>The handle.</returns>
        public static IFCAnyHandle GetOrCreateMaterialHandle(ExporterIFC exporterIFC, ElementId materialId)
        {
            Document     document           = ExporterCacheManager.Document;
            IFCAnyHandle materialNameHandle = ExporterCacheManager.MaterialHandleCache.Find(materialId);

            if (IFCAnyHandleUtil.IsNullOrHasNoValue(materialNameHandle))
            {
                string materialName = " <Unnamed>";
                if (materialId != ElementId.InvalidElementId)
                {
                    Material material = document.GetElement(materialId) as Material;
                    if (material != null)
                    {
                        materialName = material.Name;
                    }
                }
                materialNameHandle = IFCInstanceExporter.CreateMaterial(exporterIFC.GetFile(), materialName);

                ExporterCacheManager.MaterialHandleCache.Register(materialId, materialNameHandle);

                // associate Material with SurfaceStyle if necessary.
                IFCFile file = exporterIFC.GetFile();
                if (materialId != ElementId.InvalidElementId && !ExporterCacheManager.ExportOptionsCache.ExportAs2x2 && materialNameHandle.HasValue)
                {
                    HashSet <IFCAnyHandle> matRepHandles = IFCAnyHandleUtil.GetHasRepresentation(materialNameHandle);
                    if (matRepHandles.Count == 0)
                    {
                        Material matElem = document.GetElement(materialId) as Material;

                        ElementId fillPatternId       = (matElem != null) ? matElem.GetCutPatternId() : ElementId.InvalidElementId;
                        Autodesk.Revit.DB.Color color = (matElem != null) ? matElem.GetCutPatternColor() : new Color(0, 0, 0);

                        double planScale = 100.0;

                        HashSet <IFCAnyHandle> styles = new HashSet <IFCAnyHandle>();

                        bool hasFill = false;

                        IFCAnyHandle styledRepItem = null;
                        IFCAnyHandle matStyleHnd   = CategoryUtil.GetOrCreateMaterialStyle(document, exporterIFC, materialId);
                        if (!IFCAnyHandleUtil.IsNullOrHasNoValue(matStyleHnd))
                        {
                            styles.Add(matStyleHnd);

                            if (fillPatternId != ElementId.InvalidElementId && !ExporterCacheManager.ExportOptionsCache.ExportAs2x3CoordinationView2)
                            {
                                IFCAnyHandle cutStyleHnd = exporterIFC.GetOrCreateFillPattern(fillPatternId, color, planScale);
                                if (cutStyleHnd.HasValue)
                                {
                                    styles.Add(cutStyleHnd);
                                    hasFill = true;
                                }
                            }

                            IFCAnyHandle presStyleHnd = IFCInstanceExporter.CreatePresentationStyleAssignment(file, styles);

                            HashSet <IFCAnyHandle> presStyleSet = new HashSet <IFCAnyHandle>();
                            presStyleSet.Add(presStyleHnd);

                            IFCAnyHandle styledItemHnd = IFCInstanceExporter.CreateStyledItem(file, styledRepItem, presStyleSet, null);

                            IFCAnyHandle contextOfItems = exporterIFC.Get3DContextHandle("");

                            string repId   = "Style";
                            string repType = (hasFill) ? "Material and Cut Pattern" : "Material";
                            HashSet <IFCAnyHandle> repItems = new HashSet <IFCAnyHandle>();
                            repItems.Add(styledItemHnd);

                            IFCAnyHandle styleRepHnd = IFCInstanceExporter.CreateStyledRepresentation(file, contextOfItems, repId, repType, repItems);

                            List <IFCAnyHandle> repList = new List <IFCAnyHandle>();
                            repList.Add(styleRepHnd);

                            IFCAnyHandle matDefRepHnd = IFCInstanceExporter.CreateMaterialDefinitionRepresentation(file, null, null, repList, materialNameHandle);
                        }
                    }
                }
            }
            return(materialNameHandle);
        }
Exemple #2
0
        /// <summary>
        /// Creates openings if there is necessary.
        /// </summary>
        /// <param name="elementHandle">The element handle to create openings.</param>
        /// <param name="element">The element to create openings.</param>
        /// <param name="info">The extrusion data.</param>
        /// <param name="extraParams">The extrusion creation data.</param>
        /// <param name="offsetTransform">The offset transform from ExportBody, or the identity transform.</param>
        /// <param name="exporterIFC">The ExporterIFC object.</param>
        /// <param name="originalPlacement">The original placement handle.</param>
        /// <param name="setter">The PlacementSetter.</param>
        /// <param name="wrapper">The ProductWrapper.</param>
        private static void CreateOpeningsIfNecessaryBase(IFCAnyHandle elementHandle, Element element, IList <IFCExtrusionData> info,
                                                          IFCExtrusionCreationData extraParams, Transform offsetTransform, ExporterIFC exporterIFC,
                                                          IFCAnyHandle originalPlacement, IFCPlacementSetter setter, ProductWrapper wrapper)
        {
            if (IFCAnyHandleUtil.IsNullOrHasNoValue(elementHandle))
            {
                return;
            }

            int sz = info.Count;

            if (sz == 0)
            {
                return;
            }

            using (IFCTransformSetter transformSetter = IFCTransformSetter.Create())
            {
                if (offsetTransform != null)
                {
                    transformSetter.Initialize(exporterIFC, offsetTransform.Inverse);
                }

                IFCFile   file       = exporterIFC.GetFile();
                ElementId categoryId = CategoryUtil.GetSafeCategoryId(element);
                Document  document   = element.Document;

                string openingObjectType = "Opening";

                int openingNumber = 1;
                for (int curr = info.Count - 1; curr >= 0; curr--)
                {
                    IFCAnyHandle extrusionHandle = ExtrusionExporter.CreateExtrudedSolidFromExtrusionData(exporterIFC, element, info[curr]);
                    if (IFCAnyHandleUtil.IsNullOrHasNoValue(extrusionHandle))
                    {
                        continue;
                    }

                    IFCAnyHandle styledItemHnd = BodyExporter.CreateSurfaceStyleForRepItem(exporterIFC, document,
                                                                                           extrusionHandle, ElementId.InvalidElementId);

                    HashSet <IFCAnyHandle> bodyItems = new HashSet <IFCAnyHandle>();
                    bodyItems.Add(extrusionHandle);

                    IFCAnyHandle         contextOfItems  = exporterIFC.Get3DContextHandle("Body");
                    IFCAnyHandle         bodyRep         = RepresentationUtil.CreateSweptSolidRep(exporterIFC, element, categoryId, contextOfItems, bodyItems, null);
                    IList <IFCAnyHandle> representations = new List <IFCAnyHandle>();
                    representations.Add(bodyRep);

                    IFCAnyHandle openingRep = IFCInstanceExporter.CreateProductDefinitionShape(file, null, null, representations);

                    IFCAnyHandle openingPlacement = ExporterUtil.CopyLocalPlacement(file, originalPlacement);
                    string       guid             = GUIDUtil.CreateGUID();
                    IFCAnyHandle ownerHistory     = exporterIFC.GetOwnerHistoryHandle();
                    string       openingName      = NamingUtil.GetIFCNamePlusIndex(element, openingNumber++);
                    string       elementId        = NamingUtil.CreateIFCElementId(element);
                    IFCAnyHandle openingElement   = IFCInstanceExporter.CreateOpeningElement(file, guid, ownerHistory,
                                                                                             openingName, null, openingObjectType, openingPlacement, openingRep, elementId);
                    wrapper.AddElement(null, openingElement, setter, extraParams, true);
                    if (ExporterCacheManager.ExportOptionsCache.ExportBaseQuantities && (extraParams != null))
                    {
                        ExporterIFCUtils.CreateOpeningQuantities(exporterIFC, openingElement, extraParams);
                    }

                    string voidGuid = GUIDUtil.CreateGUID();
                    IFCInstanceExporter.CreateRelVoidsElement(file, voidGuid, ownerHistory, null, null, elementHandle, openingElement);
                }
            }
        }