PropertyConstraintList.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) Jürgen Riegel          (juergen.riegel@web.de) 2010     *
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 #   include <assert.h>
00028 #endif
00029 
00031 
00032 #include <Base/Exception.h>
00033 #include <Base/Reader.h>
00034 #include <Base/Writer.h>
00035 
00036 #include "PropertyConstraintList.h"
00037 #include "ConstraintPy.h"
00038 
00039 using namespace App;
00040 using namespace Base;
00041 using namespace std;
00042 using namespace Sketcher;
00043 
00044 
00045 //**************************************************************************
00046 // PropertyConstraintList
00047 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00048 
00049 TYPESYSTEM_SOURCE(Sketcher::PropertyConstraintList, App::PropertyLists);
00050 
00051 //**************************************************************************
00052 // Construction/Destruction
00053 
00054 
00055 PropertyConstraintList::PropertyConstraintList() : validGeometryKeys(0), invalidGeometry(true)
00056 {
00057 
00058 }
00059 
00060 PropertyConstraintList::~PropertyConstraintList()
00061 {
00062     for (std::vector<Constraint*>::iterator it = _lValueList.begin(); it != _lValueList.end(); ++it)
00063         if (*it) delete *it;
00064 }
00065 
00066 void PropertyConstraintList::setSize(int newSize)
00067 {
00068     for (unsigned int i = newSize; i < _lValueList.size(); i++)
00069         delete _lValueList[i];
00070     _lValueList.resize(newSize);
00071 }
00072 
00073 int PropertyConstraintList::getSize(void) const
00074 {
00075     return static_cast<int>(_lValueList.size());
00076 }
00077 
00078 void PropertyConstraintList::setValue(const Constraint* lValue)
00079 {
00080     if (lValue) {
00081         aboutToSetValue();
00082         Constraint* newVal = lValue->clone();
00083         for (unsigned int i = 0; i < _lValueList.size(); i++)
00084             delete _lValueList[i];
00085         _lValueList.resize(1);
00086         _lValueList[0] = newVal;
00087         hasSetValue();
00088     }
00089 }
00090 
00091 void PropertyConstraintList::setValues(const std::vector<Constraint*>& lValue)
00092 {
00093     aboutToSetValue();
00094     applyValues(lValue);
00095     hasSetValue();
00096 }
00097 
00098 void PropertyConstraintList::applyValues(const std::vector<Constraint*>& lValue)
00099 {
00100     std::vector<Constraint*> oldVals(_lValueList);
00101     _lValueList.resize(lValue.size());
00102     // copy all objects
00103     for (unsigned int i = 0; i < lValue.size(); i++)
00104         _lValueList[i] = lValue[i]->clone();
00105     for (unsigned int i = 0; i < oldVals.size(); i++)
00106         delete oldVals[i];
00107 }
00108 
00109 PyObject *PropertyConstraintList::getPyObject(void)
00110 {
00111     PyObject* list = PyList_New(getSize());
00112     for (int i = 0; i < getSize(); i++)
00113         PyList_SetItem( list, i, _lValueList[i]->getPyObject());
00114     return list;
00115 }
00116 
00117 void PropertyConstraintList::setPyObject(PyObject *value)
00118 {
00119     if (PyList_Check(value)) {
00120         Py_ssize_t nSize = PyList_Size(value);
00121         std::vector<Constraint*> values;
00122         values.resize(nSize);
00123 
00124         for (Py_ssize_t i=0; i < nSize; ++i) {
00125             PyObject* item = PyList_GetItem(value, i);
00126             if (!PyObject_TypeCheck(item, &(ConstraintPy::Type))) {
00127                 std::string error = std::string("types in list must be 'Constraint', not ");
00128                 error += item->ob_type->tp_name;
00129                 throw Py::TypeError(error);
00130             }
00131 
00132             values[i] = static_cast<ConstraintPy*>(item)->getConstraintPtr();
00133         }
00134 
00135         setValues(values);
00136     }
00137     else if (PyObject_TypeCheck(value, &(ConstraintPy::Type))) {
00138         ConstraintPy *pcObject = static_cast<ConstraintPy*>(value);
00139         setValue(pcObject->getConstraintPtr());
00140     }
00141     else {
00142         std::string error = std::string("type must be 'Constraint' or list of 'Constraint', not ");
00143         error += value->ob_type->tp_name;
00144         throw Py::TypeError(error);
00145     }
00146 }
00147 
00148 void PropertyConstraintList::Save(Writer &writer) const
00149 {
00150     writer.Stream() << writer.ind() << "<ConstraintList count=\"" << getSize() <<"\">" << endl;
00151     writer.incInd();
00152     for (int i = 0; i < getSize(); i++)
00153         _lValueList[i]->Save(writer);
00154     writer.decInd();
00155     writer.Stream() << writer.ind() << "</ConstraintList>" << endl ;
00156 }
00157 
00158 void PropertyConstraintList::Restore(Base::XMLReader &reader)
00159 {
00160     // read my element
00161     reader.readElement("ConstraintList");
00162     // get the value of my attribute
00163     int count = reader.getAttributeAsInteger("count");
00164 
00165     std::vector<Constraint*> values;
00166     values.reserve(count);
00167     for (int i = 0; i < count; i++) {
00168         Constraint *newC = new Constraint();
00169         newC->Restore(reader);
00170         values.push_back(newC);
00171     }
00172 
00173     reader.readEndElement("ConstraintList");
00174 
00175     // assignment
00176     setValues(values);
00177 }
00178 
00179 Property *PropertyConstraintList::Copy(void) const
00180 {
00181     PropertyConstraintList *p = new PropertyConstraintList();
00182     p->applyValidGeometryKeys(validGeometryKeys);
00183     p->applyValues(_lValueList);
00184     return p;
00185 }
00186 
00187 void PropertyConstraintList::Paste(const Property &from)
00188 {
00189     const PropertyConstraintList& FromList = dynamic_cast<const PropertyConstraintList&>(from);
00190     aboutToSetValue();
00191     applyValues(FromList._lValueList);
00192     applyValidGeometryKeys(FromList.validGeometryKeys);
00193     hasSetValue();
00194 }
00195 
00196 unsigned int PropertyConstraintList::getMemSize(void) const
00197 {
00198     int size = sizeof(PropertyConstraintList);
00199     for (int i = 0; i < getSize(); i++)
00200         size += _lValueList[i]->getMemSize();
00201     return size;
00202 }
00203 
00204 void PropertyConstraintList::acceptGeometry(const std::vector<Part::Geometry *> &GeoList)
00205 {
00206     aboutToSetValue();
00207     validGeometryKeys.clear();
00208     validGeometryKeys.reserve(GeoList.size());
00209     for (std::vector< Part::Geometry * >::const_iterator it=GeoList.begin();
00210          it != GeoList.end(); ++it)
00211         validGeometryKeys.push_back((*it)->getTypeId().getKey());
00212     invalidGeometry = false;
00213     hasSetValue();
00214 }
00215 
00216 void PropertyConstraintList::applyValidGeometryKeys(const std::vector<unsigned int> &keys)
00217 {
00218     validGeometryKeys = keys;
00219 }
00220 
00221 void PropertyConstraintList::checkGeometry(const std::vector<Part::Geometry *> &GeoList)
00222 {
00223     if (validGeometryKeys.size() != GeoList.size()) {
00224         invalidGeometry = true;
00225         return;
00226     }
00227 
00228     unsigned int i=0;
00229     for (std::vector< Part::Geometry * >::const_iterator it=GeoList.begin();
00230          it != GeoList.end(); ++it, i++) {
00231         if (validGeometryKeys[i] != (*it)->getTypeId().getKey()) {
00232             invalidGeometry = true;
00233             return;
00234         }
00235     }
00236 
00237     if (invalidGeometry) {
00238         invalidGeometry = false;
00239         touch();
00240     }
00241 }
00242 
00243 std::vector<Constraint *> PropertyConstraintList::_emptyValueList(0);

Generated on Wed Nov 23 19:00:30 2011 for FreeCAD by  doxygen 1.6.1