FreeCAD

Форк
0
/
ParameterPy.cpp 
889 строк · 27.3 Кб
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 program is free software; you can redistribute it and/or modify  *
7
 *   it under the terms of the GNU Library General Public License (LGPL)   *
8
 *   as published by the Free Software Foundation; either version 2 of     *
9
 *   the License, or (at your option) any later version.                   *
10
 *   for detail see the LICENCE text file.                                 *
11
 *                                                                         *
12
 *   FreeCAD is distributed in the hope that it will be useful,            *
13
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15
 *   GNU Library General Public License for more details.                  *
16
 *                                                                         *
17
 *   You should have received a copy of the GNU Library General Public     *
18
 *   License along with FreeCAD; if not, write to the Free Software        *
19
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
20
 *   USA                                                                   *
21
 *                                                                         *
22
 ***************************************************************************/
23

24

25
#include "PreCompiled.h"
26

27
#ifndef _PreComp_
28
#ifdef FC_OS_WIN32
29
#include <xercesc/sax/SAXParseException.hpp>
30
#endif
31
#include <list>
32
#include <sstream>
33
#include <string>
34
#include <utility>
35
#endif
36

37
#ifdef FC_OS_LINUX
38
#include <unistd.h>
39
#endif
40

41
#include "Parameter.h"
42
#include "Exception.h"
43
#include "Interpreter.h"
44

45

