ViewProviderTransformDemolding.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net>     *
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 <Inventor/nodes/SoDrawStyle.h>
00028 # include <Inventor/nodes/SoIndexedFaceSet.h>
00029 # include <Inventor/nodes/SoMaterial.h>
00030 # include <Inventor/nodes/SoMaterialBinding.h>
00031 # include <Inventor/draggers/SoTrackballDragger.h>
00032 # include <Inventor/nodes/SoAntiSquish.h>
00033 # include <Inventor/nodes/SoSurroundScale.h>
00034 # include <Inventor/nodes/SoSeparator.h>
00035 # include <Inventor/manips/SoTransformerManip.h>
00036 #endif
00037 
00038 #include "ViewProviderTransformDemolding.h"
00039 
00041 #include <Base/Console.h>
00042 #include <Base/Parameter.h>
00043 #include <Base/Exception.h>
00044 #include <App/Application.h>
00045 #include <Gui/Selection.h>
00046 #include <Gui/SoFCSelection.h>
00047 #include <Base/Sequencer.h>
00048 
00049 
00050 #include <Mod/Mesh/App/MeshFeature.h>
00051 #include <Mod/Mesh/App/Mesh.h>
00052 #include <Mod/Mesh/App/Core/Iterator.h>
00053 
00054 using Mesh::Feature;
00055 using MeshCore::MeshKernel;
00056 using MeshCore::MeshFacetIterator;
00057 using MeshCore::MeshGeomFacet;
00058 using namespace MeshGui;
00059 
00060 
00061 PROPERTY_SOURCE(MeshGui::ViewProviderMeshTransformDemolding, MeshGui::ViewProviderMesh)
00062 
00063 
00064 ViewProviderMeshTransformDemolding::ViewProviderMeshTransformDemolding()
00065 {
00066   pcTrackballDragger = new SoTrackballDragger;
00067   pcTrackballDragger->ref();
00068 }
00069 
00070 ViewProviderMeshTransformDemolding::~ViewProviderMeshTransformDemolding()
00071 {
00072   pcTrackballDragger->unref();
00073 }
00074 
00075 void ViewProviderMeshTransformDemolding::attach(App::DocumentObject *pcFeat)
00076 {
00077   // creats the satandard viewing modes
00078   ViewProviderMesh::attach(pcFeat);
00079 
00080   SoGroup* pcDemoldRoot = new SoGroup();
00081 
00082   SoDrawStyle *pcFlatStyle = new SoDrawStyle();
00083   pcFlatStyle->style = SoDrawStyle::FILLED;
00084   pcDemoldRoot->addChild(pcFlatStyle);
00085 
00086   // dragger
00087   SoSeparator * surroundsep = new SoSeparator;
00088 
00089   SoSurroundScale * ss = new SoSurroundScale;
00090   ss->numNodesUpToReset = 1;
00091   ss->numNodesUpToContainer = 2;
00092   surroundsep->addChild(ss);
00093 
00094   SoAntiSquish * antisquish = new SoAntiSquish;
00095   antisquish->sizing = SoAntiSquish::AVERAGE_DIMENSION ;
00096   surroundsep->addChild(antisquish);
00097 
00098   pcTrackballDragger->addValueChangedCallback(sValueChangedCallback,this); 
00099   pcTrackballDragger->addFinishCallback (sDragEndCallback,this); 
00100   surroundsep->addChild(pcTrackballDragger);
00101 
00102   pcTransformDrag = new SoTransform();
00103 
00104 
00105   SoMaterialBinding* pcMatBinding = new SoMaterialBinding;
00106   //pcMatBinding->value = SoMaterialBinding::PER_VERTEX_INDEXED;
00107   pcMatBinding->value = SoMaterialBinding::PER_FACE_INDEXED;
00108   pcColorMat = new SoMaterial;
00109   pcColorMat->diffuseColor.set1Value(0, 1,1,0);
00110   pcColorMat->diffuseColor.set1Value(1, 1,0,0);
00111   pcColorMat->diffuseColor.set1Value(2, 0,1,0);
00112   
00113   pcDemoldRoot->addChild(surroundsep);
00114   pcDemoldRoot->addChild(pcTransformDrag);
00115   pcDemoldRoot->addChild(pcColorMat);
00116   pcDemoldRoot->addChild(pcMatBinding);
00117   pcDemoldRoot->addChild(pcHighlight);
00118 
00119   // adding to the switch
00120   addDisplayMaskMode(pcDemoldRoot, "Demold");
00121 
00122   calcNormalVector();
00123   calcMaterialIndex(SbRotation());
00124   // geting center point
00125   center = dynamic_cast<Feature*>(pcObject)->Mesh.getValue().getKernel().GetBoundBox().CalcCenter();
00126 
00127   //SoGetBoundingBoxAction  boxAction;
00128   //pcHighlight->getBoundingBox(&boxAction);
00129   //SbVector3f Center = boxAction->getCenter();
00130 }
00131 
00132 void ViewProviderMeshTransformDemolding::calcNormalVector(void)
00133 {
00134   const MeshKernel& cMesh = dynamic_cast<Feature*>(pcObject)->Mesh.getValue().getKernel();
00135 
00136   MeshFacetIterator cFIt(cMesh);
00137   for( cFIt.Init(); cFIt.More(); cFIt.Next())
00138   {
00139     const MeshGeomFacet& rFace = *cFIt;
00140 
00141     Base::Vector3f norm(rFace.GetNormal());
00142     normalVector.push_back(SbVec3f(norm.x,norm.y,norm.z));
00143   }
00144 }
00145 
00146 void ViewProviderMeshTransformDemolding::calcMaterialIndex(const SbRotation &rot)
00147 {
00148   // 3.1415926535897932384626433832795
00149   SbVec3f Up(0,0,1),result;
00150 
00151   unsigned long i=0;
00152   for( std::vector<SbVec3f>::const_iterator it=normalVector.begin();it != normalVector.end(); ++it,i++)
00153   {
00154     rot.multVec(*it,result);
00155 
00156     float Angle = acos( (result.dot(Up)) / (result.length() * Up.length()) ) * (180/3.1415926535);
00157 
00158     if(Angle < 87.0){
00159 //      pcMeshFaces->materialIndex .set1Value(i, 2);
00160     }else if(Angle > 90.0){
00161 //      pcMeshFaces->materialIndex .set1Value(i, 1 );
00162     }else{
00163 //      pcMeshFaces->materialIndex .set1Value(i, 0 );
00164     }
00165 
00166   }
00167 }
00168 
00169 void ViewProviderMeshTransformDemolding::sValueChangedCallback(void *This, SoDragger *)
00170 {
00171   static_cast<ViewProviderMeshTransformDemolding*>(This)->valueChangedCallback();
00172 }
00173 
00174 void ViewProviderMeshTransformDemolding::sDragEndCallback(void *This, SoDragger *)
00175 {
00176   static_cast<ViewProviderMeshTransformDemolding*>(This)->DragEndCallback();
00177 }
00178 
00179 void ViewProviderMeshTransformDemolding::DragEndCallback(void)
00180 {
00181   SbRotation rot = pcTrackballDragger->rotation.getValue();
00182   calcMaterialIndex(rot);
00183 
00184   Base::Console().Log("View: Finish draging\n");
00185 
00186 }
00187 
00188 void ViewProviderMeshTransformDemolding::valueChangedCallback(void)
00189 {
00190   //Base::Console().Log("Value change Callback\n");
00191   //setTransformation(pcTrackballDragger->getMotionMatrix());
00192   //pcTransform->rotation = pcTrackballDragger->rotation;
00193   SbMatrix temp;
00194   SbRotation rot = pcTrackballDragger->rotation.getValue();
00195 
00196   //calcMaterialIndex(rot);
00197 
00198   temp.setTransform( SbVec3f(0,0,0),    // no transformation
00199                      rot,               // rotation from the dragger
00200                      SbVec3f(1,1,1),    // no scaling
00201                      SbRotation() ,     // no scaling oriantation
00202                      SbVec3f(center.x,center.y,center.z)); // center of rotaion
00203   pcTransformDrag->setMatrix( temp );
00204 }
00205 
00206 void ViewProviderMeshTransformDemolding::setDisplayMode(const char* ModeName)
00207 {
00208   if ( strcmp("Demold",ModeName) == 0 )
00209     setDisplayMaskMode("Demold");
00210   ViewProviderMesh::setDisplayMode(ModeName);
00211 }
00212 
00213 const char* ViewProviderMeshTransformDemolding::getDefaultDisplayMode() const
00214 {
00215   return "Demold";
00216 }
00217 
00218 std::vector<std::string> ViewProviderMeshTransformDemolding::getDisplayModes(void) const
00219 {
00220   std::vector<std::string> StrList = ViewProviderMesh::getDisplayModes();
00221   StrList.push_back("Demold");
00222   return StrList;
00223 }

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