| 1 |
#include "SelectionSetInterface.h" |
| 2 |
|
| 3 |
#include "imap.h" |
| 4 |
#include "iselection.h" |
| 5 |
|
| 6 |
namespace script |
| 7 |
{ |
| 8 |
|
| 9 |
ScriptSelectionSet::ScriptSelectionSet(const selection::ISelectionSetPtr& set) : |
| 10 |
_set(set) |
| 11 |
{} |
| 12 |
|
| 13 |
const std::string& ScriptSelectionSet::getName() |
| 14 |
{ |
| 15 |
return (_set) ? _set->getName() : _emptyStr; |
| 16 |
} |
| 17 |
|
| 18 |
bool ScriptSelectionSet::empty() |
| 19 |
{ |
| 20 |
return (_set) ? _set->empty() : true; |
| 21 |
} |
| 22 |
|
| 23 |
void ScriptSelectionSet::select() |
| 24 |
{ |
| 25 |
if (_set) _set->select(); |
| 26 |
} |
| 27 |
|
| 28 |
void ScriptSelectionSet::deselect() |
| 29 |
{ |
| 30 |
if (_set) _set->deselect(); |
| 31 |
} |
| 32 |
|
| 33 |
void ScriptSelectionSet::clear() |
| 34 |
{ |
| 35 |
if (_set) _set->clear(); |
| 36 |
} |
| 37 |
|
| 38 |
void ScriptSelectionSet::assignFromCurrentScene() |
| 39 |
{ |
| 40 |
if (_set) _set->assignFromCurrentScene(); |
| 41 |
} |
| 42 |
|
| 43 |
std::string ScriptSelectionSet::_emptyStr; |
| 44 |
|
| 45 |
// --------------------------- |
| 46 |
|
| 47 |
inline selection::ISelectionSetManager& GetMapSelectionSetManager() |
| 48 |
{ |
| 49 |
if (!GlobalMapModule().getRoot()) |
| 50 |
{ |
| 51 |
throw std::runtime_error("No map loaded."); |
| 52 |
} |
| 53 |
|
| 54 |
return GlobalMapModule().getRoot()->getSelectionSetManager(); |
| 55 |
} |
| 56 |
|
| 57 |
void SelectionSetInterface::foreachSelectionSet(selection::ISelectionSetManager::Visitor& visitor) |
| 58 |
{ |
| 59 |
try |
| 60 |
{ |
| 61 |
GetMapSelectionSetManager().foreachSelectionSet(visitor); |
| 62 |
} |
| 63 |
catch (const std::runtime_error& ex) |
| 64 |
{ |
| 65 |
rError() << ex.what() << std::endl; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
ScriptSelectionSet SelectionSetInterface::createSelectionSet(const std::string& name) |
| 70 |
{ |
| 71 |
try |
| 72 |
{ |
| 73 |
return ScriptSelectionSet(GetMapSelectionSetManager().createSelectionSet(name)); |
| 74 |
} |
| 75 |
catch (const std::runtime_error & ex) |
| 76 |
{ |
| 77 |
rError() << ex.what() << std::endl; |
| 78 |
return ScriptSelectionSet(selection::ISelectionSetPtr()); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
void SelectionSetInterface::deleteSelectionSet(const std::string& name) |
| 83 |
{ |
| 84 |
try |
| 85 |
{ |
| 86 |
GetMapSelectionSetManager().deleteSelectionSet(name); |
| 87 |
} |
| 88 |
catch (const std::runtime_error & ex) |
| 89 |
{ |
| 90 |
rError() << ex.what() << std::endl; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
void SelectionSetInterface::deleteAllSelectionSets() |
| 95 |
{ |
| 96 |
try |
| 97 |
{ |
| 98 |
GetMapSelectionSetManager().deleteAllSelectionSets(); |
| 99 |
} |
| 100 |
catch (const std::runtime_error & ex) |
| 101 |
{ |
| 102 |
rError() << ex.what() << std::endl; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
ScriptSelectionSet SelectionSetInterface::findSelectionSet(const std::string& name) |
| 107 |
{ |
| 108 |
try |
| 109 |
{ |
| 110 |
return ScriptSelectionSet(GetMapSelectionSetManager().findSelectionSet(name)); |
| 111 |
} |
| 112 |
catch (const std::runtime_error & ex) |
| 113 |
{ |
| 114 |
rError() << ex.what() << std::endl; |
| 115 |
return ScriptSelectionSet(selection::ISelectionSetPtr()); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// IScriptInterface implementation |
| 120 |
void SelectionSetInterface::registerInterface(py::module& scope, py::dict& globals) |
| 121 |
{ |
| 122 |
// Expose the SelectionSystem::Visitor interface |
| 123 |
py::class_<selection::ISelectionSetManager::Visitor, SelectionSetVisitorWrapper> visitor(scope, "SelectionSetVisitor"); |
| 124 |
|
| 125 |
visitor.def(py::init<>()); |
| 126 |
visitor.def("visit", &selection::ISelectionSetManager::Visitor::visit); |
| 127 |
|
| 128 |
// Add SelectionSet declaration |
| 129 |
py::class_<ScriptSelectionSet> selectionSet(scope, "SelectionSet"); |
| 130 |
|
| 131 |
selectionSet.def(py::init<const selection::ISelectionSetPtr&>()); |
| 132 |
selectionSet.def("getName", &ScriptSelectionSet::getName, py::return_value_policy::reference); |
| 133 |
selectionSet.def("empty", &ScriptSelectionSet::empty); |
| 134 |
selectionSet.def("clear", &ScriptSelectionSet::clear); |
| 135 |
selectionSet.def("select", &ScriptSelectionSet::select); |
| 136 |
selectionSet.def("deselect", &ScriptSelectionSet::deselect); |
| 137 |
selectionSet.def("assignFromCurrentScene", &ScriptSelectionSet::assignFromCurrentScene); |
| 138 |
|
| 139 |
// Add the module declaration to the given python namespace |
| 140 |
py::class_<SelectionSetInterface> selectionSetManager(scope, "SelectionSetManager"); |
| 141 |
|
| 142 |
selectionSetManager.def("foreachSelectionSet", &SelectionSetInterface::foreachSelectionSet); |
| 143 |
selectionSetManager.def("createSelectionSet", &SelectionSetInterface::createSelectionSet); |
| 144 |
selectionSetManager.def("deleteSelectionSet", &SelectionSetInterface::deleteSelectionSet); |
| 145 |
selectionSetManager.def("deleteAllSelectionSets", &SelectionSetInterface::deleteAllSelectionSets); |
| 146 |
selectionSetManager.def("findSelectionSet", &SelectionSetInterface::findSelectionSet); |
| 147 |
|
| 148 |
// Now point the Python variable "GlobalSelectionSetManager" to this instance |
| 149 |
globals["GlobalSelectionSetManager"] = this; |
| 150 |
} |
| 151 |
|
| 152 |
} // namespace script |