00001 /*************************************************************************** 00002 * Copyright (c) 2006 Werner Mayer <wmayer[at]users.sourceforge.net> * 00003 * * 00004 * This file is part of the FreeCAD CAx development system. * 00005 * * 00006 * This library is free software; you can redistribute it and/or * 00007 * modify it under the terms of the GNU Library General Public * 00008 * License as published by the Free Software Foundation; either * 00009 * version 2 of the License, or (at your option) any later version. * 00010 * * 00011 * This library is distributed in the hope that it will be useful, * 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00014 * GNU Library General Public License for more details. * 00015 * * 00016 * You should have received a copy of the GNU Library General Public * 00017 * License along with this library; see the file COPYING.LIB. If not, * 00018 * write to the Free Software Foundation, Inc., 59 Temple Place, * 00019 * Suite 330, Boston, MA 02111-1307, USA * 00020 * * 00021 ***************************************************************************/ 00022 00023 00024 #include "PreCompiled.h" 00025 00026 #ifndef _PreComp_ 00027 #endif 00028 00029 #include "DocumentObjectGroup.h" 00030 #include "DocumentObjectGroupPy.h" 00031 #include "Document.h" 00032 00033 using namespace App; 00034 00035 PROPERTY_SOURCE(App::DocumentObjectGroup, App::DocumentObject) 00036 00037 00038 DocumentObjectGroup::DocumentObjectGroup() 00039 { 00040 ADD_PROPERTY_TYPE(Group,(0),"Base",(App::PropertyType)(Prop_ReadOnly|Prop_Output),"List of referenced objects"); 00041 } 00042 00043 DocumentObjectGroup::~DocumentObjectGroup() 00044 { 00045 } 00046 00047 DocumentObject* DocumentObjectGroup::addObject(const char* sType, const char* pObjectName) 00048 { 00049 DocumentObject* obj = getDocument()->addObject(sType, pObjectName); 00050 if (obj) addObject(obj); 00051 return obj; 00052 } 00053 00054 void DocumentObjectGroup::addObject(DocumentObject* obj) 00055 { 00056 if (!hasObject(obj)) { 00057 std::vector<DocumentObject*> grp = Group.getValues(); 00058 grp.push_back(obj); 00059 Group.setValues(grp); 00060 } 00061 } 00062 00063 void DocumentObjectGroup::removeObject(DocumentObject* obj) 00064 { 00065 std::vector<DocumentObject*> grp = Group.getValues(); 00066 for (std::vector<DocumentObject*>::iterator it = grp.begin(); it != grp.end(); ++it) { 00067 if (*it == obj) { 00068 grp.erase(it); 00069 Group.setValues(grp); 00070 break; 00071 } 00072 } 00073 } 00074 00075 void DocumentObjectGroup::removeObjectsFromDocument() 00076 { 00077 std::vector<DocumentObject*> grp = Group.getValues(); 00078 for (std::vector<DocumentObject*>::iterator it = grp.begin(); it != grp.end(); ++it) { 00079 removeObjectFromDocument(*it); 00080 } 00081 } 00082 00083 void DocumentObjectGroup::removeObjectFromDocument(DocumentObject* obj) 00084 { 00085 // remove all children 00086 if (obj->getTypeId().isDerivedFrom(DocumentObjectGroup::getClassTypeId())) { 00087 std::vector<DocumentObject*> grp = static_cast<DocumentObjectGroup*>(obj)->Group.getValues(); 00088 for (std::vector<DocumentObject*>::iterator it = grp.begin(); it != grp.end(); ++it) { 00089 // recursive call to remove all subgroups 00090 removeObjectFromDocument(*it); 00091 } 00092 } 00093 00094 this->getDocument()->remObject(obj->getNameInDocument()); 00095 } 00096 00097 DocumentObject *DocumentObjectGroup::getObject(const char *Name) const 00098 { 00099 DocumentObject* obj = getDocument()->getObject(Name); 00100 if (obj && hasObject(obj)) 00101 return obj; 00102 return 0; 00103 } 00104 00105 bool DocumentObjectGroup::hasObject(DocumentObject* obj) const 00106 { 00107 const std::vector<DocumentObject*>& grp = Group.getValues(); 00108 for (std::vector<DocumentObject*>::const_iterator it = grp.begin(); it != grp.end(); ++it) { 00109 if (*it == obj) 00110 return true; 00111 } 00112 00113 return false; 00114 } 00115 00116 bool DocumentObjectGroup::isChildOf(DocumentObjectGroup* group) const 00117 { 00118 const std::vector<DocumentObject*>& grp = group->Group.getValues(); 00119 for (std::vector<DocumentObject*>::const_iterator it = grp.begin(); it != grp.end(); ++it) { 00120 if (*it == this) 00121 return true; 00122 if ((*it)->getTypeId().isDerivedFrom(DocumentObjectGroup::getClassTypeId())) { 00123 if (this->isChildOf(static_cast<DocumentObjectGroup*>(*it))) 00124 return true; 00125 } 00126 } 00127 00128 return false; 00129 } 00130 00131 std::vector<DocumentObject*> DocumentObjectGroup::getObjects() const 00132 { 00133 return Group.getValues(); 00134 } 00135 00136 std::vector<DocumentObject*> DocumentObjectGroup::getObjectsOfType(const Base::Type& typeId) const 00137 { 00138 std::vector<DocumentObject*> type; 00139 const std::vector<DocumentObject*>& grp = Group.getValues(); 00140 for (std::vector<DocumentObject*>::const_iterator it = grp.begin(); it != grp.end(); ++it) { 00141 if ( (*it)->getTypeId().isDerivedFrom(typeId)) 00142 type.push_back(*it); 00143 } 00144 00145 return type; 00146 } 00147 00148 int DocumentObjectGroup::countObjectsOfType(const Base::Type& typeId) const 00149 { 00150 int type=0; 00151 const std::vector<DocumentObject*>& grp = Group.getValues(); 00152 for (std::vector<DocumentObject*>::const_iterator it = grp.begin(); it != grp.end(); ++it) { 00153 if ( (*it)->getTypeId().isDerivedFrom(typeId)) 00154 type++; 00155 } 00156 00157 return type; 00158 } 00159 00160 DocumentObjectGroup* DocumentObjectGroup::getGroupOfObject(DocumentObject* obj) 00161 { 00162 const Document* doc = obj->getDocument(); 00163 std::vector<DocumentObject*> grps = doc->getObjectsOfType(DocumentObjectGroup::getClassTypeId()); 00164 for (std::vector<DocumentObject*>::iterator it = grps.begin(); it != grps.end(); ++it) { 00165 DocumentObjectGroup* grp = (DocumentObjectGroup*)(*it); 00166 if (grp->hasObject(obj)) 00167 return grp; 00168 } 00169 00170 return 0; 00171 } 00172 00173 PyObject *DocumentObjectGroup::getPyObject() 00174 { 00175 if (PythonObject.is(Py::_None())){ 00176 // ref counter is set to 1 00177 PythonObject = Py::Object(new DocumentObjectGroupPy(this),true); 00178 } 00179 return Py::new_reference_to(PythonObject); 00180 } 00181 00182 // Python Sketcher feature --------------------------------------------------------- 00183 00184 namespace App { 00186 PROPERTY_SOURCE_TEMPLATE(App::DocumentObjectGroupPython, App::DocumentObjectGroup) 00187 template<> const char* App::DocumentObjectGroupPython::getViewProviderName(void) const { 00188 return "Gui::ViewProviderDocumentObjectGroup"; 00189 } 00191 00192 // explicit template instantiation 00193 template class AppExport FeaturePythonT<App::DocumentObjectGroup>; 00194 }