PropertyGeo.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) Jürgen Riegel          (juergen.riegel@web.de) 2002     *
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/Writer.h>
00034 #include <Base/Reader.h>
00035 #include <Base/Stream.h>
00036 #include <Base/Rotation.h>
00037 #include <Base/VectorPy.h>
00038 #include <Base/MatrixPy.h>
00039 #include <Base/PlacementPy.h>
00040 
00041 #include "Placement.h"
00042 
00043 #include "PropertyGeo.h"
00044 
00045 using namespace App;
00046 using namespace Base;
00047 using namespace std;
00048 
00049 
00050 
00051 
00052 //**************************************************************************
00053 //**************************************************************************
00054 // PropertyVector
00055 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00056 
00057 TYPESYSTEM_SOURCE(App::PropertyVector , App::Property);
00058 
00059 //**************************************************************************
00060 // Construction/Destruction
00061 
00062 
00063 PropertyVector::PropertyVector()
00064 {
00065 
00066 }
00067 
00068 
00069 PropertyVector::~PropertyVector()
00070 {
00071 
00072 }
00073 
00074 //**************************************************************************
00075 // Base class implementer
00076 
00077 
00078 void PropertyVector::setValue(const Base::Vector3f &vec)
00079 {
00080     aboutToSetValue();
00081     _cVec=vec;
00082     hasSetValue();
00083 }
00084 
00085 void PropertyVector::setValue(float x, float y, float z)
00086 {
00087     aboutToSetValue();
00088     _cVec=Vector3f(x,y,z);
00089     hasSetValue();
00090 }
00091 
00092 const Base::Vector3f & PropertyVector::getValue(void)const
00093 {
00094     return _cVec;
00095 }
00096 
00097 PyObject *PropertyVector::getPyObject(void)
00098 {
00099     return new Base::VectorPy(_cVec);
00100 }
00101 
00102 void PropertyVector::setPyObject(PyObject *value)
00103 {
00104     if (PyObject_TypeCheck(value, &(Base::VectorPy::Type))) {
00105         Base::VectorPy  *pcObject = static_cast<Base::VectorPy*>(value);
00106         Base::Vector3d* val = pcObject->getVectorPtr();
00107         Base::Vector3f vec((float)val->x,(float)val->y,(float)val->z);
00108         setValue(vec);
00109     }
00110     else if (PyTuple_Check(value)&&PyTuple_Size(value)==3) {
00111         PyObject* item;
00112         Base::Vector3f cVec;
00113         // x
00114         item = PyTuple_GetItem(value,0);
00115         if (PyFloat_Check(item))
00116             cVec.x = (float)PyFloat_AsDouble(item);
00117         else if (PyInt_Check(item))
00118             cVec.x = (float)PyInt_AsLong(item);
00119         else
00120             throw Base::Exception("Not allowed type used in tuple (float expected)...");
00121         // y
00122         item = PyTuple_GetItem(value,1);
00123         if (PyFloat_Check(item))
00124             cVec.y = (float)PyFloat_AsDouble(item);
00125         else if (PyInt_Check(item))
00126             cVec.y = (float)PyInt_AsLong(item);
00127         else
00128             throw Base::Exception("Not allowed type used in tuple (float expected)...");
00129         // z
00130         item = PyTuple_GetItem(value,2);
00131         if (PyFloat_Check(item))
00132             cVec.z = (float)PyFloat_AsDouble(item);
00133         else if (PyInt_Check(item))
00134             cVec.z = (float)PyInt_AsLong(item);
00135         else
00136             throw Base::Exception("Not allowed type used in tuple (float expected)...");
00137         setValue( cVec );
00138     }
00139     else {
00140         std::string error = std::string("type must be 'Vector' or tuple of three floats, not ");
00141         error += value->ob_type->tp_name;
00142         throw Py::TypeError(error);
00143     }
00144 }
00145 
00146 void PropertyVector::Save (Base::Writer &writer) const
00147 {
00148     writer.Stream() << writer.ind() << "<PropertyVector valueX=\"" <<  _cVec.x << "\" valueY=\"" <<  _cVec.y << "\" valueZ=\"" <<  _cVec.z <<"\"/>" << endl;
00149 }
00150 
00151 void PropertyVector::Restore(Base::XMLReader &reader)
00152 {
00153     // read my Element
00154     reader.readElement("PropertyVector");
00155     // get the value of my Attribute
00156     aboutToSetValue();
00157     _cVec.x = (float)reader.getAttributeAsFloat("valueX");
00158     _cVec.y = (float)reader.getAttributeAsFloat("valueY");
00159     _cVec.z = (float)reader.getAttributeAsFloat("valueZ");
00160     hasSetValue();
00161 }
00162 
00163 
00164 Property *PropertyVector::Copy(void) const
00165 {
00166     PropertyVector *p= new PropertyVector();
00167     p->_cVec = _cVec;
00168     return p;
00169 }
00170 
00171 void PropertyVector::Paste(const Property &from)
00172 {
00173     aboutToSetValue();
00174     _cVec = dynamic_cast<const PropertyVector&>(from)._cVec;
00175     hasSetValue();
00176 }
00177 
00178 
00179 //**************************************************************************
00180 // PropertyVectorList
00181 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00182 
00183 TYPESYSTEM_SOURCE(App::PropertyVectorList , App::PropertyLists);
00184 
00185 //**************************************************************************
00186 // Construction/Destruction
00187 
00188 PropertyVectorList::PropertyVectorList()
00189 {
00190 
00191 }
00192 
00193 PropertyVectorList::~PropertyVectorList()
00194 {
00195 
00196 }
00197 
00198 //**************************************************************************
00199 // Base class implementer
00200 
00201 void PropertyVectorList::setSize(int newSize)
00202 {
00203     _lValueList.resize(newSize);
00204 }
00205 
00206 int PropertyVectorList::getSize(void) const
00207 {
00208     return static_cast<int>(_lValueList.size());
00209 }
00210 
00211 void PropertyVectorList::setValue(const Base::Vector3f& lValue)
00212 {
00213     aboutToSetValue();
00214     _lValueList.resize(1);
00215     _lValueList[0]=lValue;
00216     hasSetValue();
00217 }
00218 
00219 void PropertyVectorList::setValue(float x, float y, float z)
00220 {
00221     aboutToSetValue();
00222     _lValueList.resize(1);
00223     _lValueList[0].Set(x,y,z);
00224     hasSetValue();
00225 }
00226 
00227 void PropertyVectorList::setValues(const std::vector<Base::Vector3f>& values)
00228 {
00229     aboutToSetValue();
00230     _lValueList = values;
00231     hasSetValue();
00232 }
00233 
00234 PyObject *PropertyVectorList::getPyObject(void)
00235 {
00236     PyObject* list = PyList_New(        getSize() );
00237 
00238     for (int i = 0;i<getSize(); i++)
00239         PyList_SetItem( list, i, new VectorPy(  _lValueList[i]));
00240 
00241     return list;
00242 }
00243 
00244 void PropertyVectorList::setPyObject(PyObject *value)
00245 {
00246     if (PyList_Check(value)) {
00247         Py_ssize_t nSize = PyList_Size(value);
00248         std::vector<Base::Vector3f> values;
00249         values.resize(nSize);
00250 
00251         for (Py_ssize_t i=0; i<nSize;++i) {
00252             PyObject* item = PyList_GetItem(value, i);
00253             PropertyVector val;
00254             val.setPyObject( item );
00255             values[i] = val.getValue();
00256         }
00257 
00258         setValues(values);
00259     }
00260     else if (PyObject_TypeCheck(value, &(VectorPy::Type))) {
00261         Base::VectorPy  *pcObject = static_cast<Base::VectorPy*>(value);
00262         Base::Vector3d* val = pcObject->getVectorPtr();
00263         Base::Vector3f vec((float)val->x,(float)val->y,(float)val->z);
00264         setValue(vec);
00265     }
00266     else if (PyTuple_Check(value) && PyTuple_Size(value) == 3) {
00267         PropertyVector val;
00268         val.setPyObject( value );
00269         setValue( val.getValue() );
00270     }
00271     else {
00272         std::string error = std::string("type must be 'Vector' or list of 'Vector', not ");
00273         error += value->ob_type->tp_name;
00274         throw Py::TypeError(error);
00275     }
00276 }
00277 
00278 void PropertyVectorList::Save (Base::Writer &writer) const
00279 {
00280     if (!writer.isForceXML()) {
00281         writer.Stream() << writer.ind() << "<VectorList file=\"" << writer.addFile(getName(), this) << "\"/>" << std::endl;
00282     }
00283 }
00284 
00285 void PropertyVectorList::Restore(Base::XMLReader &reader)
00286 {
00287     reader.readElement("VectorList");
00288     std::string file (reader.getAttribute("file") );
00289 
00290     if (!file.empty()) {
00291         // initate a file read
00292         reader.addFile(file.c_str(),this);
00293     }
00294 }
00295 
00296 void PropertyVectorList::SaveDocFile (Base::Writer &writer) const
00297 {
00298     Base::OutputStream str(writer.Stream());
00299     uint32_t uCt = (uint32_t)getSize();
00300     str << uCt;
00301     for (std::vector<Base::Vector3f>::const_iterator it = _lValueList.begin(); it != _lValueList.end(); ++it) {
00302         str << it->x << it->y << it->z;
00303     }
00304 }
00305 
00306 void PropertyVectorList::RestoreDocFile(Base::Reader &reader)
00307 {
00308     Base::InputStream str(reader);
00309     uint32_t uCt=0;
00310     str >> uCt;
00311     std::vector<Base::Vector3f> values(uCt);
00312     for (std::vector<Base::Vector3f>::iterator it = values.begin(); it != values.end(); ++it) {
00313         str >> it->x >> it->y >> it->z;
00314     }
00315     setValues(values);
00316 }
00317 
00318 Property *PropertyVectorList::Copy(void) const
00319 {
00320     PropertyVectorList *p= new PropertyVectorList();
00321     p->_lValueList = _lValueList;
00322     return p;
00323 }
00324 
00325 void PropertyVectorList::Paste(const Property &from)
00326 {
00327     aboutToSetValue();
00328     _lValueList = dynamic_cast<const PropertyVectorList&>(from)._lValueList;
00329     hasSetValue();
00330 }
00331 
00332 unsigned int PropertyVectorList::getMemSize (void) const
00333 {
00334     return static_cast<unsigned int>(_lValueList.size() * sizeof(Base::Vector3f));
00335 }
00336 
00337 //**************************************************************************
00338 //**************************************************************************
00339 // PropertyMatrix
00340 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00341 
00342 TYPESYSTEM_SOURCE(App::PropertyMatrix , App::Property);
00343 
00344 //**************************************************************************
00345 // Construction/Destruction
00346 
00347 
00348 PropertyMatrix::PropertyMatrix()
00349 {
00350 
00351 }
00352 
00353 
00354 PropertyMatrix::~PropertyMatrix()
00355 {
00356 
00357 }
00358 
00359 //**************************************************************************
00360 // Base class implementer
00361 
00362 
00363 void PropertyMatrix::setValue(const Base::Matrix4D &mat)
00364 {
00365     aboutToSetValue();
00366     _cMat=mat;
00367     hasSetValue();
00368 }
00369 
00370 
00371 const Base::Matrix4D & PropertyMatrix::getValue(void)const
00372 {
00373     return _cMat;
00374 }
00375 
00376 PyObject *PropertyMatrix::getPyObject(void)
00377 {
00378     return new Base::MatrixPy(_cMat);
00379 }
00380 
00381 void PropertyMatrix::setPyObject(PyObject *value)
00382 {
00383     if (PyObject_TypeCheck(value, &(Base::MatrixPy::Type))) {
00384         Base::MatrixPy  *pcObject = (Base::MatrixPy*)value;
00385         setValue( pcObject->value() );
00386     }
00387     else if (PyTuple_Check(value)&&PyTuple_Size(value)==16) {
00388         PyObject* item;
00389         Base::Matrix4D cMatrix;
00390 
00391         for (int x=0; x<4;x++) {
00392             for (int y=0; y<4;y++) {
00393                 item = PyTuple_GetItem(value,x+y*4);
00394                 if (PyFloat_Check(item))
00395                     cMatrix[x][y] = PyFloat_AsDouble(item);
00396                 else if (PyInt_Check(item))
00397                     cMatrix[x][y] = (double)PyInt_AsLong(item);
00398                 else
00399                     throw Base::Exception("Not allowed type used in matrix tuple (a number expected)...");
00400             }
00401         }
00402 
00403         setValue( cMatrix );
00404     }
00405     else {
00406         std::string error = std::string("type must be 'Matrix' or tuple of 16 float or int, not ");
00407         error += value->ob_type->tp_name;
00408         throw Py::TypeError(error);
00409     }
00410 }
00411 
00412 void PropertyMatrix::Save (Base::Writer &writer) const
00413 {
00414     writer.Stream() << writer.ind() << "<PropertyMatrix";
00415     writer.Stream() << " a11=\"" <<  _cMat[0][0] << "\" a12=\"" <<  _cMat[0][1] << "\" a13=\"" <<  _cMat[0][2] << "\" a14=\"" <<  _cMat[0][3] << "\"";
00416     writer.Stream() << " a21=\"" <<  _cMat[1][0] << "\" a22=\"" <<  _cMat[1][1] << "\" a23=\"" <<  _cMat[1][2] << "\" a24=\"" <<  _cMat[1][3] << "\"";
00417     writer.Stream() << " a31=\"" <<  _cMat[2][0] << "\" a32=\"" <<  _cMat[2][1] << "\" a33=\"" <<  _cMat[2][2] << "\" a34=\"" <<  _cMat[2][3] << "\"";
00418     writer.Stream() << " a41=\"" <<  _cMat[3][0] << "\" a42=\"" <<  _cMat[3][1] << "\" a43=\"" <<  _cMat[3][2] << "\" a44=\"" <<  _cMat[3][3] << "\"";
00419     writer.Stream() <<"/>" << endl;
00420 }
00421 
00422 void PropertyMatrix::Restore(Base::XMLReader &reader)
00423 {
00424     // read my Element
00425     reader.readElement("PropertyMatrix");
00426     // get the value of my Attribute
00427     aboutToSetValue();
00428     _cMat[0][0] = (float)reader.getAttributeAsFloat("a11");
00429     _cMat[0][1] = (float)reader.getAttributeAsFloat("a12");
00430     _cMat[0][2] = (float)reader.getAttributeAsFloat("a13");
00431     _cMat[0][3] = (float)reader.getAttributeAsFloat("a14");
00432 
00433     _cMat[1][0] = (float)reader.getAttributeAsFloat("a21");
00434     _cMat[1][1] = (float)reader.getAttributeAsFloat("a22");
00435     _cMat[1][2] = (float)reader.getAttributeAsFloat("a23");
00436     _cMat[1][3] = (float)reader.getAttributeAsFloat("a24");
00437 
00438     _cMat[2][0] = (float)reader.getAttributeAsFloat("a31");
00439     _cMat[2][1] = (float)reader.getAttributeAsFloat("a32");
00440     _cMat[2][2] = (float)reader.getAttributeAsFloat("a33");
00441     _cMat[2][3] = (float)reader.getAttributeAsFloat("a34");
00442 
00443     _cMat[3][0] = (float)reader.getAttributeAsFloat("a41");
00444     _cMat[3][1] = (float)reader.getAttributeAsFloat("a42");
00445     _cMat[3][2] = (float)reader.getAttributeAsFloat("a43");
00446     _cMat[3][3] = (float)reader.getAttributeAsFloat("a44");
00447     hasSetValue();
00448 }
00449 
00450 
00451 Property *PropertyMatrix::Copy(void) const
00452 {
00453     PropertyMatrix *p= new PropertyMatrix();
00454     p->_cMat = _cMat;
00455     return p;
00456 }
00457 
00458 void PropertyMatrix::Paste(const Property &from)
00459 {
00460     aboutToSetValue();
00461     _cMat = dynamic_cast<const PropertyMatrix&>(from)._cMat;
00462     hasSetValue();
00463 }
00464 
00465 //**************************************************************************
00466 //**************************************************************************
00467 // PropertyPlacement
00468 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00469 
00470 TYPESYSTEM_SOURCE(App::PropertyPlacement , App::Property);
00471 
00472 //**************************************************************************
00473 // Construction/Destruction
00474 
00475 
00476 PropertyPlacement::PropertyPlacement()
00477 {
00478 
00479 }
00480 
00481 
00482 PropertyPlacement::~PropertyPlacement()
00483 {
00484 
00485 }
00486 
00487 //**************************************************************************
00488 // Base class implementer
00489 
00490 
00491 void PropertyPlacement::setValue(const Base::Placement &pos)
00492 {
00493     aboutToSetValue();
00494     _cPos=pos;
00495     hasSetValue();
00496 }
00497 
00498 const Base::Placement & PropertyPlacement::getValue(void)const
00499 {
00500     return _cPos;
00501 }
00502 
00503 PyObject *PropertyPlacement::getPyObject(void)
00504 {
00505     return new Base::PlacementPy(new Base::Placement(_cPos));
00506 }
00507 
00508 void PropertyPlacement::setPyObject(PyObject *value)
00509 {
00510     if (PyObject_TypeCheck(value, &(Base::MatrixPy::Type))) {
00511         Base::MatrixPy  *pcObject = (Base::MatrixPy*)value;
00512         Base::Matrix4D mat = pcObject->value();
00513         Base::Placement p;
00514         p.fromMatrix(mat);
00515         setValue(p);
00516     }
00517     else if (PyObject_TypeCheck(value, &(Base::PlacementPy::Type))) {
00518         setValue(*static_cast<Base::PlacementPy*>(value)->getPlacementPtr());
00519     }
00520     else {
00521         std::string error = std::string("type must be 'Matrix' or 'Placement', not ");
00522         error += value->ob_type->tp_name;
00523         throw Py::TypeError(error);
00524     }
00525 }
00526 
00527 void PropertyPlacement::Save (Base::Writer &writer) const
00528 {
00529     writer.Stream() << writer.ind() << "<PropertyPlacement";
00530     writer.Stream() << " Px=\"" <<  _cPos.getPosition().x 
00531                     << "\" Py=\"" <<  _cPos.getPosition().y
00532                     << "\" Pz=\"" <<  _cPos.getPosition().z << "\"";
00533     writer.Stream() << " Q0=\"" <<  _cPos.getRotation()[0]
00534                     << "\" Q1=\"" <<  _cPos.getRotation()[1]
00535                     << "\" Q2=\"" <<  _cPos.getRotation()[2]
00536                     << "\" Q3=\"" <<  _cPos.getRotation()[3] << "\"";
00537     writer.Stream() <<"/>" << endl;
00538 }
00539 
00540 void PropertyPlacement::Restore(Base::XMLReader &reader)
00541 {
00542     // read my Element
00543     reader.readElement("PropertyPlacement");
00544     // get the value of my Attribute
00545     aboutToSetValue();
00546     _cPos = Base::Placement(Vector3d(reader.getAttributeAsFloat("Px"),
00547                                      reader.getAttributeAsFloat("Py"),
00548                                      reader.getAttributeAsFloat("Pz")),
00549                             Rotation(reader.getAttributeAsFloat("Q0"),
00550                                      reader.getAttributeAsFloat("Q1"),
00551                                      reader.getAttributeAsFloat("Q2"),
00552                                      reader.getAttributeAsFloat("Q3")));
00553     hasSetValue();
00554 }
00555 
00556 
00557 Property *PropertyPlacement::Copy(void) const
00558 {
00559     PropertyPlacement *p= new PropertyPlacement();
00560     p->_cPos = _cPos;
00561     return p;
00562 }
00563 
00564 void PropertyPlacement::Paste(const Property &from)
00565 {
00566     aboutToSetValue();
00567     _cPos = dynamic_cast<const PropertyPlacement&>(from)._cPos;
00568     hasSetValue();
00569 }
00570 
00571 //**************************************************************************
00572 //**************************************************************************
00573 // PropertyPlacement
00574 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00575 
00576 TYPESYSTEM_SOURCE(App::PropertyPlacementLink , App::PropertyLink);
00577 
00578 //**************************************************************************
00579 // Construction/Destruction
00580 
00581 
00582 PropertyPlacementLink::PropertyPlacementLink()
00583 {
00584 
00585 }
00586 
00587 
00588 PropertyPlacementLink::~PropertyPlacementLink()
00589 {
00590 
00591 }
00592 
00593 App::Placement * PropertyPlacementLink::getPlacementObject(void) const
00594 {
00595     if (_pcLink->getTypeId().isDerivedFrom(App::Placement::getClassTypeId()))
00596         return dynamic_cast<App::Placement*>(_pcLink);
00597     else
00598         return 0;
00599 
00600 }
00601 
00602 //**************************************************************************
00603 // Base class implementer
00604 
00605 Property *PropertyPlacementLink::Copy(void) const
00606 {
00607     PropertyPlacementLink *p= new PropertyPlacementLink();
00608     p->_pcLink = _pcLink;
00609     return p;
00610 }
00611 
00612 void PropertyPlacementLink::Paste(const Property &from)
00613 {
00614     aboutToSetValue();
00615     _pcLink = dynamic_cast<const PropertyPlacementLink&>(from)._pcLink;
00616     hasSetValue();
00617 }
00618 
00619 // ------------------------------------------------------------
00620 
00621 TYPESYSTEM_SOURCE_ABSTRACT(App::PropertyGeometry , App::Property);
00622 
00623 PropertyGeometry::PropertyGeometry()
00624 {
00625 
00626 }
00627 
00628 PropertyGeometry::~PropertyGeometry()
00629 {
00630 
00631 }
00632 
00633 // ------------------------------------------------------------
00634 
00635 TYPESYSTEM_SOURCE_ABSTRACT(App::PropertyComplexGeoData , App::PropertyGeometry);
00636 
00637 PropertyComplexGeoData::PropertyComplexGeoData()
00638 {
00639 
00640 }
00641 
00642 PropertyComplexGeoData::~PropertyComplexGeoData()
00643 {
00644 
00645 }

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