46
namespace Base
47
{
48

49
class ParameterGrpObserver: public ParameterGrp::ObserverType  // NOLINT
50
{
51
public:
52
    explicit ParameterGrpObserver(const Py::Object& obj)
53
    {
54
        inst = obj;
55
    }
56
    ParameterGrpObserver(const Py::Object& obj, const Py::Object& callable, ParameterGrp* target)
57
        : callable(callable)
58
        , _target(target)
59
        , inst(obj)
60
    {}
61
    ~ParameterGrpObserver() override
62
    {
63
        Base::PyGILStateLocker lock;
64
        inst = Py::None();
65
        callable = Py::None();
66
    }
67
    void OnChange(ParameterGrp::SubjectType& rCaller, ParameterGrp::MessageType Reason) override
68
    {
69
        Base::PyGILStateLocker lock;
70
        try {
71
            ParameterGrp& rGrp = static_cast<ParameterGrp&>(rCaller);
72
            ParameterGrp::handle hGrp(&rGrp);
73
            Py::Callable method(this->inst.getAttr(std::string("onChange")));
74
            Py::Tuple args(2);
75
            args.setItem(0, Py::asObject(GetPyObject(hGrp)));
76
            // A Reason of null indicates to clear the parameter group
77
            if (Reason && Reason[0] != '\0') {
78
                args.setItem(1, Py::String(Reason));
79
            }
80
            method.apply(args);
81
        }
82
        catch (Py::Exception&) {
83
            Base::PyException e;  // extract the Python error text
84
            e.ReportException();
85
        }
86
    }
87
    bool isEqual(const Py::Object& obj) const
88
    {
89
        return this->inst.is(obj);
90
    }
91

92
public:
93
    Py::Object callable;
94
    boost::signals2::scoped_connection conn;
95
    ParameterGrp* _target = nullptr;  // no reference counted, do not access
96

97
private:
98
    Py::Object inst;
99
};
100

101
using ParameterGrpObserverList = std::list<ParameterGrpObserver*>;
102

103
class ParameterGrpPy: public Py::PythonExtension<ParameterGrpPy>  // NOLINT
104
{
105
public:
106
    static void init_type();  // announce properties and methods
107

108
    explicit ParameterGrpPy(const Base::Reference<ParameterGrp>& rcParamGrp);
109
    ~ParameterGrpPy() override;
110

111
    Py::Object repr() override;
112

113
    Py::Object getGroup(const Py::Tuple&);
114
    Py::Object getGroupName(const Py::Tuple&);
115
    Py::Object getGroups(const Py::Tuple&);
116
    Py::Object remGroup(const Py::Tuple&);
117
    Py::Object hasGroup(const Py::Tuple&);
118

119
    Py::Object getManager(const Py::Tuple&);
120
    Py::Object getParent(const Py::Tuple&);
121

122
    Py::Object isEmpty(const Py::Tuple&);
123
    Py::Object clear(const Py::Tuple&);
124

125
    Py::Object attach(const Py::Tuple&);
126
    Py::Object attachManager(const Py::Tuple& args);
127
    Py::Object detach(const Py::Tuple&);
128
    Py::Object notify(const Py::Tuple&);
129
    Py::Object notifyAll(const Py::Tuple&);
130

131
    Py::Object setBool(const Py::Tuple&);
132
    Py::Object getBool(const Py::Tuple&);
133
    Py::Object getBools(const Py::Tuple&);
134
    Py::Object remBool(const Py::Tuple&);
135

136
    Py::Object setInt(const Py::Tuple&);
137
    Py::Object getInt(const Py::Tuple&);
138
    Py::Object getInts(const Py::Tuple&);
139
    Py::Object remInt(const Py::Tuple&);
140

141
    Py::Object setUnsigned(const Py::Tuple&);
142
    Py::Object getUnsigned(const Py::Tuple&);
143
    Py::Object getUnsigneds(const Py::Tuple&);
144
    Py::Object remUnsigned(const Py::Tuple&);
145

146
    Py::Object setFloat(const Py::Tuple&);
147
    Py::Object getFloat(const Py::Tuple&);
148
    Py::Object getFloats(const Py::Tuple&);
149
    Py::Object remFloat(const Py::Tuple&);
150

151
    Py::Object setString(const Py::Tuple&);
152
    Py::Object getString(const Py::Tuple&);
153
    Py::Object getStrings(const Py::Tuple&);
154
    Py::Object remString(const Py::Tuple&);
155

156
    Py::Object importFrom(const Py::Tuple&);
157
    Py::Object insert(const Py::Tuple&);
158
    Py::Object exportTo(const Py::Tuple&);
159

160
    Py::Object getContents(const Py::Tuple&);
161

162
private:
163
    ParameterGrp::handle _cParamGrp;
164
    ParameterGrpObserverList _observers;
165
};
166

167
// ---------------------------------------------------------
168

169
void ParameterGrpPy::init_type()
170
{
171
    behaviors().name("ParameterGrp");
172
    behaviors().doc("Python interface class to set parameters");
173
    // you must have overwritten the virtual functions
174
    behaviors().supportRepr();
175
    behaviors().supportGetattr();
176
    behaviors().supportSetattr();
177
    behaviors().readyType();
178

179
    add_varargs_method("GetGroup", &ParameterGrpPy::getGroup, "GetGroup(str)");
180
    add_varargs_method("GetGroupName", &ParameterGrpPy::getGroupName, "GetGroupName()");
181
    add_varargs_method("GetGroups", &ParameterGrpPy::getGroups, "GetGroups()");
182
    add_varargs_method("RemGroup", &ParameterGrpPy::remGroup, "RemGroup(str)");
183
    add_varargs_method("HasGroup", &ParameterGrpPy::hasGroup, "HasGroup(str)");
184

185
    add_varargs_method("Manager", &ParameterGrpPy::getManager, "Manager()");
186
    add_varargs_method("Parent", &ParameterGrpPy::getParent, "Parent()");
187

188
    add_varargs_method("IsEmpty", &ParameterGrpPy::isEmpty, "IsEmpty()");
189
    add_varargs_method("Clear", &ParameterGrpPy::clear, "Clear()");
190

191
    add_varargs_method("Attach", &ParameterGrpPy::attach, "Attach()");
192
    add_varargs_method(
193
        "AttachManager",
194
        &ParameterGrpPy::attachManager,
195
        "AttachManager(observer) -- attach parameter manager for notification\n\n"
196
        "This method attaches a user defined observer to the manager (i.e. the root)\n"
197
        "of the current parameter group to receive notification of all its parameters\n"
198
        "and those from its sub-groups\n\n"
199
        "The method expects the observer to have a callable attribute as shown below\n"
200
        "       slotParamChanged(param, tp, name, value)\n"
201
        "where 'param' is the parameter group causing the change, 'tp' is the type of\n"
202
        "the parameter, 'name' is the name of the parameter, and 'value' is the current\n"
203
        "value.\n\n"
204
        "The possible value of type are, 'FCBool', 'FCInt', 'FCUint', 'FCFloat', 'FCText',\n"
205
        "and 'FCParamGroup'. The notification is triggered when value is changed, in which\n"
206
        "case 'value' contains the new value in text form, or, when the parameter is removed,\n"
207
        "in which case 'value' is empty.\n\n"
208
        "For 'FCParamGroup' type, the observer will be notified in the following events.\n"
209
        "* Group creation: both 'name' and 'value' contain the name of the new group\n"
210
        "* Group removal: both 'name' and 'value' are empty\n"
211
        "* Group rename: 'name' is the new name, and 'value' is the old name");
212
    add_varargs_method("Detach", &ParameterGrpPy::detach, "Detach()");
213
    add_varargs_method("Notify", &ParameterGrpPy::notify, "Notify()");
214
    add_varargs_method("NotifyAll", &ParameterGrpPy::notifyAll, "NotifyAll()");
215

216
    add_varargs_method("SetBool", &ParameterGrpPy::setBool, "SetBool()");
217
    add_varargs_method("GetBool", &ParameterGrpPy::getBool, "GetBool()");
218
    add_varargs_method("GetBools", &ParameterGrpPy::getBools, "GetBools()");
219
    add_varargs_method("RemBool", &ParameterGrpPy::remBool, "RemBool()");
220

221
    add_varargs_method("SetInt", &ParameterGrpPy::setInt, "SetInt()");
222
    add_varargs_method("GetInt", &ParameterGrpPy::getInt, "GetInt()");
223
    add_varargs_method("GetInts", &ParameterGrpPy::getInts, "GetInts()");
224
    add_varargs_method("RemInt", &ParameterGrpPy::remInt, "RemInt()");
225

226
    add_varargs_method("SetUnsigned", &ParameterGrpPy::setUnsigned, "SetUnsigned()");
227
    add_varargs_method("GetUnsigned", &ParameterGrpPy::getUnsigned, "GetUnsigned()");
228
    add_varargs_method("GetUnsigneds", &ParameterGrpPy::getUnsigneds, "GetUnsigneds()");
229
    add_varargs_method("RemUnsigned", &ParameterGrpPy::remUnsigned, "RemUnsigned()");
230

231
    add_varargs_method("SetFloat", &ParameterGrpPy::setFloat, "SetFloat()");
232
    add_varargs_method("GetFloat", &ParameterGrpPy::getFloat, "GetFloat()");
233
    add_varargs_method("GetFloats", &ParameterGrpPy::getFloats, "GetFloats()");
234
    add_varargs_method("RemFloat", &ParameterGrpPy::remFloat, "RemFloat()");
235

236
    add_varargs_method("SetString", &ParameterGrpPy::setString, "SetString()");
237
    add_varargs_method("GetString", &ParameterGrpPy::getString, "GetString()");
238
    add_varargs_method("GetStrings", &ParameterGrpPy::getStrings, "GetStrings()");
239
    add_varargs_method("RemString", &ParameterGrpPy::remString, "RemString()");
240

241
    add_varargs_method("Import", &ParameterGrpPy::importFrom, "Import()");
242
    add_varargs_method("Insert", &ParameterGrpPy::insert, "Insert()");
243
    add_varargs_method("Export", &ParameterGrpPy::exportTo, "Export()");
244

245
    add_varargs_method("GetContents", &ParameterGrpPy::getContents, "GetContents()");
246
}
247

248
ParameterGrpPy::ParameterGrpPy(const Base::Reference<ParameterGrp>& rcParamGrp)
249
    : _cParamGrp(rcParamGrp)
250
{}
251

252
ParameterGrpPy::~ParameterGrpPy()
253
{
254
    try {
255
        for (ParameterGrpObserver* obs : _observers) {
256
            if (!obs->_target) {
257
                _cParamGrp->Detach(obs);
258
            }
259
            delete obs;
260
        }
261
    }
262
    catch (...) {
263
    }
264
}
265

266
Py::Object ParameterGrpPy::repr()
267
{
268
    std::stringstream s;
269
    s << "<ParameterGrp at " << this << ">";
270
    return Py::String(s.str());  // NOLINT
271
}
272

273
Py::Object ParameterGrpPy::importFrom(const Py::Tuple& args)
274
{
275
    char* pstr = nullptr;
276
    if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) {
277
        throw Py::Exception();
278
    }
279

280
    _cParamGrp->importFrom(pstr);
281
    return Py::None();
282
}
283

