24
#include "PreCompiled.h"
26
#include <Base/Tools.h>
29
#include "GeoFeatureGroupExtension.h"
30
#include "GroupExtensionPy.h"
34
namespace sp = std::placeholders;
36
EXTENSION_PROPERTY_SOURCE(App::GroupExtension, App::DocumentObjectExtension)
39
EXTENSION_PROPERTY_SOURCE_TEMPLATE(App::GroupExtensionPython, App::GroupExtension)
42
template class AppExport ExtensionPythonT<GroupExtensionPythonT<GroupExtension>>;
45
GroupExtension::GroupExtension()
47
initExtensionType(GroupExtension::getExtensionClassTypeId());
49
EXTENSION_ADD_PROPERTY_TYPE(Group,(nullptr),"Base",(App::PropertyType)(Prop_None),"List of referenced objects");
51
EXTENSION_ADD_PROPERTY_TYPE(_GroupTouched, (false), "Base",
52
PropertyType(Prop_Hidden|Prop_Transient),0);
55
GroupExtension::~GroupExtension() = default;
57
DocumentObject* GroupExtension::addObject(const char* sType, const char* pObjectName)
59
DocumentObject* obj = getExtendedObject()->getDocument()->addObject(sType, pObjectName);
60
if(!allowObject(obj)) {
61
getExtendedObject()->getDocument()->removeObject(obj->getNameInDocument());
68
std::vector<DocumentObject*> GroupExtension::addObject(DocumentObject* obj)
70
std::vector<DocumentObject*> vec = {obj};
71
return addObjects(vec);
74
std::vector< DocumentObject* > GroupExtension::addObjects(std::vector< DocumentObject* > objs) {
76
std::vector<DocumentObject*> added;
77
std::vector<DocumentObject*> grp = Group.getValues();
78
for(auto obj : objs) {
88
auto *group = App::GroupExtension::getGroupOfObject(obj);
89
if(group && group != getExtendedObject())
90
group->getExtensionByType<App::GroupExtension>()->removeObject(obj);
93
auto geogrp = GeoFeatureGroupExtension::getGroupOfObject(getExtendedObject());
94
auto objgrp = GeoFeatureGroupExtension::getGroupOfObject(obj);
95
if( geogrp != objgrp ) {
98
geogrp->getExtensionByType<GeoFeatureGroupExtension>()->addObject(obj);
100
objgrp->getExtensionByType<GeoFeatureGroupExtension>()->removeObject(obj);
104
added.push_back(obj);
107
Group.setValues(grp);
112
std::vector< DocumentObject* > GroupExtension::setObjects(std::vector< DocumentObject* > obj) {
114
Group.setValues(std::vector< DocumentObject* > ());
115
return addObjects(obj);
118
std::vector<DocumentObject*> GroupExtension::removeObject(DocumentObject* obj)
120
std::vector<DocumentObject*> vec = {obj};
121
return removeObjects(vec);
124
std::vector< DocumentObject* > GroupExtension::removeObjects(std::vector< DocumentObject* > objs) {
126
const std::vector<DocumentObject*> & grp = Group.getValues();
127
std::vector<DocumentObject*> newGrp = grp;
128
std::vector<DocumentObject*> removed;
130
std::vector<DocumentObject*>::iterator end = newGrp.end();
131
for(auto obj : objs) {
132
auto res = std::remove(newGrp.begin(), end, obj);
135
removed.push_back(obj);
139
newGrp.erase(end, newGrp.end());
140
if (grp.size() != newGrp.size()) {
141
Group.setValues (newGrp);
147
void GroupExtension::removeObjectsFromDocument()
149
while (Group.getSize() > 0) {
153
const std::vector<DocumentObject*> & grp = Group.getValues();
154
removeObjectFromDocument(grp.front());
158
void GroupExtension::removeObjectFromDocument(DocumentObject* obj)
161
if (!obj || !obj->isAttachedToDocument())
165
if (obj->hasExtension(GroupExtension::getExtensionClassTypeId())) {
166
GroupExtension *grp = static_cast<GroupExtension*>(obj->getExtension(GroupExtension::getExtensionClassTypeId()));
169
grp->removeObjectsFromDocument();
172
getExtendedObject()->getDocument()->removeObject(obj->getNameInDocument());
175
DocumentObject *GroupExtension::getObject(const char *Name) const
177
DocumentObject* obj = getExtendedObject()->getDocument()->getObject(Name);
178
if (obj && hasObject(obj))
183
bool GroupExtension::hasObject(const DocumentObject* obj, bool recursive) const
185
if (obj == getExtendedObject()) {
190
const std::vector<DocumentObject*>& grp = Group.getValues();
191
for (auto child : grp) {
199
else if (child == getExtendedObject()) {
200
throw Base::RuntimeError("Cyclic dependencies detected: Search cannot be performed");
202
else if ( recursive && child->hasExtension(GroupExtension::getExtensionClassTypeId()) ) {
203
App::GroupExtension *subGroup = static_cast<App::GroupExtension *> (
204
child->getExtension(GroupExtension::getExtensionClassTypeId()));
205
std::vector<const GroupExtension*> history;
206
history.push_back(this);
208
if (subGroup->recursiveHasObject (obj, subGroup, history)) {
216
catch (const Base::RuntimeError& e) {
222
bool GroupExtension::recursiveHasObject(const DocumentObject* obj, const GroupExtension* group,
223
std::vector< const GroupExtension* > history) const {
228
history.push_back(this);
231
if(group->hasObject(obj, false))
236
for (auto child : group->Group.getValues()) {
241
if ( child->hasExtension(GroupExtension::getExtensionClassTypeId()) ) {
243
auto ext = child->getExtensionByType<GroupExtension>();
245
if (std::find(history.begin(), history.end(), ext) != history.end()) {
246
throw Base::RuntimeError("Cyclic dependencies detected: Search cannot be performed");
249
if (recursiveHasObject(obj, ext, history)) {
257
bool GroupExtension::isChildOf(const GroupExtension* group, bool recursive) const
259
return group->hasObject(getExtendedObject(), recursive);
262
const std::vector<DocumentObject*> &GroupExtension::getObjects() const
264
return Group.getValues();
267
std::vector<DocumentObject*> GroupExtension::getObjectsOfType(const Base::Type& typeId) const
269
std::vector<DocumentObject*> type;
270
const std::vector<DocumentObject*>& grp = Group.getValues();
271
for (auto it : grp) {
272
if (it->getTypeId().isDerivedFrom(typeId))
279
int GroupExtension::countObjectsOfType(const Base::Type& typeId) const
282
const std::vector<DocumentObject*>& grp = Group.getValues();
283
for (auto it : grp) {
284
if ( it->getTypeId().isDerivedFrom(typeId))
291
DocumentObject* GroupExtension::getGroupOfObject(const DocumentObject* obj)
296
for (auto o : obj->getInList()) {
297
if (o->hasExtension(App::GroupExtension::getExtensionClassTypeId(), false))
299
if (o->hasExtension(App::GroupExtensionPython::getExtensionClassTypeId(), false))
306
PyObject* GroupExtension::getExtensionPyObject() {
308
if (ExtensionPythonObject.is(Py::_None())){
310
auto grp = new GroupExtensionPy(this);
311
ExtensionPythonObject = Py::Object(grp,true);
313
return Py::new_reference_to(ExtensionPythonObject);
316
void GroupExtension::extensionOnChanged(const Property* p) {
320
if((this->getExtensionTypeId() == GroupExtension::getExtensionClassTypeId())
321
&& p == &Group && !Group.testStatus(Property::User3))
323
if(!getExtendedObject()->isRestoring() &&
324
!getExtendedObject()->getDocument()->isPerformingTransaction()) {
327
auto corrected = Group.getValues();
328
for(auto obj : Group.getValues()) {
332
auto list = obj->getInList();
333
for (auto in : list) {
334
if(in->hasExtension(App::GroupExtension::getExtensionClassTypeId(), false) &&
335
in != getExtendedObject()) {
337
corrected.erase(std::remove(corrected.begin(), corrected.end(), obj), corrected.end());
344
Base::ObjectStatusLocker<Property::Status, Property> guard(Property::User3, &Group);
345
Group.setValues(corrected);
346
throw Base::RuntimeError("Object can only be in a single Group");
353
for(auto obj : Group.getValue()) {
354
if(obj && obj->isAttachedToDocument()) {
356
_Conns[obj] = obj->signalChanged.connect(std::bind(
357
&GroupExtension::slotChildChanged,this,sp::_1, sp::_2));
363
App::Extension::extensionOnChanged(p);
366
void GroupExtension::slotChildChanged(const DocumentObject &obj, const Property &prop) {
367
if(&prop == &obj.Visibility)
368
_GroupTouched.touch();
371
bool GroupExtension::extensionGetSubObject(DocumentObject *&ret, const char *subname,
372
PyObject **pyObj, Base::Matrix4D *mat, bool , int depth) const
375
if(!subname || *subname==0) {
376
auto obj = Base::freecad_dynamic_cast<const DocumentObject>(getExtendedContainer());
377
ret = const_cast<DocumentObject*>(obj);
380
dot=strchr(subname,'.');
384
ret = Group.findUsingMap(std::string(subname,dot));
386
std::string name = std::string(subname+1,dot);
387
for(auto child : Group.getValues()) {
388
if(name == child->Label.getStrValue()){
396
return ret->getSubObject(dot+1,pyObj,mat,true,depth+1);
399
bool GroupExtension::extensionGetSubObjects(std::vector<std::string> &ret, int) const {
400
for(auto obj : Group.getValues()) {
401
if(obj && obj->isAttachedToDocument())
402
ret.push_back(std::string(obj->getNameInDocument())+'.');
407
App::DocumentObjectExecReturn *GroupExtension::extensionExecute() {
409
_GroupTouched.touch();
410
return inherited::extensionExecute();
413
std::vector<App::DocumentObject*> GroupExtension::getAllChildren() const {
414
std::vector<DocumentObject*> res;
415
std::set<DocumentObject*> rset;
416
getAllChildren(res,rset);
420
void GroupExtension::getAllChildren(std::vector<App::DocumentObject*> &res,
421
std::set<App::DocumentObject*> &rset) const
423
for(auto obj : Group.getValues()) {
424
if(!obj || !obj->isAttachedToDocument())
426
if(!rset.insert(obj).second)
429
auto ext = obj->getExtensionByType<GroupExtension>(true,false);
431
ext->getAllChildren(res,rset);