diff --git a/src/App/Application.cpp b/src/App/Application.cpp index 5514d8031f0d..a7641aa18211 100644 --- a/src/App/Application.cpp +++ b/src/App/Application.cpp @@ -98,8 +98,8 @@ #include "Annotation.h" #include "MeasureDistance.h" #include "Placement.h" -#include "GeoFeatureGroup.h" -#include "OriginGroup.h" +#include "GeoFeatureGroupExtension.h" +#include "OriginGroupExtension.h" #include "Part.h" #include "OriginFeature.h" #include "Origin.h" diff --git a/src/App/CMakeLists.txt b/src/App/CMakeLists.txt index 85f0125d10cc..fd6bf5ee67ae 100644 --- a/src/App/CMakeLists.txt +++ b/src/App/CMakeLists.txt @@ -59,8 +59,8 @@ generate_from_xml(DocumentObjectExtensionPy) generate_from_xml(GroupExtensionPy) generate_from_xml(DocumentObjectGroupPy) generate_from_xml(GeoFeaturePy) -#generate_from_xml(GeoFeatureGroupPy) -#generate_from_xml(OriginGroupPy) +generate_from_xml(GeoFeatureGroupExtensionPy) +generate_from_xml(OriginGroupExtensionPy) generate_from_xml(PartPy) generate_from_xml(ComplexGeoDataPy) @@ -78,8 +78,8 @@ SET(FreeCADApp_XML_SRCS DocumentObjectGroupPy.xml DocumentObjectPy.xml GeoFeaturePy.xml -# GeoFeatureGroupPy.xml -# OriginGroupPy.xml + GeoFeatureGroupExtensionPy.xml + OriginGroupExtensionPy.xml PartPy.xml DocumentPy.xml PropertyContainerPy.xml @@ -98,6 +98,7 @@ SET(Document_CPP_SRCS DocumentObjectExtension.cpp DocumentObjectExtensionPyImp.cpp ExtensionContainerPyImp.cpp + GroupExtension.cpp GroupExtensionPyImp.cpp DocumentObjectFileIncluded.cpp DocumentObjectGroup.cpp @@ -111,10 +112,10 @@ SET(Document_CPP_SRCS FeaturePython.cpp FeatureTest.cpp GeoFeature.cpp -# GeoFeatureGroupPyImp.cpp - GeoFeatureGroup.cpp -# OriginGroupPyImp.cpp - OriginGroup.cpp + GeoFeatureGroupExtensionPyImp.cpp + GeoFeatureGroupExtension.cpp + OriginGroupExtensionPyImp.cpp + OriginGroupExtension.cpp PartPyImp.cpp Part.cpp Origin.cpp @@ -136,6 +137,7 @@ SET(Document_HPP_SRCS Document.h DocumentObject.h Extension.h + GroupExtension.h DocumentObjectExtension.h DocumentObjectFileIncluded.h DocumentObjectGroup.h @@ -149,8 +151,8 @@ SET(Document_HPP_SRCS FeaturePythonPyImp.inl FeatureTest.h GeoFeature.h - GeoFeatureGroup.h - OriginGroup.h + GeoFeatureGroupExtension.h + OriginGroupExtension.h Part.h Origin.h Path.h diff --git a/src/App/DocumentObjectGroup.cpp b/src/App/DocumentObjectGroup.cpp index 1e6e7b6a8543..e005dc59642b 100644 --- a/src/App/DocumentObjectGroup.cpp +++ b/src/App/DocumentObjectGroup.cpp @@ -28,169 +28,11 @@ #include "DocumentObjectGroup.h" #include "DocumentObjectGroupPy.h" -#include "GroupExtensionPy.h" #include "Document.h" #include "FeaturePythonPyImp.h" using namespace App; -PROPERTY_SOURCE(App::GroupExtension, App::DocumentObjectExtension) - -GroupExtension::GroupExtension() -{ - initExtension(GroupExtension::getClassTypeId()); - - ADD_PROPERTY_TYPE(Group,(0),"Base",(App::PropertyType)(Prop_Output),"List of referenced objects"); -} - -GroupExtension::~GroupExtension() -{ -} - -DocumentObject* GroupExtension::addObject(const char* sType, const char* pObjectName) -{ - DocumentObject* obj = getExtendedObject()->getDocument()->addObject(sType, pObjectName); - if (obj) addObject(obj); - return obj; -} - -void GroupExtension::addObject(DocumentObject* obj) -{ - if (!hasObject(obj)) { - std::vector grp = Group.getValues(); - grp.push_back(obj); - Group.setValues(grp); - } -} - -void GroupExtension::removeObject(DocumentObject* obj) -{ - const std::vector & grp = Group.getValues(); - std::vector newGrp; - - std::remove_copy (grp.begin(), grp.end(), std::back_inserter (newGrp), obj); - if (grp.size() != newGrp.size()) { - Group.setValues (newGrp); - } -} - -void GroupExtension::removeObjectsFromDocument() -{ - const std::vector & grp = Group.getValues(); - // Use set so iterate on each linked object exactly one time (in case of multiple links to the same document) - std::set grpSet (grp.begin(), grp.end()); - - for (std::set::iterator it = grpSet.begin(); it != grpSet.end(); ++it) { - removeObjectFromDocument(*it); - } -} - -void GroupExtension::removeObjectFromDocument(DocumentObject* obj) -{ - // remove all children - if (obj->hasExtension(GroupExtension::getClassTypeId())) { - GroupExtension *grp = static_cast(obj->getExtension(GroupExtension::getClassTypeId())); - // recursive call to remove all subgroups - grp->removeObjectsFromDocument(); - } - - getExtendedObject()->getDocument()->remObject(obj->getNameInDocument()); -} - -DocumentObject *GroupExtension::getObject(const char *Name) const -{ - DocumentObject* obj = getExtendedObject()->getDocument()->getObject(Name); - if (obj && hasObject(obj)) - return obj; - return 0; -} - -bool GroupExtension::hasObject(const DocumentObject* obj, bool recursive) const -{ - const std::vector& grp = Group.getValues(); - for (std::vector::const_iterator it = grp.begin(); it != grp.end(); ++it) { - if (*it == obj) { - return true; - } else if ( recursive && (*it)->hasExtension(GroupExtension::getClassTypeId()) ) { - App::GroupExtension *subGroup = static_cast ((*it)->getExtension(GroupExtension::getClassTypeId())); - if (subGroup->hasObject (obj, recursive)) { - return true; - } - } - } - - return false; -} - -bool GroupExtension::isChildOf(const GroupExtension* group) const -{ - const std::vector& grp = group->Group.getValues(); - for (std::vector::const_iterator it = grp.begin(); it != grp.end(); ++it) { - if (*it == getExtendedObject()) - return true; - if ((*it)->hasExtension(GroupExtension::getClassTypeId())) { - if (this->isChildOf(static_cast((*it)->getExtension(GroupExtension::getClassTypeId())))) - return true; - } - } - - return false; -} - -std::vector GroupExtension::getObjects() const -{ - return Group.getValues(); -} - -std::vector GroupExtension::getObjectsOfType(const Base::Type& typeId) const -{ - std::vector type; - const std::vector& grp = Group.getValues(); - for (std::vector::const_iterator it = grp.begin(); it != grp.end(); ++it) { - if ( (*it)->getTypeId().isDerivedFrom(typeId)) - type.push_back(*it); - } - - return type; -} - -int GroupExtension::countObjectsOfType(const Base::Type& typeId) const -{ - int type=0; - const std::vector& grp = Group.getValues(); - for (std::vector::const_iterator it = grp.begin(); it != grp.end(); ++it) { - if ( (*it)->getTypeId().isDerivedFrom(typeId)) - type++; - } - - return type; -} - -DocumentObject* GroupExtension::getGroupOfObject(const DocumentObject* obj) -{ - const Document* doc = obj->getDocument(); - std::vector grps = doc->getObjectsOfType(GroupExtension::getClassTypeId()); - for (std::vector::const_iterator it = grps.begin(); it != grps.end(); ++it) { - GroupExtension* grp = (GroupExtension*)(*it); - if (grp->hasObject(obj)) - return *it; - } - - return 0; -} - -PyObject* GroupExtension::getExtensionPyObject(void) { - - if (ExtensionPythonObject.is(Py::_None())){ - // ref counter is set to 1 - auto grp = new GroupExtensionPy(this); - ExtensionPythonObject = Py::Object(grp,true); - } - return Py::new_reference_to(ExtensionPythonObject); -} - - - PROPERTY_SOURCE_WITH_EXTENSIONS(App::DocumentObjectGroup, App::DocumentObject, (App::GroupExtension)) DocumentObjectGroup::DocumentObjectGroup(void): DocumentObject(), GroupExtension() { @@ -215,7 +57,6 @@ PyObject *DocumentObjectGroup::getPyObject() // Python feature --------------------------------------------------------- namespace App { -PROPERTY_SOURCE_TEMPLATE(App::GroupExtensionPython, App::GroupExtension) /// @cond DOXERR PROPERTY_SOURCE_TEMPLATE(App::DocumentObjectGroupPython, App::DocumentObjectGroup) diff --git a/src/App/DocumentObjectGroup.h b/src/App/DocumentObjectGroup.h index 7dd4ee63d31f..42a4dd148014 100644 --- a/src/App/DocumentObjectGroup.h +++ b/src/App/DocumentObjectGroup.h @@ -27,81 +27,13 @@ #include "FeaturePython.h" #include "DocumentObject.h" #include "PropertyLinks.h" -#include "DocumentObjectExtension.h" +#include "GroupExtension.h" #include namespace App { -class DocumentObjectGroup; -class GroupExtensionPy; - -class AppExport GroupExtension : public DocumentObjectExtension -{ - PROPERTY_HEADER(App::GroupExtension); - -public: - /// Constructor - GroupExtension(void); - virtual ~GroupExtension(); - - /** @name Object handling */ - //@{ - /** Adds an object of \a sType with \a pObjectName to the document this group belongs to and - * append it to this group as well. - */ - DocumentObject *addObject(const char* sType, const char* pObjectName); - /* Adds the object \a obj to this group. - */ - void addObject(DocumentObject* obj); - /** Removes an object from this group. - */ - void removeObject(DocumentObject* obj); - /** Removes all children objects from this group and the document. - */ - void removeObjectsFromDocument(); - /** Returns the object of this group with \a Name. If the group doesn't have such an object 0 is returned. - * @note This method might return 0 even if the document this group belongs to contains an object with this name. - */ - DocumentObject *getObject(const char* Name) const; - /** - * Checks whether the object \a obj is part of this group. - * @param obj the object to check for. - * @param recursive if true check also if the obj is child of some sub group (default is false). - */ - bool hasObject(const DocumentObject* obj, bool recursive=false) const; - /** - * Checks whether this group object is a child (or sub-child) - * of the given group object. - */ - bool isChildOf(const GroupExtension*) const; - /** Returns a list of all objects this group does have. - */ - std::vector getObjects() const; - /** Returns a list of all objects of \a typeId this group does have. - */ - std::vector getObjectsOfType(const Base::Type& typeId) const; - /** Returns the number of objects of \a typeId this group does have. - */ - int countObjectsOfType(const Base::Type& typeId) const; - /** Returns the object group of the document which the given object \a obj is part of. - * In case this object is not part of a group 0 is returned. - */ - static DocumentObject* getGroupOfObject(const DocumentObject* obj); - //@} - virtual PyObject* getExtensionPyObject(void); - - /// Properties - PropertyLinkList Group; - -private: - void removeObjectFromDocument(DocumentObject*); -}; - -//no virtual functions to override, simple derivative is enough -typedef App::ExtensionPython GroupExtensionPython; - class DocumentObjectGroup : public DocumentObject, public GroupExtension { PROPERTY_HEADER_WITH_EXTENSIONS(App::DocumentObjectGroup); diff --git a/src/App/GeoFeatureGroup.cpp b/src/App/GeoFeatureGroupExtension.cpp similarity index 99% rename from src/App/GeoFeatureGroup.cpp rename to src/App/GeoFeatureGroupExtension.cpp index 4db4da2287fc..fa18b92c4b43 100644 --- a/src/App/GeoFeatureGroup.cpp +++ b/src/App/GeoFeatureGroupExtension.cpp @@ -29,7 +29,7 @@ #include -#include "GeoFeatureGroup.h" +#include "GeoFeatureGroupExtension.h" //#include "GeoFeatureGroupPy.h" //#include "FeaturePythonPyImp.h" diff --git a/src/App/GeoFeatureGroup.h b/src/App/GeoFeatureGroupExtension.h similarity index 100% rename from src/App/GeoFeatureGroup.h rename to src/App/GeoFeatureGroupExtension.h diff --git a/src/App/GeoFeatureGroupPy.xml b/src/App/GeoFeatureGroupExtensionPy.xml similarity index 62% rename from src/App/GeoFeatureGroupPy.xml rename to src/App/GeoFeatureGroupExtensionPy.xml index a5ffdccecbef..30884ba176e2 100644 --- a/src/App/GeoFeatureGroupPy.xml +++ b/src/App/GeoFeatureGroupExtensionPy.xml @@ -1,14 +1,14 @@ + FatherInclude="App/GroupExtensionPy.h" + FatherNamespace="App"> This class handles placeable group of document objects diff --git a/src/App/GeoFeatureGroupPyImp.cpp b/src/App/GeoFeatureGroupExtensionPyImp.cpp similarity index 83% rename from src/App/GeoFeatureGroupPyImp.cpp rename to src/App/GeoFeatureGroupExtensionPyImp.cpp index 478948e1830e..774282f43ab9 100644 --- a/src/App/GeoFeatureGroupPyImp.cpp +++ b/src/App/GeoFeatureGroupExtensionPyImp.cpp @@ -24,27 +24,27 @@ #include "PreCompiled.h" -#include "App/GeoFeatureGroup.h" +#include "App/GeoFeatureGroupExtension.h" -// inclusion of the generated files (generated out of GeoFeatureGroupPy.xml) -#include "GeoFeatureGroupPy.h" -#include "GeoFeatureGroupPy.cpp" +// inclusion of the generated files (generated out of GeoFeatureGroupExtensionPy.xml) +#include "GeoFeatureGroupExtensionPy.h" +#include "GeoFeatureGroupExtensionPy.cpp" using namespace App; // returns a string which represents the object e.g. when printed in python -std::string GeoFeatureGroupPy::representation(void) const +std::string GeoFeatureGroupExtensionPy::representation(void) const { return std::string(""); } -PyObject *GeoFeatureGroupPy::getCustomAttributes(const char* /*attr*/) const +PyObject *GeoFeatureGroupExtensionPy::getCustomAttributes(const char* /*attr*/) const { return 0; } -int GeoFeatureGroupPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) +int GeoFeatureGroupExtensionPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) { return 0; } diff --git a/src/App/GroupExtension.cpp b/src/App/GroupExtension.cpp new file mode 100644 index 000000000000..abd670a4a630 --- /dev/null +++ b/src/App/GroupExtension.cpp @@ -0,0 +1,193 @@ +/*************************************************************************** + * Copyright (c) 2006 Werner Mayer * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + + +#include "PreCompiled.h" + +#ifndef _PreComp_ +#endif + +#include "DocumentObjectGroup.h" +#include "DocumentObjectGroupPy.h" +#include "GroupExtensionPy.h" +#include "Document.h" +#include "FeaturePythonPyImp.h" + +using namespace App; + +PROPERTY_SOURCE(App::GroupExtension, App::DocumentObjectExtension) + +GroupExtension::GroupExtension() +{ + initExtension(GroupExtension::getClassTypeId()); + + ADD_PROPERTY_TYPE(Group,(0),"Base",(App::PropertyType)(Prop_Output),"List of referenced objects"); +} + +GroupExtension::~GroupExtension() +{ +} + +DocumentObject* GroupExtension::addObject(const char* sType, const char* pObjectName) +{ + DocumentObject* obj = getExtendedObject()->getDocument()->addObject(sType, pObjectName); + if (obj) addObject(obj); + return obj; +} + +void GroupExtension::addObject(DocumentObject* obj) +{ + if (!hasObject(obj)) { + std::vector grp = Group.getValues(); + grp.push_back(obj); + Group.setValues(grp); + } +} + +void GroupExtension::removeObject(DocumentObject* obj) +{ + const std::vector & grp = Group.getValues(); + std::vector newGrp; + + std::remove_copy (grp.begin(), grp.end(), std::back_inserter (newGrp), obj); + if (grp.size() != newGrp.size()) { + Group.setValues (newGrp); + } +} + +void GroupExtension::removeObjectsFromDocument() +{ + const std::vector & grp = Group.getValues(); + // Use set so iterate on each linked object exactly one time (in case of multiple links to the same document) + std::set grpSet (grp.begin(), grp.end()); + + for (std::set::iterator it = grpSet.begin(); it != grpSet.end(); ++it) { + removeObjectFromDocument(*it); + } +} + +void GroupExtension::removeObjectFromDocument(DocumentObject* obj) +{ + // remove all children + if (obj->hasExtension(GroupExtension::getClassTypeId())) { + GroupExtension *grp = static_cast(obj->getExtension(GroupExtension::getClassTypeId())); + + // recursive call to remove all subgroups + grp->removeObjectsFromDocument(); + } + + getExtendedObject()->getDocument()->remObject(obj->getNameInDocument()); +} + +DocumentObject *GroupExtension::getObject(const char *Name) const +{ + DocumentObject* obj = getExtendedObject()->getDocument()->getObject(Name); + if (obj && hasObject(obj)) + return obj; + return 0; +} + +bool GroupExtension::hasObject(const DocumentObject* obj, bool recursive) const +{ + const std::vector& grp = Group.getValues(); + for (std::vector::const_iterator it = grp.begin(); it != grp.end(); ++it) { + if (*it == obj) { + return true; + } else if ( recursive && (*it)->hasExtension(GroupExtension::getClassTypeId()) ) { + App::GroupExtension *subGroup = static_cast ((*it)->getExtension(GroupExtension::getClassTypeId())); + + if (subGroup->hasObject (obj, recursive)) { + return true; + } + } + } + + return false; +} + +bool GroupExtension::isChildOf(const GroupExtension* group) const +{ + const std::vector& grp = group->Group.getValues(); + for (std::vector::const_iterator it = grp.begin(); it != grp.end(); ++it) { + if (*it == getExtendedObject()) + return true; + if ((*it)->hasExtension(GroupExtension::getClassTypeId())) { + if (this->isChildOf(static_cast((*it)->getExtension(GroupExtension::getClassTypeId())))) + + return true; + } + } + + return false; +} + +std::vector GroupExtension::getObjects() const +{ + return Group.getValues(); +} + +std::vector GroupExtension::getObjectsOfType(const Base::Type& typeId) const +{ + std::vector type; + const std::vector& grp = Group.getValues(); + for (std::vector::const_iterator it = grp.begin(); it != grp.end(); ++it) { + if ( (*it)->getTypeId().isDerivedFrom(typeId)) + type.push_back(*it); + } + + return type; +} + +int GroupExtension::countObjectsOfType(const Base::Type& typeId) const +{ + int type=0; + const std::vector& grp = Group.getValues(); + for (std::vector::const_iterator it = grp.begin(); it != grp.end(); ++it) { + if ( (*it)->getTypeId().isDerivedFrom(typeId)) + type++; + } + + return type; +} + +DocumentObject* GroupExtension::getGroupOfObject(const DocumentObject* obj) +{ + const Document* doc = obj->getDocument(); + std::vector grps = doc->getObjectsOfType(GroupExtension::getClassTypeId()); + for (std::vector::const_iterator it = grps.begin(); it != grps.end(); ++it) { + GroupExtension* grp = (GroupExtension*)(*it); + if (grp->hasObject(obj)) + return *it; + } + + return 0; +} + +PyObject* GroupExtension::getExtensionPyObject(void) { + + if (ExtensionPythonObject.is(Py::_None())){ + // ref counter is set to 1 + auto grp = new GroupExtensionPy(this); + ExtensionPythonObject = Py::Object(grp,true); + } + return Py::new_reference_to(ExtensionPythonObject); +} diff --git a/src/App/GroupExtension.h b/src/App/GroupExtension.h new file mode 100644 index 000000000000..2eee37a4252c --- /dev/null +++ b/src/App/GroupExtension.h @@ -0,0 +1,105 @@ +/*************************************************************************** + * Copyright (c) 2006 Werner Mayer * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + + +#ifndef APP_GROUPEXTENSION_H +#define APP_GROUPEXTENSION_H + +#include "FeaturePython.h" +#include "DocumentObject.h" +#include "PropertyLinks.h" +#include "DocumentObjectExtension.h" +#include + + +namespace App +{ +class DocumentObjectGroup; +class GroupExtensionPy; + +class AppExport GroupExtension : public DocumentObjectExtension +{ + PROPERTY_HEADER(App::GroupExtension); + +public: + /// Constructor + GroupExtension(void); + virtual ~GroupExtension(); + + /** @name Object handling */ + //@{ + /** Adds an object of \a sType with \a pObjectName to the document this group belongs to and + * append it to this group as well. + */ + DocumentObject *addObject(const char* sType, const char* pObjectName); + /* Adds the object \a obj to this group. + */ + void addObject(DocumentObject* obj); + /** Removes an object from this group. + */ + void removeObject(DocumentObject* obj); + /** Removes all children objects from this group and the document. + */ + void removeObjectsFromDocument(); + /** Returns the object of this group with \a Name. If the group doesn't have such an object 0 is returned. + * @note This method might return 0 even if the document this group belongs to contains an object with this name. + */ + DocumentObject *getObject(const char* Name) const; + /** + * Checks whether the object \a obj is part of this group. + * @param obj the object to check for. + * @param recursive if true check also if the obj is child of some sub group (default is false). + */ + bool hasObject(const DocumentObject* obj, bool recursive=false) const; + /** + * Checks whether this group object is a child (or sub-child) + * of the given group object. + */ + bool isChildOf(const GroupExtension*) const; + /** Returns a list of all objects this group does have. + */ + std::vector getObjects() const; + /** Returns a list of all objects of \a typeId this group does have. + */ + std::vector getObjectsOfType(const Base::Type& typeId) const; + /** Returns the number of objects of \a typeId this group does have. + */ + int countObjectsOfType(const Base::Type& typeId) const; + /** Returns the object group of the document which the given object \a obj is part of. + * In case this object is not part of a group 0 is returned. + */ + static DocumentObject* getGroupOfObject(const DocumentObject* obj); + //@} + + virtual PyObject* getExtensionPyObject(void); + + /// Properties + PropertyLinkList Group; + +private: + void removeObjectFromDocument(DocumentObject*); +}; + +} //namespace App + + +#endif // APP_GROUPEXTENSION_H diff --git a/src/App/GroupExtensionPy.xml b/src/App/GroupExtensionPy.xml new file mode 100644 index 000000000000..703d310cb797 --- /dev/null +++ b/src/App/GroupExtensionPy.xml @@ -0,0 +1,52 @@ + + + + + + Extension class which allows grouping of document objects + + + + Create and add an object with given type and name to the group + + + + + Add an object to the group + + + + + Remove an object from the group + + + + + Remove all child objects from the group and document + + + + + Return the object with the given name + + + + + hasObject(obj, recursive=false) + Checks if the group has a given object + @param obj the object to check for. + @param recursive if true check also if the obj is child of some sub group (default is false). + + + + + + diff --git a/src/App/GroupExtensionPyImp.cpp b/src/App/GroupExtensionPyImp.cpp new file mode 100644 index 000000000000..c99a5a8a18ff --- /dev/null +++ b/src/App/GroupExtensionPyImp.cpp @@ -0,0 +1,177 @@ +/*************************************************************************** + * Copyright (c) 2007 Werner Mayer * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + + +#include "PreCompiled.h" + +#include "DocumentObjectGroup.h" +#include "Document.h" +#include + +// inclusion of the generated files (generated out of GroupExtensionPy.xml) +#include "GroupExtensionPy.h" +#include "GroupExtensionPy.cpp" +#include "DocumentObjectPy.h" + +using namespace App; + +// returns a string which represent the object e.g. when printed in python +std::string GroupExtensionPy::representation(void) const +{ + return std::string(""); +} + +PyObject* GroupExtensionPy::newObject(PyObject *args) +{ + char *sType,*sName=0; + if (!PyArg_ParseTuple(args, "s|s", &sType,&sName)) // convert args: Python->C + return NULL; + + DocumentObject *object = getGroupExtensionPtr()->addObject(sType, sName); + if ( object ) { + return object->getPyObject(); + } + else { + PyErr_Format(Base::BaseExceptionFreeCADError, "Cannot create object of type '%s'", sType); + return NULL; + } +} + +PyObject* GroupExtensionPy::addObject(PyObject *args) +{ + PyObject *object; + if (!PyArg_ParseTuple(args, "O!", &(DocumentObjectPy::Type), &object)) // convert args: Python->C + return NULL; // NULL triggers exception + + DocumentObjectPy* docObj = static_cast(object); + if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->getNameInDocument()) { + PyErr_SetString(Base::BaseExceptionFreeCADError, "Cannot add an invalid object"); + return NULL; + } + + if (docObj->getDocumentObjectPtr()->getDocument() != getGroupExtensionPtr()->getExtendedObject()->getDocument()) { + PyErr_SetString(Base::BaseExceptionFreeCADError, "Cannot add an object from another document to this group"); + return NULL; + } + if (docObj->getDocumentObjectPtr() == this->getGroupExtensionPtr()->getExtendedObject()) { + PyErr_SetString(Base::BaseExceptionFreeCADError, "Cannot add a group object to itself"); + return NULL; + } + if (docObj->getDocumentObjectPtr()->hasExtension(GroupExtension::getClassTypeId())) { + App::GroupExtension* docGrp = docObj->getDocumentObjectPtr()->getExtensionByType(); + if (docGrp->hasObject(getGroupExtensionPtr()->getExtendedObject())) { + PyErr_SetString(Base::BaseExceptionFreeCADError, "Cannot add a group object to a child group"); + return NULL; + } + } + + GroupExtension* grp = getGroupExtensionPtr(); + + grp->addObject(docObj->getDocumentObjectPtr()); + Py_Return; +} + +PyObject* GroupExtensionPy::removeObject(PyObject *args) +{ + PyObject *object; + if (!PyArg_ParseTuple(args, "O!", &(DocumentObjectPy::Type), &object)) // convert args: Python->C + return NULL; // NULL triggers exception + + DocumentObjectPy* docObj = static_cast(object); + if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->getNameInDocument()) { + PyErr_SetString(Base::BaseExceptionFreeCADError, "Cannot remove an invalid object"); + return NULL; + } + if (docObj->getDocumentObjectPtr()->getDocument() != getGroupExtensionPtr()->getExtendedObject()->getDocument()) { + PyErr_SetString(Base::BaseExceptionFreeCADError, "Cannot remove an object from another document from this group"); + return NULL; + } + + GroupExtension* grp = getGroupExtensionPtr(); + + grp->removeObject(docObj->getDocumentObjectPtr()); + Py_Return; +} + +PyObject* GroupExtensionPy::removeObjectsFromDocument(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) // convert args: Python->C + return NULL; // NULL triggers exception + + getGroupExtensionPtr()->removeObjectsFromDocument(); + Py_Return; +} + +PyObject* GroupExtensionPy::getObject(PyObject *args) +{ + char* pcName; + if (!PyArg_ParseTuple(args, "s", &pcName)) // convert args: Python->C + return NULL; // NULL triggers exception + + DocumentObject* obj = getGroupExtensionPtr()->getObject(pcName); + if ( obj ) { + return obj->getPyObject(); + } else { + Py_Return; + } +} + +PyObject* GroupExtensionPy::hasObject(PyObject *args) +{ + PyObject *object; + PyObject *recursivePy = 0; + int recursive = 0; + if (!PyArg_ParseTuple(args, "O!|O", &(DocumentObjectPy::Type), &object, &recursivePy)) + return NULL; // NULL triggers exception + + DocumentObjectPy* docObj = static_cast(object); + if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->getNameInDocument()) { + PyErr_SetString(Base::BaseExceptionFreeCADError, "Cannot check an invalid object"); + return NULL; + } + if (docObj->getDocumentObjectPtr()->getDocument() != getGroupExtensionPtr()->getExtendedObject()->getDocument()) { + PyErr_SetString(Base::BaseExceptionFreeCADError, "Cannot check an object from another document with this group"); + return NULL; + } + if (recursivePy) { + recursive = PyObject_IsTrue(recursivePy); + if ( recursive == -1) { + // Note: shouldn't happen + PyErr_SetString(PyExc_ValueError, "The recursive parameter should be of boolean type"); + return 0; + } + } + + bool v = getGroupExtensionPtr()->hasObject(docObj->getDocumentObjectPtr(), recursive); + return PyBool_FromLong(v ? 1 : 0); +} + +PyObject *GroupExtensionPy::getCustomAttributes(const char* /*attr*/) const +{ + return 0; +} + +int GroupExtensionPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) +{ + return 0; +} + diff --git a/src/App/OriginGroup.cpp b/src/App/OriginGroupExtension.cpp similarity index 99% rename from src/App/OriginGroup.cpp rename to src/App/OriginGroupExtension.cpp index cb1276a6d69f..4cc984f2edc6 100644 --- a/src/App/OriginGroup.cpp +++ b/src/App/OriginGroupExtension.cpp @@ -21,7 +21,7 @@ ***************************************************************************/ #include "PreCompiled.h" -#include "OriginGroup.h" +#include "OriginGroupExtension.h" #ifndef _PreComp_ #endif diff --git a/src/App/OriginGroup.h b/src/App/OriginGroupExtension.h similarity index 98% rename from src/App/OriginGroup.h rename to src/App/OriginGroupExtension.h index f8c3f6fdc20c..7a258e66676f 100644 --- a/src/App/OriginGroup.h +++ b/src/App/OriginGroupExtension.h @@ -23,7 +23,7 @@ #ifndef ORIGINGROUP_H_QHTU73IF #define ORIGINGROUP_H_QHTU73IF -#include "GeoFeatureGroup.h" +#include "GeoFeatureGroupExtension.h" #include "PropertyLinks.h" namespace App { diff --git a/src/App/OriginGroupPy.xml b/src/App/OriginGroupExtensionPy.xml similarity index 66% rename from src/App/OriginGroupPy.xml rename to src/App/OriginGroupExtensionPy.xml index f85670446d00..8a44fa579d11 100644 --- a/src/App/OriginGroupPy.xml +++ b/src/App/OriginGroupExtensionPy.xml @@ -1,13 +1,13 @@ diff --git a/src/App/OriginGroupPyImp.cpp b/src/App/OriginGroupExtensionPyImp.cpp similarity index 84% rename from src/App/OriginGroupPyImp.cpp rename to src/App/OriginGroupExtensionPyImp.cpp index 9f4d10ef961f..e18462bea9ee 100644 --- a/src/App/OriginGroupPyImp.cpp +++ b/src/App/OriginGroupExtensionPyImp.cpp @@ -23,16 +23,16 @@ #include "PreCompiled.h" -#include "App/OriginGroup.h" +#include "App/OriginGroupExtension.h" // inclusion of the generated files (generated out of OriginGroupPy.xml) -#include "OriginGroupPy.h" -#include "OriginGroupPy.cpp" +#include "OriginGroupExtensionPy.h" +#include "OriginGroupExtensionPy.cpp" using namespace App; // returns a string which represents the object e.g. when printed in python -std::string OriginGroupPy::representation(void) const +std::string OriginGroupExtensionPy::representation(void) const { return std::string(""); } @@ -43,12 +43,12 @@ std::string OriginGroupPy::representation(void) const -PyObject *OriginGroupPy::getCustomAttributes(const char* /*attr*/) const +PyObject *OriginGroupExtensionPy::getCustomAttributes(const char* /*attr*/) const { return 0; } -int OriginGroupPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) +int OriginGroupExtensionPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) { return 0; } diff --git a/src/App/Part.h b/src/App/Part.h index 2b3f12a31d2d..d34388fe4878 100644 --- a/src/App/Part.h +++ b/src/App/Part.h @@ -24,7 +24,7 @@ #ifndef APP_Part_H #define APP_Part_H -#include "OriginGroup.h" +#include "OriginGroupExtension.h" #include "PropertyLinks.h" diff --git a/src/Gui/ViewProviderGeoFeatureGroup.cpp b/src/Gui/ViewProviderGeoFeatureGroup.cpp index ce45add96f26..68d65b5dfd97 100644 --- a/src/Gui/ViewProviderGeoFeatureGroup.cpp +++ b/src/Gui/ViewProviderGeoFeatureGroup.cpp @@ -28,7 +28,7 @@ # include #endif -#include +#include #include "ViewProviderGeoFeatureGroup.h" diff --git a/src/Gui/ViewProviderOriginGroup.cpp b/src/Gui/ViewProviderOriginGroup.cpp index a2b1ec1fe77c..57e1db81d511 100644 --- a/src/Gui/ViewProviderOriginGroup.cpp +++ b/src/Gui/ViewProviderOriginGroup.cpp @@ -29,7 +29,7 @@ #endif #include -#include +#include #include #include "Application.h" diff --git a/src/Mod/PartDesign/Gui/ReferenceSelection.cpp b/src/Mod/PartDesign/Gui/ReferenceSelection.cpp index 5a67cde458b0..fa2e72ee5f97 100644 --- a/src/Mod/PartDesign/Gui/ReferenceSelection.cpp +++ b/src/Mod/PartDesign/Gui/ReferenceSelection.cpp @@ -32,7 +32,7 @@ #endif #include -#include +#include #include #include #include diff --git a/src/Mod/PartDesign/Gui/ViewProviderDatum.cpp b/src/Mod/PartDesign/Gui/ViewProviderDatum.cpp index bb14d9652a35..b447581665a0 100644 --- a/src/Mod/PartDesign/Gui/ViewProviderDatum.cpp +++ b/src/Mod/PartDesign/Gui/ViewProviderDatum.cpp @@ -53,7 +53,7 @@ #endif #include -#include +#include #include #include #include