1
/***************************************************************************
2
* Copyright (c) 2016 Victor Titov (DeepSOIC) <vv.titov@gmail.com> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
23
#include "PreCompiled.h"
26
# include <Bnd_Box.hxx>
27
# include <BRep_Builder.hxx>
28
# include <BRep_Tool.hxx>
29
# include <BRepAdaptor_Surface.hxx>
30
# include <BRepBuilderAPI_MakeFace.hxx>
31
# include <BRepCheck_Analyzer.hxx>
32
# include <BRepBndLib.hxx>
33
# include <Geom_Plane.hxx>
34
# include <IntTools_FClass2d.hxx>
35
# include <Precision.hxx>
36
# include <ShapeAnalysis.hxx>
37
# include <ShapeAnalysis_Surface.hxx>
38
# include <ShapeExtend_Explorer.hxx>
39
# include <ShapeFix_Shape.hxx>
40
# include <ShapeFix_Wire.hxx>
42
# include <TopExp_Explorer.hxx>
43
# include <TopTools_IndexedMapOfShape.hxx>
44
# include <TopTools_HSequenceOfShape.hxx>
48
#include "FaceMakerCheese.h"
53
TYPESYSTEM_SOURCE(Part::FaceMakerCheese, Part::FaceMakerPublic)
56
TopoDS_Face FaceMakerCheese::validateFace(const TopoDS_Face& face)
58
BRepCheck_Analyzer aChecker(face);
59
if (!aChecker.IsValid()) {
60
TopoDS_Wire outerwire = ShapeAnalysis::OuterWire(face);
61
TopTools_IndexedMapOfShape myMap;
64
TopExp_Explorer xp(face,TopAbs_WIRE);
69
BRepBuilderAPI_MakeFace mkFace(fix.WireAPIMake());
71
if (!myMap.Contains(xp.Current())) {
72
fix.Load(TopoDS::Wire(xp.Current()));
74
mkFace.Add(fix.WireAPIMake());
79
aChecker.Init(mkFace.Face());
80
if (!aChecker.IsValid()) {
81
ShapeFix_Shape fix(mkFace.Face());
82
fix.SetPrecision(Precision::Confusion());
83
fix.SetMaxTolerance(Precision::Confusion());
84
fix.SetMaxTolerance(Precision::Confusion());
86
fix.FixWireTool()->Perform();
87
fix.FixFaceTool()->Perform();
88
TopoDS_Face fixedFace = TopoDS::Face(fix.Shape());
89
aChecker.Init(fixedFace);
90
if (!aChecker.IsValid())
91
Standard_Failure::Raise("Failed to validate broken face");
100
bool FaceMakerCheese::Wire_Compare::operator() (const TopoDS_Wire& w1, const TopoDS_Wire& w2)
104
BRepBndLib::Add(w1, box1);
109
BRepBndLib::Add(w2, box2);
113
return box1.SquareExtent() < box2.SquareExtent();
116
bool FaceMakerCheese::isInside(const TopoDS_Wire& wire1, const TopoDS_Wire& wire2)
119
BRepBndLib::Add(wire1, box1);
123
BRepBndLib::Add(wire2, box2);
126
if (box1.IsOut(box2))
129
double prec = Precision::Confusion();
131
BRepBuilderAPI_MakeFace mkFace(wire1);
132
if (!mkFace.IsDone())
133
Standard_Failure::Raise("Failed to create a face from wire in sketch");
134
TopoDS_Face face = validateFace(mkFace.Face());
135
BRepAdaptor_Surface adapt(face);
136
IntTools_FClass2d class2d(face, prec);
137
Handle(Geom_Surface) surf = new Geom_Plane(adapt.Plane());
138
ShapeAnalysis_Surface as(surf);
140
TopExp_Explorer xp(wire2,TopAbs_VERTEX);
142
TopoDS_Vertex v = TopoDS::Vertex(xp.Current());
143
gp_Pnt p = BRep_Tool::Pnt(v);
144
gp_Pnt2d uv = as.ValueOfUV(p, prec);
145
if (class2d.Perform(uv) == TopAbs_IN)
147
// TODO: We can make a check to see if all points are inside or all outside
148
// because otherwise we have some intersections which is not allowed
157
TopoDS_Shape FaceMakerCheese::makeFace(std::list<TopoDS_Wire>& wires)
159
BRepBuilderAPI_MakeFace mkFace(wires.front());
160
const TopoDS_Face& face = mkFace.Face();
164
BRepAdaptor_Surface adapt(face);
165
if (adapt.GetType() == GeomAbs_Plane) {
166
axis = adapt.Plane().Axis().Direction();
170
for (auto wire : wires) {
171
BRepBuilderAPI_MakeFace mkInnerFace(wire);
172
const TopoDS_Face& inner_face = mkInnerFace.Face();
173
if (inner_face.IsNull())
174
return inner_face; // failure
175
gp_Dir inner_axis(0,0,1);
176
BRepAdaptor_Surface adapt(inner_face);
177
if (adapt.GetType() == GeomAbs_Plane) {
178
inner_axis = adapt.Plane().Axis().Direction();
180
// It seems that orientation is always 'Forward' and we only have to reverse
181
// if the underlying plane have opposite normals.
182
if (axis.Dot(inner_axis) < 0)
186
return validateFace(mkFace.Face());
189
TopoDS_Shape FaceMakerCheese::makeFace(const std::vector<TopoDS_Wire>& w)
194
//FIXME: Need a safe method to sort wire that the outermost one comes last
195
// Currently it's done with the diagonal lengths of the bounding boxes
196
std::vector<TopoDS_Wire> wires = w;
197
std::sort(wires.begin(), wires.end(), Wire_Compare());
198
std::list<TopoDS_Wire> wire_list;
199
wire_list.insert(wire_list.begin(), wires.rbegin(), wires.rend());
201
// separate the wires into several independent faces
202
std::list< std::list<TopoDS_Wire> > sep_wire_list;
203
while (!wire_list.empty()) {
204
std::list<TopoDS_Wire> sep_list;
205
TopoDS_Wire wire = wire_list.front();
206
wire_list.pop_front();
207
sep_list.push_back(wire);
209
std::list<TopoDS_Wire>::iterator it = wire_list.begin();
210
while (it != wire_list.end()) {
211
if (isInside(wire, *it)) {
212
sep_list.push_back(*it);
213
it = wire_list.erase(it);
220
sep_wire_list.push_back(sep_list);
223
if (sep_wire_list.size() == 1) {
224
std::list<TopoDS_Wire>& wires = sep_wire_list.front();
225
return makeFace(wires);
227
else if (sep_wire_list.size() > 1) {
228
TopoDS_Compound comp;
229
BRep_Builder builder;
230
builder.MakeCompound(comp);
231
for (auto & it : sep_wire_list) {
232
TopoDS_Shape aFace = makeFace(it);
234
builder.Add(comp, aFace);
237
return TopoDS_Shape(std::move(comp));
245
std::string FaceMakerCheese::getUserFriendlyName() const
247
return {tr("Cheese facemaker").toStdString()};
250
std::string FaceMakerCheese::getBriefExplanation() const
252
return {tr("Supports making planar faces with holes, but no islands inside holes.").toStdString()};
255
void FaceMakerCheese::Build_Essence()
257
TopoDS_Shape faces = makeFace(this->myWires);
258
ShapeExtend_Explorer xp;
259
Handle(TopTools_HSequenceOfShape) seq = xp.SeqFromCompound(faces, Standard_True);
260
for(int i = 0 ; i < seq->Length() ; i++){
261
this->myShapesToReturn.push_back(seq->Value(i+1));