ViewProvider2DObject.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 <Standard_math.hxx>
00028 # include <Inventor/nodes/SoBaseColor.h>
00029 # include <Inventor/nodes/SoDrawStyle.h>
00030 # include <Inventor/nodes/SoMaterial.h>
00031 # include <Inventor/nodes/SoLineSet.h>
00032 # include <Inventor/nodes/SoPickStyle.h>
00033 # include <Inventor/nodes/SoSeparator.h>
00034 # include <Inventor/nodes/SoVertexProperty.h>
00035 #endif
00036 
00038 #include <Base/Parameter.h>
00039 #include <Base/ViewProj.h>
00040 
00041 #include "ViewProvider2DObject.h"
00042 #include <Mod/Part/App/PartFeature.h>
00043 
00044 
00045 //#include "Tree.h"
00046 
00047 
00048 
00049 using namespace PartGui;
00050 using namespace std;
00051 
00052 
00053 //**************************************************************************
00054 // Construction/Destruction
00055 
00056 const char* ViewProvider2DObject::GridStyleEnums[]= {"Dashed","Light",NULL};
00057 
00058 PROPERTY_SOURCE(PartGui::ViewProvider2DObject, PartGui::ViewProviderPart)
00059 
00060 ViewProvider2DObject::ViewProvider2DObject()
00061 {
00062     ADD_PROPERTY_TYPE(ShowGrid,(false),"Grid",(App::PropertyType)(App::Prop_None),"Switch the grid on/off");
00063     ADD_PROPERTY_TYPE(GridSize,(10),"Grid",(App::PropertyType)(App::Prop_None),"Gap size of the grid");
00064     ADD_PROPERTY_TYPE(GridStyle,((long)0),"Grid",(App::PropertyType)(App::Prop_None),"Appearence style of the grid");
00065     ADD_PROPERTY_TYPE(TightGrid,(true),"Grid",(App::PropertyType)(App::Prop_None),"Switch the tight grid mode on/off");
00066     ADD_PROPERTY_TYPE(GridSnap,(false),"Grid",(App::PropertyType)(App::Prop_None),"Switch the grid snap on/off");
00067 
00068     GridRoot = new SoSeparator();
00069     GridRoot->ref();
00070     MinX = MinY = -100;
00071     MaxX = MaxY = 100;
00072     GridStyle.setEnums(GridStyleEnums);
00073 
00074     pcRoot->addChild(GridRoot);
00075  
00076     sPixmap = "PartFeatureImport";
00077 }
00078 
00079 ViewProvider2DObject::~ViewProvider2DObject()
00080 {
00081      GridRoot->unref();
00082 }
00083 
00084 
00085 // **********************************************************************************
00086 
00087 SoSeparator* ViewProvider2DObject::createGrid(void)
00088 {
00089     //double dx = 10 * GridSize.getValue();                       // carpet size
00090     //double dy = 10 * GridSize.getValue();
00091     // float Size = (MaxX-MinX > MaxY-MinY) ? MaxX-MinX : MaxY-MinY;
00092     float Step = GridSize.getValue(); //pow(10,floor(log10(Size/5.0)));
00093     float MiX, MaX, MiY, MaY;
00094     if (TightGrid.getValue()) {
00095         MiX = MinX - (MaxX-MinX)*0.2f;
00096         MaX = MaxX + (MaxX-MinX)*0.2f;
00097         MiY = MinY - (MaxY-MinY)*0.2f;
00098         MaY = MaxY + (MaxY-MinY)*0.2f;
00099     }
00100     else {
00101         MiX = -exp(ceil(log(std::abs(MinX))));
00102         MiX = std::min<float>(MiX,(float)-exp(ceil(log(std::abs(0.1f*MaxX)))));
00103         MaX = exp(ceil(log(std::abs(MaxX))));
00104         MaX = std::max<float>(MaX,(float)exp(ceil(log(std::abs(0.1f*MinX)))));
00105         MiY = -exp(ceil(log(std::abs(MinY))));
00106         MiY = std::min<float>(MiY,(float)-exp(ceil(log(std::abs(0.1f*MaxY)))));
00107         MaY = exp(ceil(log(std::abs(MaxY))));
00108         MaY = std::max<float>(MaY,(float)exp(ceil(log(std::abs(0.1f*MinY)))));
00109     }
00110     //Round the values otherwise grid is not aligned with center
00111     MiX = floor(MiX / Step) * Step;
00112     MaX = ceil(MaX / Step) * Step;
00113     MiY = floor(MiY / Step) * Step;
00114     MaY = ceil(MaY / Step) * Step;
00115 
00116     double zGrid = 0.0;                     // carpet-grid separation
00117 
00118     SoSeparator *parent = GridRoot;
00119     GridRoot->removeAllChildren();
00120     SoBaseColor *mycolor;
00121     SoVertexProperty *vts;
00122 
00123    // carpet
00124  /* mycolor = new SoBaseColor;
00125     mycolor->rgb.setValue(0.2f, 0.7f, 0.7f);
00126     parent->addChild(mycolor);
00127 
00128     vts = new SoVertexProperty;
00129     vts->vertex.set1Value(0, -0.5*dx, -1.5*dy,  0.5*zGrid);
00130     vts->vertex.set1Value(1, -0.5*dx, -1.5*dy, -0.5*zGrid);
00131     vts->vertex.set1Value(2,  0.5*dx, -1.5*dy,  0.5*zGrid);
00132     vts->vertex.set1Value(3,  0.5*dx, -1.5*dy, -0.5*zGrid);
00133 
00134     SoQuadMesh *carpet = new SoQuadMesh;
00135     carpet->verticesPerColumn = 2;
00136     carpet->verticesPerRow = 2;
00137     carpet->vertexProperty = vts;
00138     parent->addChild(carpet);*/
00139 
00140     // gridlines
00141     mycolor = new SoBaseColor;
00142 
00143     mycolor->rgb.setValue(0.7f, 0.7f ,0.7f);
00144     parent->addChild(mycolor);
00145 
00146     SoDrawStyle* DefaultStyle = new SoDrawStyle;
00147     DefaultStyle->lineWidth = 1;
00148     DefaultStyle->linePattern = 0x0f0f;
00149 
00150     SoMaterial* LightStyle = new SoMaterial;
00151     LightStyle->transparency = 0.7f;
00152 
00153     if (GridStyle.getValue() == 0)
00154       parent->addChild(DefaultStyle);
00155     else
00156       parent->addChild(LightStyle);
00157 
00158     SoPickStyle* PickStyle = new SoPickStyle;
00159     PickStyle->style = SoPickStyle::UNPICKABLE;
00160     parent->addChild(PickStyle);
00161 
00162     SoLineSet *grid = new SoLineSet;
00163     vts = new SoVertexProperty;
00164     grid->vertexProperty = vts;
00165 
00166     int vi=0, l=0;
00167 
00168     // vertical lines
00169     float i;
00170     for (i=MiX; i<MaX; i+=Step) {
00171         /*float h=-0.5*dx + float(i) / gridsize * dx;*/
00172         vts->vertex.set1Value(vi++, i, MiY, zGrid);
00173         vts->vertex.set1Value(vi++, i,  MaY, zGrid);
00174         grid->numVertices.set1Value(l++, 2);
00175     }
00176 
00177     // horizontal lines
00178     for (i=MiY; i<MaY; i+=Step) {
00179         //float v=-0.5*dy + float(i) / gridsize * dy;
00180         vts->vertex.set1Value(vi++, MiX, i, zGrid);
00181         vts->vertex.set1Value(vi++,  MaX, i, zGrid);
00182         grid->numVertices.set1Value(l++, 2);
00183     }
00184     parent->addChild(vts);
00185     parent->addChild(grid);
00186 
00187     return parent;
00188 }
00189 
00190 void ViewProvider2DObject::updateData(const App::Property* prop)
00191 {
00192     ViewProviderPart::updateData(prop);
00193 
00194     if (prop->getTypeId() == Part::PropertyPartShape::getClassTypeId()) {
00195         Base::BoundBox3d bbox = static_cast<const Part::PropertyPartShape*>(prop)->getBoundingBox();
00196         GridRoot->removeAllChildren();
00197         if (!bbox.IsValid()) return;
00198         Base::Placement place = static_cast<const Part::PropertyPartShape*>(prop)->getComplexData()->getPlacement();
00199         place.invert();
00200         Base::ViewProjMatrix proj(place.toMatrix());
00201         Base::BoundBox2D bbox2d = bbox.ProjectBox(&proj);
00202         this->MinX = bbox2d.fMinX;
00203         this->MaxX = bbox2d.fMaxX;
00204         this->MinY = bbox2d.fMinY;
00205         this->MaxY = bbox2d.fMaxY;
00206         if (ShowGrid.getValue()) {
00207             createGrid();
00208         }
00209     }
00210 }
00211 
00212 void ViewProvider2DObject::onChanged(const App::Property* prop)
00213 {
00214     // call father
00215     ViewProviderPart::onChanged(prop);
00216 
00217     if (prop == &ShowGrid) {
00218         if (ShowGrid.getValue())
00219             createGrid();
00220         else
00221             GridRoot->removeAllChildren();
00222     }
00223     if ((prop == &GridSize) || (prop == &GridStyle) || (prop == &TightGrid)) {
00224         if (ShowGrid.getValue()) {
00225             GridRoot->removeAllChildren();
00226             createGrid();
00227         }
00228     }
00229 }
00230 
00231 void ViewProvider2DObject::attach(App::DocumentObject *pcFeat)
00232 {
00233     ViewProviderPart::attach(pcFeat);
00234 
00235     if (ShowGrid.getValue())
00236         createGrid();
00237 }
00238 
00239 bool ViewProvider2DObject::setEdit(int)
00240 {
00241     return false;
00242 }
00243 
00244 void ViewProvider2DObject::unsetEdit(int)
00245 {
00246 
00247 }
00248 
00249 std::vector<std::string> ViewProvider2DObject::getDisplayModes(void) const
00250 {
00251     // get the modes of the father
00252     std::vector<std::string> StrList = ViewProviderGeometryObject::getDisplayModes();
00253 
00254     // add your own modes
00255     StrList.push_back("Flat Lines");
00256     //StrList.push_back("Shaded");
00257     StrList.push_back("Wireframe");
00258     StrList.push_back("Points");
00259 
00260     return StrList;
00261 }
00262 
00263 const char* ViewProvider2DObject::getDefaultDisplayMode() const
00264 {
00265   return "Wireframe";
00266 }
00267 
00268 // -----------------------------------------------------------------------
00269 
00270 namespace Gui {
00272 PROPERTY_SOURCE_TEMPLATE(PartGui::ViewProvider2DObjectPython, PartGui::ViewProvider2DObject)
00274 
00275 // explicit template instantiation
00276 template class PartGuiExport ViewProviderPythonFeatureT<PartGui::ViewProvider2DObject>;
00277 }

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