ViewProviderCurveNet.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2004 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 # include <BRep_Tool.hxx>
00028 # include <Geom_Curve.hxx>
00029 # include <TColgp_Array1OfPnt.hxx>
00030 # include <TopoDS.hxx>
00031 # include <TopExp_Explorer.hxx>
00032 # include <Inventor/nodes/SoComplexity.h>
00033 # include <Inventor/nodes/SoCoordinate3.h>
00034 # include <Inventor/nodes/SoDrawStyle.h>
00035 # include <Inventor/nodes/SoLineSet.h>
00036 # include <Inventor/nodes/SoLocateHighlight.h>
00037 # include <Inventor/nodes/SoMaterial.h>
00038 # include <Inventor/nodes/SoTransform.h>
00039 # include <Inventor/nodes/SoSphere.h>
00040 # include <Inventor/events/SoMouseButtonEvent.h>
00041 # include <Inventor/events/SoKeyboardEvent.h>
00042 # include <Inventor/events/SoLocation2Event.h>
00043 #endif
00044 
00046 #include <Base/Console.h>
00047 #include <Base/Parameter.h>
00048 #include <Base/Exception.h>
00049 #include <Base/Sequencer.h>
00050 #include <Gui/View3DInventorViewer.h>
00051 #include <Gui/SoFCSelection.h>
00052 #include <App/Application.h>
00053 
00054 #include <Mod/Part/App/PartFeature.h>
00055 
00056 #include "ViewProviderCurveNet.h"
00057 
00058 
00059 
00060 using namespace PartGui;
00061 
00062 
00063 //**************************************************************************
00064 // Construction/Destruction
00065 
00066 PROPERTY_SOURCE(PartGui::ViewProviderCurveNet,PartGui::ViewProviderPart)
00067 
00068        
00069 ViewProviderCurveNet::ViewProviderCurveNet()
00070   : bInEdit(false),bMovePointMode(false)
00071 {
00072     LineWidth.setValue(4.0f);
00073     PointSize.setValue(0.05f);
00074     /*
00075     hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Part");
00076 
00077     fMeshDeviation      = hGrp->GetFloat("MeshDeviation",0.2);
00078     bNoPerVertexNormals = hGrp->GetBool("NoPerVertexNormals",false);
00079     lHilightColor       = hGrp->GetInt ("HilightColor",0);
00080     bQualityNormals     = hGrp->GetBool("QualityNormals",false);
00081     */
00082 }
00083 
00084 ViewProviderCurveNet::~ViewProviderCurveNet()
00085 {
00086 
00087 }
00088 
00089 void ViewProviderCurveNet::attach(App::DocumentObject *pcFeat)
00090 {
00091     // call parent attach method
00092     ViewProviderGeometryObject::attach(pcFeat);
00093 
00094     // setup the root and material for the edges
00095     SoSeparator* ModeRoot = new SoSeparator();
00096     EdgeRoot = new SoSeparator();
00097     ModeRoot->addChild(EdgeRoot);
00098     EdgeRoot->addChild(pcLineStyle);  
00099     EdgeRoot->addChild(pcLineMaterial);  
00100 
00101     // setup the root and material for the vertexes
00102     VertexRoot = new SoSeparator();
00103     ModeRoot->addChild(VertexRoot);
00104     VertexRoot->addChild(pcPointMaterial);
00105     SoComplexity *copl = new SoComplexity();
00106     copl->value = (float)0.2;
00107     VertexRoot->addChild(copl);
00108 
00109     addDisplayMaskMode(ModeRoot, "Edge");
00110 }
00111 
00112 void ViewProviderCurveNet::updateData(const App::Property* prop)
00113 {
00114     Gui::ViewProviderGeometryObject::updateData(prop);
00115     if (prop->getTypeId() == Part::PropertyPartShape::getClassTypeId()) {
00116         TopoDS_Shape cShape = static_cast<const Part::PropertyPartShape*>(prop)->getValue();
00117         if (cShape.IsNull())
00118             return;
00119 
00120         EdgeRoot->removeAllChildren();
00121         VertexRoot->removeAllChildren();
00122 
00123         try{
00124             computeEdges   (EdgeRoot,cShape);
00125             computeVertices(VertexRoot,cShape);
00126         }
00127         catch (...){
00128             Base::Console().Error("ViewProviderPart::create() Cannot compute Inventor representation for the actual shape");
00129         }
00130     }
00131 }
00132 
00133 void ViewProviderCurveNet::setDisplayMode(const char* ModeName)
00134 {
00135     if ( strcmp("Edge",ModeName)==0 )
00136         setDisplayMaskMode("Edge");
00137 
00138     ViewProviderGeometryObject::setDisplayMode(ModeName);
00139 }
00140 
00141 std::vector<std::string> ViewProviderCurveNet::getDisplayModes(void) const
00142 {
00143     // get the modes of the father
00144     std::vector<std::string> StrList;
00145 
00146     // add your own modes
00147     StrList.push_back("Edge");
00148 
00149     return StrList;
00150 }
00151 
00152 // **********************************************************************************
00153 
00154 bool ViewProviderCurveNet::setEdit(int)
00155 {
00156     bInEdit = true;
00157     return true;
00158     //getWidget()->setCursor( QCursor( 13 /*ArrowCursor*/) )
00159 }
00160 
00161 void ViewProviderCurveNet::unsetEdit(int)
00162 {
00163     bInEdit = false;
00164 }
00165 
00166 bool ViewProviderCurveNet::handleEvent(const SoEvent * const ev, Gui::View3DInventorViewer &Viewer)
00167 {
00168     SbVec3f point, norm;
00169 
00170     // get the position of the mouse
00171     const SbVec2s pos(ev->getPosition());
00172 
00173     // Keybooard events
00174     if (ev->getTypeId().isDerivedFrom(SoKeyboardEvent::getClassTypeId())) {
00175         SoKeyboardEvent * ke = (SoKeyboardEvent *)ev;
00176         switch (ke->getKey()) {
00177             case SoKeyboardEvent::LEFT_ALT:
00178             case SoKeyboardEvent::RIGHT_ALT:
00179             case SoKeyboardEvent::LEFT_CONTROL:
00180             case SoKeyboardEvent::RIGHT_CONTROL:
00181             case SoKeyboardEvent::LEFT_SHIFT:
00182             case SoKeyboardEvent::RIGHT_SHIFT:
00183                 break;
00184             default:
00185                 break;
00186         }
00187     }
00188 
00189     // switching the mouse buttons
00190     if (ev->getTypeId().isDerivedFrom(SoMouseButtonEvent::getClassTypeId())) {
00191         const SoMouseButtonEvent * const event = (const SoMouseButtonEvent *) ev;
00192         const int button = event->getButton();
00193         const SbBool press = event->getState() == SoButtonEvent::DOWN ? TRUE : FALSE;
00194 
00195         // which button pressed?
00196         switch (button) {
00197             case SoMouseButtonEvent::BUTTON1:
00198                 if (press) {
00199                     Base::Console().Log("ViewProviderCurveNet::handleEvent() press left\n");
00200 
00201                     bool bIsNode =  false;
00202                     for (std::list<Node>::iterator It = NodeList.begin();It != NodeList.end(); It++)
00203                     {
00204                         if (It->pcHighlight->isHighlighted())
00205                         {
00206                             bIsNode = true;
00207                             PointToMove = *It;
00208                             break;
00209                         }
00210                     }
00211 
00212                     if (bIsNode)
00213                     {
00214                         // set the provider in point move mode and remember the point
00215                         bMovePointMode = true;
00216                         // PointToMove = *It; ### Error 'It' is out of scope->move inside the loop
00217 
00218                         return true;
00219                     }
00220                     else if(Viewer.pickPoint(pos,point,norm))
00221                     {
00222                         Node n;
00223                         Base::Console().Log("Picked(%f,%f,%f)\n",point[0],point[1],point[2]);
00224 
00225                         SoSeparator *TransRoot = new SoSeparator();
00226                         n.pcTransform          = new SoTransform();
00227                         TransRoot->addChild(n.pcTransform);
00228                         n.pcTransform->translation.setValue(point);
00229                         n.pcHighlight          = new Gui::SoFCSelection();
00230                         SoSphere * sphere      = new SoSphere;
00231                         sphere->radius = (float)pcLineStyle->pointSize.getValue();
00232                         n.pcHighlight->addChild(sphere);
00233                         TransRoot->addChild(n.pcHighlight);
00234                         VertexRoot->addChild(TransRoot);
00235 
00236                         NodeList.push_back(n);
00237       
00238                         return true;
00239                     }
00240                 }
00241                 else // if(pressd)..
00242                 {
00243                     if (bMovePointMode)
00244                     {
00245                         bMovePointMode = false;
00246                         return true;
00247                     }
00248                 }
00249             break;
00250         }
00251     }
00252 
00253     // Mouse Movement handling
00254     if (ev->getTypeId().isDerivedFrom(SoLocation2Event::getClassTypeId())) {
00255         //    const SoLocation2Event * const event = (const SoLocation2Event *) ev;
00256 
00257         if (bMovePointMode && Viewer.pickPoint(pos,point,norm) ){
00258             PointToMove.pcTransform->translation.setValue(point);     
00259             return true;
00260         }
00261     }
00262 
00263     // event not processed
00264     return false;
00265 }
00266 
00267 Standard_Boolean ViewProviderCurveNet::computeEdges(SoSeparator* root, const TopoDS_Shape &myShape)
00268 {
00269     unsigned long ulNbOfPoints = 50;
00270 
00271     TopExp_Explorer ex;
00272     SoSeparator *EdgeRoot = new SoSeparator();
00273     root->addChild(EdgeRoot);
00274 
00275     EdgeRoot->addChild(pcLineStyle);  
00276     EdgeRoot->addChild(pcLineMaterial);  
00277 
00278     for (ex.Init(myShape, TopAbs_EDGE); ex.More(); ex.Next())
00279     {
00280         // get the shape and mesh it
00281         const TopoDS_Edge& aEdge = TopoDS::Edge(ex.Current());
00282 
00283         Standard_Real fBegin, fEnd;
00284         SbVec3f* vertices = new SbVec3f[ulNbOfPoints];
00285 
00286         Handle(Geom_Curve) hCurve = BRep_Tool::Curve(aEdge,fBegin,fEnd);
00287         float fLen   = float(fEnd - fBegin);
00288 
00289         for (unsigned long i = 0; i < ulNbOfPoints; i++)
00290         {
00291             gp_Pnt gpPt = hCurve->Value(fBegin + (fLen * float(i)) / float(ulNbOfPoints-1));
00292             vertices[i].setValue((float)(gpPt.X()),(float)(gpPt.Y()),(float)(gpPt.Z()));
00293         }
00294 
00295         // define vertices
00296         SoCoordinate3 * coords = new SoCoordinate3;
00297         coords->point.setValues(0,ulNbOfPoints, vertices);
00298         EdgeRoot->addChild(coords);
00299 
00300         // define the indexed face set
00301         SoLocateHighlight* h = new SoLocateHighlight();
00302         h->color.setValue((float)0.2,(float)0.5,(float)0.2);
00303 
00304         SoLineSet * lineset = new SoLineSet;
00305         h->addChild(lineset);
00306         EdgeRoot->addChild(h);
00307     }
00308 
00309     return true;
00310 }
00311 
00312 Standard_Boolean ViewProviderCurveNet::computeVertices(SoSeparator* root, const TopoDS_Shape &myShape)
00313 {
00314     TopExp_Explorer ex;
00315     SoSeparator *VertexRoot = new SoSeparator();
00316     root->addChild(VertexRoot);
00317 
00318     VertexRoot->addChild(pcPointMaterial);
00319 
00320     SoComplexity *copl = new SoComplexity();
00321     copl->value = (float)0.2;
00322     VertexRoot->addChild(copl);
00323 
00324     for (ex.Init(myShape, TopAbs_VERTEX); ex.More(); ex.Next())
00325     {
00326         // get the shape 
00327         const TopoDS_Vertex& aVertex = TopoDS::Vertex(ex.Current());
00328         gp_Pnt gpPt = BRep_Tool::Pnt (aVertex);
00329 
00330         SoSeparator *TransRoot = new SoSeparator();
00331         SoTransform *Trans     = new SoTransform();
00332         TransRoot->addChild(Trans);
00333         Trans->translation.setValue((float)(gpPt.X()),(float)(gpPt.Y()),(float)(gpPt.Z()));
00334 
00335         SoLocateHighlight* h = new SoLocateHighlight();
00336         h->color.setValue((float)0.2,(float)0.5,(float)0.2);
00337 
00338         SoSphere * sphere = new SoSphere;
00339         sphere->radius = (float)pcPointStyle->pointSize.getValue();
00340 
00341         h->addChild(sphere);
00342         TransRoot->addChild(h);
00343         VertexRoot->addChild(TransRoot);
00344     }
00345 
00346     return true;
00347 }

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