284
Py::Object ParameterGrpPy::insert(const Py::Tuple& args)
285
{
286
    char* pstr = nullptr;
287
    if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) {
288
        throw Py::Exception();
289
    }
290

291
    _cParamGrp->insert(pstr);
292
    return Py::None();
293
}
294

295
Py::Object ParameterGrpPy::exportTo(const Py::Tuple& args)
296
{
297
    char* pstr = nullptr;
298
    if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) {
299
        throw Py::Exception();
300
    }
301

302
    _cParamGrp->exportTo(pstr);
303
    return Py::None();
304
}
305

306
Py::Object ParameterGrpPy::getGroup(const Py::Tuple& args)
307
{
308
    char* pstr = nullptr;
309
    if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) {
310
        throw Py::Exception();
311
    }
312

313
    try {
314
        // get the Handle of the wanted group
315
        Base::Reference<ParameterGrp> handle = _cParamGrp->GetGroup(pstr);
316
        if (handle.isValid()) {
317
            // create a python wrapper class
318
            ParameterGrpPy* pcParamGrp = new ParameterGrpPy(handle);
319
            // increment the ref count
320
            return Py::asObject(pcParamGrp);
321
        }
322

323
        throw Py::RuntimeError("GetGroup failed");
324
    }
325
    catch (const Base::Exception& e) {
326
        e.setPyException();
327
        throw Py::Exception();
328
    }
