|
45 | 45 | #endif |
46 | 46 |
|
47 | 47 | #include "CrossSection.h" |
| 48 | +#include "TopoShapeOpCode.h" |
48 | 49 |
|
49 | 50 | using namespace Part; |
50 | 51 |
|
@@ -199,3 +200,97 @@ void CrossSection::connectWires (const TopTools_IndexedMapOfShape& wireMap, std: |
199 | 200 | wires.push_back(aFix.Wire()); |
200 | 201 | } |
201 | 202 | } |
| 203 | + |
| 204 | +/////////////////////////////////////////////////////////////////////////// |
| 205 | +TopoCrossSection::TopoCrossSection(double a, double b, double c, const TopoShape& s, const char *op) |
| 206 | + : a(a), b(b), c(c), shape(s), op(op?op:TOPOP_SLICE) |
| 207 | +{ |
| 208 | +} |
| 209 | + |
| 210 | +void TopoCrossSection::slice(int idx, double d, std::vector<TopoShape> &wires) const { |
| 211 | + // Fixes: 0001228: Cross section of Torus in Part Workbench fails or give wrong results |
| 212 | + // Fixes: 0001137: Incomplete slices when using Part.slice on a torus |
| 213 | + bool found = false; |
| 214 | + for(auto &s : shape.getSubTopoShapes(TopAbs_SOLID)) { |
| 215 | + sliceSolid(idx, d, s, wires); |
| 216 | + found = true; |
| 217 | + } |
| 218 | + if(!found) { |
| 219 | + for(auto &s : shape.getSubTopoShapes(TopAbs_SHELL)) { |
| 220 | + sliceSolid(idx, d, s, wires); |
| 221 | + found = true; |
| 222 | + } |
| 223 | + if(!found) { |
| 224 | + for(auto &s : shape.getSubTopoShapes(TopAbs_FACE)) |
| 225 | + sliceSolid(idx, d, s, wires); |
| 226 | + } |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +TopoShape TopoCrossSection::slice(int idx, double d) const { |
| 231 | + std::vector<TopoShape> wires; |
| 232 | + slice(idx,d,wires); |
| 233 | + return TopoShape().makECompound(wires,false,0,false); |
| 234 | +} |
| 235 | + |
| 236 | +void TopoCrossSection::sliceNonSolid(int idx, double d, |
| 237 | + const TopoShape& shape, std::vector<TopoShape>& wires) const |
| 238 | +{ |
| 239 | + BRepAlgoAPI_Section cs(shape.getShape(), gp_Pln(a,b,c,-d)); |
| 240 | + if (cs.IsDone()) { |
| 241 | + std::string prefix(op); |
| 242 | + if(idx>1) { |
| 243 | + prefix += '_'; |
| 244 | + prefix += std::to_string(idx); |
| 245 | + } |
| 246 | + auto res = TopoShape().makEShape(cs,shape,prefix.c_str()).makEWires().getSubTopoShapes(TopAbs_WIRE); |
| 247 | + wires.insert(wires.end(),res.begin(),res.end()); |
| 248 | + } |
| 249 | +} |
| 250 | + |
| 251 | +void TopoCrossSection::sliceSolid(int idx, double d, |
| 252 | + const TopoShape& shape, std::vector<TopoShape>& wires) const |
| 253 | +{ |
| 254 | + gp_Pln slicePlane(a,b,c,-d); |
| 255 | + BRepBuilderAPI_MakeFace mkFace(slicePlane); |
| 256 | + TopoShape face(idx); |
| 257 | + face.setShape(mkFace.Face()); |
| 258 | + |
| 259 | + // Make sure to choose a point that does not lie on the plane (fixes #0001228) |
| 260 | + gp_Vec tempVector(a,b,c); |
| 261 | + tempVector.Normalize();//just in case. |
| 262 | + tempVector *= (d+1.0); |
| 263 | + gp_Pnt refPoint(0.0, 0.0, 0.0); |
| 264 | + refPoint.Translate(tempVector); |
| 265 | + |
| 266 | + BRepPrimAPI_MakeHalfSpace mkSolid(TopoDS::Face(face.getShape()), refPoint); |
| 267 | + TopoShape solid(idx); |
| 268 | + std::string prefix(op); |
| 269 | + if(idx>1) { |
| 270 | + prefix += '_'; |
| 271 | + prefix += std::to_string(idx); |
| 272 | + } |
| 273 | + solid.makEShape(mkSolid,face,prefix.c_str()); |
| 274 | + BRepAlgoAPI_Cut mkCut(shape.getShape(), solid.getShape()); |
| 275 | + |
| 276 | + if (mkCut.IsDone()) { |
| 277 | + TopoShape res(shape.Tag,shape.Hasher); |
| 278 | + std::vector<TopoShape> shapes; |
| 279 | + shapes.push_back(shape); |
| 280 | + shapes.push_back(solid); |
| 281 | + res.makEShape(mkCut,shapes,prefix.c_str()); |
| 282 | + for(auto &face : res.getSubTopoShapes(TopAbs_FACE)) { |
| 283 | + BRepAdaptor_Surface adapt(TopoDS::Face(face.getShape())); |
| 284 | + if (adapt.GetType() == GeomAbs_Plane) { |
| 285 | + gp_Pln plane = adapt.Plane(); |
| 286 | + if (plane.Axis().IsParallel(slicePlane.Axis(), Precision::Confusion()) && |
| 287 | + plane.Distance(slicePlane.Location()) < Precision::Confusion()) { |
| 288 | + auto repaired_wires = TopoShape(face.Tag).makEWires( |
| 289 | + face.getSubTopoShapes(TopAbs_EDGE),prefix.c_str(),true).getSubTopoShapes(TopAbs_WIRE); |
| 290 | + wires.insert(wires.end(),repaired_wires.begin(),repaired_wires.end()); |
| 291 | + } |
| 292 | + } |
| 293 | + } |
| 294 | + } |
| 295 | +} |
| 296 | + |
0 commit comments