00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00046
00047
00048
00049 using namespace PartGui;
00050 using namespace std;
00051
00052
00053
00054
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
00090
00091
00092 float Step = GridSize.getValue();
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
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;
00117
00118 SoSeparator *parent = GridRoot;
00119 GridRoot->removeAllChildren();
00120 SoBaseColor *mycolor;
00121 SoVertexProperty *vts;
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
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
00169 float i;
00170 for (i=MiX; i<MaX; i+=Step) {
00171
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
00178 for (i=MiY; i<MaY; i+=Step) {
00179
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
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
00252 std::vector<std::string> StrList = ViewProviderGeometryObject::getDisplayModes();
00253
00254
00255 StrList.push_back("Flat Lines");
00256
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
00276 template class PartGuiExport ViewProviderPythonFeatureT<PartGui::ViewProvider2DObject>;
00277 }