Files
create/src/Mod/Part/App/CrossSection.cpp
Eric Price cf8ad66373 Fix boolean operations (#17119)
* add BRepAlgoAPI wrapper files to CMakeList

* introduce wrapper for BRepAlgoAPI_BooleanOperation and derivatives, set sensible default Fuzzyness for boolean operations

* allow default fuzzyness to be set in settings - including off

* backwards compatibility with OCCT 7.3.3 - removed newer paramateres from wrapper

* added missing header

* reverted src/Mod/Part/App/OpenCascadeAll.h to original state

* implement some changes suggested by @wwmayer

* removed unneeded destructor definitions

* moved getDefaultFuzzyValue into its own helper class

* removed unneeded references to base class default constructor

* default to SetRunParallel(Standard_True) as suggested by @FlachyJoe

* fixed license header as requested

* Added new method to autocalculate fuzzyness to BooleanOperation base class and use it for those parts that do not call the obsolete constructor but also do not set precision

* implemented static variable solution as suggested by @wwmayer

* added helix for test case

* Refactoring FCRepAlgoAPI to FCBRepAlgoAPI

* Added Test Case (fuse cylinder with helix with different working and non-working Fuzzy values)

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* increased default value of FuzzyBoolean and made tests more robust for compatibility with OCCT 7.3

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fixes for FREECAD_USE_PCH - allow testing of FREECAD_USE_PCH on linux with cmake switch (default OFF)

* removed surplus line - wasn't needed after all

* included precompiled as instructed

* set anti-corruption flag in boolean operations, ensures source shape is not damaged in OCCT 7.1 and higher

* fix #17085

* disable subtests that were too specific for now

* fixed all CLI tests - see comments in Pull Request https://github.com/FreeCAD/FreeCAD/pull/17119#issuecomment-2420772452

* Update src/Mod/PartDesign/PartDesignTests/TestTopologicalNamingProblem.py

suggested updated test by CalligaroV

Co-authored-by: Vincenzo Calligaro <vincenzo.calligaro@gmail.com>

* Update tests/src/Mod/Part/App/TopoShapeExpansion.cpp

suggested updated test by CalligaroV

Co-authored-by: Vincenzo Calligaro <vincenzo.calligaro@gmail.com>

* Update tests/src/Mod/Part/App/TopoShapeExpansion.cpp

suggested updated test by CalligaroV

Co-authored-by: Vincenzo Calligaro <vincenzo.calligaro@gmail.com>

* Update tests/src/Mod/Part/App/TopoShapeExpansion.cpp

suggested updated test by CalligaroV

Co-authored-by: Vincenzo Calligaro <vincenzo.calligaro@gmail.com>

* Update tests/src/Mod/Part/App/TopoShapeExpansion.cpp

suggested updated test by CalligaroV

Co-authored-by: Vincenzo Calligaro <vincenzo.calligaro@gmail.com>

* Update tests/src/Mod/Part/App/TopoShapeExpansion.cpp

suggested updated test by CalligaroV

Co-authored-by: Vincenzo Calligaro <vincenzo.calligaro@gmail.com>

* Update tests/src/Mod/Part/App/TopoShapeExpansion.cpp

suggested updated test by CalligaroV

Co-authored-by: Vincenzo Calligaro <vincenzo.calligaro@gmail.com>

* Update tests/src/Mod/Part/App/TopoShapeExpansion.cpp

suggested updated test by CalligaroV

Co-authored-by: Vincenzo Calligaro <vincenzo.calligaro@gmail.com>

* Update tests/src/Mod/Part/App/TopoShapeExpansion.cpp

suggested updated test by CalligaroV

Co-authored-by: Vincenzo Calligaro <vincenzo.calligaro@gmail.com>

* Update tests/src/Mod/Part/App/TopoShapeExpansion.cpp

suggested updated test by CalligaroV

Co-authored-by: Vincenzo Calligaro <vincenzo.calligaro@gmail.com>

* Update tests/src/Mod/Part/App/TopoShapeExpansion.cpp

suggested updated test by CalligaroV

Co-authored-by: Vincenzo Calligaro <vincenzo.calligaro@gmail.com>

* Update tests/src/Mod/Part/App/TopoShapeExpansion.cpp

suggested updated test by CalligaroV

Co-authored-by: Vincenzo Calligaro <vincenzo.calligaro@gmail.com>

* Update tests/src/Mod/Part/App/TopoShapeExpansion.cpp

suggested updated test by CalligaroV

Co-authored-by: Vincenzo Calligaro <vincenzo.calligaro@gmail.com>

* Update tests/src/Mod/Part/App/TopoShapeExpansion.cpp

suggested updated test by CalligaroV

Co-authored-by: Vincenzo Calligaro <vincenzo.calligaro@gmail.com>

* Update tests/src/Mod/Part/App/TopoShapeExpansion.cpp

Co-authored-by: Vincenzo Calligaro <vincenzo.calligaro@gmail.com>

* remove surplus tests from github suggestion interface bug

* hide the BooleanFuzzy variable - can now be only set by manually editing the config file
i
in FCParamGroup "Boolean"
<FCFloat Name="BooleanFuzzy" Value="10.000000000000"/>

* Suggestion by @FlachyJoe - add validity checks to the base and tool shape in any direct boolean op

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Vincenzo Calligaro <vincenzo.calligaro@gmail.com>
2024-10-21 18:38:51 +02:00

324 lines
13 KiB
C++

/***************************************************************************
* Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <algorithm>
# include <BRepAdaptor_Surface.hxx>
# include <Mod/Part/App/FCBRepAlgoAPI_Common.h>
# include <Mod/Part/App/FCBRepAlgoAPI_Cut.h>
# include <Mod/Part/App/FCBRepAlgoAPI_Section.h>
# include <BRepBuilderAPI_MakeFace.hxx>
# include <BRepBuilderAPI_MakeWire.hxx>
# include <BRepPrimAPI_MakeHalfSpace.hxx>
# include <gp_Pln.hxx>
# include <Precision.hxx>
# include <ShapeAnalysis_FreeBounds.hxx>
# include <ShapeFix_Wire.hxx>
# include <TopExp.hxx>
# include <TopExp_Explorer.hxx>
# include <TopTools_HSequenceOfShape.hxx>
# include <TopTools_IndexedMapOfShape.hxx>
# include <TopoDS.hxx>
# include <TopoDS_Edge.hxx>
# include <TopoDS_Wire.hxx>
#endif
#include "CrossSection.h"
#include "TopoShapeOpCode.h"
using namespace Part;
CrossSection::CrossSection(double a, double b, double c, const TopoDS_Shape& s)
: a(a), b(b), c(c), s(s)
{
}
std::list<TopoDS_Wire> CrossSection::slice(double d) const
{
std::list<TopoDS_Wire> wires;
// Fixes: 0001228: Cross section of Torus in Part Workbench fails or give wrong results
// Fixes: 0001137: Incomplete slices when using Part.slice on a torus
TopExp_Explorer xp;
for (xp.Init(s, TopAbs_SOLID); xp.More(); xp.Next()) {
sliceSolid(d, xp.Current(), wires);
}
for (xp.Init(s, TopAbs_SHELL, TopAbs_SOLID); xp.More(); xp.Next()) {
sliceNonSolid(d, xp.Current(), wires);
}
for (xp.Init(s, TopAbs_FACE, TopAbs_SHELL); xp.More(); xp.Next()) {
sliceNonSolid(d, xp.Current(), wires);
}
return removeDuplicates(wires);
}
std::list<TopoDS_Wire> CrossSection::removeDuplicates(const std::list<TopoDS_Wire>& wires) const
{
std::list<TopoDS_Wire> wires_reduce;
for (const auto& wire : wires) {
TopTools_IndexedMapOfShape mapOfEdges1;
TopExp::MapShapes(wire, TopAbs_EDGE, mapOfEdges1);
// The wires are independent shapes but their edges might be shared
auto it = std::find_if(wires_reduce.begin(), wires_reduce.end(), [&mapOfEdges1](const TopoDS_Wire& w) {
// same TShape and same placement but different orientation
TopTools_IndexedMapOfShape mapOfEdges2;
TopExp::MapShapes(w, TopAbs_EDGE, mapOfEdges2);
int numEdges1 = mapOfEdges1.Extent();
int numEdges2 = mapOfEdges2.Extent();
if (numEdges1 != numEdges2)
return false;
TopTools_IndexedMapOfShape::Iterator it1(mapOfEdges1);
TopTools_IndexedMapOfShape::Iterator it2(mapOfEdges2);
for (; it1.More() && it2.More(); it1.Next(), it2.Next()) {
if (!it1.Value().IsSame(it2.Value()))
return false;
}
return true;
});
if (it == wires_reduce.end()) {
wires_reduce.push_back(wire);
}
}
return wires_reduce;
}
void CrossSection::sliceNonSolid(double d, const TopoDS_Shape& shape, std::list<TopoDS_Wire>& wires) const
{
FCBRepAlgoAPI_Section cs(shape, gp_Pln(a,b,c,-d));
if (cs.IsDone()) {
std::list<TopoDS_Edge> edges;
TopExp_Explorer xp;
for (xp.Init(cs.Shape(), TopAbs_EDGE); xp.More(); xp.Next())
edges.push_back(TopoDS::Edge(xp.Current()));
connectEdges(edges, wires);
}
}
void CrossSection::sliceSolid(double d, const TopoDS_Shape& shape, std::list<TopoDS_Wire>& wires) const
{
gp_Pln slicePlane(a,b,c,-d);
BRepBuilderAPI_MakeFace mkFace(slicePlane);
TopoDS_Face face = mkFace.Face();
// Make sure to choose a point that does not lie on the plane (fixes #0001228)
gp_Vec tempVector(a,b,c);
tempVector.Normalize();//just in case.
tempVector *= (d+1.0);
gp_Pnt refPoint(0.0, 0.0, 0.0);
refPoint.Translate(tempVector);
BRepPrimAPI_MakeHalfSpace mkSolid(face, refPoint);
TopoDS_Solid solid = mkSolid.Solid();
FCBRepAlgoAPI_Cut mkCut(shape, solid);
if (mkCut.IsDone()) {
TopTools_IndexedMapOfShape mapOfFaces;
TopExp::MapShapes(mkCut.Shape(), TopAbs_FACE, mapOfFaces);
for (int i=1; i<=mapOfFaces.Extent(); i++) {
const TopoDS_Face& face = TopoDS::Face(mapOfFaces.FindKey(i));
BRepAdaptor_Surface adapt(face);
if (adapt.GetType() == GeomAbs_Plane) {
gp_Pln plane = adapt.Plane();
if (plane.Axis().IsParallel(slicePlane.Axis(), Precision::Confusion()) &&
plane.Distance(slicePlane.Location()) < Precision::Confusion()) {
// sort and repair the wires
TopTools_IndexedMapOfShape mapOfWires;
TopExp::MapShapes(face, TopAbs_WIRE, mapOfWires);
connectWires(mapOfWires, wires);
}
}
}
}
}
void CrossSection::connectEdges (const std::list<TopoDS_Edge>& edges, std::list<TopoDS_Wire>& wires) const
{
// Hint: Use ShapeAnalysis_FreeBounds::ConnectEdgesToWires() as an alternative
std::list<TopoDS_Edge> edge_list = edges;
while (!edge_list.empty()) {
BRepBuilderAPI_MakeWire mkWire;
// add and erase first edge
mkWire.Add(edge_list.front());
edge_list.erase(edge_list.begin());
TopoDS_Wire new_wire = mkWire.Wire(); // current new wire
// try to connect each edge to the wire, the wire is complete if no more edges are connectible
bool found = false;
do {
found = false;
for (std::list<TopoDS_Edge>::iterator pE = edge_list.begin(); pE != edge_list.end();++pE) {
mkWire.Add(*pE);
if (mkWire.Error() != BRepBuilderAPI_DisconnectedWire) {
// edge added ==> remove it from list
found = true;
edge_list.erase(pE);
new_wire = mkWire.Wire();
break;
}
}
}
while (found);
// Fix any topological issues of the wire
wires.push_back(fixWire(new_wire));
}
}
void CrossSection::connectWires (const TopTools_IndexedMapOfShape& wireMap, std::list<TopoDS_Wire>& wires) const
{
Handle(TopTools_HSequenceOfShape) hWires = new TopTools_HSequenceOfShape();
for (int i=1; i<=wireMap.Extent(); i++) {
const TopoDS_Shape& wire = wireMap.FindKey(i);
hWires->Append(wire);
}
Handle(TopTools_HSequenceOfShape) hSorted = new TopTools_HSequenceOfShape();
ShapeAnalysis_FreeBounds::ConnectWiresToWires(hWires, Precision::Confusion(), false, hSorted);
for (int i=1; i<=hSorted->Length(); i++) {
const TopoDS_Wire& new_wire = TopoDS::Wire(hSorted->Value(i));
// Fix any topological issues of the wire
wires.push_back(fixWire(new_wire));
}
}
TopoDS_Wire CrossSection::fixWire(const TopoDS_Wire& wire) const
{
// Fix any topological issues of the wire
ShapeFix_Wire aFix;
aFix.SetPrecision(Precision::Confusion());
aFix.Load(wire);
aFix.FixReorder();
aFix.FixConnected();
aFix.FixClosed();
return aFix.Wire();
}
TopoCrossSection::TopoCrossSection(double a, double b, double c, const TopoShape& s, const char *op)
: a(a), b(b), c(c), shape(s), op(op?op:Part::OpCodes::Slice)
{
}
void TopoCrossSection::slice(int idx, double d, std::vector<TopoShape>& wires) const
{
// Fixes: 0001228: Cross section of Torus in Part Workbench fails or give wrong results
// Fixes: 0001137: Incomplete slices when using Part.slice on a torus
bool found = false;
for (auto& s : shape.getSubTopoShapes(TopAbs_SOLID)) {
sliceSolid(idx, d, s, wires);
found = true;
}
if (!found) {
for (auto& s : shape.getSubTopoShapes(TopAbs_SHELL)) {
sliceNonSolid(idx, d, s, wires);
found = true;
}
if (!found) {
for (auto& s : shape.getSubTopoShapes(TopAbs_FACE)) {
sliceNonSolid(idx, d, s, wires);
}
}
}
}
TopoShape TopoCrossSection::slice(int idx, double d) const
{
std::vector<TopoShape> wires;
slice(idx, d, wires);
return TopoShape().makeElementCompound(
wires,
0,
TopoShape::SingleShapeCompoundCreationPolicy::returnShape);
}
void TopoCrossSection::sliceNonSolid(int idx,
double d,
const TopoShape& shape,
std::vector<TopoShape>& wires) const
{
FCBRepAlgoAPI_Section cs(shape.getShape(), gp_Pln(a, b, c, -d));
if (cs.IsDone()) {
std::string prefix(op);
prefix += Data::indexSuffix(idx);
auto res = TopoShape()
.makeElementShape(cs, shape, prefix.c_str())
.makeElementWires()
.getSubTopoShapes(TopAbs_WIRE);
wires.insert(wires.end(), res.begin(), res.end());
}
}
void TopoCrossSection::sliceSolid(int idx,
double d,
const TopoShape& shape,
std::vector<TopoShape>& wires) const
{
gp_Pln slicePlane(a, b, c, -d);
BRepBuilderAPI_MakeFace mkFace(slicePlane);
TopoShape face(idx);
face.setShape(mkFace.Face());
// Make sure to choose a point that does not lie on the plane (fixes #0001228)
gp_Vec tempVector(a, b, c);
tempVector.Normalize(); // just in case.
tempVector *= (d + 1.0);
gp_Pnt refPoint(0.0, 0.0, 0.0);
refPoint.Translate(tempVector);
BRepPrimAPI_MakeHalfSpace mkSolid(TopoDS::Face(face.getShape()), refPoint);
TopoShape solid(idx);
std::string prefix(op);
prefix += Data::indexSuffix(idx);
solid.makeElementShape(mkSolid, face, prefix.c_str());
FCBRepAlgoAPI_Cut mkCut(shape.getShape(), solid.getShape());
if (mkCut.IsDone()) {
TopoShape res(shape.Tag, shape.Hasher);
std::vector<TopoShape> shapes;
shapes.push_back(shape);
shapes.push_back(solid);
res.makeElementShape(mkCut, shapes, prefix.c_str());
for (auto& face : res.getSubTopoShapes(TopAbs_FACE)) {
BRepAdaptor_Surface adapt(TopoDS::Face(face.getShape()));
if (adapt.GetType() == GeomAbs_Plane) {
gp_Pln plane = adapt.Plane();
if (plane.Axis().IsParallel(slicePlane.Axis(), Precision::Confusion())
&& plane.Distance(slicePlane.Location()) < Precision::Confusion()) {
auto repaired_wires = TopoShape(face.Tag)
.makeElementWires(face.getSubTopoShapes(TopAbs_EDGE),
prefix.c_str(),
true)
.getSubTopoShapes(TopAbs_WIRE);
wires.insert(wires.end(), repaired_wires.begin(), repaired_wires.end());
}
}
}
}
}