329
}
330

331
Py::Object ParameterGrpPy::getManager(const Py::Tuple& args)
332
{
333
    if (!PyArg_ParseTuple(args.ptr(), "")) {
334
        throw Py::Exception();
335
    }
336

337
    // get the Handle of the wanted group
338
    Base::Reference<ParameterGrp> handle = _cParamGrp->Manager();
339
    if (handle.isValid()) {
340
        // create a python wrapper class
341
        ParameterGrpPy* pcParamGrp = new ParameterGrpPy(handle);
342
        // increment the ref count
343
        return Py::asObject(pcParamGrp);
344
    }
345

346
    return Py::None();
347
}
348

349
Py::Object ParameterGrpPy::getParent(const Py::Tuple& args)
350
{
351
    if (!PyArg_ParseTuple(args.ptr(), "")) {
352
        throw Py::Exception();
353
    }
354

355
    // get the Handle of the wanted group
356
    Base::Reference<ParameterGrp> handle = _cParamGrp->Parent();
357
    if (handle.isValid()) {
358
        // create a python wrapper class
359
        ParameterGrpPy* pcParamGrp = new ParameterGrpPy(handle);
360
        // increment the ref count
361
        return Py::asObject(pcParamGrp);
362
    }
363

364
    return Py::None();
365
}
366

367
Py::Object ParameterGrpPy::getGroupName(const Py::Tuple& args)
368
{
369
    if (!PyArg_ParseTuple(args.ptr(), "")) {
370
        throw Py::Exception();
371
    }
372

373
    // get the Handle of the wanted group
374
    std::string name = _cParamGrp->GetGroupName();
375
    return Py::String(name);  // NOLINT
376
}
377

378
Py::Object ParameterGrpPy::getGroups(const Py::Tuple& args)
379
{
380
    if (!PyArg_ParseTuple(args.ptr(), "")) {
381
        throw Py::Exception();
382
    }
383

384
    // get the Handle of the wanted group
385
    std::vector<Base::Reference<ParameterGrp>> handle = _cParamGrp->GetGroups();
386
    Py::List list;
387
    for (const auto& it : handle) {
388
        list.append(Py::String(it->GetGroupName()));
389
    }
390

391
    return list;  // NOLINT
392
}
393

394
Py::Object ParameterGrpPy::setBool(const Py::Tuple& args)
395
{
396
    char* pstr = nullptr;
397
    int Bool = 0;
398
    if (!PyArg_ParseTuple(args.ptr(), "si", &pstr, &Bool)) {
399
        throw Py::Exception();
400
    }
401

402
    _cParamGrp->SetBool(pstr, Bool != 0);
403
    return Py::None();
404
}
405

