Example #1
0
        private static IfcStyledItem ToIfcStyledItem(this Material material, IfcRepresentationItem shape, Document doc)
        {
            var color   = material.Color.ToIfcColourRgb(doc);
            var shading = new IfcSurfaceStyleShading(color);

            var styles = new List <IfcSurfaceStyleElementSelect> {
            };

            styles.Add(new IfcSurfaceStyleElementSelect(shading));
            var surfaceStyle = new IfcSurfaceStyle(material.Name, IfcSurfaceSide.POSITIVE, styles);
            var styleSelect  = new IfcPresentationStyleSelect(surfaceStyle);
            var assign       = new IfcPresentationStyleAssignment(new List <IfcPresentationStyleSelect> {
                styleSelect
            });
            var assignments = new List <IfcPresentationStyleAssignment>();

            assignments.Add(assign);
            var styledByItem = new IfcStyledItem(shape, assignments, material.Name);

            doc.AddEntity(color);
            doc.AddEntity(shading);
            doc.AddEntity(surfaceStyle);
            doc.AddEntity(styleSelect);
            doc.AddEntity(assign);

            return(styledByItem);
        }
        private static IfcAxis2Placement3D ToIfcAxis2Placement3D(this Transform transform, Document doc)
        {
            var origin    = transform.Origin.ToIfcCartesianPoint();
            var z         = transform.ZAxis.ToIfcDirection();
            var x         = transform.XAxis.ToIfcDirection();
            var placement = new IfcAxis2Placement3D(origin,
                                                    z, x);

            doc.AddEntity(origin);
            doc.AddEntity(z);
            doc.AddEntity(x);
            return(placement);
        }
Example #3
0
        /// <summary>
        /// Convert a wall to an IfcWallStandardCase
        /// </summary>
        /// <param name="wall"></param>
        /// <param name="context"></param>
        /// <param name="doc"></param>
        /// <returns></returns>
        private static IfcWallStandardCase ToIfcWallStandardCase(this Wall wall, IfcRepresentationContext context, Document doc)
        {
            var sweptArea        = wall.CenterLine.Thicken(wall.Thickness()).ToIfcArbitraryClosedProfileDef(doc);
            var extrudeDirection = Vector3.ZAxis.ToIfcDirection();

            // We don't use the Wall's transform for positioning, because
            // our walls have a transform that lays the wall "flat". Just
            // use a identity transform.
            var position = new Transform().ToIfcAxis2Placement3D(doc);
            var repItem  = new IfcExtrudedAreaSolid(sweptArea, position,
                                                    extrudeDirection, new IfcPositiveLengthMeasure(wall.Height));
            var rep = new IfcShapeRepresentation(context, "Body", "SweptSolid", new List <IfcRepresentationItem> {
                repItem
            });
            var productRep = new IfcProductDefinitionShape(new List <IfcRepresentation> {
                rep
            });
            var id             = IfcGuid.ToIfcGuid(Guid.NewGuid());
            var localPlacement = new Transform().ToIfcLocalPlacement(doc);
            var ifcWall        = new IfcWallStandardCase(new IfcGloballyUniqueId(id),
                                                         null, wall.Name, null, null, localPlacement, productRep, null);

            doc.AddEntity(sweptArea);
            doc.AddEntity(extrudeDirection);
            doc.AddEntity(position);
            doc.AddEntity(repItem);
            doc.AddEntity(rep);
            doc.AddEntity(localPlacement);
            doc.AddEntity(productRep);
            doc.AddEntity(ifcWall);

            return(ifcWall);
        }
        private static IfcSurfaceCurveSweptAreaSolid ToIfcSurfaceCurveSweptAreaSolid(this Sweep sweep, Document doc)
        {
            var position  = new Transform().ToIfcAxis2Placement3D(doc);
            var sweptArea = sweep.Profile.Perimeter.ToIfcArbitraryClosedProfileDef(doc);
            var directrix = sweep.Curve.ToIfcCurve(doc);
            var profile   = new IfcArbitraryOpenProfileDef(IfcProfileTypeEnum.CURVE, directrix);

            var extrudeDir          = Vector3.ZAxis.ToIfcDirection();
            var extrudeSurfPosition = new Transform(0, 0, -100).ToIfcAxis2Placement3D(doc);

            doc.AddEntity(extrudeSurfPosition);

            var surface = new IfcSurfaceOfLinearExtrusion(profile, position, extrudeDir, 100);

            // You must use the version of this constructor that has position, startParam,
            // and endParam. If you don't, ArchiCAD (and possibly others) will call
            // the geometry invalid.
            var solid = new IfcSurfaceCurveSweptAreaSolid(sweptArea, position, directrix, 0, 1, surface);

            doc.AddEntity(position);
            doc.AddEntity(sweptArea);
            doc.AddEntity(directrix);

            doc.AddEntity(extrudeDir);
            doc.AddEntity(profile);

            doc.AddEntity(surface);
            doc.AddEntity(solid);

            return(solid);
        }
        private static IfcFixedReferenceSweptAreaSolid ToIfcFixedReferenceSweptAreaSolid(this Sweep sweep, Transform transform, Document doc)
        {
            var position  = transform.ToIfcAxis2Placement3D(doc);
            var sweptArea = sweep.Profile.Perimeter.ToIfcArbitraryClosedProfileDef(doc);
            var directrix = sweep.Curve.ToIfcCurve(doc);
            var refDir    = sweep.Curve.TransformAt(0.0).XAxis.ToIfcDirection();
            var solid     = new IfcFixedReferenceSweptAreaSolid(sweptArea, position, directrix, 0, 1, refDir);

            doc.AddEntity(refDir);
            doc.AddEntity(position);
            doc.AddEntity(sweptArea);
            doc.AddEntity(directrix);

            doc.AddEntity(solid);
            return(solid);
        }
        private static IfcVector ToIfcVector(this Vector3 v, Document doc)
        {
            var dir    = v.ToIfcDirection();
            var vector = new IfcVector(dir, new IfcLengthMeasure(0));

            doc.AddEntity(dir);
            return(vector);
        }
        private static IfcArbitraryClosedProfileDef ToIfcArbitraryClosedProfileDef(this Polygon polygon, Document doc)
        {
            var pline   = polygon.ToIfcPolyline(doc);
            var profile = new IfcArbitraryClosedProfileDef(IfcProfileTypeEnum.AREA, pline);

            doc.AddEntity(pline);
            return(profile);
        }
