FreeCAD

Форк
0
/
FeatureTest.cpp 
330 строк · 11.2 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2002 Jürgen Riegel <juergen.riegel@web.de>              *
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

24
#include "PreCompiled.h"
25
#ifndef _PreComp_
26
#include <boost/core/ignore_unused.hpp>
27
#include <sstream>
28
#endif
29

30
#include <Base/Console.h>
31
#include <Base/Exception.h>
32
#include <Base/Interpreter.h>
33
#include <Base/Unit.h>
34
#include <CXX/Objects.hxx>
35

36
#include "FeatureTest.h"
37
#include "Material.h"
38
#include "Range.h"
39

40
#ifdef _MSC_VER
41
#pragma warning( disable : 4700 )
42
#pragma warning( disable : 4723 )
43
#endif
44

45
using namespace App;
46

47

48
PROPERTY_SOURCE(App::FeatureTest, App::DocumentObject)
49

50
const char* enums[]= {"Zero","One","Two","Three","Four",nullptr};
51
const PropertyIntegerConstraint::Constraints intPercent = {0,100,1};
52
const PropertyFloatConstraint::Constraints floatPercent = {0.0,100.0,1.0};
53

54

55
FeatureTest::FeatureTest()
56
{
57
  ADD_PROPERTY(Integer,(4711)  );
58
  ADD_PROPERTY(Float  ,(47.11f) );
59
  ADD_PROPERTY(Bool   ,(true)  );
60
  ADD_PROPERTY(BoolList,(false));
61
  ADD_PROPERTY(String ,("4711"));
62
  ADD_PROPERTY(Path   ,("c:\\temp"));
63
  ADD_PROPERTY(StringList ,("4711"));
64

65
  ADD_PROPERTY(Enum   ,(4));
66
  Enum.setEnums(enums);
67
  ADD_PROPERTY(ConstraintInt ,(5));
68
  ConstraintInt.setConstraints(&intPercent);
69
  ADD_PROPERTY(ConstraintFloat ,(5.0));
70
  ConstraintFloat.setConstraints(&floatPercent);
71

72
  App::Color c;
73
  App::Material mat(App::Material::GOLD);
74
  ADD_PROPERTY(Colour      ,(c) );
75
  ADD_PROPERTY(ColourList  ,(c) );
76
  ADD_PROPERTY(Material    ,(mat));
77
  ADD_PROPERTY(MaterialList,(mat));
78

79
  ADD_PROPERTY(Distance,(47.11f) );
80
  ADD_PROPERTY(Angle   ,(3.0f) );
81

82
  ADD_PROPERTY(IntegerList,(4711)  );
83
  ADD_PROPERTY(FloatList  ,(47.11f) );
84

85
  ADD_PROPERTY(Link       ,(nullptr));
86
  ADD_PROPERTY(LinkSub    ,(nullptr));
87
  ADD_PROPERTY(LinkList   ,(nullptr));
88
  ADD_PROPERTY(LinkSubList,(nullptr));
89

90
  ADD_PROPERTY(Vector    ,(1.0,2.0,3.0));
91
  ADD_PROPERTY(VectorList,(3.0,2.0,1.0));
92
  ADD_PROPERTY(Matrix    ,(Base::Matrix4D(1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0,16.0)));
93
  ADD_PROPERTY(Placement ,(Base::Placement()));
94

95
  // properties for recompute testing
96
  static const char* group = "Feature Test";
97
  ADD_PROPERTY_TYPE(Source1       ,(nullptr),group,Prop_None,"Source for testing links");
98
  ADD_PROPERTY_TYPE(Source2       ,(nullptr),group,Prop_None,"Source for testing links");
99
  ADD_PROPERTY_TYPE(SourceN       ,(nullptr),group,Prop_None,"Source for testing links");
100
  ADD_PROPERTY_TYPE(ExecResult    ,("empty"),group,Prop_None,"Result of the execution");
101
  ADD_PROPERTY_TYPE(ExceptionType ,(0),group,Prop_None,"The type of exception the execution method throws");
102
  ADD_PROPERTY_TYPE(ExecCount     ,(0),group,Prop_None,"Number of executions");
103

104
  // properties with types
105
  ADD_PROPERTY_TYPE(TypeHidden  ,(4711),group,Prop_Hidden,"An example property which has the type 'Hidden'"  );
106
  ADD_PROPERTY_TYPE(TypeReadOnly,(4711),group,Prop_ReadOnly ,"An example property which has the type 'ReadOnly'"  );
107
  ADD_PROPERTY_TYPE(TypeOutput  ,(4711),group,Prop_Output ,"An example property which has the type 'Output'"  );
108
  ADD_PROPERTY_TYPE(TypeTransient,(4711),group,Prop_Transient ,"An example property which has the type 'Transient'"  );
109
  ADD_PROPERTY_TYPE(TypeNoRecompute,(4711),group,Prop_NoRecompute,"An example property which has the type 'NoRecompute'");
110
  ADD_PROPERTY_TYPE(TypeAll     ,(4711),group,(App::PropertyType) (Prop_Output|Prop_ReadOnly |Prop_Hidden ),
111
      "An example property which has the types 'Output', 'ReadOnly' and 'Hidden'");
112

113
  ADD_PROPERTY(QuantityLength,(1.0));
114
  QuantityLength.setUnit(Base::Unit::Length);
115
  ADD_PROPERTY(QuantityOther,(5.0));
116
  QuantityOther.setUnit(Base::Unit(-3,1));
117
}
118

