| 1 |
#include "SelectionGroupInterface.h" |
| 2 |
|
| 3 |
#include "imap.h" |
| 4 |
#include "itextstream.h" |
| 5 |
#include <pybind11/pybind11.h> |
| 6 |
|
| 7 |
namespace script |
| 8 |
{ |
| 9 |
|
| 10 |
namespace |
| 11 |
{ |
| 12 |
|
| 13 |
inline selection::ISelectionGroupManager& getMapGroupManager() |
| 14 |
{ |
| 15 |
if (!GlobalMapModule().getRoot()) |
| 16 |
{ |
| 17 |
throw std::runtime_error("No map loaded."); |
| 18 |
} |
| 19 |
|
| 20 |
return GlobalMapModule().getRoot()->getSelectionGroupManager(); |
| 21 |
} |
| 22 |
|
| 23 |
} |
| 24 |
|
| 25 |
ScriptSelectionGroup::ScriptSelectionGroup(const selection::ISelectionGroupPtr& group) : |
| 26 |
_group(group) |
| 27 |
{} |
| 28 |
|
| 29 |
std::size_t ScriptSelectionGroup::getId() |
| 30 |
{ |
| 31 |
return _group ? _group->getId() : 0; |
| 32 |
} |
| 33 |
|
| 34 |
const std::string& ScriptSelectionGroup::getName() |
| 35 |
{ |
| 36 |
return _group ? _group->getName() : _emptyStr; |
| 37 |
} |
| 38 |
|
| 39 |
void ScriptSelectionGroup::setName(const std::string& name) |
| 40 |
{ |
| 41 |
if (_group) |
| 42 |
{ |
| 43 |
_group->setName(name); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
void ScriptSelectionGroup::addNode(const ScriptSceneNode& node) |
| 48 |
{ |
| 49 |
if (_group) |
| 50 |
{ |
| 51 |
_group->addNode(node); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
void ScriptSelectionGroup::removeNode(const scene::INodePtr& node) |
| 56 |
{ |
| 57 |
if (_group) |
| 58 |
{ |
| 59 |
_group->removeNode(node); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
std::size_t ScriptSelectionGroup::size() |
| 64 |
{ |
| 65 |
return _group ? _group->size() : 0; |
| 66 |
} |
| 67 |
|
| 68 |
void ScriptSelectionGroup::setSelected(int selected) |
| 69 |
{ |
| 70 |
if (_group) |
| 71 |
{ |
| 72 |
_group->setSelected(static_cast<bool>(selected)); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
void ScriptSelectionGroup::foreachNode(SelectionGroupVisitor& visitor) |
| 77 |
{ |
| 78 |
if (_group) |
| 79 |
{ |
| 80 |
_group->foreachNode([&](const scene::INodePtr& node) |
| 81 |
{ |
| 82 |
visitor.visit(node); |
| 83 |
}); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
std::string ScriptSelectionGroup::_emptyStr; |
| 88 |
|
| 89 |
// ----------------------------------- |
| 90 |
|
| 91 |
ScriptSelectionGroup SelectionGroupInterface::createSelectionGroup() |
| 92 |
{ |
| 93 |
try |
| 94 |
{ |
| 95 |
return ScriptSelectionGroup(getMapGroupManager().createSelectionGroup()); |
| 96 |
} |
| 97 |
catch (const std::runtime_error& ex) |
| 98 |
{ |
| 99 |
rError() << ex.what() << std::endl; |
| 100 |
return ScriptSelectionGroup(selection::ISelectionGroupPtr()); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
ScriptSelectionGroup SelectionGroupInterface::getSelectionGroup(std::size_t id) |
| 105 |
{ |
| 106 |
try |
| 107 |
{ |
| 108 |
return ScriptSelectionGroup(getMapGroupManager().getSelectionGroup(id)); |
| 109 |
} |
| 110 |
catch (const std::runtime_error& ex) |
| 111 |
{ |
| 112 |
rError() << ex.what() << std::endl; |
| 113 |
return ScriptSelectionGroup(selection::ISelectionGroupPtr()); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
ScriptSelectionGroup SelectionGroupInterface::findOrCreateSelectionGroup(std::size_t id) |
| 118 |
{ |
| 119 |
try |
| 120 |
{ |
| 121 |
return ScriptSelectionGroup(getMapGroupManager().findOrCreateSelectionGroup(id)); |
| 122 |
} |
| 123 |
catch (const std::runtime_error& ex) |
| 124 |
{ |
| 125 |
rError() << ex.what() << std::endl; |
| 126 |
return ScriptSelectionGroup(selection::ISelectionGroupPtr()); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
void SelectionGroupInterface::setGroupSelected(std::size_t id, int selected) |
| 131 |
{ |
| 132 |
try |
| 133 |
{ |
| 134 |
getMapGroupManager().setGroupSelected(id, static_cast<bool>(selected)); |
| 135 |
} |
| 136 |
catch (const std::exception& ex) |
| 137 |
{ |
| 138 |
rError() << ex.what() << std::endl; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
void SelectionGroupInterface::deleteAllSelectionGroups() |
| 143 |
{ |
| 144 |
try |
| 145 |
{ |
| 146 |
getMapGroupManager().deleteAllSelectionGroups(); |
| 147 |
} |
| 148 |
catch (const std::exception & ex) |
| 149 |
{ |
| 150 |
rError() << ex.what() << std::endl; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
void SelectionGroupInterface::deleteSelectionGroup(std::size_t id) |
| 155 |
{ |
| 156 |
try |
| 157 |
{ |
| 158 |
getMapGroupManager().deleteSelectionGroup(id); |
| 159 |
} |
| 160 |
catch (const std::exception & ex) |
| 161 |
{ |
| 162 |
rError() << ex.what() << std::endl; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
// IScriptInterface implementation |
| 167 |
void SelectionGroupInterface::registerInterface(py::module& scope, py::dict& globals) |
| 168 |
{ |
| 169 |
// Expose the SelectionGroupVisitor interface |
| 170 |
py::class_<SelectionGroupVisitor, SelectionGroupVisitorWrapper> visitor(scope, "SelectionGroupVisitor"); |
| 171 |
|
| 172 |
visitor.def(py::init<>()); |
| 173 |
visitor.def("visit", &SelectionGroupVisitor::visit); |
| 174 |
|
| 175 |
// Add SelectionGroup declaration |
| 176 |
py::class_<ScriptSelectionGroup> selectionGroup(scope, "SelectionGroup"); |
| 177 |
|
| 178 |
selectionGroup.def(py::init<const selection::ISelectionGroupPtr&>()); |
| 179 |
selectionGroup.def("getId", &ScriptSelectionGroup::getId); |
| 180 |
selectionGroup.def("getName", &ScriptSelectionGroup::getName, py::return_value_policy::reference); |
| 181 |
selectionGroup.def("setName", &ScriptSelectionGroup::setName); |
| 182 |
selectionGroup.def("addNode", &ScriptSelectionGroup::addNode); |
| 183 |
selectionGroup.def("removeNode", &ScriptSelectionGroup::removeNode); |
| 184 |
selectionGroup.def("size", &ScriptSelectionGroup::size); |
| 185 |
selectionGroup.def("setSelected", &ScriptSelectionGroup::setSelected); |
| 186 |
selectionGroup.def("foreachNode", &ScriptSelectionGroup::foreachNode); |
| 187 |
|
| 188 |
// Add the module declaration to the given python namespace |
| 189 |
py::class_<SelectionGroupInterface> selectionGroupManager(scope, "SelectionGroupManager"); |
| 190 |
|
| 191 |
selectionGroupManager.def("createSelectionGroup", &SelectionGroupInterface::createSelectionGroup); |
| 192 |
selectionGroupManager.def("getSelectionGroup", &SelectionGroupInterface::getSelectionGroup); |
| 193 |
selectionGroupManager.def("findOrCreateSelectionGroup", &SelectionGroupInterface::findOrCreateSelectionGroup); |
| 194 |
selectionGroupManager.def("setGroupSelected", &SelectionGroupInterface::setGroupSelected); |
| 195 |
selectionGroupManager.def("deleteAllSelectionGroups", &SelectionGroupInterface::deleteAllSelectionGroups); |
| 196 |
selectionGroupManager.def("deleteSelectionGroup", &SelectionGroupInterface::deleteSelectionGroup); |
| 197 |
|
| 198 |
// Now point the Python variable "GlobalSelectionGroupManager" to this instance |
| 199 |
globals["GlobalSelectionGroupManager"] = this; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
} |