406
Py::Object ParameterGrpPy::getBool(const Py::Tuple& args)
407
{
408
    char* pstr = nullptr;
409
    int Bool = 0;
410
    if (!PyArg_ParseTuple(args.ptr(), "s|i", &pstr, &Bool)) {
411
        throw Py::Exception();
412
    }
413

414
    return Py::Boolean(_cParamGrp->GetBool(pstr, Bool != 0));  // NOLINT
415
}
416

417
Py::Object ParameterGrpPy::getBools(const Py::Tuple& args)
418
{
419
    char* filter = nullptr;
420
    if (!PyArg_ParseTuple(args.ptr(), "|s", &filter)) {
421
        throw Py::Exception();
422
    }
423

424
    std::vector<std::pair<std::string, bool>> map = _cParamGrp->GetBoolMap(filter);
425
    Py::List list;
426
    for (const auto& it : map) {
427
        list.append(Py::String(it.first));
428
    }
429

430
    return list;  // NOLINT
431
}
432

433
Py::Object ParameterGrpPy::setInt(const Py::Tuple& args)
434
{
435
    char* pstr = nullptr;
436
    int Int = 0;
437
    if (!PyArg_ParseTuple(args.ptr(), "si", &pstr, &Int)) {
438
        throw Py::Exception();
439
    }
440

441
    _cParamGrp->SetInt(pstr, Int);
442
    return Py::None();
443
}
444

445
Py::Object ParameterGrpPy::getInt(const Py::Tuple& args)
446
{
447
    char* pstr = nullptr;
448
    int Int = 0;
449
    if (!PyArg_ParseTuple(args.ptr(), "s|i", &pstr, &Int)) {
450
        throw Py::Exception();
451
    }
452
    return Py::Long(_cParamGrp->GetInt(pstr, Int));  // NOLINT
453
}
454

455
Py::Object ParameterGrpPy::getInts(const Py::Tuple& args)
456
{
457
    char* filter = nullptr;
458
    if (!PyArg_ParseTuple(args.ptr(), "|s", &filter)) {
459
        throw Py::Exception();
460
    }
461

462
    std::vector<std::pair<std::string, long>> map = _cParamGrp->GetIntMap(filter);
463
    Py::List list;
464
    for (const auto& it : map) {
465
        list.append(Py::String(it.first));
466
    }
467

468
    return list;  // NOLINT
469
}
470

471
Py::Object ParameterGrpPy::setUnsigned(const Py::Tuple& args)
472
{
473
    char* pstr = nullptr;
474
    unsigned int UInt = 0;
475
    if (!PyArg_ParseTuple(args.ptr(), "sI", &pstr, &UInt)) {
476
        throw Py::Exception();
477
    }
478

479
    _cParamGrp->SetUnsigned(pstr, UInt);
480
    return Py::None();
481
}
482

483
Py::Object ParameterGrpPy::getUnsigned(const Py::Tuple& args)
484
{
485
    char* pstr = nullptr;
486
    unsigned int UInt = 0;
487
    if (!PyArg_ParseTuple(args.ptr(), "s|I", &pstr, &UInt)) {
488
        throw Py::Exception();
489
    }
490
    return Py::Long(_cParamGrp->GetUnsigned(pstr, UInt));  // NOLINT
491
}
492

493
Py::Object ParameterGrpPy::getUnsigneds(const Py::Tuple& args)
494
{
495
    char* filter = nullptr;
496
    if (!PyArg_ParseTuple(args.ptr(), "|s", &filter)) {
497
        throw Py::Exception();
498
    }
499

500
    std::vector<std::pair<std::string, unsigned long>> map = _cParamGrp->GetUnsignedMap(filter);
501
    Py::List list;
502
    for (const auto& it : map) {
503
        list.append(Py::String(it.first));
504
    }
505

506
    return list;  // NOLINT
507
}
508

509
Py::Object ParameterGrpPy::setFloat(const Py::Tuple& args)
510
{
511
    char* pstr = nullptr;
512
    double Float {};
513
    if (!PyArg_ParseTuple(args.ptr(), "sd", &pstr, &Float)) {
514
        throw Py::Exception();
515
    }
516

517
    _cParamGrp->SetFloat(pstr, Float);
518
    return Py::None();
519
}
520

