1
/***************************************************************************
2
* Copyright (c) 2008 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 <BRepAdaptor_Curve.hxx>
26
# include <BRepAdaptor_Surface.hxx>
27
# include <BRepBuilderAPI_Copy.hxx>
28
# include <BRepBuilderAPI_MakeWire.hxx>
29
# include <BRepLib_FindSurface.hxx>
30
# include <BRepPrimAPI_MakePrism.hxx>
32
# include <gp_Trsf.hxx>
33
# include <Precision.hxx>
35
# include <TopExp_Explorer.hxx>
37
# include <TopTools_IndexedMapOfShape.hxx>
40
#include <Base/Exception.h>
42
#include "FeatureExtrusion.h"
43
#include "ExtrusionHelper.h"
44
#include "Part2DObject.h"
49
PROPERTY_SOURCE(Part::Extrusion, Part::Feature)
51
const char* Extrusion::eDirModeStrings[] = {
59
std::vector<std::string> MakerEnums = {"Simple",
64
const char* enumToClass(const char* mode)
66
if (MakerEnums.at(0) == mode) {
67
return "Part::FaceMakerSimple";
69
if (MakerEnums.at(1) == mode) {
70
return "Part::FaceMakerCheese";
72
if (MakerEnums.at(2) == mode) {
73
return "Part::FaceMakerExtrusion";
75
if (MakerEnums.at(3) == mode) {
76
return "Part::FaceMakerBullseye";
79
return "Part::FaceMakerBullseye";
82
const char* classToEnum(const char* type)
84
if (strcmp(type, "Part::FaceMakerSimple") == 0) {
85
return MakerEnums.at(0).c_str();
87
if (strcmp(type, "Part::FaceMakerCheese") == 0) {
88
return MakerEnums.at(1).c_str();
90
if (strcmp(type, "Part::FaceMakerExtrusion") == 0) {
91
return MakerEnums.at(2).c_str();
93
if (strcmp(type, "Part::FaceMakerBullseye") == 0) {
94
return MakerEnums.at(3).c_str();
97
return MakerEnums.at(3).c_str();
100
void restoreFaceMakerMode(Extrusion* self)
102
const char* mode = enumToClass(self->FaceMakerMode.getValueAsString());
103
const char* type = self->FaceMakerClass.getValue();
104
if (strcmp(mode, type) != 0) {
105
self->FaceMakerMode.setValue(classToEnum(type));
110
Extrusion::Extrusion()
113
ADD_PROPERTY_TYPE(Base, (nullptr), "Extrude", App::Prop_None,
115
ADD_PROPERTY_TYPE(Dir, (Base::Vector3d(0.0, 0.0, 1.0)), "Extrude", App::Prop_None,
116
"Direction of extrusion (also magnitude, if both lengths are zero).");
117
ADD_PROPERTY_TYPE(DirMode, (dmCustom), "Extrude", App::Prop_None,
118
"Sets, how Dir is updated.");
119
DirMode.setEnums(eDirModeStrings);
120
ADD_PROPERTY_TYPE(DirLink, (nullptr), "Extrude", App::Prop_None,
121
"Link to edge defining extrusion direction.");
122
ADD_PROPERTY_TYPE(LengthFwd, (0.0), "Extrude", App::Prop_None,
123
"Length of extrusion along direction. If both LengthFwd and LengthRev are zero, magnitude of Dir is used.");
124
ADD_PROPERTY_TYPE(LengthRev, (0.0), "Extrude", App::Prop_None,
125
"Length of additional extrusion, against direction.");
126
ADD_PROPERTY_TYPE(Solid, (false), "Extrude", App::Prop_None,
127
"If true, extruding a wire yields a solid. If false, a shell.");
128
ADD_PROPERTY_TYPE(Reversed, (false), "Extrude", App::Prop_None,
129
"Set to true to swap the direction of extrusion.");
130
ADD_PROPERTY_TYPE(Symmetric, (false), "Extrude", App::Prop_None,
131
"If true, extrusion is done in both directions to a total of LengthFwd. LengthRev is ignored.");
132
ADD_PROPERTY_TYPE(TaperAngle, (0.0), "Extrude", App::Prop_None,
133
"Sets the angle of slope (draft) to apply to the sides. The angle is for outward taper; negative value yields inward tapering.");
134
ADD_PROPERTY_TYPE(TaperAngleRev, (0.0), "Extrude", App::Prop_None,
135
"Taper angle of reverse part of extrusion.");
136
// Default for old documents. See setupObject for default for new extrusions.
137
ADD_PROPERTY_TYPE(FaceMakerClass, ("Part::FaceMakerExtrusion"), "Extrude", (App::PropertyType)(App::Prop_ReadOnly | App::Prop_Hidden),
138
"If Solid is true, this sets the facemaker class to use when converting wires to faces. Otherwise, ignored.");
139
ADD_PROPERTY_TYPE(FaceMakerMode, (3L), "Extrude", App::Prop_None,
140
"If Solid is true, this sets the facemaker class to use when converting wires to faces. Otherwise, ignored.");
141
FaceMakerMode.setEnums(MakerEnums);
145
short Extrusion::mustExecute() const
147
if (Base.isTouched() ||
149
DirMode.isTouched() ||
150
DirLink.isTouched() ||
151
LengthFwd.isTouched() ||
152
LengthRev.isTouched() ||
154
Reversed.isTouched() ||
155
Symmetric.isTouched() ||
156
TaperAngle.isTouched() ||
157
TaperAngleRev.isTouched() ||
158
FaceMakerClass.isTouched())
163
bool Extrusion::fetchAxisLink(const App::PropertyLinkSub& axisLink, Base::Vector3d& basepoint, Base::Vector3d& dir)
165
if (!axisLink.getValue())
168
auto linked = axisLink.getValue();
171
if (!axisLink.getSubValues().empty() && axisLink.getSubValues()[0].length() > 0) {
172
axEdge = Feature::getTopoShape(linked, axisLink.getSubValues()[0].c_str(), true /*need element*/).getShape();
175
axEdge = Feature::getShape(linked);
179
throw Base::ValueError("DirLink shape is null");
180
if (axEdge.ShapeType() != TopAbs_EDGE)
181
throw Base::TypeError("DirLink shape is not an edge");
183
BRepAdaptor_Curve crv(TopoDS::Edge(axEdge));
186
if (crv.GetType() == GeomAbs_Line) {
187
startpoint = crv.Value(crv.FirstParameter());
188
endpoint = crv.Value(crv.LastParameter());
189
if (axEdge.Orientation() == TopAbs_REVERSED)
190
std::swap(startpoint, endpoint);
193
throw Base::TypeError("DirLink edge is not a line.");
195
basepoint.Set(startpoint.X(), startpoint.Y(), startpoint.Z());
196
gp_Vec vec = gp_Vec(startpoint, endpoint);
197
dir.Set(vec.X(), vec.Y(), vec.Z());
201
ExtrusionParameters Extrusion::computeFinalParameters()
203
ExtrusionParameters result;
205
switch (this->DirMode.getValue()) {
207
dir = this->Dir.getValue();
212
fetched = fetchAxisLink(this->DirLink, base, dir);
214
throw Base::ValueError("DirMode is set to use edge, but no edge is linked.");
215
this->Dir.setValue(dir);
218
dir = calculateShapeNormal(this->Base);
219
this->Dir.setValue(dir);
222
throw Base::ValueError("Unexpected enum value");
224
if (dir.Length() < Precision::Confusion())
225
throw Base::ValueError("Direction is zero-length");
226
result.dir = gp_Dir(dir.x, dir.y, dir.z);
227
if (this->Reversed.getValue())
228
result.dir.Reverse();
230
result.lengthFwd = this->LengthFwd.getValue();
231
result.lengthRev = this->LengthRev.getValue();
232
if (fabs(result.lengthFwd) < Precision::Confusion()
233
&& fabs(result.lengthRev) < Precision::Confusion()) {
234
result.lengthFwd = dir.Length();
237
if (this->Symmetric.getValue()) {
238
result.lengthRev = result.lengthFwd * 0.5;
239
result.lengthFwd = result.lengthFwd * 0.5;
242
if (fabs(result.lengthFwd + result.lengthRev) < Precision::Confusion())
243
throw Base::ValueError("Total length of extrusion is zero.");
245
result.solid = this->Solid.getValue();
247
result.taperAngleFwd = this->TaperAngle.getValue() * M_PI / 180.0;
248
if (fabs(result.taperAngleFwd) > M_PI * 0.5 - Precision::Angular())
249
throw Base::ValueError("Magnitude of taper angle matches or exceeds 90 degrees. That is too much.");
250
result.taperAngleRev = this->TaperAngleRev.getValue() * M_PI / 180.0;
251
if (fabs(result.taperAngleRev) > M_PI * 0.5 - Precision::Angular())
252
throw Base::ValueError("Magnitude of taper angle matches or exceeds 90 degrees. That is too much.");
254
result.faceMakerClass = this->FaceMakerClass.getValue();
259
Base::Vector3d Extrusion::calculateShapeNormal(const App::PropertyLink& shapeLink)
261
App::DocumentObject* docobj = nullptr;
263
TopoDS_Shape sh = Feature::getShape(shapeLink.getValue(), nullptr, false, &mat, &docobj);
266
throw Base::ValueError("calculateShapeNormal: link is empty");
268
//special case for sketches and the like: no matter what shape they have, use their local Z axis.
269
if (docobj->isDerivedFrom(Part::Part2DObject::getClassTypeId())) {
270
Base::Vector3d OZ(0.0, 0.0, 1.0);
271
Base::Vector3d result;
272
Base::Rotation(mat).multVec(OZ, result);
277
throw NullShapeException("calculateShapeNormal: link points to a valid object, but its shape is null.");
280
BRepLib_FindSurface planeFinder(sh, -1, /*OnlyPlane=*/true);
281
if (!planeFinder.Found())
282
throw Base::ValueError("Can't find normal direction, because the shape is not on a plane.");
284
//find plane normal and return result.
285
GeomAdaptor_Surface surf(planeFinder.Surface());
286
gp_Dir normal = surf.Plane().Axis().Direction();
288
//now we know the plane. But if there are faces, the
289
//plane normal direction is not dependent on face orientation (because findPlane only uses edges).
291
TopExp_Explorer ex(sh, TopAbs_FACE);
293
BRepAdaptor_Surface surf(TopoDS::Face(ex.Current()));
294
normal = surf.Plane().Axis().Direction();
295
if (ex.Current().Orientation() == TopAbs_REVERSED) {
300
return Base::Vector3d(normal.X(), normal.Y(), normal.Z());
303
void Extrusion::extrudeShape(TopoShape &result, const TopoShape &source, const ExtrusionParameters& params)
305
gp_Vec vec = gp_Vec(params.dir).Multiplied(params.lengthFwd + params.lengthRev);//total vector of extrusion
307
// #0000910: Circles Extrude Only Surfaces, thus use BRepBuilderAPI_Copy
308
TopoShape myShape(source.makeElementCopy());
310
if (std::fabs(params.taperAngleFwd) >= Precision::Angular()
311
|| std::fabs(params.taperAngleRev) >= Precision::Angular()) {
312
// Tapered extrusion!
313
#if defined(__GNUC__) && defined(FC_OS_LINUX)
314
Base::SignalException se;
316
std::vector<TopoShape> drafts;
317
ExtrusionHelper::makeElementDraft(params, myShape, drafts, result.Hasher);
318
if (drafts.empty()) {
319
Standard_Failure::Raise("Drafting shape failed");
322
result.makeElementCompound(drafts,
324
TopoShape::SingleShapeCompoundCreationPolicy::returnShape);
328
// Regular (non-tapered) extrusion!
329
if (source.isNull()) {
330
Standard_Failure::Raise("Cannot extrude empty shape");
333
// apply reverse part of extrusion by shifting the source shape
334
if (fabs(params.lengthRev) > Precision::Confusion()) {
336
mov.SetTranslation(gp_Vec(params.dir) * (-params.lengthRev));
337
myShape = myShape.makeElementTransform(mov);
340
// make faces from wires
342
// test if we need to make faces from wires. If there are faces - we don't.
343
if (!myShape.hasSubShape(TopAbs_FACE)) {
344
if (!myShape.Hasher) {
345
myShape.Hasher = result.Hasher;
347
myShape = myShape.makeElementFace(nullptr, params.faceMakerClass.c_str());
352
result.makeElementPrism(myShape, vec);
356
App::DocumentObjectExecReturn* Extrusion::execute()
358
App::DocumentObject* link = Base.getValue();
360
return new App::DocumentObjectExecReturn("No object linked");
364
ExtrusionParameters params = computeFinalParameters();
366
extrudeShape(result, Feature::getTopoShape(link), params);
367
this->Shape.setValue(result);
368
return App::DocumentObject::StdReturn;
370
catch (Standard_Failure& e) {
371
return new App::DocumentObjectExecReturn(e.GetMessageString());
375
//----------------------------------------------------------------
377
TYPESYSTEM_SOURCE(Part::FaceMakerExtrusion, Part::FaceMakerCheese)
379
std::string FaceMakerExtrusion::getUserFriendlyName() const
381
return {tr("Part Extrude facemaker").toStdString()};
384
std::string FaceMakerExtrusion::getBriefExplanation() const
386
return {tr("Supports making faces with holes, does not support nesting.").toStdString()};
389
#if OCC_VERSION_HEX >= 0x070600
390
void FaceMakerExtrusion::Build(const Message_ProgressRange&)
392
void FaceMakerExtrusion::Build()
396
this->myGenerated.Clear();
397
this->myShapesToReturn.clear();
398
this->myShape = TopoDS_Shape();
399
TopoDS_Shape inputShape;
400
if (mySourceShapes.empty())
401
throw Base::ValueError("No input shapes!");
402
if (mySourceShapes.size() == 1) {
403
inputShape = mySourceShapes[0].getShape();
406
TopoDS_Builder builder;
408
builder.MakeCompound(cmp);
409
for (const auto &sh : mySourceShapes) {
410
builder.Add(cmp, sh.getShape());
415
std::vector<TopoDS_Wire> wires;
416
TopTools_IndexedMapOfShape mapOfWires;
417
TopExp::MapShapes(inputShape, TopAbs_WIRE, mapOfWires);
419
// if there are no wires then check also for edges
420
if (mapOfWires.IsEmpty()) {
421
TopTools_IndexedMapOfShape mapOfEdges;
422
TopExp::MapShapes(inputShape, TopAbs_EDGE, mapOfEdges);
423
for (int i = 1; i <= mapOfEdges.Extent(); i++) {
424
BRepBuilderAPI_MakeWire mkWire(TopoDS::Edge(mapOfEdges.FindKey(i)));
425
wires.push_back(mkWire.Wire());
429
wires.reserve(mapOfWires.Extent());
430
for (int i = 1; i <= mapOfWires.Extent(); i++) {
431
wires.push_back(TopoDS::Wire(mapOfWires.FindKey(i)));
435
if (!wires.empty()) {
437
TopoDS_Shape res = FaceMakerCheese::makeFace(wires);
450
void Part::Extrusion::setupObject()
452
Part::Feature::setupObject();
453
//default for newly created features
454
this->FaceMakerMode.setValue(MakerEnums.at(3).c_str());
455
this->FaceMakerClass.setValue("Part::FaceMakerBullseye");
458
void Extrusion::onDocumentRestored()
460
restoreFaceMakerMode(this);
463
void Part::Extrusion::onChanged(const App::Property* prop)
465
if (prop == &FaceMakerMode) {
466
if (!isRestoring()) {
467
FaceMakerClass.setValue(enumToClass(FaceMakerMode.getValueAsString()));
471
Part::Feature::onChanged(prop);