Example #1
0
        /// <summary>
        /// Exports a CeilingAndFloor element to IFC.
        /// </summary>
        /// <param name="exporterIFC">The ExporterIFC object.</param>
        /// <param name="floor">The floor element.</param>
        /// <param name="geometryElement">The geometry element.</param>
        /// <param name="productWrapper">The ProductWrapper.</param>
        public static void ExportCeilingAndFloorElement(ExporterIFC exporterIFC, CeilingAndFloor floorElement, ref GeometryElement geometryElement,
                                                        ProductWrapper productWrapper)
        {
            if (geometryElement == null)
            {
                return;
            }

            IFCFile file = exporterIFC.GetFile();

            string            ifcEnumType;
            IFCExportInfoPair exportType = ExporterUtil.GetProductExportType(exporterIFC, floorElement, out ifcEnumType);
            IFCAnyHandle      type       = null;

            if (!ElementFilteringUtil.IsElementVisible(floorElement))
            {
                return;
            }

            // Check the intended IFC entity or type name is in the exclude list specified in the UI
            Common.Enums.IFCEntityType elementClassTypeEnum;
            if (Enum.TryParse <Common.Enums.IFCEntityType>(exportType.ExportInstance.ToString(), out elementClassTypeEnum) ||
                Enum.TryParse <Common.Enums.IFCEntityType>(exportType.ExportType.ToString(), out elementClassTypeEnum))
            {
                if (ExporterCacheManager.ExportOptionsCache.IsElementInExcludeList(elementClassTypeEnum))
                {
                    return;
                }
            }

            Document doc = floorElement.Document;

            using (SubTransaction tempPartTransaction = new SubTransaction(doc))
            {
                MaterialLayerSetInfo layersetInfo = new MaterialLayerSetInfo(exporterIFC, floorElement, productWrapper);
                // For IFC4RV export, Element will be split into its parts(temporarily) in order to export the wall by its parts
                // If Parts are created by code and not by user then their names should be equal to Material names.
                bool setMaterialNameToPartName         = ExporterUtil.CreateParts(floorElement, layersetInfo.MaterialIds.Count, ref geometryElement);
                ExporterUtil.ExportPartAs exportPartAs = ExporterUtil.CanExportByComponentsOrParts(floorElement);
                bool exportByComponents = exportPartAs == ExporterUtil.ExportPartAs.ShapeAspect;
                bool exportParts        = exportPartAs == ExporterUtil.ExportPartAs.Part;

                if (exportParts && !PartExporter.CanExportElementInPartExport(floorElement, floorElement.LevelId, false))
                {
                    return;
                }

                using (IFCTransaction tr = new IFCTransaction(file))
                {
                    bool canExportAsContainerOrWithExtrusionAnalyzer = (!exportParts && (floorElement is Floor));

                    if (canExportAsContainerOrWithExtrusionAnalyzer)
                    {
                        // Try to export the Floor slab as a container.  If that succeeds, we are done.
                        // If we do export the floor as a container, it will take care of the local placement and transform there, so we need to leave
                        // this out of the IFCTransformSetter and PlacementSetter scopes below, or else we'll get double transforms.
                        IFCAnyHandle floorHnd = RoofExporter.ExportRoofOrFloorAsContainer(exporterIFC, floorElement, geometryElement, productWrapper);
                        if (!IFCAnyHandleUtil.IsNullOrHasNoValue(floorHnd))
                        {
                            tr.Commit();
                            return;
                        }
                    }

                    IList <IFCAnyHandle> slabHnds        = new List <IFCAnyHandle>();
                    IList <IFCAnyHandle> brepSlabHnds    = new List <IFCAnyHandle>();
                    IList <IFCAnyHandle> nonBrepSlabHnds = new List <IFCAnyHandle>();

                    IFCAnyHandle ownerHistory = ExporterCacheManager.OwnerHistoryHandle;

                    using (IFCTransformSetter transformSetter = IFCTransformSetter.Create())
                    {
                        // Check for containment override
                        IFCAnyHandle overrideContainerHnd = null;
                        ElementId    overrideContainerId  = ParameterUtil.OverrideContainmentParameter(exporterIFC, floorElement, out overrideContainerHnd);

                        using (PlacementSetter placementSetter = PlacementSetter.Create(exporterIFC, floorElement, null, null, overrideContainerId, overrideContainerHnd))
                        {
                            IFCAnyHandle localPlacement = placementSetter.LocalPlacement;

                            // The routine ExportExtrudedSlabOpenings is called if exportedAsInternalExtrusion is true, and it requires having a valid level association.
                            // Disable calling ExportSlabAsExtrusion if we can't handle potential openings.
                            bool canExportAsInternalExtrusion = placementSetter.LevelInfo != null && !ExporterCacheManager.ExportOptionsCache.ExportAs4ReferenceView;
                            bool exportedAsInternalExtrusion  = false;

                            ElementId catId = CategoryUtil.GetSafeCategoryId(floorElement);

                            IList <IFCAnyHandle>             prodReps        = new List <IFCAnyHandle>();
                            IList <ShapeRepresentationType>  repTypes        = new List <ShapeRepresentationType>();
                            IList <IList <CurveLoop> >       extrusionLoops  = new List <IList <CurveLoop> >();
                            IList <IFCExtrusionCreationData> loopExtraParams = new List <IFCExtrusionCreationData>();
                            Plane floorPlane = GeometryUtil.CreateDefaultPlane();

                            IList <IFCAnyHandle> localPlacements = new List <IFCAnyHandle>();

                            if (!exportParts)
                            {
                                if (canExportAsContainerOrWithExtrusionAnalyzer)
                                {
                                    Floor floor = floorElement as Floor;

                                    // Next, try to use the ExtrusionAnalyzer for the limited cases it handles - 1 solid, no openings, end clippings only.
                                    // Also limited to cases with line and arc boundaries.
                                    //
                                    SolidMeshGeometryInfo  solidMeshInfo = GeometryUtil.GetSplitSolidMeshGeometry(geometryElement);
                                    IList <Solid>          solids        = solidMeshInfo.GetSolids();
                                    IList <Mesh>           meshes        = solidMeshInfo.GetMeshes();
                                    IList <GeometryObject> gObjs         = FamilyExporterUtil.RemoveInvisibleSolidsAndMeshes(floorElement.Document, exporterIFC, ref solids, ref meshes);

                                    if (solids.Count == 1 && meshes.Count == 0)
                                    {
                                        // floorExtrusionDirection is set to (0, 0, -1) because extrusionAnalyzerFloorPlane is computed from the top face of the floor
                                        XYZ floorExtrusionDirection = new XYZ(0, 0, -1);
                                        XYZ modelOrigin             = XYZ.Zero;

                                        XYZ floorOrigin = floor.GetVerticalProjectionPoint(modelOrigin, FloorFace.Top);
                                        if (floorOrigin == null)
                                        {
                                            // GetVerticalProjectionPoint may return null if FloorFace.Top is an edited face that doesn't
                                            // go through the Revit model origin.  We'll try the midpoint of the bounding box instead.
                                            BoundingBoxXYZ boundingBox = floorElement.get_BoundingBox(null);
                                            modelOrigin = (boundingBox.Min + boundingBox.Max) / 2.0;
                                            floorOrigin = floor.GetVerticalProjectionPoint(modelOrigin, FloorFace.Top);
                                        }

                                        if (floorOrigin != null)
                                        {
                                            XYZ   floorDir = floor.GetNormalAtVerticalProjectionPoint(floorOrigin, FloorFace.Top);
                                            Plane extrusionAnalyzerFloorBasePlane = GeometryUtil.CreatePlaneByNormalAtOrigin(floorDir);

                                            GenerateAdditionalInfo additionalInfo = GenerateAdditionalInfo.GenerateBody;
                                            additionalInfo |= ExporterCacheManager.ExportOptionsCache.ExportAs4 ?
                                                              GenerateAdditionalInfo.GenerateFootprint : GenerateAdditionalInfo.None;

                                            // Skip generate body item for IFC4RV. It will be handled later in PartExporter.ExportHostPartAsShapeAspects()
                                            if (exportByComponents)
                                            {
                                                additionalInfo &= ~GenerateAdditionalInfo.GenerateBody;
                                            }

                                            ExtrusionExporter.ExtraClippingData extraClippingData = null;
                                            HandleAndData floorAndProperties =
                                                ExtrusionExporter.CreateExtrusionWithClippingAndProperties(exporterIFC, floorElement, false,
                                                                                                           catId, solids[0], extrusionAnalyzerFloorBasePlane, floorOrigin, floorExtrusionDirection, null,
                                                                                                           out extraClippingData,
                                                                                                           addInfo: additionalInfo);
                                            if (extraClippingData.CompletelyClipped)
                                            {
                                                return;
                                            }

                                            IList <IFCAnyHandle> representations = new List <IFCAnyHandle>();
                                            if (floorAndProperties.Handle != null)
                                            {
                                                representations.Add(floorAndProperties.Handle);
                                                repTypes.Add(ShapeRepresentationType.SweptSolid);
                                            }

                                            // Footprint representation will only be exported in export to IFC4
                                            if (((additionalInfo & GenerateAdditionalInfo.GenerateFootprint) != 0) && (floorAndProperties.FootprintInfo != null))
                                            {
                                                IFCAnyHandle footprintShapeRep = floorAndProperties.FootprintInfo.CreateFootprintShapeRepresentation(exporterIFC);
                                                representations.Add(footprintShapeRep);
                                            }

                                            if (exportByComponents)
                                            {
                                                IFCAnyHandle prodRep = RepresentationUtil.CreateProductDefinitionShapeWithoutBodyRep(exporterIFC, floorElement, catId, geometryElement, representations);
                                                prodReps.Add(prodRep);
                                            }
                                            else if (representations.Count > 0 && floorAndProperties.Handle != null) // Only when at least the body rep exists will come here
                                            {
                                                IFCAnyHandle prodRep = IFCInstanceExporter.CreateProductDefinitionShape(file, null, null, representations);
                                                prodReps.Add(prodRep);
                                            }

                                            if (floorAndProperties.Data != null)
                                            {
                                                loopExtraParams.Add(floorAndProperties.Data);
                                            }
                                        }
                                    }
                                }

                                // Use internal routine as backup that handles openings.
                                if (prodReps.Count == 0 && canExportAsInternalExtrusion && !exportByComponents)
                                {
                                    exportedAsInternalExtrusion = ExporterIFCUtils.ExportSlabAsExtrusion(exporterIFC, floorElement,
                                                                                                         geometryElement, transformSetter, localPlacement, out localPlacements, out prodReps,
                                                                                                         out extrusionLoops, out loopExtraParams, floorPlane);
                                    PotentiallyFixPresentationLayerAssignment(floorElement, prodReps);
                                    for (int ii = 0; ii < prodReps.Count; ii++)
                                    {
                                        // all are extrusions
                                        repTypes.Add(ShapeRepresentationType.SweptSolid);

                                        // Footprint representation will only be exported in export to IFC4
                                        if (ExporterCacheManager.ExportOptionsCache.ExportAs4)
                                        {
                                            if (extrusionLoops.Count > ii)
                                            {
                                                if (extrusionLoops[ii].Count > 0)
                                                {
                                                    // Get the extrusion footprint using the first Curveloop. Transform needs to be obtained from the returned local placement
                                                    Transform    lcs = ExporterIFCUtils.GetUnscaledTransform(exporterIFC, localPlacements[ii]);
                                                    IFCAnyHandle footprintGeomRepItem = GeometryUtil.CreateIFCCurveFromCurveLoop(exporterIFC, extrusionLoops[ii][0], lcs, floorPlane.Normal);

                                                    IFCAnyHandle        contextOfItemsFootprint = exporterIFC.Get3DContextHandle("FootPrint");
                                                    ISet <IFCAnyHandle> repItem = new HashSet <IFCAnyHandle>();
                                                    repItem.Add(footprintGeomRepItem);
                                                    IFCAnyHandle         footprintShapeRepresentation = RepresentationUtil.CreateBaseShapeRepresentation(exporterIFC, contextOfItemsFootprint, "FootPrint", "Curve2D", repItem);
                                                    IList <IFCAnyHandle> reps = new List <IFCAnyHandle>();
                                                    reps.Add(footprintShapeRepresentation);
                                                    IFCAnyHandleUtil.AddRepresentations(prodReps[ii], reps);
                                                }
                                            }
                                        }
                                    }
                                }

                                IFCAnyHandle prodDefHnd;
                                if (prodReps.Count == 0)
                                {
                                    if (exportByComponents)
                                    {
                                        prodDefHnd = RepresentationUtil.CreateProductDefinitionShapeWithoutBodyRep(exporterIFC, floorElement, catId, geometryElement, null);
                                        prodReps.Add(prodDefHnd);
                                    }
                                    else
                                    {
                                        using (IFCExtrusionCreationData ecData = new IFCExtrusionCreationData())
                                        {
                                            // Brep representation using tesellation after ExportSlabAsExtrusion does not return prodReps
                                            BodyExporterOptions bodyExporterOptions = new BodyExporterOptions(true, ExportOptionsCache.ExportTessellationLevel.Medium);
                                            BodyData            bodyData;
                                            prodDefHnd = RepresentationUtil.CreateAppropriateProductDefinitionShape(exporterIFC,
                                                                                                                    floorElement, catId, geometryElement, bodyExporterOptions, null, ecData, out bodyData);
                                            if (IFCAnyHandleUtil.IsNullOrHasNoValue(prodDefHnd))
                                            {
                                                ecData.ClearOpenings();
                                                return;
                                            }

                                            prodReps.Add(prodDefHnd);
                                            repTypes.Add(bodyData.ShapeRepresentationType);
                                        }
                                    }
                                }
                            }

                            // Create the slab from either the extrusion or the BRep information.
                            string ifcGUID = GUIDUtil.CreateGUID(floorElement);

                            int numReps = exportParts ? 1 : prodReps.Count;

                            // Deal with a couple of cases that have non-standard defaults.
                            switch (exportType.ExportInstance)
                            {
                            case IFCEntityType.IfcCovering:
                                exportType.ValidatedPredefinedType = IFCValidateEntry.GetValidIFCType <IFCCoveringType>(floorElement, ifcEnumType, "FLOORING");
                                break;

                            case IFCEntityType.IfcSlab:
                                bool isBaseSlab = false;
#pragma warning disable CS0612, CS0618//AnalyticalModel is obsolette
                                AnalyticalModel analyticalModel = floorElement.GetAnalyticalModel();
                                if (analyticalModel != null)
                                {
                                    AnalyzeAs slabFoundationType = analyticalModel.GetAnalyzeAs();
                                    isBaseSlab = (slabFoundationType == AnalyzeAs.SlabOnGrade) || (slabFoundationType == AnalyzeAs.Mat);
                                }
                                exportType.ValidatedPredefinedType = IFCValidateEntry.GetValidIFCType <IFCSlabType>(floorElement, ifcEnumType, isBaseSlab ? "BASESLAB" : "FLOOR");
                                break;
#pragma warning restore CS0612, CS0618
                            }

                            for (int ii = 0; ii < numReps; ii++)
                            {
                                string ifcName = NamingUtil.GetNameOverride(floorElement, NamingUtil.GetIFCNamePlusIndex(floorElement, ii == 0 ? -1 : ii + 1));

                                string       currentGUID       = (ii == 0) ? ifcGUID : GUIDUtil.CreateGUID();
                                IFCAnyHandle localPlacementHnd = exportedAsInternalExtrusion ? localPlacements[ii] : localPlacement;

                                IFCAnyHandle slabHnd = null;
                                slabHnd = IFCInstanceExporter.CreateGenericIFCEntity(exportType, exporterIFC, floorElement, currentGUID, ownerHistory,
                                                                                     localPlacementHnd, exportParts ? null : prodReps[ii]);
                                if (IFCAnyHandleUtil.IsNullOrHasNoValue(slabHnd))
                                {
                                    return;
                                }

                                if (!string.IsNullOrEmpty(ifcName))
                                {
                                    IFCAnyHandleUtil.OverrideNameAttribute(slabHnd, ifcName);
                                }

                                // Pre IFC4 Slab does not have PredefinedType
                                if (!string.IsNullOrEmpty(exportType.ValidatedPredefinedType) && !ExporterCacheManager.ExportOptionsCache.ExportAsOlderThanIFC4)
                                {
                                    IFCAnyHandleUtil.SetAttribute(slabHnd, "PredefinedType", exportType.ValidatedPredefinedType, true);
                                }
                                if (exportParts)
                                {
                                    PartExporter.ExportHostPart(exporterIFC, floorElement, slabHnd, productWrapper, placementSetter, localPlacementHnd, null, setMaterialNameToPartName);
                                }
                                else if (exportByComponents)
                                {
                                    IFCExtrusionCreationData partECData            = new IFCExtrusionCreationData();
                                    IFCAnyHandle             hostShapeRepFromParts = PartExporter.ExportHostPartAsShapeAspects(exporterIFC, floorElement, prodReps[ii],
                                                                                                                               productWrapper, placementSetter, localPlacement, ElementId.InvalidElementId, layersetInfo, partECData);
                                    loopExtraParams.Add(partECData);
                                }

                                slabHnds.Add(slabHnd);

                                // For IFC4RV, export of the geometry is already handled in PartExporter.ExportHostPartAsShapeAspects()
                                if (!exportParts && !exportByComponents)
                                {
                                    if (repTypes[ii] == ShapeRepresentationType.Brep || repTypes[ii] == ShapeRepresentationType.Tessellation)
                                    {
                                        brepSlabHnds.Add(slabHnd);
                                    }
                                    else
                                    {
                                        nonBrepSlabHnds.Add(slabHnd);
                                    }
                                }
                            }

                            for (int ii = 0; ii < numReps; ii++)
                            {
                                IFCExtrusionCreationData loopExtraParam = ii < loopExtraParams.Count ? loopExtraParams[ii] : null;
                                productWrapper.AddElement(floorElement, slabHnds[ii], placementSetter, loopExtraParam, true, exportType);

                                type = ExporterUtil.CreateGenericTypeFromElement(floorElement, exportType, file, ownerHistory, exportType.ValidatedPredefinedType, productWrapper);
                                ExporterCacheManager.TypeRelationsCache.Add(type, slabHnds[ii]);

                                ExporterUtil.AddIntoComplexPropertyCache(slabHnds[ii], layersetInfo);
                            }

                            // This call to the native function appears to create Brep opening also when appropriate. But the creation of the IFC instances is not
                            //   controllable from the managed code. Therefore in some cases BRep geometry for Opening will still be exported even in the Reference View
                            if (exportedAsInternalExtrusion)
                            {
                                ISet <IFCAnyHandle> oldCreatedObjects = productWrapper.GetAllObjects();
                                ExporterIFCUtils.ExportExtrudedSlabOpenings(exporterIFC, floorElement, placementSetter.LevelInfo,
                                                                            localPlacements[0], slabHnds, extrusionLoops, floorPlane, productWrapper.ToNative());
                                ISet <IFCAnyHandle> newCreatedObjects = productWrapper.GetAllObjects();
                                newCreatedObjects.ExceptWith(oldCreatedObjects);
                                PotentiallyFixPresentationLayerAssignment(floorElement, newCreatedObjects);
                            }
                        }

                        if (!exportParts)
                        {
                            if (ExporterCacheManager.ExportOptionsCache.ExportAs4ReferenceView)
                            {
                                HostObjectExporter.ExportHostObjectMaterials(exporterIFC, floorElement, productWrapper.GetAnElement(),
                                                                             geometryElement, productWrapper, ElementId.InvalidElementId, Toolkit.IFCLayerSetDirection.Axis3, false, type);
                            }
                            else
                            {
                                if (nonBrepSlabHnds.Count > 0)
                                {
                                    HostObjectExporter.ExportHostObjectMaterials(exporterIFC, floorElement, nonBrepSlabHnds,
                                                                                 geometryElement, productWrapper, ElementId.InvalidElementId, Toolkit.IFCLayerSetDirection.Axis3, false, type);
                                }

                                if (brepSlabHnds.Count > 0)
                                {
                                    HostObjectExporter.ExportHostObjectMaterials(exporterIFC, floorElement, brepSlabHnds,
                                                                                 geometryElement, productWrapper, ElementId.InvalidElementId, Toolkit.IFCLayerSetDirection.Axis3, true, type);
                                }
                            }
                        }
                    }

                    tr.Commit();
                    return;
                }
            }
        }
