FreeCAD

Форк
0
/
FaceMakerCheese.cpp 
263 строки · 8.7 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2016 Victor Titov (DeepSOIC) <vv.titov@gmail.com>       *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
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.      *
10
 *                                                                         *
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.                  *
15
 *                                                                         *
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                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
#include "PreCompiled.h"
24
#ifndef _PreComp_
25
# include <algorithm>
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>
41
# include <TopoDS.hxx>
42
# include <TopExp_Explorer.hxx>
43
# include <TopTools_IndexedMapOfShape.hxx>
44
# include <TopTools_HSequenceOfShape.hxx>
45
# include <QtGlobal>
46
#endif
47

48
#include "FaceMakerCheese.h"
49

50

51
using namespace Part;
52

53
TYPESYSTEM_SOURCE(Part::FaceMakerCheese, Part::FaceMakerPublic)
54

55

56
TopoDS_Face FaceMakerCheese::validateFace(const TopoDS_Face& face)
57
{
58
    BRepCheck_Analyzer aChecker(face);
59
    if (!aChecker.IsValid()) {
60
        TopoDS_Wire outerwire = ShapeAnalysis::OuterWire(face);
61
        TopTools_IndexedMapOfShape myMap;
62
        myMap.Add(outerwire);
63

64
        TopExp_Explorer xp(face,TopAbs_WIRE);
65
        ShapeFix_Wire fix;
66
        fix.SetFace(face);
67
        fix.Load(outerwire);
68
        fix.Perform();
69
        BRepBuilderAPI_MakeFace mkFace(fix.WireAPIMake());
70
        while (xp.More()) {
71
            if (!myMap.Contains(xp.Current())) {
72
                fix.Load(TopoDS::Wire(xp.Current()));
73
                fix.Perform();
74
                mkFace.Add(fix.WireAPIMake());
75
            }
76
            xp.Next();
77
        }
78

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());
85
            fix.Perform();
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");
92
            return fixedFace;
93
        }
94
        return mkFace.Face();
95
    }
96

97
    return face;
98
}
99

100
bool FaceMakerCheese::Wire_Compare::operator() (const TopoDS_Wire& w1, const TopoDS_Wire& w2)
101
{
102
    Bnd_Box box1, box2;
103
    if (!w1.IsNull()) {
104
        BRepBndLib::Add(w1, box1);
105
        box1.SetGap(0.0);
106
    }
107

108
    if (!w2.IsNull()) {
109
        BRepBndLib::Add(w2, box2);
110
        box2.SetGap(0.0);
111
    }
112

113
    return box1.SquareExtent() < box2.SquareExtent();
114
}
115

116
bool FaceMakerCheese::isInside(const TopoDS_Wire& wire1, const TopoDS_Wire& wire2)
117
{
118
    Bnd_Box box1;
119
    BRepBndLib::Add(wire1, box1);
120
    box1.SetGap(0.0);
121

122
    Bnd_Box box2;
123
    BRepBndLib::Add(wire2, box2);
124
    box2.SetGap(0.0);
125

126
    if (box1.IsOut(box2))
127
        return false;
128

129
    double prec = Precision::Confusion();
130

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);
139

140
    TopExp_Explorer xp(wire2,TopAbs_VERTEX);
141
    while (xp.More())  {
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)
146
            return true;
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
149
        else
150
            return false;
151
        //xp.Next();
152
    }
153

154
    return false;
155
}
156

157
TopoDS_Shape FaceMakerCheese::makeFace(std::list<TopoDS_Wire>& wires)
158
{
159
    BRepBuilderAPI_MakeFace mkFace(wires.front());
160
    const TopoDS_Face& face = mkFace.Face();
161
    if (face.IsNull())
162
        return face;
163
    gp_Dir axis(0,0,1);
164
    BRepAdaptor_Surface adapt(face);
165
    if (adapt.GetType() == GeomAbs_Plane) {
166
        axis = adapt.Plane().Axis().Direction();
167
    }
168

169
    wires.pop_front();
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();
179
        }
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)
183
            wire.Reverse();
184
        mkFace.Add(wire);
185
    }
186
    return validateFace(mkFace.Face());
187
}
188

189
TopoDS_Shape FaceMakerCheese::makeFace(const std::vector<TopoDS_Wire>& w)
190
{
191
    if (w.empty())
192
        return {};
193

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());
200

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);
208

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);
214
            }
215
            else {
216
                ++it;
217
            }
218
        }
219

220
        sep_wire_list.push_back(sep_list);
221
    }
222

223
    if (sep_wire_list.size() == 1) {
224
        std::list<TopoDS_Wire>& wires = sep_wire_list.front();
225
        return makeFace(wires);
226
    }
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);
233
            if (!aFace.IsNull())
234
                builder.Add(comp, aFace);
235
        }
236

237
        return TopoDS_Shape(std::move(comp));
238
    }
239
    else {
240
        return {}; // error
241
    }
242
}
243

244

245
std::string FaceMakerCheese::getUserFriendlyName() const
246
{
247
    return {tr("Cheese facemaker").toStdString()};
248
}
249

250
std::string FaceMakerCheese::getBriefExplanation() const
251
{
252
    return {tr("Supports making planar faces with holes, but no islands inside holes.").toStdString()};
253
}
254

255
void FaceMakerCheese::Build_Essence()
256
{
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));
262
    }
263
}
264

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.