521
Py::Object ParameterGrpPy::getFloat(const Py::Tuple& args)
522
{
523
    char* pstr = nullptr;
524
    double Float = 0.0;
525
    if (!PyArg_ParseTuple(args.ptr(), "s|d", &pstr, &Float)) {
526
        throw Py::Exception();
527
    }
528

529
    return Py::Float(_cParamGrp->GetFloat(pstr, Float));  // NOLINT
530
}
531

532
Py::Object ParameterGrpPy::getFloats(const Py::Tuple& args)
533
{
534
    char* filter = nullptr;
535
    if (!PyArg_ParseTuple(args.ptr(), "|s", &filter)) {
536
        throw Py::Exception();
537
    }
538

539
    std::vector<std::pair<std::string, double>> map = _cParamGrp->GetFloatMap(filter);
540
    Py::List list;
541
    for (const auto& it : map) {
542
        list.append(Py::String(it.first));
543
    }
544

545
    return list;  // NOLINT
546
}
547

548
Py::Object ParameterGrpPy::setString(const Py::Tuple& args)
549
{
550
    char* pstr = nullptr;
551
    char* str = nullptr;
552
    if (!PyArg_ParseTuple(args.ptr(), "ss", &pstr, &str)) {
553
        throw Py::Exception();
554
    }
555

556
    _cParamGrp->SetASCII(pstr, str);
557
    return Py::None();
558
}
559

560
Py::Object ParameterGrpPy::getString(const Py::Tuple& args)
561
{
562
    char* pstr = nullptr;
563
    char* str = "";
564
    if (!PyArg_ParseTuple(args.ptr(), "s|s", &pstr, &str)) {
565
        throw Py::Exception();
566
    }
567

568
    return Py::String(_cParamGrp->GetASCII(pstr, str));  // NOLINT
569
}
570

571
Py::Object ParameterGrpPy::getStrings(const Py::Tuple& args)
572
{
573
    char* filter = nullptr;
574
    if (!PyArg_ParseTuple(args.ptr(), "|s", &filter)) {
575
        throw Py::Exception();
576
    }
577

578
    std::vector<std::pair<std::string, std::string>> map = _cParamGrp->GetASCIIMap(filter);
579
    Py::List list;
580
    for (const auto& it : map) {
581
        list.append(Py::String(it.first));
582
    }
583

584
    return list;  // NOLINT
585
}
586

587
Py::Object ParameterGrpPy::remInt(const Py::Tuple& args)
588
{
589
    char* pstr = nullptr;
590
    if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) {
591
        throw Py::Exception();
592
    }
593

594
    _cParamGrp->RemoveInt(pstr);
595
    return Py::None();
596
}
597

598
Py::Object ParameterGrpPy::remUnsigned(const Py::Tuple& args)
599
{
600
    char* pstr = nullptr;
601
    if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) {
602
        throw Py::Exception();
603
    }
604

605
    _cParamGrp->RemoveUnsigned(pstr);
606
    return Py::None();
607
}
608

609
Py::Object ParameterGrpPy::remBool(const Py::Tuple& args)
610
{
611
    char* pstr = nullptr;
612
    if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) {
613
        throw Py::Exception();
614
    }
615

616
    _cParamGrp->RemoveBool(pstr);
617
    return Py::None();
618
}
619

620
Py::Object ParameterGrpPy::remGroup(const Py::Tuple& args)
621
{
622
    char* pstr = nullptr;
623
    if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) {
624
        throw Py::Exception();
625
    }
626

627
    _cParamGrp->RemoveGrp(pstr);
628
    return Py::None();
629
}
630

631
Py::Object ParameterGrpPy::remFloat(const Py::Tuple& args)
632
{
633
    char* pstr = nullptr;
634
    if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) {
635
        throw Py::Exception();
636
    }
637

638
    _cParamGrp->RemoveFloat(pstr);
639
    return Py::None();
640
}
641

642
Py::Object ParameterGrpPy::remString(const Py::Tuple& args)
643
{
644
    char* pstr = nullptr;
645
    if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) {
646
        throw Py::Exception();
647
    }
648

649
    _cParamGrp->RemoveASCII(pstr);
650
    return Py::None();
651
}
652

653
Py::Object ParameterGrpPy::clear(const Py::Tuple& args)
654
{
655
    if (!PyArg_ParseTuple(args.ptr(), "")) {
656
        throw Py::Exception();
657
    }
658

659
    _cParamGrp->Clear();
660
    return Py::None();
661
}
662