Example #2
0
        /// <summary>
        /// Create the "Body" IfcRepresentation for a beam if it is representable by an extrusion, possibly with clippings and openings.
        /// </summary>
        /// <param name="exporterIFC">The exporterIFC class.</param>
        /// <param name="element">The beam element.T</param>
        /// <param name="catId">The category id.</param>
        /// <param name="geomObjects">The list of solids and meshes representing the beam's geometry.
        /// <param name="axisInfo">The beam axis information.</param>
        /// <returns>The BeamBodyAsExtrusionInfo class which contains the created handle (if any) and other information, or null.</returns>
        private static BeamBodyAsExtrusionInfo CreateBeamGeometryAsExtrusion(ExporterIFC exporterIFC, Element element, ElementId catId,
                                                                             IList <GeometryObject> geomObjects, BeamAxisInfo axisInfo)
        {
            // If we have a beam with a Linear location line that only has one solid geometry,
            // we will try to use the ExtrusionAnalyzer to generate an extrusion with 0 or more clippings.
            // This code is currently limited in that it will not process beams with openings, so we
            // use other methods below if this one fails.
            if (geomObjects == null || geomObjects.Count != 1 || (!(geomObjects[0] is Solid)) || axisInfo == null || !(axisInfo.Axis is Line))
            {
                return(null);
            }

            BeamBodyAsExtrusionInfo info = new BeamBodyAsExtrusionInfo();

            info.DontExport = false;
            info.Materials  = new HashSet <ElementId>();
            info.Slope      = 0.0;

            Transform orientTrf = axisInfo.LCSAsTransform;

            Solid solid = geomObjects[0] as Solid;

            XYZ beamDirection = orientTrf.BasisX;
            XYZ planeXVec     = orientTrf.BasisY.Normalize();
            XYZ planeYVec     = orientTrf.BasisZ.Normalize();

            MaterialAndProfile materialAndProfile = null;
            FootPrintInfo      footPrintInfo      = null;

            string profileName = NamingUtil.GetProfileName(element);

            Plane beamExtrusionBasePlane   = GeometryUtil.CreatePlaneByXYVectorsAtOrigin(planeXVec, planeYVec);
            GenerateAdditionalInfo addInfo = GenerateAdditionalInfo.GenerateBody | GenerateAdditionalInfo.GenerateProfileDef;

            ExtrusionExporter.ExtraClippingData extraClippingData = null;
            info.RepresentationHandle = ExtrusionExporter.CreateExtrusionWithClipping(exporterIFC, element, false,
                                                                                      catId, solid, beamExtrusionBasePlane, orientTrf.Origin, beamDirection, null,
                                                                                      out extraClippingData,
                                                                                      out footPrintInfo, out materialAndProfile, addInfo: addInfo, profileName: profileName);
            if (extraClippingData.CompletelyClipped)
            {
                info.DontExport = true;
                return(null);
            }

            if (!IFCAnyHandleUtil.IsNullOrHasNoValue(info.RepresentationHandle))
            {
                // This is used by the BeamSlopeCalculator.  This should probably be generated automatically by
                // CreateExtrusionWithClipping.
                IFCExtrusionBasis bestAxis = (Math.Abs(beamDirection[0]) > Math.Abs(beamDirection[1])) ?
                                             IFCExtrusionBasis.BasisX : IFCExtrusionBasis.BasisY;
                info.Slope = GeometryUtil.GetSimpleExtrusionSlope(beamDirection, bestAxis);
                ElementId materialId = BodyExporter.GetBestMaterialIdFromGeometryOrParameter(solid, element);
                if (materialId != ElementId.InvalidElementId)
                {
                    info.Materials.Add(materialId);
                }
            }

            if (materialAndProfile != null)
            {
                info.MaterialAndProfile = materialAndProfile;
            }

            return(info);
        }