|
| 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 | +/******************************************************************************** |
| 21 | + * * |
| 22 | + * Example that generates a Constructive Solid Geometry example * |
| 23 | + * * |
| 24 | + ********************************************************************************/ |
| 25 | + |
| 26 | +#include <string> |
| 27 | +#include <iostream> |
| 28 | +#include <fstream> |
| 29 | + |
| 30 | +#include "../ifcparse/Ifc2x3.h" |
| 31 | +#include "../ifcparse/IfcUtil.h" |
| 32 | +#include "../ifcparse/IfcHierarchyHelper.h" |
| 33 | + |
| 34 | +typedef std::string S; |
| 35 | +typedef IfcWrite::IfcGuidHelper guid; |
| 36 | +boost::none_t const null = (static_cast<boost::none_t>(0)); |
| 37 | + |
| 38 | +class Node { |
| 39 | +private: |
| 40 | + typedef enum { |
| 41 | + OP_ADD, OP_SUBTRACT, OP_INTERSECT, OP_TERMINAL |
| 42 | + } Op; |
| 43 | + typedef enum { |
| 44 | + PRIM_BOX, PRIM_CONE, PRIM_CYLINDER, PRIM_PYRAMID, PRIM_SPHERE |
| 45 | + } Prim; |
| 46 | + |
| 47 | + double x,y,z, zx,zy,zz, xx,xy,xz, a,b,c; |
| 48 | + const Node *left, *right; |
| 49 | + |
| 50 | + Op op; |
| 51 | + Prim prim; |
| 52 | + |
| 53 | + Node& operate(Op op, const Node& p) { |
| 54 | + left = new Node(*this); |
| 55 | + right = new Node(p); |
| 56 | + this->op = op; |
| 57 | + return *this; |
| 58 | + } |
| 59 | + |
| 60 | + Node(Prim p, double la, double lb=0., double lc=0.) |
| 61 | + : prim(p), op(OP_TERMINAL), |
| 62 | + x(0.), y(0.), z(0.), |
| 63 | + zx(0.), zy(0.), zz(1.), |
| 64 | + xx(1.), xy(0.), xz(0.), |
| 65 | + a(la), b(lb), c(lc) {} |
| 66 | +public: |
| 67 | + static Node Sphere(double r) { |
| 68 | + return Node(PRIM_SPHERE, r); |
| 69 | + } |
| 70 | + static Node Box(double dx, double dy, double dz) { |
| 71 | + return Node(PRIM_BOX, dx, dy, dz); |
| 72 | + } |
| 73 | + static Node Pyramid(double dx, double dy, double dz) { |
| 74 | + return Node(PRIM_PYRAMID, dx, dy, dz); |
| 75 | + } |
| 76 | + static Node Cylinder(double r, double h) { |
| 77 | + return Node(PRIM_CYLINDER, r, h); |
| 78 | + } |
| 79 | + static Node Cone(double r, double h) { |
| 80 | + return Node(PRIM_CONE, r, h); |
| 81 | + } |
| 82 | + |
| 83 | + Node& move( |
| 84 | + double px = 0., double py = 0., double pz = 0., |
| 85 | + double zx = 0., double zy = 0., double zz = 1., |
| 86 | + double xx = 1., double xy = 0., double xz = 0.) |
| 87 | + { |
| 88 | + this->x = px; this->y = py; this->z = pz; |
| 89 | + this->zx = zx; this->zy = zy; this->zz = zz; |
| 90 | + this->xx = xx; this->xy = xy; this->xz = xz; |
| 91 | + return *this; |
| 92 | + } |
| 93 | + |
| 94 | + Node& add(const Node& p) { |
| 95 | + return operate(OP_ADD, p); |
| 96 | + } |
| 97 | + Node& subtract(const Node& p) { |
| 98 | + return operate(OP_SUBTRACT, p); |
| 99 | + } |
| 100 | + Node& intersect(const Node& p) { |
| 101 | + return operate(OP_INTERSECT, p); |
| 102 | + } |
| 103 | + |
| 104 | + IfcSchema::IfcRepresentationItem* serialize(IfcHierarchyHelper& file) const { |
| 105 | + IfcSchema::IfcRepresentationItem* my; |
| 106 | + if (op == OP_TERMINAL) { |
| 107 | + IfcSchema::IfcAxis2Placement3D* place = file.addPlacement3d(x,y,z,zx,zy,zz,xx,xy,xz); |
| 108 | + if (prim == PRIM_SPHERE) { |
| 109 | + my = new IfcSchema::IfcSphere(place, a); |
| 110 | + } else if (prim == PRIM_BOX) { |
| 111 | + my = new IfcSchema::IfcBlock(place, a, b, c); |
| 112 | + } else if (prim == PRIM_PYRAMID) { |
| 113 | + my = new IfcSchema::IfcRectangularPyramid(place, a, b, c); |
| 114 | + } else if (prim == PRIM_CYLINDER) { |
| 115 | + my = new IfcSchema::IfcRightCircularCylinder(place, b, a); |
| 116 | + } else if (prim == PRIM_CONE) { |
| 117 | + my = new IfcSchema::IfcRightCircularCone(place, b, a); |
| 118 | + } |
| 119 | + } else { |
| 120 | + IfcSchema::IfcBooleanOperator::IfcBooleanOperator o; |
| 121 | + if (op == OP_ADD) { |
| 122 | + o = IfcSchema::IfcBooleanOperator::IfcBooleanOperator_UNION; |
| 123 | + } else if (op == OP_SUBTRACT) { |
| 124 | + o = IfcSchema::IfcBooleanOperator::IfcBooleanOperator_DIFFERENCE; |
| 125 | + } else if (op == OP_INTERSECT) { |
| 126 | + o = IfcSchema::IfcBooleanOperator::IfcBooleanOperator_INTERSECTION; |
| 127 | + } |
| 128 | + my = new IfcSchema::IfcBooleanResult(o, left->serialize(file), right->serialize(file)); |
| 129 | + } |
| 130 | + file.AddEntity(my); |
| 131 | + return my; |
| 132 | + } |
| 133 | +}; |
| 134 | + |
| 135 | +int main(int argc, char** argv) { |
| 136 | + const char filename[] = "IfcCsgPrimitive.ifc"; |
| 137 | + IfcHierarchyHelper file; |
| 138 | + file.filename(filename); |
| 139 | + |
| 140 | + IfcSchema::IfcRepresentationItem* csg1 = Node::Box(8000.,6000.,3000.).subtract( |
| 141 | + Node::Box(7600.,5600.,2800.).move(200.,200.,200.) |
| 142 | + ).add( |
| 143 | + Node::Pyramid(8000.,6000.,3000.).move(0,0,3000.).add( |
| 144 | + Node::Cylinder(1000.,4000.).move(4000.,1000.,4000., 0.,1.,0.) |
| 145 | + ).subtract( |
| 146 | + Node::Pyramid(7600.,5600.,2800.).move(200.,200.,3000.) |
| 147 | + ).subtract( |
| 148 | + Node::Cylinder(900.,4000.).move(4000.,1000.,4000., 0.,1.,0.).intersect( |
| 149 | + Node::Box(2000.,4000.,1000.).move(3000.,1000.,4000.) |
| 150 | + ) |
| 151 | + ) |
| 152 | + ).serialize(file); |
| 153 | + |
| 154 | + const double x = 1000.; const double y = -4000.; |
| 155 | + |
| 156 | + IfcSchema::IfcRepresentationItem* csg2 = Node::Sphere(5000.).move(x,y,-4500.).intersect( |
| 157 | + Node::Box(6000., 6000., 6000.).move(x-3000., y-3000., 0.) |
| 158 | + ).add( |
| 159 | + Node::Cone(500., 3000.).move(x,y).add( |
| 160 | + Node::Cone(1500., 1000.).move(x,y, 900.).add( |
| 161 | + Node::Cone(1100., 1000.).move(x,y, 1800.).add( |
| 162 | + Node::Cone(750., 600.).move(x,y, 2700.) |
| 163 | + )))).serialize(file); |
| 164 | + |
| 165 | + IfcSchema::IfcBuildingElementProxy* product = new IfcSchema::IfcBuildingElementProxy( |
| 166 | + guid(), 0, S("IfcCsgPrimitive"), null, null, 0, 0, null, null); |
| 167 | + |
| 168 | + file.addBuildingProduct(product); |
| 169 | + |
| 170 | + product->setOwnerHistory(file.getSingle<IfcSchema::IfcOwnerHistory>()); |
| 171 | + |
| 172 | + product->setObjectPlacement(file.addLocalPlacement()); |
| 173 | + |
| 174 | + IfcSchema::IfcRepresentation::list reps (new IfcTemplatedEntityList<IfcSchema::IfcRepresentation>()); |
| 175 | + IfcSchema::IfcRepresentationItem::list items (new IfcTemplatedEntityList<IfcSchema::IfcRepresentationItem>()); |
| 176 | + |
| 177 | + items->push(csg1); |
| 178 | + items->push(csg2); |
| 179 | + IfcSchema::IfcShapeRepresentation* rep = new IfcSchema::IfcShapeRepresentation( |
| 180 | + file.getSingle<IfcSchema::IfcRepresentationContext>(), S("Body"), S("CSG"), items); |
| 181 | + reps->push(rep); |
| 182 | + |
| 183 | + IfcSchema::IfcProductDefinitionShape* shape = new IfcSchema::IfcProductDefinitionShape(0, 0, reps); |
| 184 | + file.AddEntity(rep); |
| 185 | + file.AddEntity(shape); |
| 186 | + |
| 187 | + product->setRepresentation(shape); |
| 188 | + |
| 189 | + file.getSingle<IfcSchema::IfcProject>()->setName("IfcCompositeProfileDef"); |
| 190 | + |
| 191 | + std::ofstream f(filename); |
| 192 | + f << file; |
| 193 | +} |
0 commit comments