Example #8
0
        /// <summary>
        /// Convert a transform to an IfcLocalPlacement.
        /// </summary>
        /// <param name="transform"></param>
        /// <param name="doc"></param>
        /// <returns></returns>
        private static IfcLocalPlacement ToIfcLocalPlacement(this Transform transform, Document doc)
        {
            var placement      = transform.ToIfcAxis2Placement3D(doc);
            var localPlacement = new IfcLocalPlacement(new IfcAxis2Placement(placement));

            doc.AddEntity(placement);
            return(localPlacement);
        }
        private static IfcShellBasedSurfaceModel ToIfcShellBasedSurfaceModel(this Lamina lamina, Document doc)
        {
            var plane  = lamina.Perimeter.Plane().ToIfcPlane(doc);
            var outer  = lamina.Perimeter.ToIfcCurve(doc);
            var bplane = new IfcCurveBoundedPlane(plane, outer, new List <IfcCurve> {
            });

            var bounds     = new List <IfcFaceBound> {
            };
            var loop       = lamina.Perimeter.ToIfcPolyLoop(doc);
            var faceBounds = new IfcFaceBound(loop, true);

            bounds.Add(faceBounds);

            var face      = new IfcFaceSurface(bounds, bplane, true);
            var openShell = new IfcOpenShell(new List <IfcFace> {
                face
            });

            var shell = new IfcShell(openShell);
            var ssm   = new IfcShellBasedSurfaceModel(new List <IfcShell> {
                shell
            });

            doc.AddEntity(plane);
            doc.AddEntity(outer);
            doc.AddEntity(bplane);
            doc.AddEntity(loop);
            doc.AddEntity(faceBounds);
            doc.AddEntity(face);
            doc.AddEntity(openShell);

            return(ssm);
        }
        internal static IfcExtrudedAreaSolid ToIfcExtrudedAreaSolid(this Extrude extrude, Document doc)
        {
            var position = new Transform().ToIfcAxis2Placement3D(doc);

            var extrudeDepth     = extrude.Height;
            var extrudeProfile   = extrude.Profile.Perimeter.ToIfcArbitraryClosedProfileDef(doc);
            var extrudeDirection = Vector3.ZAxis.ToIfcDirection();;

            var solid = new IfcExtrudedAreaSolid(extrudeProfile, position,
                                                 extrudeDirection, new IfcPositiveLengthMeasure(extrude.Height));

            doc.AddEntity(extrudeProfile);
            doc.AddEntity(extrudeDirection);
            doc.AddEntity(position);
            doc.AddEntity(solid);

            return(solid);
        }
        private static IfcTrimmedCurve ToIfcTrimmedCurve(this Arc arc, Document doc)
        {
            var placement = new Transform().ToIfcAxis2Placement3D(doc);
            var ifcCircle = new IfcCircle(new IfcAxis2Placement(placement), new IfcPositiveLengthMeasure(arc.Radius));
            var trim1     = new IfcTrimmingSelect(arc.StartAngle);
            var trim2     = new IfcTrimmingSelect(arc.EndAngle);
            var tc        = new IfcTrimmedCurve(ifcCircle, new List <IfcTrimmingSelect> {
                trim1
            }, new List <IfcTrimmingSelect> {
                trim2
            },
                                                true, IfcTrimmingPreference.PARAMETER);

            doc.AddEntity(placement);
            doc.AddEntity(ifcCircle);

            return(tc);
        }
        private static IfcPlane ToIfcPlane(this Plane plane, Document doc)
        {
            var t        = new Transform(plane.Origin, plane.Normal);
            var position = t.ToIfcAxis2Placement3D(doc);
            var ifcPlane = new IfcPlane(position);

            doc.AddEntity(position);

            return(ifcPlane);
        }
        private static IfcProductDefinitionShape ToIfcProductDefinitionShape(this List <IfcRepresentationItem> geoms, string shapeType, IfcRepresentationContext context, Document doc)
        {
            var rep   = new IfcShapeRepresentation(context, "Body", shapeType, geoms);
            var shape = new IfcProductDefinitionShape(new List <IfcRepresentation> {
                rep
            });

            doc.AddEntity(rep);

            return(shape);
        }
        private static List <IfcCartesianPoint> ToIfcCartesianPointList(this IList <Vector3> pts, Document doc)
        {
            var icps = new List <IfcCartesianPoint>();

            foreach (var pt in pts)
            {
                var icp = pt.ToIfcCartesianPoint();
                doc.AddEntity(icp);
                icps.Add(icp);
            }
            return(icps);
        }
        private static IfcPolyline ToIfcPolyline(this Polyline polygon, Document doc)
        {
            var points = new List <IfcCartesianPoint>();

            foreach (var v in polygon.Vertices)
            {
                var p = v.ToIfcCartesianPoint();
                doc.AddEntity(p);
                points.Add(p);
            }
            return(new IfcPolyline(points));
        }
