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 <Poly_Polygon3D.hxx>
00028 # include <BRepBndLib.hxx>
00029 # include <BRepMesh.hxx>
00030 # include <BRepMesh_IncrementalMesh.hxx>
00031 # include <BRep_Tool.hxx>
00032 # include <BRepTools.hxx>
00033 # include <BRepAdaptor_Curve.hxx>
00034 # include <BRepAdaptor_Surface.hxx>
00035 # include <GeomAbs_CurveType.hxx>
00036 # include <GeomAbs_SurfaceType.hxx>
00037 # include <Geom_BezierCurve.hxx>
00038 # include <Geom_BSplineCurve.hxx>
00039 # include <Geom_BezierSurface.hxx>
00040 # include <Geom_BSplineSurface.hxx>
00041 # include <GeomAPI_ProjectPointOnSurf.hxx>
00042 # include <GeomLProp_SLProps.hxx>
00043 # include <gp_Trsf.hxx>
00044 # include <Poly_Array1OfTriangle.hxx>
00045 # include <Poly_Triangulation.hxx>
00046 # include <TColgp_Array1OfPnt.hxx>
00047 # include <TopoDS.hxx>
00048 # include <TopoDS_Edge.hxx>
00049 # include <TopoDS_Wire.hxx>
00050 # include <TopoDS_Face.hxx>
00051 # include <TopoDS_Shape.hxx>
00052 # include <TopoDS_Iterator.hxx>
00053 # include <TopExp_Explorer.hxx>
00054 # include <TopExp.hxx>
00055 # include <TopTools_IndexedMapOfShape.hxx>
00056 # include <Poly_PolygonOnTriangulation.hxx>
00057 # include <TColStd_Array1OfInteger.hxx>
00058 # include <TopTools_ListOfShape.hxx>
00059 # include <Inventor/SoPickedPoint.h>
00060 # include <Inventor/events/SoMouseButtonEvent.h>
00061 # include <Inventor/nodes/SoCoordinate3.h>
00062 # include <Inventor/nodes/SoDrawStyle.h>
00063 # include <Inventor/nodes/SoIndexedFaceSet.h>
00064 # include <Inventor/nodes/SoLineSet.h>
00065 # include <Inventor/nodes/SoLocateHighlight.h>
00066 # include <Inventor/nodes/SoMaterial.h>
00067 # include <Inventor/nodes/SoNormal.h>
00068 # include <Inventor/nodes/SoNormalBinding.h>
00069 # include <Inventor/nodes/SoPointSet.h>
00070 # include <Inventor/nodes/SoPolygonOffset.h>
00071 # include <Inventor/nodes/SoShapeHints.h>
00072 # include <Inventor/nodes/SoSwitch.h>
00073 # include <Inventor/nodes/SoGroup.h>
00074 # include <Inventor/nodes/SoSphere.h>
00075 # include <Inventor/nodes/SoScale.h>
00076 #endif
00077
00079 #include <Base/Console.h>
00080 #include <Base/Parameter.h>
00081 #include <Base/Exception.h>
00082 #include <App/Application.h>
00083 #include <App/Document.h>
00084 #include <Gui/SoFCSelection.h>
00085 #include <Gui/Selection.h>
00086 #include <Gui/View3DInventorViewer.h>
00087
00088
00089 #include "ViewProvider.h"
00090 #include "SoFCShapeObject.h"
00091
00092 #include <Mod/Part/App/PartFeature.h>
00093 #include <Mod/Part/App/PrimitiveFeature.h>
00094
00095
00096 using namespace PartGui;
00097
00098 #if defined(FC_USE_FAST_SHAPE_RENDERING)
00099
00100 PROPERTY_SOURCE(PartGui::ViewProviderPart, PartGui::ViewProviderPartExt)
00101
00102
00103 ViewProviderPart::ViewProviderPart()
00104 {
00105 }
00106
00107 ViewProviderPart::~ViewProviderPart()
00108 {
00109 }
00110 #else
00111 PROPERTY_SOURCE(PartGui::ViewProviderPart, PartGui::ViewProviderPartBase)
00112
00113
00114 ViewProviderPart::ViewProviderPart()
00115 {
00116 }
00117
00118 ViewProviderPart::~ViewProviderPart()
00119 {
00120 }
00121 #endif
00122
00123
00124
00125 void ViewProviderShapeBuilder::buildNodes(const App::Property* prop, std::vector<SoNode*>& nodes) const
00126 {
00127 }
00128
00129 void ViewProviderShapeBuilder::createShape(const App::Property* prop, SoSeparator* coords) const
00130 {
00131 }
00132
00133
00134 PROPERTY_SOURCE(PartGui::ViewProviderPartBase, Gui::ViewProviderGeometryObject)
00135
00136
00137
00138
00139
00140 App::PropertyFloatConstraint::Constraints ViewProviderPartBase::floatRange = {1.0f,64.0f,1.0f};
00141 const char* ViewProviderPartBase::LightingEnums[]= {"One side","Two side",NULL};
00142
00143 ViewProviderPartBase::ViewProviderPartBase() : pcControlPoints(0)
00144 {
00145 App::Material mat;
00146 mat.ambientColor.set(0.2f,0.2f,0.2f);
00147 mat.diffuseColor.set(0.1f,0.1f,0.1f);
00148 mat.specularColor.set(0.0f,0.0f,0.0f);
00149 mat.emissiveColor.set(0.0f,0.0f,0.0f);
00150 mat.shininess = 1.0f;
00151 mat.transparency = 0.0f;
00152 ADD_PROPERTY(LineMaterial,(mat));
00153 ADD_PROPERTY(PointMaterial,(mat));
00154 ADD_PROPERTY(LineColor,(mat.diffuseColor));
00155 ADD_PROPERTY(PointColor,(mat.diffuseColor));
00156 ADD_PROPERTY(LineWidth,(2.0f));
00157 LineWidth.setConstraints(&floatRange);
00158 PointSize.setConstraints(&floatRange);
00159 ADD_PROPERTY(PointSize,(2.0f));
00160 ADD_PROPERTY(ControlPoints,(false));
00161 ADD_PROPERTY(Lighting,(1));
00162 Lighting.setEnums(LightingEnums);
00163
00164 EdgeRoot = new SoSeparator();
00165 EdgeRoot->ref();
00166 FaceRoot = new SoSeparator();
00167 FaceRoot->ref();
00168 VertexRoot = new SoSeparator();
00169 VertexRoot->ref();
00170 pcLineMaterial = new SoMaterial;
00171 pcLineMaterial->ref();
00172 LineMaterial.touch();
00173
00174 pcPointMaterial = new SoMaterial;
00175 pcPointMaterial->ref();
00176 PointMaterial.touch();
00177
00178 pcLineStyle = new SoDrawStyle();
00179 pcLineStyle->ref();
00180 pcLineStyle->style = SoDrawStyle::LINES;
00181 pcLineStyle->lineWidth = LineWidth.getValue();
00182
00183 pcPointStyle = new SoDrawStyle();
00184 pcPointStyle->ref();
00185 pcPointStyle->style = SoDrawStyle::POINTS;
00186 pcPointStyle->pointSize = PointSize.getValue();
00187
00188 pShapeHints = new SoShapeHints;
00189 pShapeHints->shapeType = SoShapeHints::UNKNOWN_SHAPE_TYPE;
00190 pShapeHints->ref();
00191 Lighting.touch();
00192
00193 sPixmap = "Tree_Part";
00194 loadParameter();
00195 }
00196
00197 ViewProviderPartBase::~ViewProviderPartBase()
00198 {
00199 EdgeRoot->unref();
00200 FaceRoot->unref();
00201 VertexRoot->unref();
00202 pcLineMaterial->unref();
00203 pcPointMaterial->unref();
00204 pcLineStyle->unref();
00205 pcPointStyle->unref();
00206 pShapeHints->unref();
00207 }
00208
00209 void ViewProviderPartBase::onChanged(const App::Property* prop)
00210 {
00211 if (prop == &LineWidth) {
00212 pcLineStyle->lineWidth = LineWidth.getValue();
00213 }
00214 else if (prop == &PointSize) {
00215 pcPointStyle->pointSize = PointSize.getValue();
00216 }
00217 else if (prop == &LineColor) {
00218 const App::Color& c = LineColor.getValue();
00219 pcLineMaterial->diffuseColor.setValue(c.r,c.g,c.b);
00220 if (c != LineMaterial.getValue().diffuseColor)
00221 LineMaterial.setDiffuseColor(c);
00222 }
00223 else if (prop == &PointColor) {
00224 const App::Color& c = PointColor.getValue();
00225 pcPointMaterial->diffuseColor.setValue(c.r,c.g,c.b);
00226 if (c != PointMaterial.getValue().diffuseColor)
00227 PointMaterial.setDiffuseColor(c);
00228 }
00229 else if (prop == &LineMaterial) {
00230 const App::Material& Mat = LineMaterial.getValue();
00231 if (LineColor.getValue() != Mat.diffuseColor)
00232 LineColor.setValue(Mat.diffuseColor);
00233 pcLineMaterial->ambientColor.setValue(Mat.ambientColor.r,Mat.ambientColor.g,Mat.ambientColor.b);
00234 pcLineMaterial->diffuseColor.setValue(Mat.diffuseColor.r,Mat.diffuseColor.g,Mat.diffuseColor.b);
00235 pcLineMaterial->specularColor.setValue(Mat.specularColor.r,Mat.specularColor.g,Mat.specularColor.b);
00236 pcLineMaterial->emissiveColor.setValue(Mat.emissiveColor.r,Mat.emissiveColor.g,Mat.emissiveColor.b);
00237 pcLineMaterial->shininess.setValue(Mat.shininess);
00238 pcLineMaterial->transparency.setValue(Mat.transparency);
00239 }
00240 else if (prop == &PointMaterial) {
00241 const App::Material& Mat = PointMaterial.getValue();
00242 if (PointColor.getValue() != Mat.diffuseColor)
00243 PointColor.setValue(Mat.diffuseColor);
00244 pcPointMaterial->ambientColor.setValue(Mat.ambientColor.r,Mat.ambientColor.g,Mat.ambientColor.b);
00245 pcPointMaterial->diffuseColor.setValue(Mat.diffuseColor.r,Mat.diffuseColor.g,Mat.diffuseColor.b);
00246 pcPointMaterial->specularColor.setValue(Mat.specularColor.r,Mat.specularColor.g,Mat.specularColor.b);
00247 pcPointMaterial->emissiveColor.setValue(Mat.emissiveColor.r,Mat.emissiveColor.g,Mat.emissiveColor.b);
00248 pcPointMaterial->shininess.setValue(Mat.shininess);
00249 pcPointMaterial->transparency.setValue(Mat.transparency);
00250 }
00251 else if (prop == &ControlPoints) {
00252 App::DocumentObject* obj = this->pcObject;
00253 App::Property* shape = obj->getPropertyByName("Shape");
00254 showControlPoints(ControlPoints.getValue(), shape);
00255 }
00256 else if (prop == &Lighting) {
00257 if (Lighting.getValue() == 0)
00258 pShapeHints->vertexOrdering = SoShapeHints::UNKNOWN_ORDERING;
00259 else
00260 pShapeHints->vertexOrdering = SoShapeHints::COUNTERCLOCKWISE;
00261 }
00262 else {
00263 ViewProviderGeometryObject::onChanged(prop);
00264 }
00265 }
00266
00267 void ViewProviderPartBase::attach(App::DocumentObject *pcFeat)
00268 {
00269
00270 ViewProviderGeometryObject::attach(pcFeat);
00271
00272 SoGroup* pcNormalRoot = new SoGroup();
00273 SoGroup* pcFlatRoot = new SoGroup();
00274 SoGroup* pcWireframeRoot = new SoGroup();
00275 SoGroup* pcPointsRoot = new SoGroup();
00276
00277
00278 pShapeHints->vertexOrdering = SoShapeHints::COUNTERCLOCKWISE;
00279 pShapeHints->shapeType = SoShapeHints::UNKNOWN_SHAPE_TYPE;
00280
00281
00282
00283 SoPolygonOffset* offset = new SoPolygonOffset();
00284
00285
00286 pcNormalRoot->addChild(pShapeHints);
00287 pcNormalRoot->addChild(EdgeRoot);
00288 pcNormalRoot->addChild(offset);
00289 pcNormalRoot->addChild(FaceRoot);
00290 pcNormalRoot->addChild(VertexRoot);
00291
00292
00293 pcFlatRoot->addChild(pShapeHints);
00294 pcFlatRoot->addChild(FaceRoot);
00295
00296
00297 pcWireframeRoot->addChild(EdgeRoot);
00298 pcWireframeRoot->addChild(VertexRoot);
00299
00300
00301 pcPointsRoot->addChild(VertexRoot);
00302
00303
00304 addDisplayMaskMode(pcNormalRoot, "Flat Lines");
00305 addDisplayMaskMode(pcFlatRoot, "Shaded");
00306 addDisplayMaskMode(pcWireframeRoot, "Wireframe");
00307 addDisplayMaskMode(pcPointsRoot, "Point");
00308 }
00309
00310 void ViewProviderPartBase::setDisplayMode(const char* ModeName)
00311 {
00312 if ( strcmp("Flat Lines",ModeName)==0 )
00313 setDisplayMaskMode("Flat Lines");
00314 else if ( strcmp("Shaded",ModeName)==0 )
00315 setDisplayMaskMode("Shaded");
00316 else if ( strcmp("Wireframe",ModeName)==0 )
00317 setDisplayMaskMode("Wireframe");
00318 else if ( strcmp("Points",ModeName)==0 )
00319 setDisplayMaskMode("Point");
00320
00321 ViewProviderGeometryObject::setDisplayMode( ModeName );
00322 }
00323
00324 std::vector<std::string> ViewProviderPartBase::getDisplayModes(void) const
00325 {
00326
00327 std::vector<std::string> StrList = ViewProviderGeometryObject::getDisplayModes();
00328
00329
00330 StrList.push_back("Flat Lines");
00331 StrList.push_back("Shaded");
00332 StrList.push_back("Wireframe");
00333 StrList.push_back("Points");
00334
00335 return StrList;
00336 }
00337
00338 void ViewProviderPartBase::shapeInfoCallback(void * ud, SoEventCallback * n)
00339 {
00340 const SoMouseButtonEvent * mbe = (SoMouseButtonEvent *)n->getEvent();
00341 Gui::View3DInventorViewer* view = reinterpret_cast<Gui::View3DInventorViewer*>(n->getUserData());
00342
00343
00344 n->getAction()->setHandled();
00345 if (mbe->getButton() == SoMouseButtonEvent::BUTTON2 && mbe->getState() == SoButtonEvent::UP) {
00346 n->setHandled();
00347 view->setEditing(false);
00348 view->getWidget()->setCursor(QCursor(Qt::ArrowCursor));
00349 view->removeEventCallback(SoMouseButtonEvent::getClassTypeId(), shapeInfoCallback);
00350 }
00351 else if (mbe->getButton() == SoMouseButtonEvent::BUTTON1 && mbe->getState() == SoButtonEvent::DOWN) {
00352 const SoPickedPoint * point = n->getPickedPoint();
00353 if (point == NULL) {
00354 Base::Console().Message("No point picked.\n");
00355 return;
00356 }
00357
00358 n->setHandled();
00359
00360
00361
00362 Gui::ViewProvider* vp = static_cast<Gui::ViewProvider*>(view->getViewProviderByPath(point->getPath()));
00363 if (!vp || !vp->getTypeId().isDerivedFrom(ViewProviderPartBase::getClassTypeId()))
00364 return;
00365 ViewProviderPartBase* that = static_cast<ViewProviderPartBase*>(vp);
00366 TopoDS_Shape sh = that->getShape(point);
00367 if (!sh.IsNull()) {
00368 SbVec3f pt = point->getPoint();
00369 Base::Console().Message("(%.6f, %.6f, %.6f, %d)\n", pt[0], pt[1], pt[2], sh.HashCode(IntegerLast()));
00370 }
00371 }
00372 }
00373
00374 TopoDS_Shape ViewProviderPartBase::getShape(const SoPickedPoint* point) const
00375 {
00376 if (point && point->getPath()->getTail()->getTypeId().isDerivedFrom(SoVertexShape::getClassTypeId())) {
00377 SoVertexShape* vs = static_cast<SoVertexShape*>(point->getPath()->getTail());
00378 std::map<SoVertexShape*, TopoDS_Shape>::const_iterator it = vertexShapeMap.find(vs);
00379 if (it != vertexShapeMap.end())
00380 return it->second;
00381 }
00382
00383 return TopoDS_Shape();
00384 }
00385
00386 bool ViewProviderPartBase::loadParameter()
00387 {
00388 bool changed = false;
00389 ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath
00390 ("User parameter:BaseApp/Preferences/Mod/Part");
00391 float deviation = hGrp->GetFloat("MeshDeviation",0.2);
00392 bool novertexnormals = hGrp->GetBool("NoPerVertexNormals",false);
00393 bool qualitynormals = hGrp->GetBool("QualityNormals",false);
00394
00395 if (this->meshDeviation != deviation) {
00396 this->meshDeviation = deviation;
00397 changed = true;
00398 }
00399 if (this->noPerVertexNormals != novertexnormals) {
00400 this->noPerVertexNormals = novertexnormals;
00401 changed = true;
00402 }
00403 if (this->qualityNormals != qualitynormals) {
00404 this->qualityNormals = qualitynormals;
00405 changed = true;
00406 }
00407
00408 return changed;
00409 }
00410
00411 void ViewProviderPartBase::reload()
00412 {
00413 if (loadParameter()) {
00414 App::Property* shape = pcObject->getPropertyByName("Shape");
00415 if (shape) update(shape);
00416 }
00417 }
00418
00419 void ViewProviderPartBase::updateData(const App::Property* prop)
00420 {
00421 if (prop->getTypeId() == Part::PropertyPartShape::getClassTypeId()) {
00422 TopoDS_Shape cShape = static_cast<const Part::PropertyPartShape*>(prop)->getValue();
00423
00424
00425 vertexShapeMap.clear();
00426 EdgeRoot->removeAllChildren();
00427 FaceRoot->removeAllChildren();
00428 VertexRoot->removeAllChildren();
00429
00430 if (cShape.IsNull())
00431 return;
00432
00433 try {
00434
00435 Bnd_Box bounds;
00436 BRepBndLib::Add(cShape, bounds);
00437 bounds.SetGap(0.0);
00438 Standard_Real xMin, yMin, zMin, xMax, yMax, zMax;
00439 bounds.Get(xMin, yMin, zMin, xMax, yMax, zMax);
00440 Standard_Real deflection = ((xMax-xMin)+(yMax-yMin)+(zMax-zMin))/300.0 *
00441 this->meshDeviation;
00442
00443 BRepMesh::Mesh(cShape,deflection);
00444
00445
00446
00447 TopLoc_Location aLoc;
00448 cShape.Location(aLoc);
00449 computeFaces (FaceRoot,cShape,deflection);
00450 computeEdges (EdgeRoot,cShape);
00451 computeVertices(VertexRoot,cShape);
00452
00453
00454
00455
00456 if (pcControlPoints) {
00457 pcControlPoints->removeAllChildren();
00458 showControlPoints(this->ControlPoints.getValue(), prop);
00459 }
00460 }
00461 catch (...){
00462 Base::Console().Error("Cannot compute Inventor representation for the shape of %s.\n",
00463 pcObject->getNameInDocument());
00464
00465
00466 #if defined(__GNUC__) && defined(__LP64__) && !defined(_OCC64)
00467 std::string exe = App::Application::Config()["ExeName"];
00468 Base::Console().Error("IMPORTANT: Apparently, %s isn't built with the OpenCASCADE-internal "
00469 "define '_OCC64'.\nReconfigure the build system with the missing define "
00470 "(e.g. ./configure CXXFLAGS=\"-D_OCC64\") and run a complete rebuild.\n",
00471 exe.c_str());
00472 #endif
00473 }
00474 }
00475 Gui::ViewProviderGeometryObject::updateData(prop);
00476 }
00477
00478 Standard_Boolean ViewProviderPartBase::computeEdges(SoGroup* EdgeRoot, const TopoDS_Shape &myShape)
00479 {
00480
00481
00482 EdgeRoot->addChild(pcLineMaterial);
00483 EdgeRoot->addChild(pcLineStyle);
00484
00485
00486 TopTools_IndexedMapOfShape M;
00487 TopExp::MapShapes(myShape, TopAbs_EDGE, M);
00488
00489
00490 TopTools_IndexedDataMapOfShapeListOfShape edge2Face;
00491 TopExp::MapShapesAndAncestors(myShape, TopAbs_EDGE, TopAbs_FACE, edge2Face);
00492
00493
00494
00495 for (int i=0; i < M.Extent(); i++) {
00496
00497
00498
00499 const TopoDS_Edge& aEdge = TopoDS::Edge(M(i+1));
00500
00501
00502 gp_Trsf myTransf;
00503 TopLoc_Location aLoc;
00504
00505
00506 Handle(Poly_Polygon3D) aPoly = BRep_Tool::Polygon3D(aEdge, aLoc);
00507
00508 SbVec3f* vertices;
00509 Standard_Integer nbNodesInFace;
00510
00511
00512 if (!aPoly.IsNull()) {
00513 if (!aLoc.IsIdentity()) {
00514 myTransf = aLoc.Transformation();
00515 }
00516
00517
00518
00519 nbNodesInFace = aPoly->NbNodes();
00520 vertices = new SbVec3f[nbNodesInFace];
00521
00522 const TColgp_Array1OfPnt& Nodes = aPoly->Nodes();
00523
00524 gp_Pnt V;
00525 for (Standard_Integer i=0;i < nbNodesInFace;i++) {
00526 V = Nodes(i+1);
00527 V.Transform(myTransf);
00528 vertices[i].setValue((float)(V.X()),(float)(V.Y()),(float)(V.Z()));
00529 }
00530 }
00531 else {
00532
00533
00534
00535
00536 const TopoDS_Face& aFace = TopoDS::Face(edge2Face.FindFromKey(aEdge).First());
00537
00538
00539 Handle(Poly_Triangulation) aPolyTria = BRep_Tool::Triangulation(aFace,aLoc);
00540 if (!aLoc.IsIdentity()) {
00541 myTransf = aLoc.Transformation();
00542 }
00543
00544
00545
00546 if (aPolyTria.IsNull()) return false;
00547
00548
00549 Handle(Poly_PolygonOnTriangulation) aPoly = BRep_Tool::PolygonOnTriangulation(aEdge, aPolyTria, aLoc);
00550 if (aPoly.IsNull())
00551 continue;
00552
00553
00554 nbNodesInFace = aPoly->NbNodes();
00555 vertices = new SbVec3f[nbNodesInFace];
00556
00557 const TColStd_Array1OfInteger& indices = aPoly->Nodes();
00558 const TColgp_Array1OfPnt& Nodes = aPolyTria->Nodes();
00559
00560 gp_Pnt V;
00561 int pos = 0;
00562
00563 for (Standard_Integer i=indices.Lower();i <= indices.Upper();i++) {
00564 V = Nodes(indices(i));
00565 V.Transform(myTransf);
00566 vertices[pos++].setValue((float)(V.X()),(float)(V.Y()),(float)(V.Z()));
00567 }
00568 }
00569
00570
00571 SoCoordinate3 * coords = new SoCoordinate3;
00572 coords->point.setValues(0,nbNodesInFace, vertices);
00573 delete [] vertices;
00574 EdgeRoot->addChild(coords);
00575
00576
00577 Gui::SoFCSelection* sel = createFromSettings();
00578 SbString name("Edge");
00579 name += SbString(i+1);
00580 sel->objectName = pcObject->getNameInDocument();
00581 sel->documentName = pcObject->getDocument()->getName();
00582 sel->subElementName = name;
00583 sel->style = Gui::SoFCSelection::EMISSIVE_DIFFUSE;
00584
00585
00586
00587 SoLineSet * lineset = new SoLineSet;
00588 sel->addChild(lineset);
00589 EdgeRoot->addChild(sel);
00590 vertexShapeMap[lineset] = aEdge;
00591 }
00592
00593 return true;
00594 }
00595
00596 Standard_Boolean ViewProviderPartBase::computeVertices(SoGroup* VertexRoot, const TopoDS_Shape &myShape)
00597 {
00598 #if 0 // new implementation of computeVertice
00599 VertexRoot->addChild(pcPointMaterial);
00600 VertexRoot->addChild(pcPointStyle);
00601
00602
00603 SoCoordinate3 * coords = new SoCoordinate3;
00604 VertexRoot->addChild(coords);
00605
00606 TopExp_Explorer ex;
00607 int iCnt=0;
00608 for (ex.Init(myShape, TopAbs_VERTEX); ex.More(); ex.Next()) {
00609 iCnt++;
00610 }
00611
00612 coords->point.setNum(iCnt);
00613
00614 int i=1;
00615 for (ex.Init(myShape, TopAbs_VERTEX); ex.More(); ex.Next()) {
00616
00617 const TopoDS_Vertex& aVertex = TopoDS::Vertex(ex.Current());
00618 gp_Pnt pnt = BRep_Tool::Pnt(aVertex);
00619 coords->point.set1Value(i++, (float)pnt.X(), (float)pnt.Y(), (float)pnt.Z());
00620 }
00621
00622
00623 Gui::SoFCSelection* sel = createFromSettings();
00624 SbString name("Point");
00625 name += SbString(i);
00626 sel->objectName = pcObject->getNameInDocument();
00627 sel->documentName = pcObject->getDocument()->getName();
00628 sel->subElementName = name;
00629 sel->style = Gui::SoFCSelection::EMISSIVE_DIFFUSE;
00630
00631
00632
00633 SoPointSet * pointset = new SoPointSet;
00634 sel->addChild(pointset);
00635 VertexRoot->addChild(sel);
00636
00637 return true;
00638 #else
00639 VertexRoot->addChild(pcPointMaterial);
00640 VertexRoot->addChild(pcPointStyle);
00641
00642
00643
00644 TopTools_IndexedMapOfShape M;
00645 TopExp::MapShapes(myShape, TopAbs_VERTEX, M);
00646
00647
00648
00649
00650 for (int i=0; i<M.Extent(); i++)
00651 {
00652 const TopoDS_Vertex& aVertex = TopoDS::Vertex(M(i+1));
00653
00654
00655 Gui::SoFCSelection* sel = createFromSettings();
00656 SbString name("Point");
00657 name += SbString(i+1);
00658 sel->objectName = pcObject->getNameInDocument();
00659 sel->documentName = pcObject->getDocument()->getName();
00660 sel->subElementName = name;
00661 sel->style = Gui::SoFCSelection::EMISSIVE_DIFFUSE;
00662
00663
00664
00665
00666 SoCoordinate3 * coords = new SoCoordinate3;
00667 coords->point.setNum(1);
00668 VertexRoot->addChild(coords);
00669
00670
00671
00672
00673 gp_Pnt pnt = BRep_Tool::Pnt(aVertex);
00674 coords->point.set1Value(0, (float)pnt.X(), (float)pnt.Y(), (float)pnt.Z());
00675
00676
00677 SoPointSet * pointset = new SoPointSet;
00678 sel->addChild(pointset);
00679 VertexRoot->addChild(sel);
00680
00681 }
00682
00683
00684 return true;
00685
00686 #endif
00687 }
00688
00689 Standard_Boolean ViewProviderPartBase::computeFaces(SoGroup* FaceRoot, const TopoDS_Shape &myShape, double defl)
00690 {
00691 TopExp_Explorer ex;
00692
00693 FaceRoot->addChild(pcShapeMaterial);
00694
00695
00696 BRepMesh_IncrementalMesh MESH(myShape,defl);
00697
00698 int i = 1;
00699 for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next(),i++) {
00700
00701 const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
00702
00703
00704
00705 Standard_Integer nbNodesInFace,nbTriInFace;
00706 SbVec3f* vertices=0;
00707 SbVec3f* vertexnormals=0;
00708 int32_t* cons=0;
00709
00710 transferToArray(aFace,&vertices,&vertexnormals,&cons,nbNodesInFace,nbTriInFace);
00711
00712 if (!vertices)
00713 continue;
00714
00715 if (!this->noPerVertexNormals) {
00716
00717 SoNormal * norm = new SoNormal;
00718 norm->vector.setValues(0, nbNodesInFace, vertexnormals);
00719 FaceRoot->addChild(norm);
00720
00721
00722 SoNormalBinding * normb = new SoNormalBinding;
00723 normb->value = SoNormalBinding::PER_VERTEX_INDEXED;
00724 FaceRoot->addChild(normb);
00725 }
00726
00727
00728 SoCoordinate3 * coords = new SoCoordinate3;
00729 coords->point.setValues(0,nbNodesInFace, vertices);
00730 FaceRoot->addChild(coords);
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748 Gui::SoFCSelection* sel = createFromSettings();
00749 SbString name("Face");
00750 name += SbString(i);
00751 sel->objectName = pcObject->getNameInDocument();
00752 sel->documentName = pcObject->getDocument()->getName();
00753 sel->subElementName = name;
00754 sel->style = Gui::SoFCSelection::EMISSIVE;
00755
00756
00757
00758 SoIndexedFaceSet * faceset = new SoIndexedFaceSet;
00759 faceset->coordIndex.setValues(0,4*nbTriInFace,(const int32_t*) cons);
00760 sel->addChild(faceset);
00761 FaceRoot->addChild(sel);
00762 vertexShapeMap[faceset] = aFace;
00763
00764
00765
00766
00767 delete [] vertexnormals;
00768 delete [] vertices;
00769 delete [] cons;
00770 }
00771
00772 return true;
00773 }
00774
00775 void ViewProviderPartBase::transferToArray(const TopoDS_Face& aFace,SbVec3f** vertices,SbVec3f** vertexnormals,
00776 int32_t** cons,int &nbNodesInFace,int &nbTriInFace )
00777 {
00778 TopLoc_Location aLoc;
00779
00780
00781
00782 Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
00783
00784 if (aPoly.IsNull()) return;
00785
00786
00787 gp_Trsf myTransf;
00788 Standard_Boolean identity = true;
00789 if (!aLoc.IsIdentity()) {
00790 identity = false;
00791 myTransf = aLoc.Transformation();
00792 }
00793
00794 Standard_Integer i;
00795
00796 nbNodesInFace = aPoly->NbNodes();
00797 nbTriInFace = aPoly->NbTriangles();
00798 *vertices = new SbVec3f[nbNodesInFace];
00799 *vertexnormals = new SbVec3f[nbNodesInFace];
00800 for(i=0;i < nbNodesInFace;i++) {
00801 (*vertexnormals)[i]= SbVec3f(0.0,0.0,0.0);
00802 }
00803
00804 *cons = new int32_t[4*(nbTriInFace)];
00805
00806
00807 TopAbs_Orientation orient = aFace.Orientation();
00808
00809
00810 const Poly_Array1OfTriangle& Triangles = aPoly->Triangles();
00811 const TColgp_Array1OfPnt& Nodes = aPoly->Nodes();
00812 for (i=1;i<=nbTriInFace;i++) {
00813
00814 Standard_Integer N1,N2,N3;
00815 Triangles(i).Get(N1,N2,N3);
00816
00817
00818 if ( orient != TopAbs_FORWARD ) {
00819 Standard_Integer tmp = N1;
00820 N1 = N2;
00821 N2 = tmp;
00822 }
00823
00824 gp_Pnt V1 = Nodes(N1);
00825 gp_Pnt V2 = Nodes(N2);
00826 gp_Pnt V3 = Nodes(N3);
00827
00828
00829 if (!identity) {
00830 V1.Transform(myTransf);
00831 V2.Transform(myTransf);
00832 V3.Transform(myTransf);
00833 }
00834
00835 if (!this->noPerVertexNormals) {
00836
00837 gp_Vec v1(V1.X(),V1.Y(),V1.Z()),v2(V2.X(),V2.Y(),V2.Z()),v3(V3.X(),V3.Y(),V3.Z());
00838 gp_Vec Normal = (v2-v1)^(v3-v1);
00839
00840
00841
00842
00843 (*vertexnormals)[N1-1] += SbVec3f(Normal.X(),Normal.Y(),Normal.Z());
00844 (*vertexnormals)[N2-1] += SbVec3f(Normal.X(),Normal.Y(),Normal.Z());
00845 (*vertexnormals)[N3-1] += SbVec3f(Normal.X(),Normal.Y(),Normal.Z());
00846 }
00847
00848 (*vertices)[N1-1].setValue((float)(V1.X()),(float)(V1.Y()),(float)(V1.Z()));
00849 (*vertices)[N2-1].setValue((float)(V2.X()),(float)(V2.Y()),(float)(V2.Z()));
00850 (*vertices)[N3-1].setValue((float)(V3.X()),(float)(V3.Y()),(float)(V3.Z()));
00851
00852 int j = i - 1;
00853 N1--; N2--; N3--;
00854 (*cons)[4*j] = N1; (*cons)[4*j+1] = N2; (*cons)[4*j+2] = N3; (*cons)[4*j+3] = SO_END_FACE_INDEX;
00855 }
00856
00857
00858 for(i=0;i < nbNodesInFace;i++) {
00859 if (this->qualityNormals) {
00860 gp_Dir clNormal;
00861
00862 try {
00863 Handle_Geom_Surface Surface = BRep_Tool::Surface(aFace);
00864
00865 gp_Pnt vertex((*vertices)[i][0], (*vertices)[i][1], (*vertices)[i][2]);
00866 GeomAPI_ProjectPointOnSurf ProPntSrf(vertex, Surface);
00867 Standard_Real fU, fV; ProPntSrf.Parameters(1, fU, fV);
00868
00869 GeomLProp_SLProps clPropOfFace(Surface, fU, fV, 2, gp::Resolution());
00870
00871 clNormal = clPropOfFace.Normal();
00872 SbVec3f temp = SbVec3f(clNormal.X(),clNormal.Y(),clNormal.Z());
00873
00874
00875 if ( temp.dot((*vertexnormals)[i]) < 0 )
00876 temp = -temp;
00877 (*vertexnormals)[i] = temp;
00878
00879 }
00880 catch(...){}
00881 }
00882 else if ((*vertexnormals)[i].sqrLength() > 0.001){
00883 (*vertexnormals)[i].normalize();
00884 }
00885 }
00886 }
00887
00888 void ViewProviderPartBase::showControlPoints(bool show, const App::Property* prop)
00889 {
00890 if (!pcControlPoints && show) {
00891 pcControlPoints = new SoSwitch();
00892 pcRoot->addChild(pcControlPoints);
00893 }
00894
00895 if (pcControlPoints) {
00896 pcControlPoints->whichChild = (show ? SO_SWITCH_ALL : SO_SWITCH_NONE);
00897 }
00898
00899 if (!show || !pcControlPoints || pcControlPoints->getNumChildren() > 0)
00900 return;
00901
00902
00903 if (prop && prop->getTypeId() == Part::PropertyPartShape::getClassTypeId()) {
00904 const TopoDS_Shape& shape = static_cast<const Part::PropertyPartShape*>(prop)->getValue();
00905 if (shape.IsNull())
00906 return;
00907 switch (shape.ShapeType())
00908 {
00909 case TopAbs_EDGE:
00910 {
00911 const TopoDS_Edge& edge = TopoDS::Edge(shape);
00912 showControlPointsOfEdge(edge);
00913 } break;
00914 case TopAbs_WIRE:
00915 {
00916 const TopoDS_Wire& wire = TopoDS::Wire(shape);
00917 showControlPointsOfWire(wire);
00918 } break;
00919 case TopAbs_FACE:
00920 {
00921 const TopoDS_Face& face = TopoDS::Face(shape);
00922 showControlPointsOfFace(face);
00923 } break;
00924 default:
00925 break;
00926 }
00927 }
00928 }
00929
00930 void ViewProviderPartBase::showControlPointsOfEdge(const TopoDS_Edge& edge)
00931 {
00932 std::list<gp_Pnt> poles, knots;
00933 Standard_Integer nCt=0;
00934 BRepAdaptor_Curve curve(edge);
00935 switch (curve.GetType())
00936 {
00937 case GeomAbs_BezierCurve:
00938 {
00939 Handle(Geom_BezierCurve) hBezier = curve.Bezier();
00940 nCt = hBezier->NbPoles();
00941 for (Standard_Integer i = 1; i <= nCt; i++)
00942 poles.push_back(hBezier->Pole(i));
00943 if (hBezier->IsClosed()) {
00944 nCt++;
00945 poles.push_back(hBezier->Pole(1));
00946 }
00947 } break;
00948 case GeomAbs_BSplineCurve:
00949 {
00950 Handle(Geom_BSplineCurve) hBSpline = curve.BSpline();
00951 nCt = hBSpline->NbPoles();
00952 for (Standard_Integer i = 1; i <= nCt; i++)
00953 poles.push_back(hBSpline->Pole(i));
00954 if (hBSpline->IsClosed()) {
00955 nCt++;
00956 poles.push_back(hBSpline->Pole(1));
00957 }
00958 for (Standard_Integer i = hBSpline->FirstUKnotIndex()+1; i <= hBSpline->LastUKnotIndex()-1; i++)
00959 knots.push_back(hBSpline->Value(hBSpline->Knot(i)));
00960 } break;
00961 default:
00962 break;
00963 }
00964
00965 if (poles.empty())
00966 return;
00967
00968 SoCoordinate3 * coords = new SoCoordinate3;
00969 coords->point.setNum(nCt + knots.size());
00970
00971 int index=0;
00972 SbVec3f* verts = coords->point.startEditing();
00973 for (std::list<gp_Pnt>::iterator p = poles.begin(); p != poles.end(); ++p) {
00974 verts[index++].setValue((float)p->X(), (float)p->Y(), (float)p->Z());
00975 }
00976 for (std::list<gp_Pnt>::iterator k = knots.begin(); k != knots.end(); ++k) {
00977 verts[index++].setValue((float)k->X(), (float)k->Y(), (float)k->Z());
00978 }
00979 coords->point.finishEditing();
00980
00981
00982 SoFCControlPoints* control = new SoFCControlPoints();
00983 control->numPolesU = nCt;
00984 control->numPolesV = 1;
00985
00986 SoSeparator* nodes = new SoSeparator();
00987 nodes->addChild(coords);
00988 nodes->addChild(control);
00989
00990 pcControlPoints->addChild(nodes);
00991 }
00992
00993 void ViewProviderPartBase::showControlPointsOfWire(const TopoDS_Wire& wire)
00994 {
00995 TopoDS_Iterator it;
00996 for (it.Initialize(wire); it.More(); it.Next()) {
00997 if (it.Value().ShapeType() == TopAbs_EDGE) {
00998 const TopoDS_Edge& edge = TopoDS::Edge(it.Value());
00999 BRepAdaptor_Curve curve(edge);
01000
01001 std::list<gp_Pnt> poles, knots;
01002 gp_Pnt start, end;
01003 switch (curve.GetType())
01004 {
01005 case GeomAbs_BezierCurve:
01006 {
01007 Handle(Geom_BezierCurve) hBezier = curve.Bezier();
01008 for (Standard_Integer i = 1; i <= hBezier->NbPoles(); i++)
01009 poles.push_back(hBezier->Pole(i));
01010 start = hBezier->StartPoint();
01011 end = hBezier->EndPoint();
01012 } break;
01013 case GeomAbs_BSplineCurve:
01014 {
01015 Handle(Geom_BSplineCurve) hBSpline = curve.BSpline();
01016 for (Standard_Integer i = 1; i <= hBSpline->NbPoles(); i++)
01017 poles.push_back(hBSpline->Pole(i));
01018 start = hBSpline->StartPoint();
01019 end = hBSpline->EndPoint();
01020 for (Standard_Integer i = hBSpline->FirstUKnotIndex()+1; i <= hBSpline->LastUKnotIndex()-1; i++)
01021 knots.push_back(hBSpline->Value(hBSpline->Knot(i)));
01022 } break;
01023 default:
01024 break;
01025 }
01026 }
01027 }
01028 }
01029
01030 void ViewProviderPartBase::showControlPointsOfFace(const TopoDS_Face& face)
01031 {
01032 std::list<gp_Pnt> knots;
01033 std::vector<std::vector<gp_Pnt> > poles;
01034 Standard_Integer nCtU=0, nCtV=0;
01035 BRepAdaptor_Surface surface(face);
01036
01037 BRepAdaptor_Surface clSurface(face);
01038 switch (clSurface.GetType())
01039 {
01040 case GeomAbs_BezierSurface:
01041 {
01042 Handle(Geom_BezierSurface) hBezier = surface.Bezier();
01043 nCtU = hBezier->NbUPoles();
01044 nCtV = hBezier->NbVPoles();
01045 poles.resize(nCtU);
01046 for (Standard_Integer u = 1; u <= nCtU; u++) {
01047 poles[u-1].resize(nCtV);
01048 for (Standard_Integer v = 1; v <= nCtV; v++)
01049 poles[u-1][v-1] = hBezier->Pole(u, v);
01050 }
01051 } break;
01052 case GeomAbs_BSplineSurface:
01053 {
01054 Handle(Geom_BSplineSurface) hBSpline = surface.BSpline();
01055 nCtU = hBSpline->NbUPoles();
01056 nCtV = hBSpline->NbVPoles();
01057 poles.resize(nCtU);
01058 for (Standard_Integer u = 1; u <= nCtU; u++) {
01059 poles[u-1].resize(nCtV);
01060 for (Standard_Integer v = 1; v <= nCtV; v++)
01061 poles[u-1][v-1] = hBSpline->Pole(u, v);
01062 }
01063
01064
01065
01066 for (Standard_Integer u = 1; u <= hBSpline->NbUKnots(); u++) {
01067 for (Standard_Integer v = 1; v <= hBSpline->NbVKnots(); v++)
01068 knots.push_back(hBSpline->Value(hBSpline->UKnot(u), hBSpline->VKnot(v)));
01069 }
01070 } break;
01071 default:
01072 break;
01073 }
01074
01075 if (poles.empty())
01076 return;
01077
01078 SoCoordinate3 * coords = new SoCoordinate3;
01079 coords->point.setNum(nCtU * nCtV + knots.size());
01080
01081 int index=0;
01082 SbVec3f* verts = coords->point.startEditing();
01083 for (std::vector<std::vector<gp_Pnt> >::iterator u = poles.begin(); u != poles.end(); ++u) {
01084 for (std::vector<gp_Pnt>::iterator v = u->begin(); v != u->end(); ++v) {
01085 verts[index++].setValue((float)v->X(), (float)v->Y(), (float)v->Z());
01086 }
01087 }
01088 for (std::list<gp_Pnt>::iterator k = knots.begin(); k != knots.end(); ++k) {
01089 verts[index++].setValue((float)k->X(), (float)k->Y(), (float)k->Z());
01090 }
01091 coords->point.finishEditing();
01092
01093
01094 SoFCControlPoints* control = new SoFCControlPoints();
01095 control->numPolesU = nCtU;
01096 control->numPolesV = nCtV;
01097
01098
01099
01100
01101
01102
01103 SoSeparator* nodes = new SoSeparator();
01104 nodes->addChild(coords);
01105 nodes->addChild(control);
01106
01107 pcControlPoints->addChild(nodes);
01108 }
01109
01110
01111
01112 PROPERTY_SOURCE(PartGui::ViewProviderEllipsoid, PartGui::ViewProviderPartBase)
01113
01114 ViewProviderEllipsoid::ViewProviderEllipsoid()
01115 {
01116 pSphere = new SoSphere();
01117 pSphere->ref();
01118 pScaling = new SoScale();
01119 pScaling->ref();
01120 }
01121
01122 ViewProviderEllipsoid::~ViewProviderEllipsoid()
01123 {
01124 pSphere->unref();
01125 pScaling->unref();
01126 }
01127
01128 void ViewProviderEllipsoid::updateData(const App::Property* prop)
01129 {
01130 if (prop->getTypeId() == Part::PropertyPartShape::getClassTypeId()) {
01131 const TopoDS_Shape& cShape = static_cast<const Part::PropertyPartShape*>(prop)->getValue();
01132
01133
01134 EdgeRoot->removeAllChildren();
01135 FaceRoot->removeAllChildren();
01136 VertexRoot->removeAllChildren();
01137
01138 if (cShape.IsNull())
01139 return;
01140 App::DocumentObject* object = this->getObject();
01141 if (object && object->isDerivedFrom(Part::Ellipsoid::getClassTypeId())) {
01142 float angle1 = static_cast<Part::Ellipsoid*>(object)->Angle1.getValue();
01143 float angle2 = static_cast<Part::Ellipsoid*>(object)->Angle2.getValue();
01144 float angle3 = static_cast<Part::Ellipsoid*>(object)->Angle3.getValue();
01145 float radius1 = static_cast<Part::Ellipsoid*>(object)->Radius1.getValue();
01146 float radius2 = static_cast<Part::Ellipsoid*>(object)->Radius2.getValue();
01147 if (angle1 == -90.0f && angle2 == 90.0f && angle3 == 360.0f) {
01148 float scale = radius1/radius2;
01149 pScaling->scaleFactor.setValue(1,1,scale);
01150 pSphere->radius.setValue(radius2);
01151 FaceRoot->addChild(pScaling);
01152 FaceRoot->addChild(pSphere);
01153 return;
01154 }
01155 }
01156
01157
01158 ViewProviderPartBase::updateData(prop);
01159 }
01160 else {
01161 Gui::ViewProviderGeometryObject::updateData(prop);
01162 }
01163 }