Edge2TracObject.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2010 Jürgen 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 #include "PreCompiled.h"
00025 
00026 #ifndef _PreComp_
00027 #endif
00028 
00029 #include "Edge2TracObject.h"
00030 //#include <App/DocumentObjectPy.h>
00031 //#include <Base/Placement.h>
00032 #include <Base/Sequencer.h>
00033 #include <Mod/Part/App/edgecluster.h>
00034 #include <Mod/Part/App/PartFeature.h>
00035 #include <TopoDS.hxx>
00036 #include <TopoDS_Edge.hxx>
00037 #include <TopoDS_Vertex.hxx>
00038 #include <BRep_Tool.hxx>
00039 #include <BRepAdaptor_Curve.hxx>
00040 #include <CPnts_AbscissaPoint.hxx>
00041 #include <TopExp.hxx>
00042 #include "Waypoint.h"
00043 #include "Trajectory.h"
00044 
00045 using namespace Robot;
00046 using namespace App;
00047 
00048 PROPERTY_SOURCE(Robot::Edge2TracObject, Robot::TrajectoryObject)
00049 
00050 
00051 Edge2TracObject::Edge2TracObject()
00052 {
00053 
00054     ADD_PROPERTY_TYPE( Source,      (0)  , "Edge2Trac",Prop_None,"Edges to generate the Trajectory");
00055     ADD_PROPERTY_TYPE( SegValue,    (0.5), "Edge2Trac",Prop_None,"Max deviation from original geometry");
00056     ADD_PROPERTY_TYPE( UseRotation, (0)  , "Edge2Trac",Prop_None,"use orientation of the edge");
00057     NbrOfEdges = 0;
00058     NbrOfCluster = 0;
00059 }
00060 
00061 Edge2TracObject::~Edge2TracObject()
00062 {
00063 }
00064 
00065 App::DocumentObjectExecReturn *Edge2TracObject::execute(void)
00066 {
00067     App::DocumentObject* link = Source.getValue();
00068     if (!link)
00069         return new App::DocumentObjectExecReturn("No object linked");
00070     if (!link->getTypeId().isDerivedFrom(Part::Feature::getClassTypeId()))
00071         return new App::DocumentObjectExecReturn("Linked object is not a Part object");
00072     Part::Feature *base = static_cast<Part::Feature*>(Source.getValue());
00073     const Part::TopoShape& TopShape = base->Shape.getShape();
00074 
00075     const std::vector<std::string>& SubVals = Source.getSubValuesStartsWith("Edge");
00076     if (SubVals.size() == 0)
00077         return new App::DocumentObjectExecReturn("No Edges specified");
00078 
00079     // container for all the edges 
00080     std::vector<TopoDS_Edge> edges;
00081 
00082     // run throug the edge name and get the real objects from the TopoShape
00083     for (std::vector<std::string>::const_iterator it= SubVals.begin();it!=SubVals.end();++it) {
00084          TopoDS_Edge edge = TopoDS::Edge(TopShape.getSubShape(it->c_str()));
00085          edges.push_back(edge);
00086     }
00087 
00088     // instanciate a edge cluster sorter and get the result 
00089     Part::Edgecluster acluster(edges);
00090     Part::tEdgeClusterVector aclusteroutput = acluster.GetClusters();
00091 
00092     if(aclusteroutput.size() == 0)
00093         return new App::DocumentObjectExecReturn("No Edges specified");
00094 
00095     // set the number of cluster and edges 
00096     NbrOfCluster = aclusteroutput.size();
00097     NbrOfEdges = 0;
00098     for(std::vector<std::vector<TopoDS_Edge> >::const_iterator it=aclusteroutput.begin();it!=aclusteroutput.end();++it)
00099         NbrOfEdges += it->size();
00100 
00101     // trajectory to fill
00102     Robot::Trajectory trac;
00103     bool first = true;
00104 
00105     // cycle trough the cluster
00106     for(std::vector<std::vector<TopoDS_Edge> >::const_iterator it=aclusteroutput.begin();it!=aclusteroutput.end();++it)
00107     {
00108         // cycle through the edges of the cluster
00109         for(std::vector<TopoDS_Edge>::const_iterator it2=it->begin();it2!=it->end();++it2)
00110         {
00111             BRepAdaptor_Curve adapt(*it2);
00112             
00113             switch(adapt.GetType())
00114             {
00115             case GeomAbs_Line:
00116                 {
00117                 // get start and end point
00118                 gp_Pnt P1 = adapt.Value(adapt.FirstParameter());
00119                 gp_Pnt P2 = adapt.Value(adapt.LastParameter());
00120 
00121                 Base::Rotation R1;
00122                 Base::Rotation R2;
00123 
00124                 // if orientation is used 
00125                 if(UseRotation.getValue()) {
00126                     // here get the orientation of the start and end point...
00127                     //R1 = ;
00128                     //R2 = ;
00129 
00130                 }
00131 
00132                 // if reverse orintation, switch the points
00133                 if ( it2->Orientation() == TopAbs_REVERSED )
00134                 {
00135                      //switch the points and orientation
00136                      gp_Pnt tmpP = P1;
00137                      Base::Rotation tmpR = R1;
00138                      P1 = P2;
00139                      R1 = R2;
00140                      R2 = tmpR;
00141                      P2 = tmpP;
00142                 }
00143                 if(first){
00144                     Waypoint wp("Pt",Base::Placement(Base::Vector3d(P1.X(),P1.Y(),P1.Z()),R1));
00145                     trac.addWaypoint(wp);
00146                     first = false;
00147                 }
00148                 Waypoint wp("Pt",Base::Placement(Base::Vector3d(P2.X(),P2.Y(),P2.Z()),R2));
00149                 trac.addWaypoint(wp);
00150                 break;
00151                 }
00152             case GeomAbs_BSplineCurve:
00153                 {
00154                 Standard_Real Length    = CPnts_AbscissaPoint::Length(adapt);
00155                 Standard_Real ParLength = adapt.LastParameter()-adapt.FirstParameter();
00156                 Standard_Real NbrSegments = Round(Length / SegValue.getValue());
00157 
00158                 Standard_Real beg = adapt.FirstParameter();
00159                 Standard_Real end = adapt.LastParameter();
00160                 Standard_Real stp = ParLength / NbrSegments;
00161                                 bool reversed = false;
00162                 if (it2->Orientation() == TopAbs_REVERSED) {
00163                     std::swap(beg, end);
00164                     stp = - stp;
00165                                         reversed = true;
00166                 }
00167 
00168                 if (first) 
00169                     first = false;
00170                 else
00171                     beg += stp;
00172                 Base::SequencerLauncher seq("Create way points", static_cast<size_t>((end-beg)/stp));
00173                                 if(reversed)
00174                                 {
00175                                         for (;beg > end; beg += stp) {
00176                                                 gp_Pnt P = adapt.Value(beg);
00177                         Base::Rotation R1;
00178                         // if orientation is used 
00179                         if(UseRotation.getValue()) {
00180                             // here get the orientation of the start and end point...
00181                             //R1 = ;
00182                         }
00183 
00184                                                 Waypoint wp("Pt",Base::Placement(Base::Vector3d(P.X(),P.Y(),P.Z()),R1));
00185                                                 trac.addWaypoint(wp);
00186                                                 seq.next();
00187                                         }
00188                                 }
00189                                 else
00190                                 {
00191                                         for (;beg < end; beg += stp) {
00192                                                 gp_Pnt P = adapt.Value(beg);
00193                         Base::Rotation R1;
00194                         // if orientation is used 
00195                         if(UseRotation.getValue()) {
00196                             // here get the orientation of the start and end point...
00197                             //R1 = ;
00198                         }
00199                                                 Waypoint wp("Pt",Base::Placement(Base::Vector3d(P.X(),P.Y(),P.Z()),R1));
00200                                                 trac.addWaypoint(wp);
00201                                                 seq.next();
00202                                         }
00203                                 }
00204                 
00205                 } break;
00206             case GeomAbs_Circle:
00207                 {
00208                 Standard_Real Length    = CPnts_AbscissaPoint::Length(adapt);
00209                 Standard_Real ParLength = adapt.LastParameter()-adapt.FirstParameter();
00210                 Standard_Real NbrSegments = Round(Length / SegValue.getValue());
00211                 Standard_Real SegLength   = ParLength / NbrSegments;
00212                                 
00213                                 if ( it2->Orientation() == TopAbs_REVERSED )
00214                                 {
00215                                         //Beginning and End switch
00216                                         double i = adapt.LastParameter();
00217                                         if(first) 
00218                                                 first=false;
00219                                         else
00220                                                 i -= SegLength;
00221                                         for (;i>adapt.FirstParameter();i-= SegLength){
00222                                                 gp_Pnt P = adapt.Value(i);
00223                         Base::Rotation R1;
00224                         // if orientation is used 
00225                         if(UseRotation.getValue()) {
00226                             // here get the orientation of the start and end point...
00227                             //R1 = ;
00228                         }
00229                                                 Waypoint wp("Pt",Base::Placement(Base::Vector3d(P.X(),P.Y(),P.Z()),R1));
00230                                                 trac.addWaypoint(wp);
00231                                         }
00232                                 }
00233                                 else
00234                                 {
00235                                         double i = adapt.FirstParameter();
00236                                         if(first) 
00237                                                 first=false;
00238                                         else
00239                                                 i += SegLength;
00240                                         for (;i<adapt.LastParameter();i+= SegLength)
00241                                         {
00242                                                 gp_Pnt P = adapt.Value(i);
00243                         Base::Rotation R1;
00244                         // if orientation is used 
00245                         if(UseRotation.getValue()) {
00246                             // here get the orientation of the start and end point...
00247                             //R1 = ;
00248                         }
00249                                                 Waypoint wp("Pt",Base::Placement(Base::Vector3d(P.X(),P.Y(),P.Z()),R1));
00250                                                 trac.addWaypoint(wp);
00251                                         }
00252                                         
00253                                 }
00254                                 break;
00255                 }
00256 
00257             default:
00258                 throw Base::Exception("Unknown Edge type in Robot::Edge2TracObject::execute()");
00259             }
00260            
00261 
00262 
00263         }
00264     }
00265 
00266     Trajectory.setValue(trac);
00267     
00268     return App::DocumentObject::StdReturn;
00269 }
00270 
00271 
00272 //short Edge2TracObject::mustExecute(void) const
00273 //{
00274 //    return 0;
00275 //}
00276 
00277 void Edge2TracObject::onChanged(const Property* prop)
00278 {
00279  
00280     App::GeoFeature::onChanged(prop);
00281 }

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