Example #16
0
        private static IfcExtrudedAreaSolid ToIfcExtrudedAreaSolid(this IExtrude extrude, Transform transform, Document doc)
        {
            var position = new Transform().ToIfcAxis2Placement3D(doc);

            double extrudeDepth = 0.0;
            IfcArbitraryClosedProfileDef extrudeProfile = null;
            IfcDirection extrudeDirection = null;

            extrudeProfile   = extrude.Profile.Perimeter.ToIfcArbitraryClosedProfileDef(doc);
            extrudeDirection = extrude.ExtrudeDirection.ToIfcDirection();
            extrudeDepth     = extrude.ExtrudeDepth;

            var solid = new IfcExtrudedAreaSolid(extrudeProfile, position,
                                                 extrudeDirection, new IfcPositiveLengthMeasure(extrude.ExtrudeDepth));

            doc.AddEntity(extrudeProfile);
            doc.AddEntity(extrudeDirection);
            doc.AddEntity(position);
            doc.AddEntity(solid);

            return(solid);
        }
        internal static IfcLocalPlacement ToIfcLocalPlacement(this Transform transform, Document doc, IfcObjectPlacement parent = null)
        {
            var placement      = transform.ToIfcAxis2Placement3D(doc);
            var localPlacement = new IfcLocalPlacement(new IfcAxis2Placement(placement));

            if (parent != null)
            {
                localPlacement.PlacementRelTo = parent;
            }

            doc.AddEntity(placement);
            return(localPlacement);
        }
Example #18
0
        private static IfcTrimmedCurve ToIfcTrimmedCurve(this Arc arc, Document doc)
        {
            var t         = new Transform(arc.Plane.Origin, arc.Plane.Normal);
            var placement = t.ToIfcAxis2Placement3D(doc);
            var ifcCircle = new IfcCircle(new IfcAxis2Placement(placement), new IfcPositiveLengthMeasure(arc.Radius));
            var start     = arc.Start.ToIfcCartesianPoint();
            var end       = arc.End.ToIfcCartesianPoint();
            var trim1     = new IfcTrimmingSelect(start);
            var trim2     = new IfcTrimmingSelect(end);
            var tc        = new IfcTrimmedCurve(ifcCircle, new List <IfcTrimmingSelect> {
                trim1
            }, new List <IfcTrimmingSelect> {
                trim2
            },
                                                true, IfcTrimmingPreference.CARTESIAN);

            doc.AddEntity(start);
            doc.AddEntity(end);
            doc.AddEntity(placement);
            doc.AddEntity(ifcCircle);

            return(tc);
        }
        private static IfcPolyline ToIfcPolyline(this Polygon polygon, Document doc)
        {
            var points = new List <IfcCartesianPoint>();

            foreach (var v in polygon.Vertices)
            {
                var p = v.ToIfcCartesianPoint();
                doc.AddEntity(p);
                points.Add(p);
            }
            // Add the first point to close the curve.
            points.Add(points.First());
            return(new IfcPolyline(points));
        }
Example #20
0
        private static IfcProductDefinitionShape ToIfcProductDefinitionShape(this IfcGeometricRepresentationItem geom, IfcRepresentationContext context, Document doc)
        {
            var rep = new IfcShapeRepresentation(context, "Body", "SweptSolid",
                                                 new List <IfcRepresentationItem> {
                geom
            });
            var shape = new IfcProductDefinitionShape(new List <IfcRepresentation> {
                rep
            });

            doc.AddEntity(rep);

            return(shape);
        }
        private static IfcTrimmedCurve ToIfcTrimmedCurve(this Line line, Document doc)
        {
            var start = line.Start.ToIfcCartesianPoint();
            var end   = line.End.ToIfcCartesianPoint();

            var dir = line.Direction().ToIfcVector(doc);

            var ifcLine = new IfcLine(start, dir);
            var trim1   = new IfcTrimmingSelect(start);
            var trim2   = new IfcTrimmingSelect(end);
            var tc      = new IfcTrimmedCurve(ifcLine, new List <IfcTrimmingSelect> {
                trim1
            }, new List <IfcTrimmingSelect> {
                trim2
            },
                                              true, IfcTrimmingPreference.CARTESIAN);

            doc.AddEntity(start);
            doc.AddEntity(end);
            doc.AddEntity(dir);
            doc.AddEntity(ifcLine);

            return(tc);
        }
