PointsFeature.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) Juergen Riegel         <juergen.riegel@web.de>          *
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 
00025 #include "PreCompiled.h"
00026 
00027 #ifndef _PreComp_
00028 #endif
00029 #include <vector>
00030 
00031 #include <Base/Console.h>
00032 #include <Base/Exception.h>
00033 #include <Base/Stream.h>
00034 #include <Base/Writer.h>
00035 
00036 
00037 #include "PointsFeature.h"
00038 
00039 using namespace Points;
00040 
00041 
00042 //===========================================================================
00043 // Feature
00044 //===========================================================================
00045 
00046 PROPERTY_SOURCE(Points::Feature, App::GeoFeature)
00047 
00048 Feature::Feature() 
00049 {
00050     ADD_PROPERTY(Points, (PointKernel()));
00051 }
00052 
00053 Feature::~Feature()
00054 {
00055 }
00056 
00057 App::DocumentObjectExecReturn *Feature::execute(void)
00058 {
00059     return App::DocumentObject::StdReturn;
00060 }
00061 
00062 void Feature::Restore(Base::XMLReader &reader)
00063 {
00064     GeoFeature::Restore(reader);
00065 }
00066 
00067 void Feature::RestoreDocFile(Base::Reader &reader)
00068 {
00069     // This gets only invoked if a points file has been added from Restore()
00070     Points.RestoreDocFile(reader);
00071 }
00072 
00073 void Feature::onChanged(const App::Property* prop)
00074 {
00075     // if the placement has changed apply the change to the point data as well
00076     if (prop == &this->Placement) {
00077         PointKernel& pts = const_cast<PointKernel&>(this->Points.getValue());
00078         pts.setTransform(this->Placement.getValue().toMatrix());
00079     }
00080     // if the point data has changed check and adjust the transformation as well
00081     else if (prop == &this->Points) {
00082         Base::Placement p;
00083         p.fromMatrix(this->Points.getValue().getTransform());
00084         if (p != this->Placement.getValue())
00085             this->Placement.setValue(p);
00086     }
00087     
00088     GeoFeature::onChanged(prop);
00089 }
00090 
00091 // ------------------------------------------------------------------
00092 
00093 PROPERTY_SOURCE(Points::Export, Points::Feature)
00094 
00095 Export::Export(void)
00096 {
00097     ADD_PROPERTY(Sources ,(0));
00098     ADD_PROPERTY(FileName,(""));
00099     ADD_PROPERTY(Format  ,(""));
00100 }
00101 
00102 App::DocumentObjectExecReturn *Export::execute(void)
00103 {
00104   // ask for write permission
00105   Base::FileInfo fi(FileName.getValue());
00106   Base::FileInfo di(fi.dirPath().c_str());
00107   if ((fi.exists() && !fi.isWritable()) || !di.exists() || !di.isWritable())
00108   {
00109       return new App::DocumentObjectExecReturn("No write permission for file");
00110   }
00111 
00112   Base::ofstream str(fi, std::ios::out | std::ios::binary);
00113 
00114   if (fi.hasExtension("asc"))
00115   {
00116     const std::vector<App::DocumentObject*>& features = Sources.getValues();
00117     for ( std::vector<App::DocumentObject*>::const_iterator it = features.begin(); it != features.end(); ++it )
00118     {
00119       Feature *pcFeat  = dynamic_cast<Feature*>(*it);
00120       const PointKernel& kernel = pcFeat->Points.getValue();
00121 
00122       str << "# " << pcFeat->getNameInDocument() << " Number of points: " << kernel.size() << std::endl;
00123       for ( PointKernel::const_iterator it = kernel.begin(); it != kernel.end(); ++it )
00124         str << it->x << " " << it->y << " " << it->z << std::endl;
00125     }
00126   }
00127   else
00128   {
00129       return new App::DocumentObjectExecReturn("File format not supported");
00130   }
00131 
00132   return App::DocumentObject::StdReturn;
00133 }
00134 
00135 // ---------------------------------------------------------
00136 
00137 namespace App {
00139 PROPERTY_SOURCE_TEMPLATE(Points::FeaturePython, Points::Feature)
00140 template<> const char* Points::FeaturePython::getViewProviderName(void) const {
00141     return "PointsGui::ViewProviderPython";
00142 }
00144 
00145 // explicit template instantiation
00146 template class PointsExport FeaturePythonT<Points::Feature>;
00147 }
00148 

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