FreeCAD

Форк
0
/
FeaturePartBoolean.cpp 
145 строк · 5.7 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net>     *
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 <memory>
26

27
# include <BRepAlgoAPI_BooleanOperation.hxx>
28
# include <BRepCheck_Analyzer.hxx>
29
# include <Standard_Failure.hxx>
30
#endif
31

32
#include <App/Application.h>
33
#include <Base/Parameter.h>
34

35
#include "FeaturePartBoolean.h"
36
#include "TopoShapeOpCode.h"
37
#include "modelRefine.h"
38

39

40
using namespace Part;
41

42
PROPERTY_SOURCE_ABSTRACT(Part::Boolean, Part::Feature)
43

44

45
Boolean::Boolean()
46
{
47
    ADD_PROPERTY(Base,(nullptr));
48
    ADD_PROPERTY(Tool,(nullptr));
49
    ADD_PROPERTY_TYPE(History,(ShapeHistory()), "Boolean", (App::PropertyType)
50
        (App::Prop_Output|App::Prop_Transient|App::Prop_Hidden), "Shape history");
51
    History.setSize(0);
52

53
    ADD_PROPERTY_TYPE(Refine,(0),"Boolean",(App::PropertyType)(App::Prop_None),"Refine shape (clean up redundant edges) after this boolean operation");
54

55
    //init Refine property
56
    Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter()
57
        .GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/Part/Boolean");
58
    this->Refine.setValue(hGrp->GetBool("RefineModel", false));
59
}
60

61
short Boolean::mustExecute() const
62
{
63
    if (Base.getValue() && Tool.getValue()) {
64
        if (Base.isTouched()) {
65
            return 1;
66
        }
67
        if (Tool.isTouched()) {
68
            return 1;
69
        }
70
    }
71
    return 0;
72
}
73

74
const char *Boolean::opCode() const
75
{
76
    return Part::OpCodes::Boolean;
77
}
78

79
App::DocumentObjectExecReturn* Boolean::execute()
80
{
81
    try {
82
#if defined(__GNUC__) && defined(FC_OS_LINUX)
83
        Base::SignalException se;
84
#endif
85
        auto base = Base.getValue();
86
        auto tool = Tool.getValue();
87

88
        if (!base || !tool) {
89
            return new App::DocumentObjectExecReturn("Linked object is not a Part object");
90
        }
91
        std::vector<TopoShape> shapes;
92
        shapes.reserve(2);
93
        // Now, let's get the TopoDS_Shape
94
        shapes.push_back(Feature::getTopoShape(Base.getValue()));
95
        auto BaseShape = shapes[0].getShape();
96
        if (BaseShape.IsNull()) {
97
            throw NullShapeException("Base shape is null");
98
        }
99
        shapes.push_back(Feature::getTopoShape(Tool.getValue()));
100
        auto ToolShape = shapes[1].getShape();
101
        if (ToolShape.IsNull()) {
102
            throw NullShapeException("Tool shape is null");
103
        }
104

105
        std::unique_ptr<BRepAlgoAPI_BooleanOperation> mkBool(makeOperation(BaseShape, ToolShape));
106
        if (!mkBool->IsDone()) {
107
            std::stringstream error;
108
            error << "Boolean operation failed";
109
            if (BaseShape.ShapeType() != TopAbs_SOLID) {
110
                error << std::endl << base->Label.getValue() << " is not a solid";
111
            }
112
            if (ToolShape.ShapeType() != TopAbs_SOLID) {
113
                error << std::endl << tool->Label.getValue() << " is not a solid";
114
            }
115
            return new App::DocumentObjectExecReturn(error.str());
116
        }
117
        TopoDS_Shape resShape = mkBool->Shape();
118
        if (resShape.IsNull()) {
119
            return new App::DocumentObjectExecReturn("Resulting shape is null");
120
        }
121
        Base::Reference<ParameterGrp> hGrp = App::GetApplication()
122
                                                 .GetUserParameter()
123
                                                 .GetGroup("BaseApp")
124
                                                 ->GetGroup("Preferences")
125
                                                 ->GetGroup("Mod/Part/Boolean");
126

127
        if (hGrp->GetBool("CheckModel", true)) {
128
            BRepCheck_Analyzer aChecker(resShape);
129
            if (!aChecker.IsValid()) {
130
                return new App::DocumentObjectExecReturn("Resulting shape is invalid");
131
            }
132
        }
133
        TopoShape res(0);
134
        res.makeElementShape(*mkBool, shapes, opCode());
135
        if (this->Refine.getValue()) {
136
            res = res.makeElementRefine();
137
        }
138
        this->Shape.setValue(res);
139
        return Part::Feature::execute();
140
    }
141
    catch (...) {
142
        return new App::DocumentObjectExecReturn(
143
            "A fatal error occurred when running boolean operation");
144
    }
145
}
146

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

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

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

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