FreeCAD

Форк
0
/
FeatureMeshSolid.cpp 
358 строк · 12.4 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2006 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

25
#include <App/Document.h>
26

27
#include "FeatureMeshSolid.h"
28

29

30
namespace Mesh
31
{
32
const App::PropertyIntegerConstraint::Constraints intSampling = {0, INT_MAX, 1};
33
const App::PropertyLength::Constraints floatRange = {0.0, FLT_MAX, 1.0};
34
}  // namespace Mesh
35

36
using namespace Mesh;
37
using namespace MeshCore;
38

39
PROPERTY_SOURCE(Mesh::Sphere, Mesh::Feature)
40

41
Sphere::Sphere()
42
{
43
    ADD_PROPERTY(Radius, (5.0));
44
    ADD_PROPERTY(Sampling, (50));
45
    Radius.setConstraints(&floatRange);
46
    Sampling.setConstraints(&intSampling);
47
}
48

49
short Sphere::mustExecute() const
50
{
51
    if (Radius.isTouched() || Sampling.isTouched()) {
52
        return 1;
53
    }
54
    return Feature::mustExecute();
55
}
56

57
App::DocumentObjectExecReturn* Sphere::execute()
58
{
59
    std::unique_ptr<MeshObject> mesh(
60
        MeshObject::createSphere((float)Radius.getValue(), Sampling.getValue()));
61
    if (mesh.get()) {
62
        mesh->setPlacement(this->Placement.getValue());
63
        Mesh.setValue(mesh->getKernel());
64
        return App::DocumentObject::StdReturn;
65
    }
66
    else {
67
        return new App::DocumentObjectExecReturn("Cannot create sphere", this);
68
    }
69
}
70

71
void Sphere::handleChangedPropertyType(Base::XMLReader& reader,
72
                                       const char* TypeName,
73
                                       App::Property* prop)
74
{
75
    if (prop == &Radius && strcmp(TypeName, "App::PropertyFloatConstraint") == 0) {
76
        App::PropertyFloatConstraint r;
77
        r.Restore(reader);
78
        Radius.setValue(r.getValue());
79
    }
80
    else {
81
        Mesh::Feature::handleChangedPropertyType(reader, TypeName, prop);
82
    }
83
}
84

85
// -------------------------------------------------------------
86

87
PROPERTY_SOURCE(Mesh::Ellipsoid, Mesh::Feature)
88

89
Ellipsoid::Ellipsoid()
90
{
91
    ADD_PROPERTY(Radius1, (2.0));
92
    ADD_PROPERTY(Radius2, (4.0));
93
    ADD_PROPERTY(Sampling, (50));
94
    Radius1.setConstraints(&floatRange);
95
    Radius2.setConstraints(&floatRange);
96
    Sampling.setConstraints(&intSampling);
97
}
98

99
short Ellipsoid::mustExecute() const
100
{
101
    if (Radius1.isTouched() || Radius2.isTouched() || Sampling.isTouched()) {
102
        return 1;
103
    }
104
    return Feature::mustExecute();
105
}
106

107
App::DocumentObjectExecReturn* Ellipsoid::execute()
108
{
109
    std::unique_ptr<MeshObject> mesh(MeshObject::createEllipsoid((float)Radius1.getValue(),
110
                                                                 (float)Radius2.getValue(),
111
                                                                 Sampling.getValue()));
112
    if (mesh.get()) {
113
        mesh->setPlacement(this->Placement.getValue());
114
        Mesh.setValue(mesh->getKernel());
115
        return App::DocumentObject::StdReturn;
116
    }
117
    else {
118
        return new App::DocumentObjectExecReturn("Cannot create ellipsoid", this);
119
    }
120
}
121

122
void Ellipsoid::handleChangedPropertyType(Base::XMLReader& reader,
123
                                          const char* TypeName,
124
                                          App::Property* prop)
125
{
126
    if ((prop == &Radius1 || prop == &Radius2)
127
        && strcmp(TypeName, "App::PropertyFloatConstraint") == 0) {
128
        App::PropertyFloatConstraint r;
129
        r.Restore(reader);
130
        static_cast<App::PropertyLength*>(prop)->setValue(r.getValue());
131
    }
132
    else {
133
        Mesh::Feature::handleChangedPropertyType(reader, TypeName, prop);
134
    }
135
}
136

137
// -------------------------------------------------------------
138

139
PROPERTY_SOURCE(Mesh::Cylinder, Mesh::Feature)
140

