File size: 5,467 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 | // 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/>. *
* *
**************************************************************************/
#ifndef MEASURE_MEASUREBASE_H
#define MEASURE_MEASUREBASE_H
#include <Mod/Measure/MeasureGlobal.h>
#include <memory>
#include <QString>
#include <App/DocumentObject.h>
#include <App/MeasureManager.h>
#include <App/DocumentObserver.h>
#include <App/PropertyStandard.h>
#include <App/PropertyUnits.h>
#include <App/FeaturePython.h>
#include <App/Link.h>
#include <Base/Quantity.h>
#include <Base/Placement.h>
#include <Base/Interpreter.h>
#include <Mod/Part/App/MeasureInfo.h>
#include <Mod/Part/App/MeasureClient.h> // needed?
namespace Measure
{
class MeasureExport MeasureBase: public App::DocumentObject
{
PROPERTY_HEADER_WITH_OVERRIDE(Measure::MeasureBase);
public:
MeasureBase();
~MeasureBase() override = default;
App::PropertyPlacement Placement;
// fastsignals::signal<void (const MeasureBase*)> signalGuiInit;
// return PyObject as MeasureBasePy
PyObject* getPyObject() override;
// Initialize measurement properties from selection
virtual void parseSelection(const App::MeasureSelection& selection);
virtual QString getResultString();
virtual std::vector<std::string> getInputProps();
virtual App::Property* getResultProp()
{
return {};
}
// Return the objects that are measured
virtual std::vector<App::DocumentObject*> getSubject() const;
private:
Py::Object getProxyObject() const;
protected:
void onDocumentRestored() override;
};
// Create a scriptable object based on MeasureBase
using MeasurePython = App::FeaturePythonT<MeasureBase>;
template<typename T>
class MeasureExport MeasureBaseExtendable: public MeasureBase
{
using GeometryHandler = std::function<Part::MeasureInfoPtr(const App::SubObjectT&)>;
using HandlerMap = std::map<std::string, GeometryHandler>;
public:
static void addGeometryHandler(const std::string& module, GeometryHandler callback)
{
_mGeometryHandlers[module] = callback;
}
static GeometryHandler getGeometryHandler(const std::string& module)
{
if (!hasGeometryHandler(module)) {
return {};
}
return _mGeometryHandlers[module];
}
static Part::MeasureInfoPtr getMeasureInfo(App::SubObjectT& subObjT)
{
// Resolve App::Link
App::DocumentObject* sub = subObjT.getSubObject();
if (!sub) {
return nullptr;
}
if (sub->isDerivedFrom<App::Link>()) {
auto link = static_cast<App::Link*>(sub);
sub = link->getLinkedObject(true);
}
// Get the Geometry handler based on the module
const char* className = sub->getTypeId().getName();
std::string mod = Base::Type::getModuleName(className);
auto handler = getGeometryHandler(mod);
if (!handler) {
Base::Console().log(
"MeasureBaseExtendable::getMeasureInfo: No geometry handler "
"available for submitted element type"
);
return nullptr;
}
return handler(subObjT);
}
static void addGeometryHandlers(const std::vector<std::string>& modules, GeometryHandler callback)
{
// TODO: this will replace a callback with a later one. Should we check that there isn't
// already a handler defined for this module?
for (auto& mod : modules) {
_mGeometryHandlers[mod] = callback;
}
}
static bool hasGeometryHandler(const std::string& module)
{
return (_mGeometryHandlers.count(module) > 0);
}
private:
inline static HandlerMap _mGeometryHandlers = MeasureBaseExtendable<T>::HandlerMap();
};
} // namespace Measure
#endif // MEASURE_MEASUREBASE_H
|