Example #22
0
        private static IfcOpeningElement ToIfcOpeningElement(this Opening opening, IfcRepresentationContext context, Document doc, IfcObjectPlacement parent)
        {
            // var sweptArea = opening.Profile.Perimeter.ToIfcArbitraryClosedProfileDef(doc);
            // We use the Z extrude direction because the direction is
            // relative to the local placement, which is a transform at the
            // beam's end with the Z axis pointing along the direction.

            // var extrudeDirection = opening.ExtrudeDirection.ToIfcDirection();
            // var position = new Transform().ToIfcAxis2Placement3D(doc);
            // var solid = new IfcExtrudedAreaSolid(sweptArea, position,
            //     extrudeDirection, new IfcPositiveLengthMeasure(opening.ExtrudeDepth));

            var solid          = opening.ToIfcExtrudedAreaSolid(new Transform(), doc);
            var localPlacement = new Transform().ToIfcLocalPlacement(doc, parent);

            var shape = new IfcShapeRepresentation(context, "Body", "SweptSolid", new List <IfcRepresentationItem> {
                solid
            });
            var productRep = new IfcProductDefinitionShape(new List <IfcRepresentation> {
                shape
            });

            var ifcOpening = new IfcOpeningElement(IfcGuid.ToIfcGuid(Guid.NewGuid()), null, null, null, null, localPlacement, productRep, null);

            // doc.AddEntity(sweptArea);
            // doc.AddEntity(extrudeDirection);
            // doc.AddEntity(position);
            // doc.AddEntity(repItem);

            doc.AddEntity(solid);
            doc.AddEntity(localPlacement);
            doc.AddEntity(shape);
            doc.AddEntity(productRep);

            return(ifcOpening);
        }
Example #23
0
        private static IfcBeam ToIfcBeam(this Beam beam, IfcRepresentationContext context, Document doc)
        {
            var sweptArea = beam.ElementType.Profile.Perimeter.ToIfcArbitraryClosedProfileDef(doc);
            var line      = beam.Curve as Line;

            if (line == null)
            {
                throw new Exception("The beam could not be exported to IFC. Only linear beams are currently supported.");
            }

            // We use the Z extrude direction because the direction is
            // relative to the local placement, which is a transform at the
            // beam's end with the Z axis pointing along the direction.

            var extrudeDirection = Vector3.ZAxis.ToIfcDirection();

            var position = new Transform().ToIfcAxis2Placement3D(doc);
            var repItem  = new IfcExtrudedAreaSolid(sweptArea, position,
                                                    extrudeDirection, new IfcPositiveLengthMeasure(beam.Curve.Length()));
            var localPlacement = beam.Curve.TransformAt(0.0).ToIfcLocalPlacement(doc);
            var rep            = new IfcShapeRepresentation(context, "Body", "SweptSolid", new List <IfcRepresentationItem> {
                repItem
            });
            var productRep = new IfcProductDefinitionShape(new List <IfcRepresentation> {
                rep
            });
            var ifcBeam = new IfcBeam(IfcGuid.ToIfcGuid(Guid.NewGuid()), null, null, null, null, localPlacement, productRep, null);

            doc.AddEntity(sweptArea);
            doc.AddEntity(extrudeDirection);
            doc.AddEntity(position);
            doc.AddEntity(repItem);
            doc.AddEntity(rep);
            doc.AddEntity(localPlacement);
            doc.AddEntity(productRep);
            doc.AddEntity(ifcBeam);

            return(ifcBeam);
        }
Example #24
0
        private static IfcSurfaceCurveSweptAreaSolid ToIfcSurfaceCurveSweptAreaSolid(this ISweepAlongCurve sweep, Transform transform, Document doc)
        {
            var position  = transform.ToIfcAxis2Placement3D(doc);
            var sweptArea = sweep.Profile.Perimeter.ToIfcArbitraryClosedProfileDef(doc);
            var directrix = sweep.Curve.ToIfcCurve(doc);

            var extrudeDir = Vector3.ZAxis.ToIfcDirection();
            var profile    = new IfcArbitraryClosedProfileDef(IfcProfileTypeEnum.CURVE, directrix);
            var surface    = new IfcSurfaceOfLinearExtrusion(profile, position, extrudeDir, new IfcPositiveLengthMeasure(1.0));

            var solid = new IfcSurfaceCurveSweptAreaSolid(sweptArea, position, directrix, sweep.StartSetback, sweep.EndSetback, surface);

            doc.AddEntity(position);
            doc.AddEntity(sweptArea);
            doc.AddEntity(directrix);

            doc.AddEntity(extrudeDir);
            doc.AddEntity(profile);

            doc.AddEntity(surface);
            doc.AddEntity(solid);

            return(solid);
        }
