File size: 7,235 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************

 *   Copyright (c) 2023 David Friedli <david[at]friedli-be.ch>             *

 *                                                                         *

 *   This file is part of FreeCAD.                                         *

 *                                                                         *

 *   FreeCAD is free software: you can redistribute it and/or modify it    *

 *   under the terms of the GNU Lesser General Public License as           *

 *   published by the Free Software Foundation, either version 2.1 of the  *

 *   License, or (at your option) any later version.                       *

 *                                                                         *

 *   FreeCAD is distributed in the hope that it will be useful, but        *

 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *

 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      *

 *   Lesser General Public License for more details.                       *

 *                                                                         *

 *   You should have received a copy of the GNU Lesser General Public      *

 *   License along with FreeCAD. If not, see                               *

 *   <https://www.gnu.org/licenses/>.                                      *

 *                                                                         *

 **************************************************************************/

#include <Mod/Measure/MeasureGlobal.h>

#include <App/PropertyGeo.h>
#include <Base/PlacementPy.h>
#include <App/FeaturePythonPyImp.h>
#include <App/DocumentObjectPy.h>

#include "MeasureBase.h"
// Generated from MeasureBasePy.xml
#include "MeasureBasePy.h"

using namespace Measure;


PROPERTY_SOURCE(Measure::MeasureBase, App::DocumentObject)

MeasureBase::MeasureBase()
{
    ADD_PROPERTY_TYPE(
        Placement,
        (Base::Placement()),
        nullptr,
        App::PropertyType(App::Prop_ReadOnly | App::Prop_Output | App::Prop_NoRecompute),
        "Visual placement of the measurement"
    );
}


PyObject* MeasureBase::getPyObject(void)

{
    if (PythonObject.is(Py::_None())) {
        // ref counter is set to 1
        PythonObject = Py::Object(new MeasureBasePy(this), true);
    }
    return Py::new_reference_to(PythonObject);
}

Py::Object MeasureBase::getProxyObject() const

{
    Base::PyGILStateLocker lock;
    App::Property* prop = this->getPropertyByName("Proxy");
    if (!prop) {
        return Py::None();
    }
    return dynamic_cast<App::PropertyPythonObject*>(prop)->getValue();
};

std::vector<App::DocumentObject*> MeasureBase::getSubject() const

{
    Base::PyGILStateLocker lock;

    Py::Object proxy = getProxyObject();

    // Pass the feature object to the proxy
    Py::Tuple args(1);
    args.setItem(0, Py::Object(const_cast<MeasureBase*>(this)->getPyObject()));

    Py::Object ret;
    try {
        ret = proxy.callMemberFunction("getSubject", args);
    }
    catch (Py::Exception&) {
        Base::PyException e;
        e.reportException();
        return {};
    }

    Py::Sequence retTuple(ret);
    std::vector<App::DocumentObject*> retVec;
    for (Py::Object o : retTuple) {
        retVec.push_back(static_cast<App::DocumentObjectPy*>(o.ptr())->getDocumentObjectPtr());
    }

    return retVec;
};


void MeasureBase::parseSelection(const App::MeasureSelection& selection)

{
    Base::PyGILStateLocker lock;

    Py::Object proxy = getProxyObject();

    // Convert selection to python list
    Py::Tuple selectionPy = App::MeasureManager::getSelectionPy(selection);

    Py::Tuple args(2);

    // Pass the feature object to the proxy
    args.setItem(0, Py::Object(const_cast<MeasureBase*>(this)->getPyObject()));
    args.setItem(1, selectionPy);

    // Call the parseSelection method of the proxy object
    try {
        proxy.callMemberFunction("parseSelection", args);
    }
    catch (Py::Exception&) {
        Base::PyException e;
        e.reportException();
    }
}


std::vector<std::string> MeasureBase::getInputProps()

{
    Base::PyGILStateLocker lock;
    Py::Object proxy = getProxyObject();

    if (proxy.isNone()) {
        return {};
    }

    Py::Object ret;
    try {
        ret = proxy.callMemberFunction("getInputProps");
    }
    catch (Py::Exception&) {
        Base::PyException e;
        e.reportException();
        return {};
    }
    Py::Sequence propsPy(ret);

    // Get cpp vector from propsPy
    std::vector<std::string> props;
    for (Py::Object o : propsPy) {
        props.push_back(o.as_string());
    }

    return props;
}


QString MeasureBase::getResultString()

{
    Py::Object proxy = getProxyObject();
    Base::PyGILStateLocker lock;

    if (!proxy.isNone()) {

        // Pass the feature object to the proxy
        Py::Tuple args(1);
        args.setItem(0, Py::Object(const_cast<MeasureBase*>(this)->getPyObject()));

        Py::Object ret;
        try {
            ret = proxy.callMemberFunction("getResultString", args);
        }
        catch (Py::Exception&) {
            Base::PyException e;
            e.reportException();
            return QString();
        }
        return QString::fromStdString(ret.as_string());
    }

    App::Property* prop = getResultProp();
    if (prop == nullptr) {
        return QString();
    }

    if (prop->isDerivedFrom<App::PropertyQuantity>()) {
        return QString::fromStdString(
            static_cast<App::PropertyQuantity*>(prop)->getQuantityValue().getUserString()
        );
    }


    return QString();
}

void MeasureBase::onDocumentRestored()

{
    // Force recompute the measurement
    recompute();
}

// Python Drawing feature ---------------------------------------------------------

namespace App
{
/// @cond DOXERR
PROPERTY_SOURCE_TEMPLATE(Measure::MeasurePython, Measure::MeasureBase)
template<>
const char* Measure::MeasurePython::getViewProviderName(void) const
{
    std::string objName = this->getNameInDocument();

    // check object's name, this is brute-forceish way to determine
    // VP name for COM, but at this point python assignments haven't
    // been run, so we have no way to determine that easily
    if (objName.starts_with("Center_of_mass")

        || objName.find("CenterOfMass") != std::string::npos
        || objName.find("centerofmass") != std::string::npos) {
        return "MeasureGui::ViewProviderMeasureCOM";
    }

    return "MeasureGui::ViewProviderMeasure";
}
template<>
PyObject* Measure::MeasurePython::getPyObject()
{
    if (PythonObject.is(Py::_None())) {
        // ref counter is set to 1
        PythonObject = Py::Object(new FeaturePythonPyT<Measure::MeasureBasePy>(this), true);
    }
    return Py::new_reference_to(PythonObject);
}
/// @endcond

// explicit template instantiation
template class MeasureExport FeaturePythonT<Measure::MeasureBase>;
}  // namespace App