1
/***************************************************************************
2
* Copyright (c) 2019 Werner Mayer <wmayer[at]users.sourceforge.net> *
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"
25
# include <BRep_Builder.hxx>
26
# include <BRepAdaptor_Curve.hxx>
27
# include <BRepBuilderAPI_MakeEdge.hxx>
28
# include <BRepBuilderAPI_MakeWire.hxx>
29
# include <Geom_BSplineCurve.hxx>
30
# include <Precision.hxx>
31
# include <ShapeConstruct_Curve.hxx>
32
# include <Standard_ConstructionError.hxx>
33
# include <Standard_Version.hxx>
34
# include <TopExp_Explorer.hxx>
35
# include <TopLoc_Location.hxx>
37
# include <TopoDS_Compound.hxx>
38
# include <TopoDS_Edge.hxx>
39
# include <TopoDS_Wire.hxx>
40
# include <TopTools_ListIteratorOfListOfShape.hxx>
41
# include <TopTools_ListOfShape.hxx>
42
# include <TopTools_MapOfShape.hxx>
45
#include "BRepOffsetAPI_MakeOffsetFix.h"
50
BRepOffsetAPI_MakeOffsetFix::BRepOffsetAPI_MakeOffsetFix() = default;
52
BRepOffsetAPI_MakeOffsetFix::BRepOffsetAPI_MakeOffsetFix(const GeomAbs_JoinType Join, const Standard_Boolean IsOpenResult)
54
mkOffset.Init(Join, IsOpenResult);
57
BRepOffsetAPI_MakeOffsetFix::~BRepOffsetAPI_MakeOffsetFix() = default;
59
void BRepOffsetAPI_MakeOffsetFix::AddWire(const TopoDS_Wire& Spine)
61
TopoDS_Wire wire = Spine;
63
TopExp_Explorer xp(wire, TopAbs_EDGE);
69
TopLoc_Location edgeLocation;
71
BRepBuilderAPI_MakeWire mkWire;
72
TopExp_Explorer xp(wire, TopAbs_EDGE);
74
// The trick is to reset the placement of an edge before
75
// passing it to BRepOffsetAPI_MakeOffset because then it
76
// will create the expected result.
77
// Afterwards apply the placement again on the result shape.
78
// See the method MakeWire()
79
TopoDS_Edge edge = TopoDS::Edge(xp.Current());
80
edgeLocation = edge.Location();
81
edge.Location(TopLoc_Location());
83
myLocations.emplace_back(edge, edgeLocation);
89
// Make sure that the new wire has the same orientation as the source wire
90
// because otherwise the offset will shrink the wire
91
wire.Orientation(Spine.Orientation());
93
mkOffset.AddWire(wire);
97
void BRepOffsetAPI_MakeOffsetFix::Perform (const Standard_Real Offset, const Standard_Real Alt)
99
mkOffset.Perform(Offset, Alt);
102
#if OCC_VERSION_HEX >= 0x070600
103
void BRepOffsetAPI_MakeOffsetFix::Build(const Message_ProgressRange&)
105
void BRepOffsetAPI_MakeOffsetFix::Build()
111
void BRepOffsetAPI_MakeOffsetFix::Init(const TopoDS_Face& Spine, const GeomAbs_JoinType Join,
112
const Standard_Boolean IsOpenResult)
114
mkOffset.Init(Spine, Join, IsOpenResult);
117
void BRepOffsetAPI_MakeOffsetFix::Init(const GeomAbs_JoinType Join, const Standard_Boolean IsOpenResult)
119
mkOffset.Init(Join, IsOpenResult);
122
Standard_Boolean BRepOffsetAPI_MakeOffsetFix::IsDone() const
124
return mkOffset.IsDone();
127
void BRepOffsetAPI_MakeOffsetFix::MakeWire(TopoDS_Shape& wire)
129
// get the edges of the wire and check which of the stored edges
130
// serve as input of the wire
131
TopTools_MapOfShape aMap;
132
TopExp_Explorer xp(wire, TopAbs_EDGE);
134
aMap.Add(xp.Current());
138
std::list<TopoDS_Edge> edgeList;
139
for (const auto& itLoc : myLocations) {
140
TopTools_ListOfShape newShapes = mkOffset.Generated(itLoc.first);
141
// Check generated shapes for the vertexes, too
142
TopExp_Explorer xpv(itLoc.first, TopAbs_VERTEX);
144
TopTools_ListOfShape newEdge = mkOffset.Generated(xpv.Current());
145
if (!newEdge.IsEmpty()) {
146
newShapes.Append(newEdge);
150
for (TopTools_ListIteratorOfListOfShape it(newShapes); it.More(); it.Next()) {
151
TopoDS_Shape newShape = it.Value();
153
if (aMap.Contains(newShape)) {
154
newShape.Move(itLoc.second);
155
edgeList.push_back(TopoDS::Edge(newShape));
160
if (!edgeList.empty()) {
161
BRepBuilderAPI_MakeWire mkWire;
162
mkWire.Add(edgeList.front());
163
edgeList.pop_front();
164
wire = mkWire.Wire();
169
for (std::list<TopoDS_Edge>::iterator pE = edgeList.begin(); pE != edgeList.end(); ++pE) {
171
if (mkWire.Error() != BRepBuilderAPI_DisconnectedWire) {
172
// edge added ==> remove it from list
175
wire = mkWire.Wire();
184
const TopoDS_Shape& BRepOffsetAPI_MakeOffsetFix::Shape()
186
if (myResult.IsNull()) {
187
TopoDS_Shape result = mkOffset.Shape();
188
if (result.IsNull()) {
193
if (result.ShapeType() == TopAbs_WIRE) {
196
else if (result.ShapeType() == TopAbs_COMPOUND) {
197
BRep_Builder builder;
198
TopoDS_Compound comp;
199
builder.MakeCompound(comp);
201
TopExp_Explorer xp(result, TopAbs_WIRE);
203
TopoDS_Wire wire = TopoDS::Wire(xp.Current());
205
builder.Add(comp, wire);
217
const TopTools_ListOfShape& BRepOffsetAPI_MakeOffsetFix::Generated(const TopoDS_Shape& S)
219
return mkOffset.Generated(S);
222
const TopTools_ListOfShape& BRepOffsetAPI_MakeOffsetFix::Modified (const TopoDS_Shape& S)
224
return mkOffset.Modified(S);
227
Standard_Boolean BRepOffsetAPI_MakeOffsetFix::IsDeleted (const TopoDS_Shape& S)
229
return mkOffset.IsDeleted(S);
232
TopoDS_Shape BRepOffsetAPI_MakeOffsetFix::Replace(GeomAbs_CurveType type, const TopoDS_Shape& S) const
235
throw Standard_ConstructionError("Input shape is null");
238
if (type == GeomAbs_BSplineCurve)
241
if (S.ShapeType() == TopAbs_COMPOUND) {
242
BRep_Builder builder;
243
TopoDS_Compound comp;
244
builder.MakeCompound(comp);
245
comp.Location(S.Location());
247
TopExp_Explorer xp(S, TopAbs_WIRE);
249
TopoDS_Wire wire = TopoDS::Wire(xp.Current());
250
wire = ReplaceEdges(type, wire);
251
builder.Add(comp, wire);
255
return TopoDS_Compound(std::move(comp));
257
else if (S.ShapeType() == TopAbs_WIRE) {
258
return ReplaceEdges(type, TopoDS::Wire(S));
261
throw Standard_ConstructionError("Wrong shape type");
265
TopoDS_Wire BRepOffsetAPI_MakeOffsetFix::ReplaceEdges(GeomAbs_CurveType type, const TopoDS_Wire& wire) const
267
BRepBuilderAPI_MakeWire mkWire;
268
for (TopExp_Explorer xp(wire, TopAbs_EDGE); xp.More(); xp.Next()) {
269
TopoDS_Edge edge = TopoDS::Edge(xp.Current());
270
TopLoc_Location edgeLocation = edge.Location();
272
BRepAdaptor_Curve curve(edge);
274
if (curve.GetType() == type) {
275
ShapeConstruct_Curve scc;
276
double u = curve.FirstParameter();
277
double v = curve.LastParameter();
278
Handle(Geom_BSplineCurve) spline = scc.ConvertToBSpline(curve.Curve().Curve(), u, v, Precision::Confusion());
279
if (!spline.IsNull()) {
280
BRepBuilderAPI_MakeEdge mkEdge(spline, u, v);
281
edge = mkEdge.Edge();
282
edge.Location(edgeLocation);
289
return mkWire.Wire();