Example #25
0
        /// <summary>
        /// Write the model to IFC.
        /// </summary>
        /// <param name="model"></param>
        /// <param name="path">The path to the generated IFC STEP file.</param>
        public static void ToIFC(this Model model, string path)
        {
            var ifc = new Document("Elements", "Elements", Environment.UserName,
                                   null, null, null, "Elements", null, null,
                                   null, null, null, null, null, null
                                   );

            var proj = ifc.AllInstancesOfType <IfcProject>().FirstOrDefault();

            // Add a site
            var site          = new IfcSite(IfcGuid.ToIfcGuid(Guid.NewGuid()), null, IfcElementCompositionEnum.ELEMENT);
            var projAggregate = new IfcRelAggregates(IfcGuid.ToIfcGuid(Guid.NewGuid()), null, proj, new List <IfcObjectDefinition> {
                site
            });

            // Add building and building storey
            var building  = new IfcBuilding(IfcGuid.ToIfcGuid(Guid.NewGuid()), null, IfcElementCompositionEnum.ELEMENT);
            var storey    = new IfcBuildingStorey(IfcGuid.ToIfcGuid(Guid.NewGuid()), null, IfcElementCompositionEnum.ELEMENT);
            var aggregate = new IfcRelAggregates(IfcGuid.ToIfcGuid(Guid.NewGuid()), null, building, new List <IfcObjectDefinition> {
                storey
            });

            // Aggregate the building into the site
            var siteAggregate = new IfcRelAggregates(IfcGuid.ToIfcGuid(Guid.NewGuid()), null, site, new List <IfcObjectDefinition> {
                building
            });

            ifc.AddEntity(site);
            ifc.AddEntity(projAggregate);
            ifc.AddEntity(building);
            ifc.AddEntity(storey);
            ifc.AddEntity(aggregate);
            ifc.AddEntity(siteAggregate);

            var products = new List <IfcProduct>();
            var context  = ifc.AllInstancesOfType <IfcGeometricRepresentationContext>().FirstOrDefault();

            foreach (var e in model.Elements.Values)
            {
                try
                {
                    products.AddRange(e.ToIfcProducts(context, ifc));
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"There was an error writing an element of type {e.GetType()} to IFC: " + ex.Message);
                    Console.WriteLine(ex.StackTrace);
                    continue;
                }
            }

            var spatialRel = new IfcRelContainedInSpatialStructure(IfcGuid.ToIfcGuid(Guid.NewGuid()), null, products, storey);

            ifc.AddEntity(spatialRel);

            if (File.Exists(path))
            {
                File.Delete(path);
            }
            File.WriteAllText(path, ifc.ToSTEP(path));
        }
        /// <summary>
        /// Write the model to IFC.
        /// </summary>
        /// <param name="model"></param>
        /// <param name="path">The path to the generated IFC STEP file.</param>
        public static void ToIFC(this Model model, string path)
        {
            var ifc = new Document("Elements", "Elements", Environment.UserName,
                                   null, null, null, "Elements", null, null,
                                   null, null, null, null, null, null
                                   );

            var proj = ifc.AllInstancesOfType <IfcProject>().FirstOrDefault();

            // Add a site
            var site = new IfcSite(IfcGuid.ToIfcGuid(Guid.NewGuid()),
                                   null,
                                   "Hypar Site",
                                   "The default site generated by Hypar",
                                   null,
                                   null,
                                   null,
                                   null,
                                   IfcElementCompositionEnum.ELEMENT,
                                   new IfcCompoundPlaneAngleMeasure(new List <int> {
                0, 0, 0
            }),
                                   new IfcCompoundPlaneAngleMeasure(new List <int> {
                0, 0, 0
            }),
                                   0,
                                   null,
                                   null);
            var projAggregate = new IfcRelAggregates(IfcGuid.ToIfcGuid(Guid.NewGuid()), proj, new List <IfcObjectDefinition> {
                site
            });

            // Add building and building storey
            var building = new IfcBuilding(IfcGuid.ToIfcGuid(Guid.NewGuid()),
                                           null,
                                           "Default Building",
                                           "The default building generated by Hypar.",
                                           null,
                                           null,
                                           null,
                                           null,
                                           IfcElementCompositionEnum.ELEMENT,
                                           0,
                                           0,
                                           null);
            var storey = new IfcBuildingStorey(IfcGuid.ToIfcGuid(Guid.NewGuid()),
                                               null,
                                               "Default Storey",
                                               "The default storey generated by Hypar",
                                               null,
                                               null,
                                               null,
                                               null,
                                               IfcElementCompositionEnum.ELEMENT,
                                               0);
            var aggregate = new IfcRelAggregates(IfcGuid.ToIfcGuid(Guid.NewGuid()), building, new List <IfcObjectDefinition> {
                storey
            });

            // Aggregate the building into the site
            var siteAggregate = new IfcRelAggregates(IfcGuid.ToIfcGuid(Guid.NewGuid()), site, new List <IfcObjectDefinition> {
                building
            });

            ifc.AddEntity(site);
            ifc.AddEntity(projAggregate);
            ifc.AddEntity(building);
            ifc.AddEntity(storey);
            ifc.AddEntity(aggregate);
            ifc.AddEntity(siteAggregate);

            var products = new List <IfcProduct>();
            var context  = ifc.AllInstancesOfType <IfcGeometricRepresentationContext>().FirstOrDefault();

            // IfcRelAssociatesMaterial
            // IfcMaterialDefinitionRepresentation
            // https://forums.buildingsmart.org/t/where-and-how-will-my-colors-be-saved-in-ifc/1806/12
            var styleAssignments = new Dictionary <Guid, List <IfcStyleAssignmentSelect> >();

            var white = Colors.White.ToIfcColourRgb();

            ifc.AddEntity(white);

            // TODO: Fix color support in all applications.
            // https://forums.buildingsmart.org/t/why-is-it-so-difficult-to-get-colors-to-show-up/2312/12
            foreach (var m in model.AllElementsOfType <Material>())
            {
                var material = new IfcMaterial(m.Name, null, "Hypar");
                ifc.AddEntity(material);

                var color = m.Color.ToIfcColourRgb();
                ifc.AddEntity(color);

                var transparency = new IfcNormalisedRatioMeasure(1.0 - m.Color.Alpha);

                var shading = new IfcSurfaceStyleShading(color, transparency);
                ifc.AddEntity(shading);

                var styles = new List <IfcSurfaceStyleElementSelect> {
                    new IfcSurfaceStyleElementSelect(shading),
                };
                var surfaceStyle = new IfcSurfaceStyle(material.Name, IfcSurfaceSide.BOTH, styles);
                ifc.AddEntity(surfaceStyle);

                var styleAssign = new IfcStyleAssignmentSelect(surfaceStyle);
                var assignments = new List <IfcStyleAssignmentSelect>()
                {
                    styleAssign
                };
                styleAssignments.Add(m.Id, assignments);
            }


            foreach (var e in model.Elements.Values.Where(e =>
            {
                var t = e.GetType();
                return(((e is GeometricElement &&
                         !((GeometricElement)e).IsElementDefinition) || e is ElementInstance) &&
                       t != typeof(ModelCurve) &&
                       t != typeof(ModelPoints) &&
                       t != typeof(AnalysisMesh));
            }))
            {
                try
                {
                    products.AddRange(e.ToIfcProducts(context, ifc, styleAssignments));
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"There was an error writing an element of type {e.GetType()} to IFC: " + ex.Message);
                    Console.WriteLine(ex.StackTrace);
                    continue;
                }
            }

            var spatialRel = new IfcRelContainedInSpatialStructure(IfcGuid.ToIfcGuid(Guid.NewGuid()), products, storey);

            ifc.AddEntity(spatialRel);

            if (File.Exists(path))
            {
                File.Delete(path);
            }
            File.WriteAllText(path, ifc.ToSTEP(path));
        }
