1 // TriangulationDoc.cpp : implementation of the CTriangulationDoc class
2 //
3
4 #include "stdafx.h"
5
6 #include "TriangulationDoc.h"
7
8 #include "TriangulationApp.h"
9
10 #include "OCCDemo_Presentation.h"
11
12 #include <OCC_3dView.h>
13
14 #include "..\res\resource.h"
15
16 #include <AIS_ListOfInteractive.hxx>
17 #include <AIS_ListIteratorOfListOfInteractive.hxx>
18
19 /////////////////////////////////////////////////////////////////////////////
20 // CTriangulationDoc
21
IMPLEMENT_DYNCREATE(CTriangulationDoc,CDocument)22 IMPLEMENT_DYNCREATE(CTriangulationDoc, CDocument)
23
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)
38 //}}AFX_MSG_MAP
39 END_MESSAGE_MAP()
40
41 /////////////////////////////////////////////////////////////////////////////
42 // CTriangulationDoc construction/destruction
43
44 CTriangulationDoc::CTriangulationDoc()
45 {
46 myPresentation = OCCDemo_Presentation::Current;
47 myPresentation->SetDocument(this);
48
49 strcpy_s(myDataDir, "Data");
50 strcpy_s(myLastPath, ".");
51 }
52
~CTriangulationDoc()53 CTriangulationDoc::~CTriangulationDoc()
54 {
55 }
56
57 /////////////////////////////////////////////////////////////////////////////
58 // CTriangulationDoc diagnostics
59
60 #ifdef _DEBUG
AssertValid() const61 void CTriangulationDoc::AssertValid() const
62 {
63 CDocument::AssertValid();
64 }
65
Dump(CDumpContext & dc) const66 void CTriangulationDoc::Dump(CDumpContext& dc) const
67 {
68 CDocument::Dump(dc);
69 }
70 #endif //_DEBUG
71
OnTriangu()72 void CTriangulationDoc::OnTriangu()
73 {
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);
79 }
80
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);
85
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);
91
92 Standard_Integer result(0);
93
94 for (TopExp_Explorer ex(ShapeFused,TopAbs_FACE) ; ex.More(); ex.Next()) {
95 TopoDS_Face F =TopoDS::Face(ex.Current());
96 TopLoc_Location L;
97 Handle (Poly_Triangulation) facing = BRep_Tool::Triangulation(F,L);
98 result = result + facing->NbTriangles();
99 }
100 Fit();
101
102 TCollection_AsciiString Message ("\
103 \n\
104 TopoDS_Shape theBox = BRepPrimAPI_MakeBox(200,60,60); \n\
105 TopoDS_Shape theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(100,20,20),80); \n\
106 \n\
107 TopoDS_Shape ShapeFused = BRepBuilderAPI_Fuse(theSphere,theBox); \n\
108 \n\
109 BRepMesh::Mesh(ShapeFused,1); \n\
110 \n\
111 Standard_Integer result(0); \n\
112 \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\
118 } \n\
119 \n\
120 --- Number of created triangles ---\n");
121 TCollection_AsciiString nombre(result);
122 Message += nombre;
123 Message +=("\
124 \n\
125 \n");
126 PocessTextInDialog("Compute the triangulation on a shape", Message);
127 }
128
OnVisu()129 void CTriangulationDoc::OnVisu()
130 {
131
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);
137 }
138
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);
143
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);
150
151 BRep_Builder builder;
152 TopoDS_Compound Comp;
153 builder.MakeCompound(Comp);
154
155 for (TopExp_Explorer ex(ShapeFused,TopAbs_FACE) ; ex.More(); ex.Next()) {
156
157 TopoDS_Face F =TopoDS::Face(ex.Current());
158 TopLoc_Location L;
159 Handle (Poly_Triangulation) facing = BRep_Tool::Triangulation(F,L);
160
161 for (Standard_Integer i=1;i<=(facing->NbTriangles());i++) {
162 const Poly_Triangle trian = facing->Triangle (i);
163 Standard_Integer index1,index2,index3,M = 0, N = 0;
164 trian.Get(index1,index2,index3);
165
166 for (Standard_Integer j=1;j<=3;j++) {
167 switch (j) {
168 case 1 :
169 M = index1;
170 N = index2;
171 break;
172 case 2 :
173 N = index3;
174 break;
175 case 3 :
176 M = index2;
177 }
178
179 BRepBuilderAPI_MakeEdge ME(facing->Node (M), facing->Node (N));
180 if (ME.IsDone()) {
181 builder.Add(Comp,ME.Edge());
182 }
183 }
184 }
185 }
186 Handle (AIS_Shape) atriangulation = new AIS_Shape(Comp);
187 myAISContext->SetDisplayMode (atriangulation, 0, Standard_False);
188 myAISContext->SetColor (atriangulation, Quantity_NOC_WHITE, Standard_False);
189 myAISContext->Display (atriangulation, Standard_False);
190
191 Fit();
192
193 TCollection_AsciiString Message ("\
194 \n\
195 TopoDS_Shape theBox = BRepPrimAPI_MakeBox(200,60,60); \n\
196 TopoDS_Shape theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(100,20,20),80); \n\
197 TopoDS_Shape ShapeFused = BRepAlgoAPI_Fuse(theSphere,theBox); \n\
198 BRepMesh::Mesh(ShapeFused,1); \n\
199 \n\
200 BRep_Builder builder; \n\
201 TopoDS_Compound Comp; \n\
202 builder.MakeCompound(Comp); \n\
203 \n\
204 for (TopExp_Explorer ex(ShapeFused,TopAbs_FACE) ; ex.More(); ex.Next()) { \n\
205 \n\
206 TopoDS_Face F =TopoDS::Face(ex.Current()); \n\
207 TopLoc_Location L; \n\
208 Handle (Poly_Triangulation) facing = BRep_Tool::Triangulation(F,L); \n\
209 \n\
210 for (Standard_Integer i=1;i<=(facing->NbTriangles());i++) { \n\
211 Poly_Triangle trian = facing->Triangle (i); \n\
212 Standard_Integer index1,index2,index3,M,N; \n\
213 trian.Get(index1,index2,index3); \n\
214 \n\
215 for (Standard_Integer j=1;j<=3;j++) { \n\
216 switch (j) { \n\
217 case 1 : \n\
218 M = index1; \n\
219 N = index2; \n\
220 break; \n\
221 case 2 : \n\
222 N = index3; \n\
223 break; \n\
224 case 3 : \n\
225 M = index2; \n\
226 } \n\
227 \n\
228 BRepBuilderAPI_MakeEdge ME(facing->Node (M),facing->Node (N)); \n\
229 if (ME.IsDone()) { \n\
230 builder.Add(Comp,ME.Edge()); \n\
231 } \n\
232 } \n\
233 } \n\
234 } \n\
235 \n\
236 Warning : The visualisation of the mesh is not optimised.\n\
237 The shared edges between triangles are dispayed twice.\n\
238 The purpose here is only to show how to decode the data structure of triangulation.\n\
239 \n");
240 PocessTextInDialog("Visualize the triangulation on a shape", Message);
241
242 }
243
244
245
246
OnClear()247 void CTriangulationDoc::OnClear()
248 {
249 AIS_ListOfInteractive aList;
250 myAISContext->DisplayedObjects(aList);
251 AIS_ListIteratorOfListOfInteractive aListIterator;
252 for(aListIterator.Initialize(aList);aListIterator.More();aListIterator.Next()){
253 myAISContext->Remove (aListIterator.Value(), Standard_False);
254 }
255
256 TopoDS_Shape theBox = BRepPrimAPI_MakeBox(200, 60, 60).Shape();
257 TopoDS_Shape theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(100, 20, 20), 80).Shape();
258 TopoDS_Shape ShapeFused = BRepAlgoAPI_Fuse(theSphere,theBox);
259 BRepMesh_IncrementalMesh(ShapeFused,1);
260
261
262 Handle (AIS_Shape) aSection = new AIS_Shape(ShapeFused);
263 myAISContext->SetDisplayMode (aSection, 1, Standard_False);
264 myAISContext->SetColor (aSection, Quantity_NOC_RED, Standard_False);
265 myAISContext->SetMaterial (aSection, Graphic3d_NOM_GOLD, Standard_False);
266 myAISContext->Display (aSection, Standard_False);
267
268 BRepTools::Clean(ShapeFused);
269
270 TCollection_AsciiString test;
271 if (!BRepTools::Triangulation(ShapeFused,1)) {
272 test = ("In fact the triangulation has been removed\n");
273 }
274
275 Fit();
276 TCollection_AsciiString Message ("\
277 \n\
278 TopoDS_Shape theBox = BRepPrimAPI_MakeBox(200,60,60); \n\
279 TopoDS_Shape theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(100,20,20),80); \n\
280 TopoDS_Shape ShapeFused = BRepAlgoAPI_Fuse(theSphere,theBox); \n\
281 BRepMesh::Mesh(ShapeFused,1); \n\
282 \n\
283 BRepTools::Clean(ShapeFused); \n\
284 \n\
285 if (!BRepTools::Triangulation(ShapeFused,1)) { \n\
286 TCollection_AsciiString test(<In fact the triangulation has been removed>); \n\
287 } \n\
288 \n\
289 --- Result ---\n");
290
291 Message += test;
292 Message +=("\
293 \n\
294 \n");
295
296 PocessTextInDialog("Remove the triangulation", Message);
297
298 }
299
300 /*******************************************************************************
301 ********************* T E S S E L A T E **************************************
302 *******************************************************************************/
303
Start()304 void CTriangulationDoc::Start()
305 {
306 myPresentation->Init();
307 OnBUTTONStart();
308 }
309
OnFileNew()310 void CTriangulationDoc::OnFileNew()
311 {
312 OnNewDocument();
313 Start();
314 }
315
InitViewButtons()316 void CTriangulationDoc::InitViewButtons()
317 {
318 //POSITION pos = GetFirstViewPosition();
319 /* LLS
320 while (pos != NULL)
321 {
322 COCCDemoView* pView = (COCCDemoView*) GetNextView(pos);
323 pView->InitButtons();
324 }
325 */
326 }
327
DoSample()328 void CTriangulationDoc::DoSample()
329 {
330 InitViewButtons();
331
332 HCURSOR hOldCursor = ::GetCursor();
333 HCURSOR hNewCursor = AfxGetApp()->LoadStandardCursor(IDC_APPSTARTING);
334
335 SetCursor(hNewCursor);
336 {
337 try
338 {
339 myPresentation->DoSample();
340 }
341 catch (Standard_Failure const& anException)
342 {
343 Standard_SStream aSStream;
344 aSStream << "An exception was caught: " << anException << std::ends;
345 CString aMsg = aSStream.str().c_str();
346 // aSStream.rdbuf()->freeze(0); // allow deletion of dynamic array
347 AfxMessageBox (aMsg);
348 }
349 }
350 SetCursor(hOldCursor);
351 }
352
OnBUTTONStart()353 void CTriangulationDoc::OnBUTTONStart()
354 {
355 myAISContext->EraseAll (Standard_True);
356 myPresentation->FirstSample();
357 DoSample();
358 }
359
OnBUTTONEnd()360 void CTriangulationDoc::OnBUTTONEnd()
361 {
362 myAISContext->EraseAll (Standard_True);
363 myPresentation->LastSample();
364 DoSample();
365 }
366
OnBUTTONRepeat()367 void CTriangulationDoc::OnBUTTONRepeat()
368 {
369 DoSample();
370 }
371
OnBUTTONNext()372 void CTriangulationDoc::OnBUTTONNext()
373 {
374 if (!myPresentation->AtLastSample())
375 {
376 myPresentation->NextSample();
377 DoSample();
378 }
379 }
380
OnBUTTONPrev()381 void CTriangulationDoc::OnBUTTONPrev()
382 {
383 if (!myPresentation->AtFirstSample())
384 {
385 myPresentation->PrevSample();
386 DoSample();
387 }
388 }
389
OnUpdateBUTTONNext(CCmdUI * pCmdUI)390 void CTriangulationDoc::OnUpdateBUTTONNext(CCmdUI* pCmdUI)
391 {
392 pCmdUI->Enable (!myPresentation->AtLastSample());
393 }
394
OnUpdateBUTTONPrev(CCmdUI * pCmdUI)395 void CTriangulationDoc::OnUpdateBUTTONPrev(CCmdUI* pCmdUI)
396 {
397 pCmdUI->Enable (!myPresentation->AtFirstSample());
398 }
399
OnDumpView()400 void CTriangulationDoc::OnDumpView()
401 {
402 for (POSITION aPos = GetFirstViewPosition(); aPos != NULL;)
403 {
404 OCC_3dView* pView = (OCC_3dView* )GetNextView (aPos);
405 pView->UpdateWindow();
406 }
407
408 Handle(V3d_View) aView = myViewer->ActiveViews().First();
409 ExportView (aView);
410 }
411
Fit()412 void CTriangulationDoc::Fit()
413 {
414 CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
415 CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
416 OCC_3dView *pView = (OCC_3dView*)pChild->GetActiveView();
417 pView->FitAll();
418 }
419