119
FeatureTest::~FeatureTest() = default;
120

121
short FeatureTest::mustExecute() const
122
{
123
    return DocumentObject::mustExecute();
124
}
125

126
DocumentObjectExecReturn *FeatureTest::execute()
127
{
128
    // Enum handling
129
    Enumeration enumObj1 = Enum.getEnum();
130
    enumObj1.setValue(7, false);
131
    enumObj1.setValue(4, true);
132

133
    Enumeration enumObj2 = Enum.getEnum();
134
    enumObj2.setValue(4, true);
135

136
    Enumeration enumObj3(enumObj2);
137
    const char* val = enumObj3.getCStr();
138
    enumObj3.isValue(val);
139
    enumObj3.getEnumVector();
140

141
    Enumeration enumObj4("Single item");
142
    enumObj4.setEnums(enums);
143
    boost::ignore_unused(enumObj4 == enumObj2);
144
    enumObj4.setEnums(nullptr);
145
    enumObj4 = enumObj2;
146
    boost::ignore_unused(enumObj4 == enumObj4.getCStr());
147

148
    Enumeration enumObj5(enums, enums[3]);
149
    enumObj5.isValue(enums[2]);
150
    enumObj5.isValue(enums[3]);
151
    enumObj5.contains(enums[1]);
152

153
    Enumeration enumObj6;
154
    enumObj6.setEnums(enums);
155
    enumObj6.setValue(enums[1]);
156
    std::vector<std::string> list;
157
    list.emplace_back("Hello");
158
    list.emplace_back("World");
159
    enumObj6.setEnums(list);
160
    enumObj6.setValue(list.back());
161

162
    int *i=nullptr,j;
163
    float f;
164
    void *s;
165
    std::string t;
166

167
    // Code analyzers may complain about some errors. This can be ignored
168
    // because this is done on purpose to test the error handling mechanism
169
    switch(ExceptionType.getValue())
170
    {
171
        case 0: break;
172
        case 1: throw std::runtime_error("Test Exception");
173
        case 2: throw Base::RuntimeError("FeatureTestException::execute(): Testexception");
174
        default: (void)i; (void)j; (void)f; (void)s; (void)t; break;
175
    }
176

177
    ExecCount.setValue(ExecCount.getValue() + 1);
178

179
    ExecResult.setValue("Exec");
180

181
    return DocumentObject::StdReturn;
182
}
183

184
// ----------------------------------------------------------------------------
185

186
PROPERTY_SOURCE(App::FeatureTestException, App::FeatureTest)
187

188

189
FeatureTestException::FeatureTestException()
190
{
191
    ADD_PROPERTY(ExceptionType,(Base::Exception::getClassTypeId().getKey())  );
192
}
193

194
DocumentObjectExecReturn *FeatureTestException::execute()
195
{
196
    //ExceptionType;
197
    throw Base::RuntimeError("FeatureTestException::execute(): Testexception  ;-)");
198

199
    return nullptr;
200
}
201

202
// ----------------------------------------------------------------------------
203

204
PROPERTY_SOURCE(App::FeatureTestColumn, App::DocumentObject)
205

206

207
FeatureTestColumn::FeatureTestColumn()
208
{
209
    ADD_PROPERTY_TYPE(Column, ("A"), "Test", App::Prop_None, "");
210
    ADD_PROPERTY_TYPE(Silent, (false), "Test", App::Prop_None, "");
211
    ADD_PROPERTY_TYPE(Value, (0L), "Test", App::Prop_Output, "");
212
}
213

214
DocumentObjectExecReturn *FeatureTestColumn::execute()
215
{
216
    Value.setValue(decodeColumn(Column.getStrValue(), Silent.getValue()));
217
    return nullptr;
218
}
219

220
// ----------------------------------------------------------------------------
221

222
PROPERTY_SOURCE(App::FeatureTestRow, App::DocumentObject)
223

224

225
FeatureTestRow::FeatureTestRow()
226
{
227
    ADD_PROPERTY_TYPE(Row, ("1"), "Test", App::Prop_None, "");
228
    ADD_PROPERTY_TYPE(Silent, (false), "Test", App::Prop_None, "");
229
    ADD_PROPERTY_TYPE(Value, (0L), "Test", App::Prop_Output, "");
230
}
231

232
DocumentObjectExecReturn *FeatureTestRow::execute()
233
{
234
    Value.setValue(decodeRow(Row.getStrValue(), Silent.getValue()));
235
    return nullptr;
236
}
237

238
// ----------------------------------------------------------------------------
239

240
PROPERTY_SOURCE(App::FeatureTestAbsAddress, App::DocumentObject)
241

242

243
FeatureTestAbsAddress::FeatureTestAbsAddress()
244
{
245
    ADD_PROPERTY_TYPE(Address, (""), "Test", Prop_None, "");
246
    ADD_PROPERTY_TYPE(Valid, (false), "Test", PropertyType(Prop_Output | Prop_ReadOnly), "");
247
}
248

249
DocumentObjectExecReturn *FeatureTestAbsAddress::execute()
250
{
251
    CellAddress address;
252
    Valid.setValue(address.parseAbsoluteAddress(Address.getValue()));
253
    return StdReturn;
254
}
255

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

258
PROPERTY_SOURCE(App::FeatureTestPlacement, App::DocumentObject)
259

260

261
FeatureTestPlacement::FeatureTestPlacement()
262
{
263
    ADD_PROPERTY_TYPE(Input1, (Base::Placement()), "Test", Prop_None, "");
264
    ADD_PROPERTY_TYPE(Input2, (Base::Placement()), "Test", Prop_None, "");
265
    ADD_PROPERTY_TYPE(MultLeft, (Base::Placement()), "Test", Prop_Output, "");
266
    ADD_PROPERTY_TYPE(MultRight, (Base::Placement()), "Test", Prop_Output, "");
267
}
268

269
DocumentObjectExecReturn *FeatureTestPlacement::execute()
270
{
271
    Base::Placement p1 = Input1.getValue();
272
    Base::Placement q1 = Input1.getValue();
273
    Base::Placement p2 = Input2.getValue();
274
    MultLeft.setValue(p1.multLeft(p2));
275
    MultRight.setValue(q1.multRight(p2));
276
    return nullptr;
277
}
278

279
// ----------------------------------------------------------------------------
280

281
PROPERTY_SOURCE(App::FeatureTestAttribute, App::DocumentObject)
282

283

284
FeatureTestAttribute::FeatureTestAttribute()
285
{
286
    ADD_PROPERTY(Object, (Py::Object()));
287
    ADD_PROPERTY(Attribute, ("Name"));
288
}
289

290
FeatureTestAttribute::~FeatureTestAttribute()
291
{
292
    Base::PyGILStateLocker lock;
293
    try {
294
        Object.getValue().getAttr("Name");
295
#if PYCXX_VERSION_MAJOR >= 7
296
        Py::ifPyErrorThrowCxxException();
297
#else
298
        if (PyErr_Occurred())
299
            throw Py::RuntimeError();
300
#endif
301
    }
302
    catch (Py::RuntimeError& e) {
303
        e.clear();
304
    }
305
    catch (Py::Exception& e) {
306
        e.clear();
307
        Base::Console().Error("Unexpected exception in ~FeatureTestRemoval()\n");
308
    }
309
}
310

311
DocumentObjectExecReturn *FeatureTestAttribute::execute()
312
{
313
    Base::PyGILStateLocker lock;
314
    try {
315
        Object.getValue().getAttr(Attribute.getValue());
316
#if PYCXX_VERSION_MAJOR >= 7
317
        Py::ifPyErrorThrowCxxException();
318
#else
319
        if (PyErr_Occurred())
320
            throw Py::AttributeError();
321
#endif
322
    }
323
    catch (Py::AttributeError& e) {
324
        e.clear();
325
        std::stringstream str;
326
        str << "No such attribute '" << Attribute.getValue() << "'";
327
        throw Base::AttributeError(str.str());
328
    }
329
    return StdReturn;
330
}
331

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

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

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

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