| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2023 David Friedli <[email protected]> * |
| 3 |
* Copyright (c) 2023 Wandererfan <[email protected]> * |
| 4 |
* * |
| 5 |
* This file is part of FreeCAD. * |
| 6 |
* * |
| 7 |
* FreeCAD is free software: you can redistribute it and/or modify it * |
| 8 |
* under the terms of the GNU Lesser General Public License as * |
| 9 |
* published by the Free Software Foundation, either version 2.1 of the * |
| 10 |
* License, or (at your option) any later version. * |
| 11 |
* * |
| 12 |
* FreeCAD is distributed in the hope that it will be useful, but * |
| 13 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 15 |
* Lesser General Public License for more details. * |
| 16 |
* * |
| 17 |
* You should have received a copy of the GNU Lesser General Public * |
| 18 |
* License along with FreeCAD. If not, see * |
| 19 |
* <https://www.gnu.org/licenses/>. * |
| 20 |
* * |
| 21 |
**************************************************************************/ |
| 22 |
|
| 23 |
#include <Base/Console.h> |
| 24 |
#include <Base/Interpreter.h> |
| 25 |
#include <Base/VectorPy.h> |
| 26 |
#include <App/Document.h> |
| 27 |
#include <App/Link.h> |
| 28 |
|
| 29 |
#include "MeasureManager.h" |
| 30 |
|
| 31 |
namespace App |
| 32 |
{ |
| 33 |
|
| 34 |
std::vector<MeasureHandler> MeasureManager::_mMeasureHandlers; |
| 35 |
std::vector<MeasureType*> MeasureManager::_mMeasureTypes; |
| 36 |
|
| 37 |
MeasureManager::MeasureManager() |
| 38 |
{ |
| 39 |
// Constructor implementation |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
void MeasureManager::addMeasureHandler(const char* module, MeasureTypeMethod typeCb) |
| 44 |
{ |
| 45 |
_mMeasureHandlers.emplace_back(MeasureHandler {module, typeCb}); |
| 46 |
} |
| 47 |
|
| 48 |
bool MeasureManager::hasMeasureHandler(const char* module) |
| 49 |
{ |
| 50 |
for (MeasureHandler& handler : _mMeasureHandlers) { |
| 51 |
if (strcmp(handler.module.c_str(), module) == 0) { |
| 52 |
return true; |
| 53 |
} |
| 54 |
} |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
MeasureHandler MeasureManager::getMeasureHandler(const char* module) |
| 59 |
{ |
| 60 |
for (MeasureHandler handler : _mMeasureHandlers) { |
| 61 |
if (!strcmp(handler.module.c_str(), module)) { |
| 62 |
return handler; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
MeasureHandler empty; |
| 67 |
return empty; |
| 68 |
} |
| 69 |
|
| 70 |
MeasureHandler MeasureManager::getMeasureHandler(const App::MeasureSelectionItem& selectionItem) |
| 71 |
{ |
| 72 |
auto objT = selectionItem.object; |
| 73 |
|
| 74 |
// Resolve App::Link |
| 75 |
App::DocumentObject* sub = objT.getSubObject(); |
| 76 |
if (sub->isDerivedFrom<App::Link>()) { |
| 77 |
auto link = static_cast<App::Link*>(sub); |
| 78 |
sub = link->getLinkedObject(true); |
| 79 |
} |
| 80 |
|
| 81 |
const char* className = sub->getTypeId().getName(); |
| 82 |
std::string mod = Base::Type::getModuleName(className); |
| 83 |
|
| 84 |
return getMeasureHandler(mod.c_str()); |
| 85 |
} |
| 86 |
|
| 87 |
MeasureElementType |
| 88 |
MeasureManager::getMeasureElementType(const App::MeasureSelectionItem& selectionItem) |
| 89 |
{ |
| 90 |
auto handler = getMeasureHandler(selectionItem); |
| 91 |
if (handler.module.empty()) { |
| 92 |
return App::MeasureElementType::INVALID; |
| 93 |
} |
| 94 |
|
| 95 |
auto objT = selectionItem.object; |
| 96 |
return handler.typeCb(objT.getObject(), objT.getSubName().c_str()); |
| 97 |
} |
| 98 |
|
| 99 |
void MeasureManager::addMeasureType(MeasureType* measureType) |
| 100 |
{ |
| 101 |
_mMeasureTypes.push_back(measureType); |
| 102 |
} |
| 103 |
|
| 104 |
void MeasureManager::addMeasureType(std::string id, |
| 105 |
std::string label, |
| 106 |
std::string measureObj, |
| 107 |
MeasureValidateMethod validatorCb, |
| 108 |
MeasurePrioritizeMethod prioritizeCb) |
| 109 |
{ |
| 110 |
MeasureType* mType = |
| 111 |
new MeasureType {id, label, measureObj, validatorCb, prioritizeCb, false, nullptr}; |
| 112 |
_mMeasureTypes.push_back(mType); |
| 113 |
} |
| 114 |
|
| 115 |
void MeasureManager::addMeasureType(const char* id, |
| 116 |
const char* label, |
| 117 |
const char* measureObj, |
| 118 |
MeasureValidateMethod validatorCb, |
| 119 |
MeasurePrioritizeMethod prioritizeCb) |
| 120 |
{ |
| 121 |
addMeasureType(std::string(id), |
| 122 |
std::string(label), |
| 123 |
std::string(measureObj), |
| 124 |
validatorCb, |
| 125 |
prioritizeCb); |
| 126 |
} |
| 127 |
|
| 128 |
const std::vector<MeasureType*> MeasureManager::getMeasureTypes() |
| 129 |
{ |
| 130 |
return _mMeasureTypes; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
Py::Tuple MeasureManager::getSelectionPy(const App::MeasureSelection& selection) |
| 135 |
{ |
| 136 |
// Convert selection to python list |
| 137 |
Py::Tuple selectionPy(selection.size()); |
| 138 |
|
| 139 |
int i = 0; |
| 140 |
for (auto it : selection) { |
| 141 |
|
| 142 |
Py::Dict sel; |
| 143 |
sel.setItem("object", Py::asObject(it.object.getObject()->getPyObject())); |
| 144 |
sel.setItem("subName", Py::String(it.object.getSubName())); |
| 145 |
sel.setItem("pickedPoint", Py::asObject(new Base::VectorPy(it.pickedPoint))); |
| 146 |
|
| 147 |
selectionPy.setItem(i, sel); |
| 148 |
|
| 149 |
i++; |
| 150 |
} |
| 151 |
return selectionPy; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
std::vector<MeasureType*> MeasureManager::getValidMeasureTypes(App::MeasureSelection selection, |
| 156 |
std::string mode) |
| 157 |
{ |
| 158 |
Base::PyGILStateLocker lock; |
| 159 |
|
| 160 |
// Convert selection to python list |
| 161 |
Py::Tuple selectionPy = getSelectionPy(selection); |
| 162 |
|
| 163 |
// Store valid measure types |
| 164 |
std::vector<MeasureType*> validTypes; |
| 165 |
std::pair<int, MeasureType>(); |
| 166 |
|
| 167 |
|
| 168 |
// Loop through measure types and check if they work with given selection |
| 169 |
for (App::MeasureType* mType : getMeasureTypes()) { |
| 170 |
|
| 171 |
if (mode != "" && mType->label != mode) { |
| 172 |
continue; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
if (mType->isPython) { |
| 177 |
// Parse Python measure types |
| 178 |
auto measurePyClass = Py::Object(mType->pythonClass); |
| 179 |
|
| 180 |
Py::Tuple args(1); |
| 181 |
args.setItem(0, selectionPy); |
| 182 |
|
| 183 |
Py::Object isValid; |
| 184 |
try { |
| 185 |
isValid = measurePyClass.callMemberFunction(std::string("isValidSelection"), args); |
| 186 |
} |
| 187 |
catch (const Py::Exception&) { |
| 188 |
Base::PyException e; |
| 189 |
e.reportException(); |
| 190 |
isValid = Py::False(); |
| 191 |
} |
| 192 |
|
| 193 |
if (isValid.as_bool()) { |
| 194 |
|
| 195 |
// Check priority |
| 196 |
Py::Object isPriority; |
| 197 |
try { |
| 198 |
isPriority = measurePyClass.callMemberFunction("isPrioritySelection", args); |
| 199 |
} |
| 200 |
catch (const Py::Exception&) { |
| 201 |
Base::PyException e; |
| 202 |
e.reportException(); |
| 203 |
isPriority = Py::False(); |
| 204 |
} |
| 205 |
|
| 206 |
if (isPriority.as_bool()) { |
| 207 |
validTypes.insert(validTypes.begin(), mType); |
| 208 |
} |
| 209 |
else { |
| 210 |
validTypes.push_back(mType); |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
else { |
| 215 |
// Parse c++ measure types |
| 216 |
|
| 217 |
if (mType->validatorCb && !mType->validatorCb(selection)) { |
| 218 |
continue; |
| 219 |
} |
| 220 |
|
| 221 |
// Check if the measurement type prioritizes the given selection |
| 222 |
if (mType->prioritizeCb && mType->prioritizeCb(selection)) { |
| 223 |
validTypes.insert(validTypes.begin(), mType); |
| 224 |
} |
| 225 |
else { |
| 226 |
validTypes.push_back(mType); |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
return validTypes; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
} // namespace App |