Example #27
0
        /// <summary>
        /// Write the model to IFC.
        /// </summary>
        /// <param name="model"></param>
        /// <param name="path">The path to the generated IFC STEP file.</param>
        public static void ToIFC(this Model model, string path)
        {
            var ifc = new Document("elements", "elements", Environment.UserName,
                                   null, null, null, "elements", null, null,
                                   null, null, null, null, null, null
                                   );

            var proj = ifc.AllInstancesOfType <IfcProject>().FirstOrDefault();

            // Add a site
            var site          = new IfcSite(IfcGuid.ToIfcGuid(Guid.NewGuid()), null, IfcElementCompositionEnum.ELEMENT);
            var projAggregate = new IfcRelAggregates(IfcGuid.ToIfcGuid(Guid.NewGuid()), null, proj, new List <IfcObjectDefinition> {
                site
            });

            // Add building and building storey
            var building  = new IfcBuilding(IfcGuid.ToIfcGuid(Guid.NewGuid()), null, IfcElementCompositionEnum.ELEMENT);
            var storey    = new IfcBuildingStorey(IfcGuid.ToIfcGuid(Guid.NewGuid()), null, IfcElementCompositionEnum.ELEMENT);
            var aggregate = new IfcRelAggregates(IfcGuid.ToIfcGuid(Guid.NewGuid()), null, building, new List <IfcObjectDefinition> {
                storey
            });

            // Aggregate the building into the site
            var siteAggregate = new IfcRelAggregates(IfcGuid.ToIfcGuid(Guid.NewGuid()), null, site, new List <IfcObjectDefinition> {
                building
            });

            ifc.AddEntity(site);
            ifc.AddEntity(projAggregate);
            ifc.AddEntity(building);
            ifc.AddEntity(storey);
            ifc.AddEntity(aggregate);
            ifc.AddEntity(siteAggregate);

            // Materials
            // foreach(var m in model.Materials.Values)
            // {
            //     var ifcMaterial = new IfcMaterial(m.Name);
            //     ifc.AddEntity(ifcMaterial);
            // }

            var products = new List <IfcProduct>();
            var context  = ifc.AllInstancesOfType <IfcGeometricRepresentationContext>().FirstOrDefault();

            foreach (var e in model.Elements.Values)
            {
                try
                {
                    if (e is Wall)
                    {
                        var w       = (Wall)e;
                        var ifcWall = w.ToIfcWallStandardCase(context, ifc);
                        products.Add(ifcWall);
                    }

                    if (e is Beam)
                    {
                        var b       = (Beam)e;
                        var ifcBeam = b.ToIfcBeam(context, ifc);
                        products.Add(ifcBeam);
                    }
                }
                catch
                {
                    continue;
                }
            }

            var spatialRel = new IfcRelContainedInSpatialStructure(IfcGuid.ToIfcGuid(Guid.NewGuid()), null, products, storey);

            ifc.AddEntity(spatialRel);

            if (File.Exists(path))
            {
                File.Delete(path);
            }
            File.WriteAllText(path, ifc.ToSTEP(path));
        }
