43
45
using namespace Gui;
44
46
using namespace std;
48
SelectionObserver::SelectionObserver()
53
SelectionObserver::~SelectionObserver()
58
bool SelectionObserver::blockConnection(bool block)
60
bool ok = connectSelection.blocked();
62
connectSelection.block();
64
connectSelection.unblock();
68
bool SelectionObserver::isConnectionBlocked() const
70
return connectSelection.blocked();
73
void SelectionObserver::attachSelection()
75
if (!connectSelection.connected()) {
76
connectSelection = Selection().signalSelectionChanged.connect(boost::bind
77
(&SelectionObserver::onSelectionChanged, this, _1));
81
void SelectionObserver::detachSelection()
83
if (connectSelection.connected()) {
84
connectSelection.disconnect();
88
// -------------------------------------------
90
std::vector<SelectionObserverPython*> SelectionObserverPython::_instances;
92
SelectionObserverPython::SelectionObserverPython(const Py::Object& obj) : inst(obj)
96
SelectionObserverPython::~SelectionObserverPython()
100
void SelectionObserverPython::addObserver(const Py::Object& obj)
102
_instances.push_back(new SelectionObserverPython(obj));
105
void SelectionObserverPython::removeObserver(const Py::Object& obj)
107
SelectionObserverPython* obs=0;
108
for (std::vector<SelectionObserverPython*>::iterator it =
109
_instances.begin(); it != _instances.end(); ++it) {
110
if ((*it)->inst == obj) {
112
_instances.erase(it);
120
void SelectionObserverPython::onSelectionChanged(const SelectionChanges& msg)
124
case SelectionChanges::AddSelection:
127
case SelectionChanges::RmvSelection:
128
removeSelection(msg);
130
case SelectionChanges::SetSelection:
133
case SelectionChanges::ClrSelection:
136
case SelectionChanges::SetPreselect:
137
setPreselection(msg);
139
case SelectionChanges::RmvPreselect:
140
removePreselection(msg);
147
void SelectionObserverPython::addSelection(const SelectionChanges& msg)
149
Base::PyGILStateLocker lock;
151
if (this->inst.hasAttr(std::string("addSelection"))) {
152
Py::Callable method(this->inst.getAttr(std::string("addSelection")));
154
args.setItem(0, Py::String(msg.pDocName ? msg.pDocName : ""));
155
args.setItem(1, Py::String(msg.pObjectName ? msg.pObjectName : ""));
156
args.setItem(2, Py::String(msg.pSubName ? msg.pSubName : ""));
158
tuple[0] = Py::Float(msg.x);
159
tuple[1] = Py::Float(msg.y);
160
tuple[2] = Py::Float(msg.z);
161
args.setItem(3, tuple);
165
catch (Py::Exception&) {
166
Base::PyException e; // extract the Python error text
167
Base::Console().Error("%s\n", e.what());
171
void SelectionObserverPython::removeSelection(const SelectionChanges& msg)
173
Base::PyGILStateLocker lock;
175
if (this->inst.hasAttr(std::string("removeSelection"))) {
176
Py::Callable method(this->inst.getAttr(std::string("removeSelection")));
178
args.setItem(0, Py::String(msg.pDocName ? msg.pDocName : ""));
179
args.setItem(1, Py::String(msg.pObjectName ? msg.pObjectName : ""));
180
args.setItem(2, Py::String(msg.pSubName ? msg.pSubName : ""));
184
catch (Py::Exception&) {
185
Base::PyException e; // extract the Python error text
186
Base::Console().Error("%s\n", e.what());
190
void SelectionObserverPython::setSelection(const SelectionChanges& msg)
192
Base::PyGILStateLocker lock;
194
if (this->inst.hasAttr(std::string("setSelection"))) {
195
Py::Callable method(this->inst.getAttr(std::string("setSelection")));
197
args.setItem(0, Py::String(msg.pDocName ? msg.pDocName : ""));
201
catch (Py::Exception&) {
202
Base::PyException e; // extract the Python error text
203
Base::Console().Error("%s\n", e.what());
207
void SelectionObserverPython::clearSelection(const SelectionChanges& msg)
209
Base::PyGILStateLocker lock;
211
if (this->inst.hasAttr(std::string("clearSelection"))) {
212
Py::Callable method(this->inst.getAttr(std::string("clearSelection")));
214
args.setItem(0, Py::String(msg.pDocName ? msg.pDocName : ""));
218
catch (Py::Exception&) {
219
Base::PyException e; // extract the Python error text
220
Base::Console().Error("%s\n", e.what());
224
void SelectionObserverPython::setPreselection(const SelectionChanges& msg)
226
Base::PyGILStateLocker lock;
228
if (this->inst.hasAttr(std::string("setPreselection"))) {
229
Py::Callable method(this->inst.getAttr(std::string("setPreselection")));
231
args.setItem(0, Py::String(msg.pDocName ? msg.pDocName : ""));
232
args.setItem(1, Py::String(msg.pObjectName ? msg.pObjectName : ""));
233
args.setItem(2, Py::String(msg.pSubName ? msg.pSubName : ""));
237
catch (Py::Exception&) {
238
Base::PyException e; // extract the Python error text
239
Base::Console().Error("%s\n", e.what());
243
void SelectionObserverPython::removePreselection(const SelectionChanges& msg)
245
Base::PyGILStateLocker lock;
247
if (this->inst.hasAttr(std::string("removePreselection"))) {
248
Py::Callable method(this->inst.getAttr(std::string("removePreselection")));
250
args.setItem(0, Py::String(msg.pDocName ? msg.pDocName : ""));
251
args.setItem(1, Py::String(msg.pObjectName ? msg.pObjectName : ""));
252
args.setItem(2, Py::String(msg.pSubName ? msg.pSubName : ""));
256
catch (Py::Exception&) {
257
Base::PyException e; // extract the Python error text
258
Base::Console().Error("%s\n", e.what());
262
// -------------------------------------------
46
264
std::vector<SelectionSingleton::SelObj> SelectionSingleton::getCompleteSelection() const
48
266
std::vector<SelObj> temp;
49
267
SelObj tempSelObj;
51
for( list<_SelObj>::const_iterator It = _SelList.begin();It != _SelList.end();++It) {
269
for(std::list<_SelObj>::const_iterator It = _SelList.begin();It != _SelList.end();++It) {
52
270
tempSelObj.DocName = It->DocName.c_str();
53
271
tempSelObj.FeatName = It->FeatName.c_str();
54
272
tempSelObj.SubName = It->SubName.c_str();
313
std::vector<SelectionObject> SelectionSingleton::getSelectionEx(const char* pDocName) const
315
std::vector<SelectionObject> temp;
317
std::map<App::DocumentObject*,SelectionObject> SortMap;
319
App::Document *pcDoc;
322
pcDoc = getDocument(pDocName);
327
for (std::list<_SelObj>::const_iterator It = _SelList.begin();It != _SelList.end();++It) {
328
if (It->pDoc == pcDoc) {
329
// if the object has already an entry
330
if (SortMap.find(It->pObject) != SortMap.end()){
331
// only add sub-element
332
SortMap[It->pObject].SubNames.push_back(It->SubName);
333
SortMap[It->pObject].SelPoses.push_back(Base::Vector3d(It->x,It->y,It->z));
336
// create a new entry
337
SelectionObject tempSelObj;
338
tempSelObj.DocName = It->DocName;
339
tempSelObj.FeatName = It->FeatName;
340
tempSelObj.SubNames.push_back(It->SubName);
341
tempSelObj.TypeName = It->TypeName.c_str();
342
tempSelObj.SelPoses.push_back(Base::Vector3d(It->x,It->y,It->z));
343
SortMap.insert(std::pair<App::DocumentObject*,SelectionObject>(It->pObject,tempSelObj));
347
for (std::map<App::DocumentObject*,SelectionObject>::const_iterator It = SortMap.begin();It != SortMap.end();++It)
348
temp.push_back(It->second);
95
353
vector<App::DocumentObject*> SelectionSingleton::getObjectsOfType(const Base::Type& typeId, const char* pDocName) const
97
355
std::vector<App::DocumentObject*> temp;
461
772
"getSelection([string]) -- Return a list of selected objets\n"
462
773
"Return a list of selected objects for a given document name. If no\n"
463
774
"document is given the complete selection is returned."},
775
{"getSelectionEx", (PyCFunction) SelectionSingleton::sGetSelectionEx, 1,
776
"getSelectionEx([string]) -- Return a list of SelectionObjects\n"
777
"Return a list of SelectionObjects for a given document name. If no\n"
778
"document is given the selection of the active document is returned.\n"
779
"The SelectionObjects contain a variety of information about the selection,\n"
780
"e.g. sub-element names."},
781
{"addObserver", (PyCFunction) SelectionSingleton::sAddSelObserver, 1,
782
"addObserver(Object) -- Install an observer\n"},
783
{"removeObserver", (PyCFunction) SelectionSingleton::sRemSelObserver, 1,
784
"removeObserver(Object) -- Uninstall an observer\n"},
464
785
{NULL, NULL, 0, NULL} /* Sentinel */
868
PyObject *SelectionSingleton::sGetSelectionEx(PyObject * /*self*/, PyObject *args, PyObject * /*kwd*/)
870
char *documentName=0;
871
if (!PyArg_ParseTuple(args, "|s", &documentName)) // convert args: Python->C
872
return NULL; // NULL triggers exception
874
std::vector<SelectionObject> sel;
875
sel = Selection().getSelectionEx(documentName);
879
for (std::vector<SelectionObject>::iterator it = sel.begin(); it != sel.end(); ++it) {
880
list.append(Py::Object(new SelectionObjectPy(new SelectionObject(*it)), true));
882
return Py::new_reference_to(list);
884
catch (Py::Exception&) {
889
PyObject *SelectionSingleton::sAddSelObserver(PyObject * /*self*/, PyObject *args, PyObject * /*kwd*/)
892
if (!PyArg_ParseTuple(args, "O",&o))
895
SelectionObserverPython::addObserver(Py::Object(o));
900
PyObject *SelectionSingleton::sRemSelObserver(PyObject * /*self*/, PyObject *args, PyObject * /*kwd*/)
903
if (!PyArg_ParseTuple(args, "O",&o))
906
SelectionObserverPython::removeObserver(Py::Object(o));