1
/***************************************************************************
2
* Copyright (c) 2007 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 <BRepAlgoAPI_Fuse.hxx>
26
# include <BRepCheck_Analyzer.hxx>
27
# include <Standard_Failure.hxx>
28
# include <TopoDS_Iterator.hxx>
30
# include <TopTools_IndexedMapOfShape.hxx>
33
#include <App/Application.h>
34
#include <Base/Console.h>
35
#include <Base/Parameter.h>
37
#include "FeaturePartFuse.h"
39
#include "modelRefine.h"
40
#include "TopoShapeOpCode.h"
42
FC_LOG_LEVEL_INIT("Part",true,true);
46
PROPERTY_SOURCE(Part::Fuse, Part::Boolean)
49
Fuse::Fuse() = default;
51
BRepAlgoAPI_BooleanOperation* Fuse::makeOperation(const TopoDS_Shape& base, const TopoDS_Shape& tool) const
53
// Let's call algorithm computing a fuse operation:
54
return new BRepAlgoAPI_Fuse(base, tool);
57
const char *Fuse::opCode() const
59
return Part::OpCodes::Fuse;
62
// ----------------------------------------------------
64
PROPERTY_SOURCE(Part::MultiFuse, Part::Feature)
69
ADD_PROPERTY(Shapes,(nullptr));
71
ADD_PROPERTY_TYPE(History,(ShapeHistory()), "Boolean", (App::PropertyType)
72
(App::Prop_Output|App::Prop_Transient|App::Prop_Hidden), "Shape history");
75
ADD_PROPERTY_TYPE(Refine,(0),"Boolean",(App::PropertyType)(App::Prop_None),"Refine shape (clean up redundant edges) after this boolean operation");
77
//init Refine property
78
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter()
79
.GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/Part/Boolean");
80
this->Refine.setValue(hGrp->GetBool("RefineModel", false));
84
short MultiFuse::mustExecute() const
86
if (Shapes.isTouched())
91
App::DocumentObjectExecReturn *MultiFuse::execute()
93
std::vector<TopoShape> shapes;
94
std::vector<App::DocumentObject*> obj = Shapes.getValues();
96
std::vector<App::DocumentObject*>::iterator it;
97
for (it = obj.begin(); it != obj.end(); ++it) {
98
shapes.push_back(Feature::getTopoShape(*it));
101
bool argumentsAreInCompound = false;
102
TopoShape compoundOfArguments;
104
// if only one source shape, and it is a compound - fuse children of the compound
105
if (shapes.size() == 1) {
106
compoundOfArguments = shapes[0];
107
if (compoundOfArguments.getShape().ShapeType() == TopAbs_COMPOUND) {
109
shapes = compoundOfArguments.getSubTopoShapes();
110
argumentsAreInCompound = true;
114
if (shapes.size() >= 2) {
116
std::vector<ShapeHistory> history;
117
BRepAlgoAPI_Fuse mkFuse;
118
TopTools_ListOfShape shapeArguments, shapeTools;
119
const TopoShape& shape = shapes.front();
120
if (shape.isNull()) {
121
throw Base::RuntimeError("Input shape is null");
123
shapeArguments.Append(shape.getShape());
125
for (auto it2 = shapes.begin() + 1; it2 != shapes.end(); ++it2) {
127
throw Base::RuntimeError("Input shape is null");
129
shapeTools.Append(it2->getShape());
132
mkFuse.SetArguments(shapeArguments);
133
mkFuse.SetTools(shapeTools);
136
if (!mkFuse.IsDone()) {
137
throw Base::RuntimeError("MultiFusion failed");
141
res = res.makeShapeWithElementMap(mkFuse.Shape(), MapperMaker(mkFuse), shapes, OpCodes::Fuse);
142
for (const auto& it2 : shapes) {
144
buildHistory(mkFuse, TopAbs_FACE, res.getShape(), it2.getShape()));
147
throw Base::RuntimeError("Resulting shape is null");
150
Base::Reference<ParameterGrp> hGrp = App::GetApplication()
153
->GetGroup("Preferences")
154
->GetGroup("Mod/Part/Boolean");
155
if (hGrp->GetBool("CheckModel", true)) {
156
BRepCheck_Analyzer aChecker(res.getShape());
157
if (!aChecker.IsValid()) {
158
return new App::DocumentObjectExecReturn("Resulting shape is invalid");
161
if (this->Refine.getValue()) {
163
TopoDS_Shape oldShape = res.getShape();
164
BRepBuilderAPI_RefineModel mkRefine(oldShape);
165
// We just built an element map above for the fuse, don't erase it for a refine.
166
res.setShape(mkRefine.Shape(), false);
168
buildHistory(mkRefine, TopAbs_FACE, res.getShape(), oldShape);
169
for (auto& jt : history) {
170
jt = joinHistory(jt, hist);
173
catch (Standard_Failure&) {
178
if (argumentsAreInCompound) {
179
// combine histories of every child of source compound into one
180
ShapeHistory overallHist;
181
TopTools_IndexedMapOfShape facesOfCompound;
182
TopAbs_ShapeEnum type = TopAbs_FACE;
183
TopExp::MapShapes(compoundOfArguments.getShape(), type, facesOfCompound);
184
for (std::size_t iChild = 0; iChild < history.size();
185
iChild++) { // loop over children of source compound
186
// for each face of a child, find the inex of the face in compound, and assign
187
// the corresponding right-hand-size of the history
188
TopTools_IndexedMapOfShape facesOfChild;
189
TopExp::MapShapes(shapes[iChild].getShape(), type, facesOfChild);
190
for (std::pair<const int, ShapeHistory::List>& histitem :
191
history[iChild].shapeMap) { // loop over elements of history - that is -
192
// over faces of the child of source compound
193
int iFaceInChild = histitem.first;
194
ShapeHistory::List& iFacesInResult = histitem.second;
195
const TopoDS_Shape& srcFace = facesOfChild(
197
+ 1); //+1 to convert our 0-based to OCC 1-bsed conventions
198
int iFaceInCompound = facesOfCompound.FindIndex(srcFace) - 1;
199
overallHist.shapeMap[iFaceInCompound] =
200
iFacesInResult; // this may overwrite existing info if the same face is
201
// used in several children of compound. This shouldn't
202
// be a problem, because the histories should match
207
history.push_back(overallHist);
209
this->Shape.setValue(res);
210
this->History.setValues(history);
211
return Part::Feature::execute();
213
catch (Standard_Failure& e) {
214
return new App::DocumentObjectExecReturn(e.GetMessageString());
218
throw Base::CADKernelError("Not enough shape objects linked");