FreeCAD

Форк
0
/
FeatureScale.cpp 
161 строка · 5.9 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2023 Wanderer Fan <wandererfan@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 <BRepBuilderAPI_GTransform.hxx>
26
#include <BRepBuilderAPI_Transform.hxx>
27
#include <BRepBuilderAPI_Copy.hxx>
28
#include <gp_Pnt.hxx>
29
#include <gp_GTrsf.hxx>
30
#include <gp_Trsf.hxx>
31
#include <Precision.hxx>
32
#endif
33

34
#include <Base/Exception.h>
35

36
#include "FeatureScale.h"
37

38
using namespace Part;
39

40
PROPERTY_SOURCE(Part::Scale, Part::Feature)
41

42
Scale::Scale()
43
{
44
    ADD_PROPERTY_TYPE(Base, (nullptr), "Scale", App::Prop_None, "Shape to scale");
45
    ADD_PROPERTY_TYPE(Uniform, (true), "Scale", App::Prop_None, "If true, scale equally in all directions");
46
    ADD_PROPERTY_TYPE(UniformScale, (1.0), "Scale", App::Prop_None, "Uniform scale factor - 1.0 means no scaling");
47
    ADD_PROPERTY_TYPE(XScale, (1.0), "Scale", App::Prop_None, "Scale factor in X direction - 1.0 means no scaling");
48
    ADD_PROPERTY_TYPE(YScale, (1.0), "Scale", App::Prop_None, "Scale factor in Y direction - 1.0 means no scaling");
49
    ADD_PROPERTY_TYPE(ZScale, (1.0), "Scale", App::Prop_None, "Scale factor in Z direction - 1.0 means no scaling");
50
}
51

52
short Scale::mustExecute() const
53
{
54
    if (Base.isTouched() ||
55
        Uniform.isTouched() ||
56
        UniformScale.isTouched() ||
57
        XScale.isTouched() ||
58
        YScale.isTouched() ||
59
        ZScale.isTouched()) {
60
        return 1;
61
    }
62
    return 0;
63
}
64

65
Scale::ScaleParameters Scale::computeFinalParameters()
66
{
67
    Scale::ScaleParameters result;
68
    result.uniform = Uniform.getValue();
69
    result.uniformScale = UniformScale.getValue();
70
    result.XScale = XScale.getValue();
71
    result.YScale = YScale.getValue();
72
    result.ZScale = ZScale.getValue();
73

74
    return result;
75
}
76

77
TopoShape Scale::scaleShape(const TopoShape& source, const Scale::ScaleParameters& params)
78
{
79
    TopoShape result;
80

81
    if (params.uniform) {
82
        result = uniformScale(source, params.uniformScale);
83
        return result;
84
    }
85

86
    return nonuniformScale(source, params);
87
}
88

89
TopoShape Scale::uniformScale(const TopoShape& source, const double& factor)
90
{
91
//    Base::Console().Message("FS::uniformScale()\n");
92
    TopoDS_Shape transShape;
93
    TopoShape transTopo;
94
    try {
95
        gp_Trsf scaleTransform;
96
        scaleTransform.SetScale(gp_Pnt(0, 0, 0), factor);
97

98
        BRepBuilderAPI_Transform mkTrf(source.getShape(), scaleTransform);
99
        transShape = mkTrf.Shape();
100
    }
101
    catch (...) {
102
        return transTopo;
103
    }
104
    transTopo.setShape(transShape);
105
    return transTopo;
106
}
107

108
TopoShape Scale::nonuniformScale(const TopoShape& source, const Scale::ScaleParameters& params)
109
{
110
//    Base::Console().Message("FS::nonuniformScale()\n");
111
    Base::Matrix4D matScale;
112
    matScale.scale(params.XScale, params.YScale, params.ZScale);
113

114
    // copy the FreeCAD matrix cell values to an OCC matrix
115
    gp_GTrsf mat;
116
    mat.SetValue(1,1,matScale[0][0]);
117
    mat.SetValue(2,1,matScale[1][0]);
118
    mat.SetValue(3,1,matScale[2][0]);
119
    mat.SetValue(1,2,matScale[0][1]);
120
    mat.SetValue(2,2,matScale[1][1]);
121
    mat.SetValue(3,2,matScale[2][1]);
122
    mat.SetValue(1,3,matScale[0][2]);
123
    mat.SetValue(2,3,matScale[1][2]);
124
    mat.SetValue(3,3,matScale[2][2]);
125
    mat.SetValue(1,4,matScale[0][3]);
126
    mat.SetValue(2,4,matScale[1][3]);
127
    mat.SetValue(3,4,matScale[2][3]);
128

129
    // this copy step seems to eliminate Part.OCCError: gp_GTrsf::Trsf() - non-orthogonal GTrsf
130
    // which may to be related to the tessellation of the input shape.  See Github issue #9651
131
    // for more detail.
132
    BRepBuilderAPI_Copy copier(source.getShape());
133
    TopoShape transTopo;
134
    try {
135
        BRepBuilderAPI_GTransform mkTrf(copier.Shape(), mat, true);
136
        transTopo.setShape(mkTrf.Shape());
137
    }
138
    catch (...) {
139
        Base::Console().Warning("FeatureScale failed on nonuniform scale\n");
140
        return transTopo;
141
    }
142
    return transTopo;
143
}
144

145
App::DocumentObjectExecReturn* Scale::execute()
146
{
147
//    Base::Console().Message("FS::execute()\n");
148
    App::DocumentObject* link = Base.getValue();
149
    if (!link)
150
        return new App::DocumentObjectExecReturn("No object linked");
151

152
    try {
153
        Scale::ScaleParameters params = computeFinalParameters();
154
        TopoShape result = scaleShape(Feature::getTopoShape(link), params);
155
        this->Shape.setValue(result);
156
        return App::DocumentObject::StdReturn;
157
    }
158
    catch (Standard_Failure& e) {
159
        return new App::DocumentObjectExecReturn(e.GetMessageString());
160
    }
161
}
162

163

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

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

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

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