663
Py::Object ParameterGrpPy::isEmpty(const Py::Tuple& args)
664
{
665
    if (!PyArg_ParseTuple(args.ptr(), "")) {
666
        throw Py::Exception();
667
    }
668

669
    return Py::Boolean(_cParamGrp->IsEmpty());  // NOLINT
670
}
671

672
Py::Object ParameterGrpPy::hasGroup(const Py::Tuple& args)
673
{
674
    char* pstr = nullptr;
675
    if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) {
676
        throw Py::Exception();
677
    }
678

679
    return Py::Boolean(_cParamGrp->HasGroup(pstr));  // NOLINT
680
}
681

682
Py::Object ParameterGrpPy::attach(const Py::Tuple& args)
683
{
684
    PyObject* obj = nullptr;
685
    if (!PyArg_ParseTuple(args.ptr(), "O", &obj)) {
686
        throw Py::Exception();
687
    }
688

689
    Py::Object o(obj);
690
    if (!o.hasAttr(std::string("onChange"))) {
691
        throw Py::TypeError("Object has no onChange attribute");
692
    }
693

694
    for (ParameterGrpObserver* it : _observers) {
695
        if (it->isEqual(o)) {
696
            throw Py::RuntimeError("Object is already attached.");
697
        }
698
    }
699

700
    ParameterGrpObserver* obs = new ParameterGrpObserver(o);
701
    _cParamGrp->Attach(obs);
702
    _observers.push_back(obs);
703

704
    return Py::None();
705
}
706

707
Py::Object ParameterGrpPy::attachManager(const Py::Tuple& args)
708
{
709
    PyObject* obj = nullptr;
710
    if (!PyArg_ParseTuple(args.ptr(), "O", &obj)) {
711
        throw Py::Exception();
712
    }
713

714
    if (!_cParamGrp->Manager()) {
715
        throw Py::RuntimeError("Parameter has no manager");
716
    }
717

718
    Py::Object o(obj);
719
    if (!o.hasAttr(std::string("slotParamChanged"))) {
720
        throw Py::TypeError("Object has no slotParamChanged attribute");
721
    }
722

723
    Py::Object attr(o.getAttr("slotParamChanged"));
724
    if (!attr.isCallable()) {
725
        throw Py::TypeError("Object has no slotParamChanged callable attribute");
726
    }
727

728
    for (ParameterGrpObserver* it : _observers) {
729
        if (it->isEqual(o)) {
730
            throw Py::RuntimeError("Object is already attached.");
731
        }
732
    }
733

734
    ParameterGrpObserver* obs = new ParameterGrpObserver(o, attr, _cParamGrp);
735
    obs->conn =
736
        _cParamGrp->Manager()->signalParamChanged.connect([obs](ParameterGrp* Param,
737
                                                                ParameterGrp::ParamType Type,
738
                                                                const char* Name,
739
                                                                const char* Value) {
740
            if (!Param) {
741
                return;
742
            }
743
            for (auto p = Param; p; p = p->Parent()) {
744
                if (p == obs->_target) {
745
                    Base::PyGILStateLocker lock;
746
                    Py::TupleN args(Py::asObject(new ParameterGrpPy(Param)),
747
                                    Py::String(ParameterGrp::TypeName(Type)),
748
                                    Py::String(Name ? Name : ""),
749
                                    Py::String(Value ? Value : ""));
750
                    try {
751
                        Py::Callable(obs->callable).apply(args);
752
                    }
753
                    catch (Py::Exception&) {
754
                        Base::PyException e;
755
                        e.ReportException();
756
                    }
757
                    break;
758
                }
759
            }
760
        });
761

762
    _observers.push_back(obs);
763
    return Py::None();
764
}
765

766
Py::Object ParameterGrpPy::detach(const Py::Tuple& args)
767
{
768
    PyObject* obj = nullptr;
769
    if (!PyArg_ParseTuple(args.ptr(), "O", &obj)) {
770
        throw Py::Exception();
771
    }
772

773
    Py::Object o(obj);
774
    if (!o.hasAttr(std::string("onChange"))) {
775
        throw Py::TypeError("Object has no onChange attribute");
776
    }
777

778
    for (ParameterGrpObserverList::iterator it = _observers.begin(); it != _observers.end(); ++it) {
779
        if ((*it)->isEqual(o)) {
780
            ParameterGrpObserver* obs = *it;
781
            _observers.erase(it);
782
            _cParamGrp->Detach(obs);
783
            delete obs;
784
            break;
785
        }
786
    }
787

788
    return Py::None();
789
}
790