141
Cylinder::Cylinder()
142
{
143
    ADD_PROPERTY(Radius, (2.0));
144
    ADD_PROPERTY(Length, (10.0));
145
    ADD_PROPERTY(EdgeLength, (1.0));
146
    ADD_PROPERTY(Closed, (true));
147
    ADD_PROPERTY(Sampling, (50));
148
    Radius.setConstraints(&floatRange);
149
    Length.setConstraints(&floatRange);
150
    EdgeLength.setConstraints(&floatRange);
151
    Sampling.setConstraints(&intSampling);
152
}
153

154
short Cylinder::mustExecute() const
155
{
156
    if (Radius.isTouched() || Length.isTouched() || EdgeLength.isTouched() || Closed.isTouched()
157
        || Sampling.isTouched()) {
158
        return 1;
159
    }
160
    return Feature::mustExecute();
161
}
162

163
App::DocumentObjectExecReturn* Cylinder::execute()
164
{
165
    std::unique_ptr<MeshObject> mesh(MeshObject::createCylinder((float)Radius.getValue(),
166
                                                                (float)Length.getValue(),
167
                                                                Closed.getValue(),
168
                                                                (float)EdgeLength.getValue(),
169
                                                                Sampling.getValue()));
170
    if (mesh.get()) {
171
        mesh->setPlacement(this->Placement.getValue());
172
        Mesh.setValue(mesh->getKernel());
173
        return App::DocumentObject::StdReturn;
174
    }
175
    else {
176
        return new App::DocumentObjectExecReturn("Cannot create cylinder", this);
177
    }
178
}
179

180
void Cylinder::handleChangedPropertyType(Base::XMLReader& reader,
181
                                         const char* TypeName,
182
                                         App::Property* prop)
183
{
184
    if ((prop == &Radius || prop == &Length || prop == &EdgeLength)
185
        && strcmp(TypeName, "App::PropertyFloatConstraint") == 0) {
186
        App::PropertyFloatConstraint r;
187
        r.Restore(reader);
188
        static_cast<App::PropertyLength*>(prop)->setValue(r.getValue());
189
    }
190
    else {
191
        Mesh::Feature::handleChangedPropertyType(reader, TypeName, prop);
192
    }
193
}
194

195
// -------------------------------------------------------------
196

197
PROPERTY_SOURCE(Mesh::Cone, Mesh::Feature)
198

199
Cone::Cone()
200
{
201
    ADD_PROPERTY(Radius1, (2.0));
202
    ADD_PROPERTY(Radius2, (4.0));
203
    ADD_PROPERTY(Length, (10.0));
204
    ADD_PROPERTY(EdgeLength, (1.0));
205
    ADD_PROPERTY(Closed, (true));
206
    ADD_PROPERTY(Sampling, (50));
207
    Radius1.setConstraints(&floatRange);
208
    Radius2.setConstraints(&floatRange);
209
    Length.setConstraints(&floatRange);
210
    EdgeLength.setConstraints(&floatRange);
211
    Sampling.setConstraints(&intSampling);
212
}
213

214
short Cone::mustExecute() const
215
{
216
    if (Radius1.isTouched() || Radius2.isTouched() || Length.isTouched() || EdgeLength.isTouched()
217
        || Closed.isTouched() || Sampling.isTouched()) {
218
        return 1;
219
    }
220
    return Feature::mustExecute();
221
}
222

223
App::DocumentObjectExecReturn* Cone::execute()
224
{
225
    std::unique_ptr<MeshObject> mesh(MeshObject::createCone((float)Radius1.getValue(),
226
                                                            (float)Radius2.getValue(),
227
                                                            (float)Length.getValue(),
228
                                                            Closed.getValue(),
229
                                                            (float)EdgeLength.getValue(),
230
                                                            Sampling.getValue()));
231
    if (mesh.get()) {
232
        mesh->setPlacement(this->Placement.getValue());
233
        Mesh.setValue(mesh->getKernel());
234
        return App::DocumentObject::StdReturn;
235
    }
236
    else {
237
        return new App::DocumentObjectExecReturn("Cannot create cone", this);
238
    }
239
}
240

241
void Cone::handleChangedPropertyType(Base::XMLReader& reader,
242
                                     const char* TypeName,
243
                                     App::Property* prop)