Example #28
0
        private static List <IfcProduct> ToIfcProducts(this Element e, IfcRepresentationContext context, Document doc)
        {
            var products = new List <IfcProduct>();

            if (e is IAggregateElements)
            {
                // TODO: Create the IFC aggregation relationship
                foreach (var subEl in ((IAggregateElements)e).Elements)
                {
                    products.AddRange(subEl.ToIfcProducts(context, doc));
                }

                return(products);
            }

            IfcProductDefinitionShape shape = null;
            var localPlacement = e.Transform.ToIfcLocalPlacement(doc);
            IfcGeometricRepresentationItem geom = null;

            if (e is ISweepAlongCurve)
            {
                var sweep = (ISweepAlongCurve)e;
                geom = sweep.ToIfcSurfaceCurveSweptAreaSolid(e.Transform, doc);
            }
            else if (e is IExtrude)
            {
                var extrude = (IExtrude)e;
                geom = extrude.ToIfcExtrudedAreaSolid(e.Transform, doc);
            }
            else if (e is ILamina)
            {
                var lamina = (ILamina)e;
                geom = lamina.ToIfcShellBasedSurfaceModel(e.Transform, doc);
            }
            else
            {
                throw new Exception("Only IExtrude, ISweepAlongCurve, and ILamina representations are currently supported.");
            }

            shape = ToIfcProductDefinitionShape(geom, context, doc);

            doc.AddEntity(shape);
            doc.AddEntity(localPlacement);
            doc.AddEntity(geom);

            var product = ConvertElementToIfcProduct(e, localPlacement, shape);

            products.Add(product);
            doc.AddEntity(product);

            // If the element has openings,
            // Make opening relationships in
            // the IfcElement.
            if (e is IHasOpenings)
            {
                var openings = (IHasOpenings)e;
                if (openings.Openings.Count > 0)
                {
                    foreach (var o in openings.Openings)
                    {
                        var element = (IfcElement)product;
                        var opening = o.ToIfcOpeningElement(context, doc, localPlacement);
                        var voidRel = new IfcRelVoidsElement(IfcGuid.ToIfcGuid(Guid.NewGuid()), null, element, opening);
                        element.HasOpenings.Add(voidRel);
                        doc.AddEntity(opening);
                        doc.AddEntity(voidRel);
                    }
                }
            }

            IfcStyledItem style = null;

            if (e is IMaterial)
            {
                var m = (IMaterial)e;
                style = m.Material.ToIfcStyledItem(geom, doc);
            }
            if (e is IElementType <StructuralFramingType> )
            {
                var m = (IElementType <StructuralFramingType>)e;
                style = m.ElementType.Material.ToIfcStyledItem(geom, doc);
            }
            else if (e is IElementType <WallType> )
            {
                var m = (IElementType <WallType>)e;
                style = m.ElementType.MaterialLayers[0].Material.ToIfcStyledItem(geom, doc);
            }
            else if (e is IElementType <FloorType> )
            {
                var m = (IElementType <FloorType>)e;
                style = m.ElementType.MaterialLayers[0].Material.ToIfcStyledItem(geom, doc);
            }

            // Associate the style with the element
            style.Item = geom;

            geom.StyledByItem = new List <IfcStyledItem> {
                style
            };
            doc.AddEntity(style);

            return(products);
        }
        internal static List <IfcProduct> ToIfcProducts(this Element e,
                                                        IfcRepresentationContext context,
                                                        Document doc,
                                                        Dictionary <Guid, List <IfcStyleAssignmentSelect> > styleAssignments)
        {
            var products = new List <IfcProduct>();

            IfcProductDefinitionShape shape      = null;
            GeometricElement          geoElement = null;
            Transform trans = null;
            Guid      id    = default(Guid);

            if (e is ElementInstance)
            {
                // If we're using an element instance, get the transform
                // and the id and use those to uniquely position and
                // identify the element.
                var instance = (ElementInstance)e;
                geoElement = instance.BaseDefinition;
                id         = instance.Id;
                trans      = instance.Transform;
            }
            else if (e is GeometricElement)
            {
                // If we've go a geometric element, use its properties as-is.
                geoElement = (GeometricElement)e;
                id         = geoElement.Id;
                trans      = geoElement.Transform;
            }

            geoElement.UpdateRepresentations();

            var localPlacement = trans.ToIfcLocalPlacement(doc);

            doc.AddEntity(localPlacement);

            var geoms = new List <IfcRepresentationItem>();

            if (geoElement is MeshElement)
            {
                var meshEl  = (MeshElement)geoElement;
                var lengths = meshEl.Mesh.Vertices.Select(v => v.Position.ToArray().Select(vi => new IfcLengthMeasure(vi)).ToList()).ToList();
                var pts     = new IfcCartesianPointList3D(lengths);
                doc.AddEntity(pts);
                var indices = meshEl.Mesh.Triangles.Select(t => t.Vertices.Select(vx => new IfcPositiveInteger(vx.Index + 1)).ToList()).ToList();
                var idxs    = new List <List <IfcPositiveInteger> >(indices);
                var geom    = new IfcTriangulatedFaceSet(pts, indices);
                geom.Closed = false;
                doc.AddEntity(geom);
                geoms.Add(geom);
                shape = ToIfcProductDefinitionShape(geoms, "Tessellation", context, doc);
            }
            else
            {
                foreach (var op in geoElement.Representation.SolidOperations)
                {
                    if (op is Sweep)
                    {
                        var sweep = (Sweep)op;

                        // Neither of these entities, which are part of the
                        // IFC4 specification, and which would allow a sweep
                        // along a curve, are supported by many applications
                        // which are supposedly IFC4 compliant (Revit). For
                        // Those applications where these entities appear,
                        // the rotation of the profile is often wrong or
                        // inconsistent.
                        // geom = sweep.ToIfcSurfaceCurveSweptAreaSolid(doc);
                        // geom = sweep.ToIfcFixedReferenceSweptAreaSolid(geoElement.Transform, doc);

                        // Instead, we'll divide the curve and create a set of
                        // linear extrusions instead.
                        Polyline pline;
                        if (sweep.Curve is Line)
                        {
                            pline = sweep.Curve.ToPolyline(1);
                        }
                        else
                        {
                            pline = sweep.Curve.ToPolyline();
                        }
                        foreach (var segment in pline.Segments())
                        {
                            var position         = segment.TransformAt(0.0).ToIfcAxis2Placement3D(doc);
                            var extrudeDepth     = segment.Length();
                            var extrudeProfile   = sweep.Profile.Perimeter.ToIfcArbitraryClosedProfileDef(doc);
                            var extrudeDirection = Vector3.ZAxis.Negate().ToIfcDirection();
                            var geom             = new IfcExtrudedAreaSolid(extrudeProfile, position,
                                                                            extrudeDirection, new IfcPositiveLengthMeasure(extrudeDepth));

                            doc.AddEntity(extrudeProfile);
                            doc.AddEntity(extrudeDirection);
                            doc.AddEntity(position);
                            doc.AddEntity(geom);
                            geoms.Add(geom);
                        }
                    }
                    else if (op is Extrude)
                    {
                        var extrude = (Extrude)op;
                        var geom    = extrude.ToIfcExtrudedAreaSolid(doc);
                        doc.AddEntity(geom);
                        geoms.Add(geom);
                    }
                    else if (op is Lamina)
                    {
                        var lamina = (Lamina)op;
                        var geom   = lamina.ToIfcShellBasedSurfaceModel(doc);
                        doc.AddEntity(geom);
                        geoms.Add(geom);
                    }
                    else
                    {
                        throw new Exception("Only IExtrude, ISweepAlongCurve, and ILamina representations are currently supported.");
                    }
                }
                shape = ToIfcProductDefinitionShape(geoms, "SolidModel", context, doc);
            }
            doc.AddEntity(shape);


            // Can we use IfcMappedItem?
            // https://forums.buildingsmart.org/t/can-tessellation-typed-representation-hold-items-from-another-group/1621
            // var rep = new IfcShapeRepresentation(context, "Body", "Solids", geoms);
            // doc.AddEntity(rep);
            // var axisPt = Vector3.Origin.ToIfcCartesianPoint();
            // doc.AddEntity(axisPt);
            // var axis = new IfcAxis2Placement2D(axisPt);
            // doc.AddEntity(axis);
            // var repMap = new IfcRepresentationMap(new IfcAxis2Placement(axis), rep);
            // doc.AddEntity(repMap);
            // var x = trans.XAxis.ToIfcDirection();
            // var y = trans.YAxis.ToIfcDirection();
            // var z = trans.ZAxis.ToIfcDirection();
            // var origin = trans.Origin.ToIfcCartesianPoint();
            // var cart = new IfcCartesianTransformationOperator3D(x, y, origin, trans.XAxis.Length(), z);
            // doc.AddEntity(x);
            // doc.AddEntity(y);
            // doc.AddEntity(z);
            // doc.AddEntity(origin);
            // doc.AddEntity(cart);
            // var mappedItem = new IfcMappedItem(repMap, cart);
            // doc.AddEntity(mappedItem);
            // var shapeRep= new IfcShapeRepresentation(context, new List<IfcRepresentationItem>(){mappedItem});
            // doc.AddEntity(shapeRep);
            // shape = new IfcProductDefinitionShape(new List<IfcRepresentation>(){shapeRep});
            // doc.AddEntity(shape);

            var product = ConvertElementToIfcProduct(id, geoElement, localPlacement, shape);

            products.Add(product);
            doc.AddEntity(product);

            var ifcOpenings = doc.AllEntities.Where(ent => ent.GetType() == typeof(IfcOpeningElement)).Cast <IfcOpeningElement>();

            // If the element has openings, make opening relationships in
            // the IfcElement.
            if (e is IHasOpenings)
            {
                var openings = (IHasOpenings)e;
                if (openings.Openings.Count > 0)
                {
                    foreach (var o in openings.Openings)
                    {
                        var element = (IfcElement)product;
                        // TODO: Find the opening that we've already created that relates here
                        var opening = ifcOpenings.First(ifcO => ifcO.GlobalId == IfcGuid.ToIfcGuid(o.Id));
                        var voidRel = new IfcRelVoidsElement(IfcGuid.ToIfcGuid(Guid.NewGuid()), element, opening);
                        element.HasOpenings.Add(voidRel);
                        doc.AddEntity(voidRel);
                    }
                }
            }

            foreach (var geom in geoms)
            {
                var styledItem = new IfcStyledItem(geom, styleAssignments[geoElement.Material.Id], null);
                doc.AddEntity(styledItem);
            }

            return(products);
        }