1 // TriangulationDoc.cpp : implementation of the CTriangulationDoc class
6 #include "TriangulationDoc.h"
8 #include "TriangulationApp.h"
10 #include "OCCDemo_Presentation.h"
12 #include <OCC_3dView.h>
14 #include "..\res\resource.h"
16 #include <AIS_ListOfInteractive.hxx>
17 #include <AIS_ListIteratorOfListOfInteractive.hxx>
19 /////////////////////////////////////////////////////////////////////////////
22 IMPLEMENT_DYNCREATE(CTriangulationDoc, CDocument)
24 BEGIN_MESSAGE_MAP(CTriangulationDoc, OCC_3dBaseDoc)
25 //{{AFX_MSG_MAP(CTriangulationDoc)
26 ON_COMMAND(ID_TRIANGU, OnTriangu)
27 ON_COMMAND(ID_Clear, OnClear)
28 ON_COMMAND(ID_Visu, OnVisu)
29 ON_COMMAND(ID_BUTTONNext, OnBUTTONNext)
30 ON_COMMAND(ID_BUTTONStart, OnBUTTONStart)
31 ON_COMMAND(ID_BUTTONRepeat, OnBUTTONRepeat)
32 ON_COMMAND(ID_BUTTONPrev, OnBUTTONPrev)
33 ON_COMMAND(ID_BUTTONEnd, OnBUTTONEnd)
34 ON_UPDATE_COMMAND_UI(ID_BUTTONNext, OnUpdateBUTTONNext)
35 ON_UPDATE_COMMAND_UI(ID_BUTTONPrev, OnUpdateBUTTONPrev)
36 ON_COMMAND(ID_FILE_NEW, OnFileNew)
37 ON_COMMAND(ID_DUMP_VIEW, OnDumpView)
41 /////////////////////////////////////////////////////////////////////////////
42 // CTriangulationDoc construction/destruction
44 CTriangulationDoc::CTriangulationDoc()
46 myPresentation = OCCDemo_Presentation::Current;
47 myPresentation->SetDocument(this);
49 strcpy_s(myDataDir, "Data");
50 strcpy_s(myLastPath, ".");
53 CTriangulationDoc::~CTriangulationDoc()
57 /////////////////////////////////////////////////////////////////////////////
58 // CTriangulationDoc diagnostics
61 void CTriangulationDoc::AssertValid() const
63 CDocument::AssertValid();
66 void CTriangulationDoc::Dump(CDumpContext& dc) const
72 void CTriangulationDoc::OnTriangu()
74 AIS_ListOfInteractive aList;
75 myAISContext->DisplayedObjects(aList);
76 AIS_ListIteratorOfListOfInteractive aListIterator;
77 for(aListIterator.Initialize(aList);aListIterator.More();aListIterator.Next()){
78 myAISContext->Remove (aListIterator.Value(), Standard_False);
81 TopoDS_Shape theBox = BRepPrimAPI_MakeBox(200,60,60);
82 TopoDS_Shape theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(100,20,20),80);
83 TopoDS_Shape ShapeFused = BRepAlgoAPI_Fuse(theSphere,theBox);
84 BRepMesh_IncrementalMesh(ShapeFused,1);
86 Handle (AIS_Shape) aSection = new AIS_Shape(ShapeFused);
87 myAISContext->SetDisplayMode (aSection, 1, Standard_False);
88 myAISContext->SetColor (aSection, Quantity_NOC_RED, Standard_False);
89 myAISContext->SetMaterial (aSection, Graphic3d_NOM_GOLD, Standard_False);
90 myAISContext->Display (aSection, Standard_False);
92 Standard_Integer result(0);
94 for (TopExp_Explorer ex(ShapeFused,TopAbs_FACE) ; ex.More(); ex.Next()) {
95 TopoDS_Face F =TopoDS::Face(ex.Current());
97 Handle (Poly_Triangulation) facing = BRep_Tool::Triangulation(F,L);
98 result = result + facing->NbTriangles();
102 TCollection_AsciiString Message ("\
104 TopoDS_Shape theBox = BRepPrimAPI_MakeBox(200,60,60); \n\
105 TopoDS_Shape theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(100,20,20),80); \n\
107 TopoDS_Shape ShapeFused = BRepBuilderAPI_Fuse(theSphere,theBox); \n\
109 BRepMesh::Mesh(ShapeFused,1); \n\
111 Standard_Integer result(0); \n\
113 for (TopExp_Explorer ex(ShapeFused,TopAbs_FACE) ; ex.More(); ex.Next()) { \n\
114 TopoDS_Face F =TopoDS::Face(ex.Current()); \n\
115 TopLoc_Location L; \n\
116 Handle (Poly_Triangulation) facing = BRep_Tool::Triangulation(F,L); \n\
117 result = result + facing->NbTriangles(); \n\
120 --- Number of created triangles ---\n");
121 TCollection_AsciiString nombre(result);
126 PocessTextInDialog("Compute the triangulation on a shape", Message);
129 void CTriangulationDoc::OnVisu()
132 AIS_ListOfInteractive aList;
133 myAISContext->DisplayedObjects(aList);
134 AIS_ListIteratorOfListOfInteractive aListIterator;
135 for(aListIterator.Initialize(aList);aListIterator.More();aListIterator.Next()){
136 myAISContext->Remove (aListIterator.Value(), Standard_False);
139 TopoDS_Shape theBox = BRepPrimAPI_MakeBox(200,60,60).Shape();
140 TopoDS_Shape theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(100, 20, 20), 80).Shape();
141 TopoDS_Shape ShapeFused = BRepAlgoAPI_Fuse(theSphere, theBox).Shape();
142 BRepMesh_IncrementalMesh(ShapeFused,1);
144 Handle (AIS_Shape) aSection = new AIS_Shape(ShapeFused);
145 myAISContext->SetDisplayMode (aSection, 1, Standard_False);
146 myAISContext->SetColor (aSection, Quantity_NOC_RED, Standard_False);
147 myAISContext->SetMaterial (aSection, Graphic3d_NOM_GOLD, Standard_False);
148 myAISContext->SetTransparency (aSection, 0.1, Standard_False);
149 myAISContext->Display (aSection, Standard_False);
151 BRep_Builder builder;
152 TopoDS_Compound Comp;
153 builder.MakeCompound(Comp);
155 for (TopExp_Explorer ex(ShapeFused,TopAbs_FACE) ; ex.More(); ex.Next()) {
157 TopoDS_Face F =TopoDS::Face(ex.Current());
159 Handle (Poly_Triangulation) facing = BRep_Tool::Triangulation(F,L);
160 TColgp_Array1OfPnt tab(1,(facing->NbNodes()));
161 tab = facing->Nodes();
162 Poly_Array1OfTriangle tri(1,facing->NbTriangles());
163 tri = facing->Triangles();
165 for (Standard_Integer i=1;i<=(facing->NbTriangles());i++) {
166 Poly_Triangle trian = tri.Value(i);
167 Standard_Integer index1,index2,index3,M = 0, N = 0;
168 trian.Get(index1,index2,index3);
170 for (Standard_Integer j=1;j<=3;j++) {
183 BRepBuilderAPI_MakeEdge ME(tab.Value(M),tab.Value(N));
185 builder.Add(Comp,ME.Edge());
190 Handle (AIS_Shape) atriangulation = new AIS_Shape(Comp);
191 myAISContext->SetDisplayMode (atriangulation, 0, Standard_False);
192 myAISContext->SetColor (atriangulation, Quantity_NOC_WHITE, Standard_False);
193 myAISContext->Display (atriangulation, Standard_False);
197 TCollection_AsciiString Message ("\
199 TopoDS_Shape theBox = BRepPrimAPI_MakeBox(200,60,60); \n\
200 TopoDS_Shape theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(100,20,20),80); \n\
201 TopoDS_Shape ShapeFused = BRepAlgoAPI_Fuse(theSphere,theBox); \n\
202 BRepMesh::Mesh(ShapeFused,1); \n\
204 BRep_Builder builder; \n\
205 TopoDS_Compound Comp; \n\
206 builder.MakeCompound(Comp); \n\
208 for (TopExp_Explorer ex(ShapeFused,TopAbs_FACE) ; ex.More(); ex.Next()) { \n\
210 TopoDS_Face F =TopoDS::Face(ex.Current()); \n\
211 TopLoc_Location L; \n\
212 Handle (Poly_Triangulation) facing = BRep_Tool::Triangulation(F,L); \n\
213 TColgp_Array1OfPnt tab(1,(facing->NbNodes())); \n\
214 tab = facing->Nodes(); \n\
215 Poly_Array1OfTriangle tri(1,facing->NbTriangles()); \n\
216 tri = facing->Triangles(); \n\
218 for (Standard_Integer i=1;i<=(facing->NbTriangles());i++) { \n\
219 Poly_Triangle trian = tri.Value(i); \n\
220 Standard_Integer index1,index2,index3,M,N; \n\
221 trian.Get(index1,index2,index3); \n\
223 for (Standard_Integer j=1;j<=3;j++) { \n\
236 BRepBuilderAPI_MakeEdge ME(tab.Value(M),tab.Value(N)); \n\
237 if (ME.IsDone()) { \n\
238 builder.Add(Comp,ME.Edge()); \n\
244 Warning : The visualisation of the mesh is not optimised.\n\
245 The shared edges between triangles are dispayed twice.\n\
246 The purpose here is only to show how to decode the data structure of triangulation.\n\
248 PocessTextInDialog("Visualize the triangulation on a shape", Message);
255 void CTriangulationDoc::OnClear()
257 AIS_ListOfInteractive aList;
258 myAISContext->DisplayedObjects(aList);
259 AIS_ListIteratorOfListOfInteractive aListIterator;
260 for(aListIterator.Initialize(aList);aListIterator.More();aListIterator.Next()){
261 myAISContext->Remove (aListIterator.Value(), Standard_False);
264 TopoDS_Shape theBox = BRepPrimAPI_MakeBox(200, 60, 60).Shape();
265 TopoDS_Shape theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(100, 20, 20), 80).Shape();
266 TopoDS_Shape ShapeFused = BRepAlgoAPI_Fuse(theSphere,theBox);
267 BRepMesh_IncrementalMesh(ShapeFused,1);
270 Handle (AIS_Shape) aSection = new AIS_Shape(ShapeFused);
271 myAISContext->SetDisplayMode (aSection, 1, Standard_False);
272 myAISContext->SetColor (aSection, Quantity_NOC_RED, Standard_False);
273 myAISContext->SetMaterial (aSection, Graphic3d_NOM_GOLD, Standard_False);
274 myAISContext->Display (aSection, Standard_False);
276 BRepTools::Clean(ShapeFused);
278 TCollection_AsciiString test;
279 if (!BRepTools::Triangulation(ShapeFused,1)) {
280 test = ("In fact the triangulation has been removed\n");
284 TCollection_AsciiString Message ("\
286 TopoDS_Shape theBox = BRepPrimAPI_MakeBox(200,60,60); \n\
287 TopoDS_Shape theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(100,20,20),80); \n\
288 TopoDS_Shape ShapeFused = BRepAlgoAPI_Fuse(theSphere,theBox); \n\
289 BRepMesh::Mesh(ShapeFused,1); \n\
291 BRepTools::Clean(ShapeFused); \n\
293 if (!BRepTools::Triangulation(ShapeFused,1)) { \n\
294 TCollection_AsciiString test(<In fact the triangulation has been removed>); \n\
304 PocessTextInDialog("Remove the triangulation", Message);
308 /*******************************************************************************
309 ********************* T E S S E L A T E **************************************
310 *******************************************************************************/
312 void CTriangulationDoc::Start()
314 myPresentation->Init();
318 void CTriangulationDoc::OnFileNew()
324 void CTriangulationDoc::InitViewButtons()
326 //POSITION pos = GetFirstViewPosition();
330 COCCDemoView* pView = (COCCDemoView*) GetNextView(pos);
331 pView->InitButtons();
336 void CTriangulationDoc::DoSample()
340 HCURSOR hOldCursor = ::GetCursor();
341 HCURSOR hNewCursor = AfxGetApp()->LoadStandardCursor(IDC_APPSTARTING);
343 SetCursor(hNewCursor);
347 myPresentation->DoSample();
349 catch (Standard_Failure const& anException)
351 Standard_SStream aSStream;
352 aSStream << "An exception was caught: " << anException << std::ends;
353 CString aMsg = aSStream.str().c_str();
354 // aSStream.rdbuf()->freeze(0); // allow deletion of dynamic array
355 AfxMessageBox (aMsg);
358 SetCursor(hOldCursor);
361 void CTriangulationDoc::OnBUTTONStart()
363 myAISContext->EraseAll (Standard_True);
364 myPresentation->FirstSample();
368 void CTriangulationDoc::OnBUTTONEnd()
370 myAISContext->EraseAll (Standard_True);
371 myPresentation->LastSample();
375 void CTriangulationDoc::OnBUTTONRepeat()
380 void CTriangulationDoc::OnBUTTONNext()
382 if (!myPresentation->AtLastSample())
384 myPresentation->NextSample();
389 void CTriangulationDoc::OnBUTTONPrev()
391 if (!myPresentation->AtFirstSample())
393 myPresentation->PrevSample();
398 void CTriangulationDoc::OnUpdateBUTTONNext(CCmdUI* pCmdUI)
400 pCmdUI->Enable (!myPresentation->AtLastSample());
403 void CTriangulationDoc::OnUpdateBUTTONPrev(CCmdUI* pCmdUI)
405 pCmdUI->Enable (!myPresentation->AtFirstSample());
408 void CTriangulationDoc::OnDumpView()
410 for (POSITION aPos = GetFirstViewPosition(); aPos != NULL;)
412 OCC_3dView* pView = (OCC_3dView* )GetNextView (aPos);
413 pView->UpdateWindow();
416 myViewer->InitActiveViews();
417 Handle(V3d_View) aView = myViewer->ActiveView();
421 void CTriangulationDoc::Fit()
423 CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
424 CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
425 OCC_3dView *pView = (OCC_3dView*)pChild->GetActiveView();