244
{
245
    if ((prop == &Radius1 || prop == &Radius2 || prop == &Length || prop == &EdgeLength)
246
        && strcmp(TypeName, "App::PropertyFloatConstraint") == 0) {
247
        App::PropertyFloatConstraint r;
248
        r.Restore(reader);
249
        static_cast<App::PropertyLength*>(prop)->setValue(r.getValue());
250
    }
251
    else {
252
        Mesh::Feature::handleChangedPropertyType(reader, TypeName, prop);
253
    }
254
}
255

256
// -------------------------------------------------------------
257

258
PROPERTY_SOURCE(Mesh::Torus, Mesh::Feature)
259

260
Torus::Torus()
261
{
262
    ADD_PROPERTY(Radius1, (10.0));
263
    ADD_PROPERTY(Radius2, (2.0));
264
    ADD_PROPERTY(Sampling, (50));
265
    Radius1.setConstraints(&floatRange);
266
    Radius2.setConstraints(&floatRange);
267
    Sampling.setConstraints(&intSampling);
268
}
269

270
short Torus::mustExecute() const
271
{
272
    if (Radius1.isTouched() || Radius2.isTouched() || Sampling.isTouched()) {
273
        return 1;
274
    }
275
    return Feature::mustExecute();
276
}
277

278
App::DocumentObjectExecReturn* Torus::execute()
279
{
280
    std::unique_ptr<MeshObject> mesh(MeshObject::createTorus((float)Radius1.getValue(),
281
                                                             (float)Radius2.getValue(),
282
                                                             Sampling.getValue()));
283
    if (mesh.get()) {
284
        mesh->setPlacement(this->Placement.getValue());
285
        Mesh.setValue(mesh->getKernel());
286
        return App::DocumentObject::StdReturn;
287
    }
288
    else {
289
        return new App::DocumentObjectExecReturn("Cannot create torus", this);
290
    }
291
}
292

293
void Torus::handleChangedPropertyType(Base::XMLReader& reader,
294
                                      const char* TypeName,
295
                                      App::Property* prop)
296
{
297
    if ((prop == &Radius1 || prop == &Radius2)
298
        && strcmp(TypeName, "App::PropertyFloatConstraint") == 0) {
299
        App::PropertyFloatConstraint r;
300
        r.Restore(reader);
301
        static_cast<App::PropertyLength*>(prop)->setValue(r.getValue());
302
    }
303
    else {
304
        Mesh::Feature::handleChangedPropertyType(reader, TypeName, prop);
305
    }
306
}
307

308
// -------------------------------------------------------------
309

310
PROPERTY_SOURCE(Mesh::Cube, Mesh::Feature)
311

312
Cube::Cube()
313
{
314
    ADD_PROPERTY_TYPE(Length, (10.0f), "Cube", App::Prop_None, "The length of the cube");
315
    ADD_PROPERTY_TYPE(Width, (10.0f), "Cube", App::Prop_None, "The width of the cube");
316
    ADD_PROPERTY_TYPE(Height, (10.0f), "Cube", App::Prop_None, "The height of the cube");
317
    Length.setConstraints(&floatRange);
318
    Width.setConstraints(&floatRange);
319
    Height.setConstraints(&floatRange);
320
}
321

322
short Cube::mustExecute() const
323
{
324
    if (Length.isTouched() || Width.isTouched() || Height.isTouched()) {
325
        return 1;
326
    }
327
    return Feature::mustExecute();
328
}
329

330
App::DocumentObjectExecReturn* Cube::execute()
331
{
332
    std::unique_ptr<MeshObject> mesh(MeshObject::createCube((float)Length.getValue(),
333
                                                            (float)Width.getValue(),
334
                                                            (float)Height.getValue()));
335
    if (mesh.get()) {
336
        mesh->setPlacement(this->Placement.getValue());
337
        Mesh.setValue(mesh->getKernel());
338
        return App::DocumentObject::StdReturn;
339
    }
340
    else {
341
        return new App::DocumentObjectExecReturn("Cannot create cube", this);
342
    }
343
}
344

345
void Cube::handleChangedPropertyType(Base::XMLReader& reader,
346
                                     const char* TypeName,
347
                                     App::Property* prop)
348
{
349
    if ((prop == &Length || prop == &Width || prop == &Height)
350
        && strcmp(TypeName, "App::PropertyFloatConstraint") == 0) {
351
        App::PropertyFloatConstraint r;
352
        r.Restore(reader);
353
        static_cast<App::PropertyLength*>(prop)->setValue(r.getValue());
354
    }
355
    else {
356
        Mesh::Feature::handleChangedPropertyType(reader, TypeName, prop);
357
    }
358
}
359

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

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

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

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