791
Py::Object ParameterGrpPy::notify(const Py::Tuple& args)
792
{
793
    char* pstr = nullptr;
794
    if (!PyArg_ParseTuple(args.ptr(), "s", &pstr)) {
795
        throw Py::Exception();
796
    }
797

798
    _cParamGrp->Notify(pstr);
799
    return Py::None();
800
}
801

802
Py::Object ParameterGrpPy::notifyAll(const Py::Tuple& args)
803
{
804
    if (!PyArg_ParseTuple(args.ptr(), "")) {
805
        throw Py::Exception();
806
    }
807

808
    _cParamGrp->NotifyAll();
809
    return Py::None();
810
}
811

812
Py::Object ParameterGrpPy::getContents(const Py::Tuple& args)
813
{
814
    if (!PyArg_ParseTuple(args.ptr(), "")) {
815
        throw Py::Exception();
816
    }
817

818
    if (_cParamGrp->IsEmpty()) {
819
        return Py::None();
820
    }
821

822
    Py::List list;
823
    // filling up Text nodes
824
    std::vector<std::pair<std::string, std::string>> mcTextMap = _cParamGrp->GetASCIIMap();
825
    for (const auto& it : mcTextMap) {
826
        Py::Tuple t2(3);
827
        t2.setItem(0, Py::String("String"));
828
        t2.setItem(1, Py::String(it.first.c_str()));
829
        t2.setItem(2, Py::String(it.second.c_str()));
830
        list.append(t2);
831
    }
832

833
    // filling up Int nodes
834
    std::vector<std::pair<std::string, long>> mcIntMap = _cParamGrp->GetIntMap();
835
    for (const auto& it : mcIntMap) {
836
        Py::Tuple t3(3);
837
        t3.setItem(0, Py::String("Integer"));
838
        t3.setItem(1, Py::String(it.first.c_str()));
839
        t3.setItem(2, Py::Long(it.second));
840
        list.append(t3);
841
    }
842

843
    // filling up Float nodes
844
    std::vector<std::pair<std::string, double>> mcFloatMap = _cParamGrp->GetFloatMap();
845
    for (const auto& it : mcFloatMap) {
846
        Py::Tuple t4(3);
847
        t4.setItem(0, Py::String("Float"));
848
        t4.setItem(1, Py::String(it.first.c_str()));
849
        t4.setItem(2, Py::Float(it.second));
850
        list.append(t4);
851
    }
852

853
    // filling up bool nodes
854
    std::vector<std::pair<std::string, bool>> mcBoolMap = _cParamGrp->GetBoolMap();
855
    for (const auto& it : mcBoolMap) {
856
        Py::Tuple t5(3);
857
        t5.setItem(0, Py::String("Boolean"));
858
        t5.setItem(1, Py::String(it.first.c_str()));
859
        t5.setItem(2, Py::Boolean(it.second));
860
        list.append(t5);
861
    }
862

863
    // filling up UInt nodes
864
    std::vector<std::pair<std::string, unsigned long>> mcUIntMap = _cParamGrp->GetUnsignedMap();
865
    for (const auto& it : mcUIntMap) {
866
        Py::Tuple t6(3);
867
        t6.setItem(0, Py::String("Unsigned Long"));
868
        t6.setItem(1, Py::String(it.first.c_str()));
869
        t6.setItem(2, Py::Long(it.second));
870
        list.append(t6);
871
    }
872

873
    return list;  // NOLINT
874
}
875

876
}  // namespace Base
877

878
/** python wrapper function
879
 */
880
PyObject* GetPyObject(const Base::Reference<ParameterGrp>& hcParamGrp)
881
{
882
    static bool init = false;
883
    if (!init) {
884
        init = true;
885
        Base::ParameterGrpPy::init_type();
886
    }
887

888
    return new Base::ParameterGrpPy(hcParamGrp);
889
}
890

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

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

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

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