|
| 1 | +/******************************************************************************** |
| 2 | + * * |
| 3 | + * This file is part of IfcOpenShell. * |
| 4 | + * * |
| 5 | + * IfcOpenShell is free software: you can redistribute it and/or modify * |
| 6 | + * it under the terms of the Lesser GNU General Public License as published by * |
| 7 | + * the Free Software Foundation, either version 3.0 of the License, or * |
| 8 | + * (at your option) any later version. * |
| 9 | + * * |
| 10 | + * IfcOpenShell is distributed in the hope that it will be useful, * |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | + * Lesser GNU General Public License for more details. * |
| 14 | + * * |
| 15 | + * You should have received a copy of the Lesser GNU General Public License * |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 17 | + * * |
| 18 | + ********************************************************************************/ |
| 19 | + |
| 20 | +#include <string> |
| 21 | +#include <iostream> |
| 22 | +#include <fstream> |
| 23 | + |
| 24 | +#include <TColgp_Array2OfPnt.hxx> |
| 25 | +#include <TColgp_Array1OfPnt.hxx> |
| 26 | +#include <TColStd_Array1OfReal.hxx> |
| 27 | +#include <TColStd_Array1OfInteger.hxx> |
| 28 | + |
| 29 | +#include <Geom_BSplineSurface.hxx> |
| 30 | + |
| 31 | +#include <BRepBuilderAPI_MakeFace.hxx> |
| 32 | +#include <BRepBuilderAPI_NurbsConvert.hxx> |
| 33 | + |
| 34 | +#include <BRepPrimAPI_MakeBox.hxx> |
| 35 | +#include <BRepPrimAPI_MakeSphere.hxx> |
| 36 | + |
| 37 | +#include <BRepAlgoAPI_Cut.hxx> |
| 38 | + |
| 39 | +#include <Standard_Version.hxx> |
| 40 | + |
| 41 | +#ifdef USE_IFC4 |
| 42 | +#include "../ifcparse/Ifc4.h" |
| 43 | +#else |
| 44 | +#include "../ifcparse/Ifc2x3.h" |
| 45 | +#endif |
| 46 | + |
| 47 | +#include "../ifcparse/IfcUtil.h" |
| 48 | +#include "../ifcparse/IfcHierarchyHelper.h" |
| 49 | +#include "../ifcgeom/IfcGeom.h" |
| 50 | + |
| 51 | +#if USE_VLD |
| 52 | +#include <vld.h> |
| 53 | +#endif |
| 54 | + |
| 55 | +// Some convenience typedefs and definitions. |
| 56 | +typedef std::string S; |
| 57 | +typedef IfcParse::IfcGlobalId guid; |
| 58 | +typedef std::pair<double, double> XY; |
| 59 | +boost::none_t const null = boost::none; |
| 60 | + |
| 61 | +// The creation of Nurbs-surface for the IfcSite mesh, to be implemented lateron |
| 62 | +void createGroundShape(TopoDS_Shape& shape); |
| 63 | + |
| 64 | +int main() { |
| 65 | + |
| 66 | + // The IfcHierarchyHelper is a subclass of the regular IfcFile that provides several |
| 67 | + // convenience functions for working with geometry in IFC files. |
| 68 | + IfcHierarchyHelper file; |
| 69 | + file.header().file_name().name("IfcAdvancedHouse.ifc"); |
| 70 | + |
| 71 | + IfcSchema::IfcBuilding* building = file.addBuilding(); |
| 72 | + // By adding a building, a hierarchy has been automatically created that consists of the following |
| 73 | + // structure: IfcProject > IfcSite > IfcBuilding |
| 74 | + |
| 75 | + // Lateron changing the name of the IfcProject can be done by obtaining a reference to the |
| 76 | + // project, which has been created automatically. |
| 77 | + file.getSingle<IfcSchema::IfcProject>()->setName("IfcOpenHouse"); |
| 78 | + |
| 79 | + // To demonstrate the ability to serialize arbitrary opencascade solids a building envelope is |
| 80 | + // constructed by applying boolean operations. Naturally, in IFC, building elements should be |
| 81 | + // modeled separately, with rich parametric and relational semantics. Creating geometry in this |
| 82 | + // way does not preserve any history and is merely a demonstration of technical capabilities. |
| 83 | + TopoDS_Shape outer = BRepPrimAPI_MakeBox(gp_Pnt(-5000., -180., -2000.), gp_Pnt(5000., 5180., 3000.)).Shape(); |
| 84 | + TopoDS_Shape inner = BRepPrimAPI_MakeBox(gp_Pnt(-4640., 180., 0.), gp_Pnt(4640., 4820., 3000.)).Shape(); |
| 85 | + TopoDS_Shape window1 = BRepPrimAPI_MakeBox(gp_Pnt(-5000., -180., 400.), gp_Pnt( 500., 1180., 2000.)).Shape(); |
| 86 | + TopoDS_Shape window2 = BRepPrimAPI_MakeBox(gp_Pnt( 2070., -180., 400.), gp_Pnt(3930., 180., 2000.)).Shape(); |
| 87 | + |
| 88 | + TopoDS_Shape building_shell = BRepAlgoAPI_Cut( |
| 89 | + BRepAlgoAPI_Cut( |
| 90 | + BRepAlgoAPI_Cut(outer, inner), |
| 91 | + window1 |
| 92 | + ), |
| 93 | + window2 |
| 94 | + ); |
| 95 | + |
| 96 | + // Since the solid consists only of planar faces and straight edges it can be serialized as an |
| 97 | + // IfcFacetedBRep. If it would not be a polyhedron, serialise() can only be successful when linked |
| 98 | + // to the IFC4 model and with `advanced` set to `true` which introduces IfcAdvancedFace. It would |
| 99 | + // return `0` otherwise. |
| 100 | + IfcSchema::IfcProductDefinitionShape* building_shape = IfcGeom::serialise(building_shell, false); |
| 101 | + |
| 102 | + file.addEntity(building_shape); |
| 103 | + IfcSchema::IfcRepresentation* rep = *building_shape->Representations()->begin(); |
| 104 | + rep->setContextOfItems(file.getRepresentationContext("model")); |
| 105 | + |
| 106 | + building->setRepresentation(building_shape); |
| 107 | + |
| 108 | + // A pale white colour is assigned to the building. |
| 109 | + file.setSurfaceColour( |
| 110 | + building_shape, 0.75, 0.73, 0.68); |
| 111 | + |
| 112 | + // For the ground mesh of the IfcSite we will use a Nurbs surface created in Open Cascade. Only |
| 113 | + // in IFC4 the surface can be directly serialized. In IFC2X3 the it will have to be tesselated. |
| 114 | + TopoDS_Shape shape; |
| 115 | + createGroundShape(shape); |
| 116 | + |
| 117 | + IfcSchema::IfcProductDefinitionShape* ground_representation = IfcGeom::serialise(shape, true); |
| 118 | + if (!ground_representation) { |
| 119 | + ground_representation = IfcGeom::tesselate(shape, 100.); |
| 120 | + } |
| 121 | + file.getSingle<IfcSchema::IfcSite>()->setRepresentation(ground_representation); |
| 122 | + |
| 123 | + IfcSchema::IfcRepresentation::list::ptr ground_reps = file.getSingle<IfcSchema::IfcSite>()->Representation()->Representations(); |
| 124 | + for (IfcSchema::IfcRepresentation::list::it it = ground_reps->begin(); it != ground_reps->end(); ++it) { |
| 125 | + (*it)->setContextOfItems(file.getRepresentationContext("Model")); |
| 126 | + } |
| 127 | + file.addEntity(ground_representation); |
| 128 | + file.setSurfaceColour(ground_representation, 0.15, 0.25, 0.05); |
| 129 | + |
| 130 | + /* |
| 131 | + // Note that IFC lacks elementary surfaces that STEP does have, such as spherical_surface. |
| 132 | + // BRepBuilderAPI_NurbsConvert can be used to serialize such surfaces as nurbs surfaces. |
| 133 | + TopoDS_Shape sphere = BRepPrimAPI_MakeSphere(gp_Pnt(), 1000.).Shape(); |
| 134 | + IfcSchema::IfcProductDefinitionShape* sphere_representation = IfcGeom::serialise(sphere, true); |
| 135 | + if (S(IfcSchema::Identifier) == "IFC4") { |
| 136 | + sphere = BRepBuilderAPI_NurbsConvert(sphere, true).Shape(); |
| 137 | + sphere_representation = IfcGeom::serialise(sphere, true); |
| 138 | + } |
| 139 | + */ |
| 140 | + |
| 141 | + // Finally create a file stream for our output and write the IFC file to it. |
| 142 | + std::ofstream f("IfcAdvancedHouse.ifc"); |
| 143 | + f << file; |
| 144 | +} |
| 145 | + |
| 146 | +void createGroundShape(TopoDS_Shape& shape) { |
| 147 | + TColgp_Array2OfPnt cv (0, 4, 0, 4); |
| 148 | + cv.SetValue(0, 0, gp_Pnt(-10000, -10000, -4130)); |
| 149 | + cv.SetValue(0, 1, gp_Pnt(-10000, -4330, -4130)); |
| 150 | + cv.SetValue(0, 2, gp_Pnt(-10000, 0, -5130)); |
| 151 | + cv.SetValue(0, 3, gp_Pnt(-10000, 4330, -7130)); |
| 152 | + cv.SetValue(0, 4, gp_Pnt(-10000, 10000, -7130)); |
| 153 | + cv.SetValue(1, 0, gp_Pnt( -3330, -10000, -5130)); |
| 154 | + cv.SetValue(1, 1, gp_Pnt( -7670, -3670, 5000)); |
| 155 | + cv.SetValue(1, 2, gp_Pnt( -9000, 0, 1000)); |
| 156 | + cv.SetValue(1, 3, gp_Pnt( -7670, 7670, 6000)); |
| 157 | + cv.SetValue(1, 4, gp_Pnt( -3330, 10000, -4130)); |
| 158 | + cv.SetValue(2, 0, gp_Pnt( 0, -10000, -5530)); |
| 159 | + cv.SetValue(2, 1, gp_Pnt( 0, -3670, 3000)); |
| 160 | + cv.SetValue(2, 2, gp_Pnt( 0, 0, -12000)); |
| 161 | + cv.SetValue(2, 3, gp_Pnt( 0, 7670, 1500)); |
| 162 | + cv.SetValue(2, 4, gp_Pnt( 0, 10000, -4130)); |
| 163 | + cv.SetValue(3, 0, gp_Pnt( 3330, -10000, -6130)); |
| 164 | + cv.SetValue(3, 1, gp_Pnt( 7670, -3670, 6000)); |
| 165 | + cv.SetValue(3, 2, gp_Pnt( 9000, 0, 5000)); |
| 166 | + cv.SetValue(3, 3, gp_Pnt( 7670, 9000, 7000)); |
| 167 | + cv.SetValue(3, 4, gp_Pnt( 3330, 10000, -4130)); |
| 168 | + cv.SetValue(4, 0, gp_Pnt( 10000, -10000, -6130)); |
| 169 | + cv.SetValue(4, 1, gp_Pnt( 10000, -4330, -5130)); |
| 170 | + cv.SetValue(4, 2, gp_Pnt( 10000, 0, -4130)); |
| 171 | + cv.SetValue(4, 3, gp_Pnt( 10000, 4330, -4130)); |
| 172 | + cv.SetValue(4, 4, gp_Pnt( 10000, 10000, -8130)); |
| 173 | + TColStd_Array1OfReal knots(0, 1); |
| 174 | + knots(0) = 0; |
| 175 | + knots(1) = 1; |
| 176 | + TColStd_Array1OfInteger mult(0, 1); |
| 177 | + mult(0) = 5; |
| 178 | + mult(1) = 5; |
| 179 | + Handle(Geom_BSplineSurface) surf = new Geom_BSplineSurface(cv, knots, knots, mult, mult, 4, 4); |
| 180 | +#if OCC_VERSION_HEX < 0x60502 |
| 181 | + shape = BRepBuilderAPI_MakeFace(surf); |
| 182 | +#else |
| 183 | + shape = BRepBuilderAPI_MakeFace(surf, Precision::Confusion()); |
| 184 | +#endif |
| 185 | +} |
0 commit comments