cgma
OCCDrawTool.cpp
Go to the documentation of this file.
00001 //- Class:       OCCDrawTool
00002 //- Description: Functions to draw OCC geometry (useful for debugging or
00003 //-              in previewing operations for the user)
00004 // 
00005 //- Author: Jane Hu       
00006 
00007 // ********** BEGIN OCC INCLUDES             **********
00008 #include "gp_Pnt.hxx"
00009 #include "TopoDS.hxx"
00010 #include "TopoDS_Face.hxx"
00011 #include "TopoDS_Shape.hxx"
00012 #include "TopoDS_Edge.hxx"
00013 #include "TopoDS_Vertex.hxx"
00014 #include "TopoDS_CompSolid.hxx"
00015 #include "TopoDS_Compound.hxx"
00016 #include "TopoDS_Solid.hxx"
00017 #include "TopoDS_Shell.hxx"
00018 #include "TopTools_IndexedMapOfShape.hxx"
00019 #include "TopExp.hxx"
00020 #include "BRep_Tool.hxx"
00021 #include "GProp_GProps.hxx"
00022 #include "BRepGProp.hxx"
00023 #include <TopExp_Explorer.hxx>
00024 // ********** END OCC INCLUDES               **********
00025 
00026 // ********** BEGIN CUBIT INCLUDES            **********
00027 #include "OCCDrawTool.hpp"
00028 #include "OCCSurface.hpp"
00029 #include "OCCCurve.hpp"
00030 #include "Lump.hpp"
00031 #include "Surface.hpp"
00032 #include "Curve.hpp"
00033 #include "Point.hpp"
00034 #include "OCCQueryEngine.hpp"
00035 #include "DLIList.hpp"
00036 #include "GMem.hpp"
00037 #include "GfxPreview.hpp"
00038 // ********** END CUBIT INCLUDES              **********
00039 
00040 OCCDrawTool* OCCDrawTool::instance_ = 0;
00041 
00042 // Method: instance
00043 // provides access to the unique model for this execution.
00044 // sets up this instance on first access
00045 OCCDrawTool* OCCDrawTool::instance()
00046 {
00047   if (instance_ == 0) {
00048     instance_ = new OCCDrawTool();
00049   }
00050   return instance_;
00051 }
00052 
00053 OCCDrawTool::OCCDrawTool() 
00054 { 
00055 }
00056 
00057 OCCDrawTool::~OCCDrawTool()
00058 {
00059 }
00060 
00061 CubitStatus
00062 OCCDrawTool::draw_TopoDS_Shape( TopoDS_Shape *shape, int color, 
00063                  CubitBoolean tessellate,
00064                              CubitBoolean flush )
00065 {
00066   if( shape->ShapeType() == TopAbs_COMPOUND ||
00067       shape->ShapeType() == TopAbs_SOLID ||
00068       shape->ShapeType() == TopAbs_SHELL )
00069   {
00070     DLIList<TopoDS_Face*> Face_list;
00071     TopTools_IndexedMapOfShape M;
00072     TopExp::MapShapes(*shape, TopAbs_FACE, M);
00073     int ii;
00074     for (ii=1; ii<=M.Extent(); ii++) 
00075     {
00076      //     TopologyBridge *face = OCCQueryEngine::instance()->occ_to_cgm(M(ii));
00077      //     OCCSurface *occ_face = CAST_TO(face, OCCSurface);
00078      //     Face_list.append_unique(occ_face->get_TopoDS_Face());
00079       Face_list.append_unique( const_cast<TopoDS_Face*>(&(TopoDS::Face(M(ii)))));
00080     } 
00081     int i;
00082     for( i=Face_list.size(); i--; )
00083       draw_FACE( Face_list.get_and_step(), color, tessellate );
00084     if( flush )
00085       GfxPreview::flush();
00086     return CUBIT_SUCCESS;
00087   }
00088   else if (shape->ShapeType() == TopAbs_FACE )
00089   {
00090     return draw_FACE((TopoDS_Face *)shape , color, tessellate, flush );
00091   }
00092   else if (shape->ShapeType() == TopAbs_WIRE )
00093   {
00094     DLIList<TopoDS_Edge*> EDGE_list;
00095     TopTools_IndexedMapOfShape M;
00096     TopExp::MapShapes(*shape, TopAbs_EDGE, M);
00097     int ii;
00098     for (ii=1; ii<=M.Extent(); ii++)
00099     {
00100           EDGE_list.append_unique( const_cast<TopoDS_Edge*>(&(TopoDS::Edge(M(ii)))));
00101     }
00102     int i;
00103     for( i=EDGE_list.size(); i--; )
00104       draw_EDGE( EDGE_list.get_and_step(), color );
00105     if( flush )
00106       GfxPreview::flush();
00107     return CUBIT_SUCCESS;
00108   }
00109   else if (shape->ShapeType() == TopAbs_EDGE )
00110   {
00111     return draw_EDGE( (TopoDS_Edge*)shape, color, flush );
00112   }
00113   else if (shape->ShapeType() == TopAbs_VERTEX )
00114   {
00115     return draw_VERTEX( (TopoDS_Vertex*)shape, color, flush );
00116   }
00117   else
00118   {
00119     PRINT_ERROR( "Unsupported entity type specified - cannot draw\n" );
00120     return CUBIT_FAILURE;
00121   }
00122 
00123   return CUBIT_SUCCESS;
00124 }
00125 
00126 CubitStatus
00127 OCCDrawTool::draw_EDGE( TopoDS_Edge *EDGE_ptr, int color, CubitBoolean flush )
00128 {
00129   //OCCQueryEngine *OQE = OCCQueryEngine::instance();
00130   //Curve *curve_ptr = OQE->populate_topology_bridge( *EDGE_ptr, CUBIT_TRUE );
00131   //CubitStatus stat;
00132   //stat = draw_curve(curve_ptr, color, flush);
00133 
00134   GMem g_mem;
00135   OCCQueryEngine *OQE = OCCQueryEngine::instance();
00136   double tol = OQE->get_sme_resabs_tolerance();
00137 
00138   GProp_GProps myProps;
00139   BRepGProp::LinearProperties(*EDGE_ptr, myProps);
00140   if( myProps.Mass() < tol)
00141     return CUBIT_SUCCESS;
00142   // get the graphics
00143   CubitStatus stat;
00144   stat = OQE->get_graphics( EDGE_ptr, &g_mem );
00145 
00146   if (stat==CUBIT_FAILURE )
00147   {
00148     PRINT_ERROR("Unable to tessellate a curve for display\n" );
00149     return CUBIT_FAILURE;
00150   }
00151   else
00152   {
00153     // Draw the polyline
00154     GfxPreview::draw_polyline( g_mem.point_list(), g_mem.pointListCount, color );
00155   }
00156 
00157   if( flush )
00158     GfxPreview::flush();
00159   return CUBIT_SUCCESS;
00160 }
00161 
00162 CubitStatus 
00163 OCCDrawTool::draw_surface( Surface *surface, int color, CubitBoolean tessellate ,CubitBoolean flush)
00164 {
00165   OCCQueryEngine *OQE = OCCQueryEngine::instance();
00166   if( tessellate )
00167   {
00168     GMem g_mem ;
00169     OQE->get_graphics( surface, &g_mem);
00170 
00171     // Draw the triangles
00172     GPoint p[3];
00173     GPoint* plist = g_mem.point_list();
00174     int* facet_list = g_mem.facet_list();
00175     int i, c = 0;
00176     for( i=0; i<g_mem.facet_list_size(); i++ )
00177     {
00178       p[0] = plist[facet_list[++c]];
00179       p[2] = plist[facet_list[++c]];
00180       p[1] = plist[facet_list[++c]];
00181       c++;
00182       GfxPreview::draw_tri( p, color );
00183     }
00184   }
00185   else
00186   {
00187     // Draw curves
00188     DLIList<OCCCurve*> curve_list;
00189     CAST_TO(surface, OCCSurface)->get_curves(curve_list);
00190     int i;
00191     for( i=curve_list.size(); i--; )
00192       draw_curve( (Curve*)(curve_list.get_and_step()), color, flush );
00193   }
00194 
00195   if( flush )
00196     GfxPreview::flush();
00197   return CUBIT_SUCCESS;
00198 }
00199 
00200 CubitStatus 
00201 OCCDrawTool::draw_curve( Curve *curve, int color , CubitBoolean flush )
00202 {
00203   GMem g_mem;
00204   OCCQueryEngine *OQE = OCCQueryEngine::instance();
00205   double tol = OQE->get_sme_resabs_tolerance();
00206   if (curve->get_arc_length() < tol)
00207     return CUBIT_SUCCESS;
00208   // get the graphics
00209   CubitStatus stat;
00210   stat = OQE->get_graphics( curve, &g_mem );
00211 
00212   if (stat==CUBIT_FAILURE )
00213   {
00214     PRINT_ERROR("Unable to tessellate a curve for display\n" );
00215     return CUBIT_FAILURE;
00216   }
00217   else
00218   {
00219     // Draw the polyline
00220     GfxPreview::draw_polyline( g_mem.point_list(), g_mem.pointListCount, color );
00221   }
00222 
00223   if( flush )
00224     GfxPreview::flush();
00225   return CUBIT_SUCCESS;
00226 }
00227 
00228 CubitStatus
00229 OCCDrawTool::draw_FACE( TopoDS_Face *FACE_ptr, int color, 
00230                         CubitBoolean tessellate, 
00231                         CubitBoolean flush )
00232 {
00233   
00234   //OCCQueryEngine *OQE = OCCQueryEngine::instance();
00235   //Surface *surf_ptr = OQE->populate_topology_bridge( *FACE_ptr );
00236 
00237   //CubitStatus stat;
00238   //stat = draw_face(surf_ptr, color, tessellate, flush);
00239 
00240   OCCQueryEngine *OQE = OCCQueryEngine::instance();
00241   if( tessellate )
00242   {
00243     GMem g_mem ;
00244     OQE->get_graphics( FACE_ptr, &g_mem);
00245 
00246     // Draw the triangles
00247     GPoint p[3];
00248     GPoint* plist = g_mem.point_list();
00249     int* facet_list = g_mem.facet_list();
00250     int i, c = 0;
00251     for( i=0; i<g_mem.facet_list_size(); i++ )
00252     {
00253       p[0] = plist[facet_list[++c]];
00254       p[2] = plist[facet_list[++c]];
00255       p[1] = plist[facet_list[++c]];
00256       c++;
00257       GfxPreview::draw_tri( p, color );
00258     }
00259   }
00260 
00261   else
00262   {
00263     TopExp_Explorer Bx;    
00264     for(Bx.Init(*FACE_ptr, TopAbs_EDGE); Bx.More(); Bx.Next())
00265       draw_EDGE( const_cast<TopoDS_Edge*>(&(TopoDS::Edge( Bx.Current() ))), color, flush );   
00266   }
00267 
00268   if( flush )
00269     GfxPreview::flush();
00270   return CUBIT_SUCCESS;
00271 }
00272 
00273 CubitStatus
00274 OCCDrawTool::draw_VERTEX( TopoDS_Vertex *VERTEX_ptr, int color, CubitBoolean flush )
00275 {
00276   gp_Pnt pt = BRep_Tool::Pnt(*VERTEX_ptr);
00277   GfxPreview::draw_point( pt.X(), pt.Y(), pt.Z(), color );
00278   return CUBIT_SUCCESS;
00279 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines