0031547: Visualization - V3d_View creates V3d_Trihedron instance
[occt.git] / samples / mfc / standard / 01_Geometry / src / GeometryDoc.cpp
1 // GeometryDoc.cpp : implementation of the CGeometryDoc class
2 //
3
4 #include "stdafx.h"
5
6 #include "GeometryDoc.h"
7 #include "GeoAlgo_Sol.hxx"
8 #include "GeometryApp.h"
9 #include "GeometryView2D.h"
10 #include "GeometryView.h"
11
12 #ifdef _DEBUG
13 #undef THIS_FILE
14 static char THIS_FILE[] = __FILE__;
15 #endif
16 #include "GeomSources.h"
17
18 #define EOL "\r\n"
19
20 #define WAIT_A_LITTLE WaitForInput(500)
21
22 #define MAX_PARAM 1000 // if a surface parameter is infinite, it is assigned
23 // this value in order to display the "infinit" object in the viewer.
24
25 /////////////////////////////////////////////////////////////////////////////
26 // CGeometryDoc
27
28 IMPLEMENT_DYNCREATE(CGeometryDoc, OCC_3dBaseDoc)
29
30 BEGIN_MESSAGE_MAP(CGeometryDoc, OCC_3dBaseDoc)
31   //{{AFX_MSG_MAP(CGeometryDoc)
32   ON_COMMAND(ID_WINDOW_NEW2D, OnWindowNew2d)
33   ON_COMMAND(ID_BUTTON_Test_1, OnBUTTONTest1)
34   ON_COMMAND(ID_BUTTON_Test_2, OnBUTTONTest2)
35   ON_COMMAND(ID_BUTTON_Test_3, OnBUTTONTest3)
36   ON_COMMAND(ID_BUTTON_Test_4, OnBUTTONTest4)
37   ON_COMMAND(ID_BUTTON_Test_5, OnBUTTONTest5)
38   ON_COMMAND(ID_BUTTON_Test_6, OnBUTTONTest6)
39   ON_COMMAND(ID_BUTTON_Test_7, OnBUTTONTest7)
40   ON_COMMAND(ID_BUTTON_Test_8, OnBUTTONTest8)
41   ON_COMMAND(ID_BUTTON_Test_9, OnBUTTONTest9)
42   ON_COMMAND(ID_BUTTON_Test_23, OnBUTTONTest23)
43   ON_COMMAND(ID_BUTTON_Test_22, OnBUTTONTest22)
44   ON_COMMAND(ID_BUTTON_Test_10, OnBUTTONTest10)
45   ON_COMMAND(ID_BUTTON_Test_11, OnBUTTONTest11)
46   ON_COMMAND(ID_BUTTON_Test_12, OnBUTTONTest12)
47   ON_COMMAND(ID_BUTTON_Test_13, OnBUTTONTest13)
48   ON_COMMAND(ID_BUTTON_Test_14, OnBUTTONTest14)
49   ON_COMMAND(ID_BUTTON_Test_15, OnBUTTONTest15)
50   ON_COMMAND(ID_BUTTON_Test_16, OnBUTTONTest16)
51   ON_COMMAND(ID_BUTTON_Test_17, OnBUTTONTest17)
52   ON_COMMAND(ID_BUTTON_Test_18, OnBUTTONTest18)
53   ON_COMMAND(ID_BUTTON_Test_19, OnBUTTONTest19)
54   ON_COMMAND(ID_BUTTON_Test_20, OnBUTTONTest20)
55   ON_COMMAND(ID_BUTTON_Test_21, OnBUTTONTest21)
56   ON_COMMAND(ID_BUTTON_Test_24, OnBUTTONTest24)
57   ON_COMMAND(ID_BUTTON_Test_25, OnBUTTONTest25)
58   ON_COMMAND(ID_BUTTON_Test_26, OnBUTTONTest26)
59   ON_COMMAND(ID_BUTTON_Test_27, OnBUTTONTest27)
60   ON_COMMAND(ID_BUTTON_Test_28, OnBUTTONTest28)
61   ON_COMMAND(ID_BUTTON_Test_29, OnBUTTONTest29)
62   ON_COMMAND(ID_BUTTON_Test_30, OnBUTTONTest30)
63   ON_COMMAND(ID_BUTTON_Test_31, OnBUTTONTest31)
64   ON_COMMAND(ID_BUTTON_Test_32, OnBUTTONTest32)
65   ON_COMMAND(ID_BUTTON_Test_33, OnBUTTONTest33)
66   ON_COMMAND(ID_BUTTON_Test_34, OnBUTTONTest34)
67   ON_COMMAND(ID_BUTTON_Test_35, OnBUTTONTest35)
68   ON_COMMAND(ID_BUTTON_Test_36, OnBUTTONTest36)
69   ON_COMMAND(ID_BUTTON_Test_37, OnBUTTONTest37)
70   ON_COMMAND(ID_BUTTON_Test_38, OnBUTTONTest38)
71   ON_COMMAND(ID_BUTTON_Test_39, OnBUTTONTest39)
72   ON_COMMAND(ID_BUTTON_Test_40, OnBUTTONTest40)
73   ON_COMMAND(ID_BUTTON_Test_41, OnBUTTONTest41)
74   ON_COMMAND(ID_BUTTON_Test_42, OnBUTTONTest42)
75   ON_COMMAND(ID_BUTTON_Test_43, OnBUTTONTest43)
76   ON_COMMAND(ID_BUTTON_Test_44, OnBUTTONTest44)
77   ON_COMMAND(ID_BUTTON_Test_45, OnBUTTONTest45)
78   ON_COMMAND(ID_BUTTON_Test_46, OnBUTTONTest46)
79   ON_COMMAND(ID_BUTTON_Test_47, OnBUTTONTest47)
80   ON_COMMAND(ID_BUTTON_Test_48, OnBUTTONTest48)
81   ON_COMMAND(ID_BUTTON_Test_49, OnBUTTONTest49)
82   ON_COMMAND(ID_BUTTON_Test_50, OnBUTTONTest50)
83   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_1  , OnUpdateBUTTONTest1  )
84   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_2  , OnUpdateBUTTONTest2  )
85   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_3  , OnUpdateBUTTONTest3  )
86   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_4  , OnUpdateBUTTONTest4  )
87   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_5  , OnUpdateBUTTONTest5  )
88   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_6  , OnUpdateBUTTONTest6  )
89   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_7  , OnUpdateBUTTONTest7  )
90   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_8  , OnUpdateBUTTONTest8  )
91   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_9  , OnUpdateBUTTONTest9  )
92   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_10 , OnUpdateBUTTONTest10 )
93   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_11 , OnUpdateBUTTONTest11 )
94   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_12 , OnUpdateBUTTONTest12 )
95   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_13 , OnUpdateBUTTONTest13 )
96   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_14 , OnUpdateBUTTONTest14 ) 
97   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_15 , OnUpdateBUTTONTest15 )
98   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_16 , OnUpdateBUTTONTest16 )
99   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_17 , OnUpdateBUTTONTest17 )
100   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_18 , OnUpdateBUTTONTest18 )
101   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_19 , OnUpdateBUTTONTest19 )
102   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_20 , OnUpdateBUTTONTest20 )
103   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_21 , OnUpdateBUTTONTest21 )
104   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_22 , OnUpdateBUTTONTest22 )
105   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_23 , OnUpdateBUTTONTest23 )
106   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_24 , OnUpdateBUTTONTest24 )
107   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_25 , OnUpdateBUTTONTest25 )
108   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_26 , OnUpdateBUTTONTest26 )
109   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_27 , OnUpdateBUTTONTest27 )
110   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_28 , OnUpdateBUTTONTest28 )
111   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_29 , OnUpdateBUTTONTest29 )
112   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_30 , OnUpdateBUTTONTest30 )
113   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_31 , OnUpdateBUTTONTest31 )
114   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_32 , OnUpdateBUTTONTest32 )
115   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_33 , OnUpdateBUTTONTest33 )
116   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_34 , OnUpdateBUTTONTest34 )
117   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_35 , OnUpdateBUTTONTest35 )
118   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_36 , OnUpdateBUTTONTest36 )
119   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_37 , OnUpdateBUTTONTest37 )
120   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_38 , OnUpdateBUTTONTest38 )
121   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_39 , OnUpdateBUTTONTest39 )
122   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_40 , OnUpdateBUTTONTest40 )
123   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_41 , OnUpdateBUTTONTest41 )
124   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_42 , OnUpdateBUTTONTest42 )
125   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_43 , OnUpdateBUTTONTest43 )
126   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_44 , OnUpdateBUTTONTest44 )
127   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_45 , OnUpdateBUTTONTest45 )
128   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_46 , OnUpdateBUTTONTest46 )
129   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_47 , OnUpdateBUTTONTest47 )
130   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_48 , OnUpdateBUTTONTest48 )
131   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_49 , OnUpdateBUTTONTest49 )
132   ON_UPDATE_COMMAND_UI(ID_BUTTON_Test_50 , OnUpdateBUTTONTest50 )
133   ON_COMMAND(ID_Create_Sol, OnCreateSol)
134   ON_COMMAND(ID_BUTTON_Simplify, OnSimplify)
135   //}}AFX_MSG_MAP
136 END_MESSAGE_MAP()
137
138 /////////////////////////////////////////////////////////////////////////////
139 // CGeometryDoc construction/destruction
140
141 CGeometryDoc::CGeometryDoc()
142 : OCC_3dBaseDoc()
143 {
144   FitMode = false;
145   AfxInitRichEdit();
146
147   myAISContext->DefaultDrawer()->UIsoAspect()->SetNumber(11);
148   myAISContext->DefaultDrawer()->VIsoAspect()->SetNumber(11);
149
150   Handle(Graphic3d_GraphicDriver) aGraphicDriver = ((OCC_App*)AfxGetApp())->GetGraphicDriver();
151   myViewer2D = new V3d_Viewer (aGraphicDriver);
152   myViewer2D->SetCircularGridValues(0,0,1,8,0);
153   myViewer2D->SetRectangularGridValues(0,0,1,1,0);
154
155   // Set view projection
156   myViewer2D->SetDefaultViewProj(V3d_Zpos);
157   myAISContext2D = new AIS_InteractiveContext(myViewer2D);      
158   myCResultDialog.Create(CResultDialog::IDD,NULL);
159
160   RECT dlgrect;
161   myCResultDialog.GetWindowRect(&dlgrect);
162   LONG width = dlgrect.right-dlgrect.left;
163   LONG height = dlgrect.bottom-dlgrect.top;
164   RECT MainWndRect;
165   AfxGetApp()->m_pMainWnd->GetWindowRect(&MainWndRect);
166   LONG left = MainWndRect.left+3;
167   LONG top = MainWndRect.top + 138;
168   myCResultDialog.MoveWindow(left,top,width,height);
169
170   ((CGeometryApp*)AfxGetApp())->CreateView2D(this);     
171   Minimize2D();
172   Put3DOnTop();
173 }
174
175 CGeometryDoc::~CGeometryDoc()
176 {
177 }
178
179 BOOL CGeometryDoc::OnNewDocument()
180 {
181   if (!CDocument::OnNewDocument())
182     return FALSE;
183
184   // TODO: add reinitialization code here
185   // (SDI documents will reuse this document)
186   // compute a graphic device --> the same for all Views
187
188   return TRUE;
189 }
190
191 void CGeometryDoc::OnWindowNew2d()
192 {
193   ((CGeometryApp*)AfxGetApp())->CreateView2D(this);
194 }
195
196 /////////////////////////////////////////////////////////////////////////////
197 // CGeometryDoc serialization
198
199 void CGeometryDoc::Serialize(CArchive& ar)
200 {
201   if (ar.IsStoring())
202   {
203     // TODO: add storing code here
204   }
205   else
206   {
207     // TODO: add loading code here
208   }
209 }
210
211 /////////////////////////////////////////////////////////////////////////////
212 // CGeometryDoc diagnostics
213
214 #ifdef _DEBUG
215 void CGeometryDoc::AssertValid() const
216 {
217   CDocument::AssertValid();
218 }
219
220 void CGeometryDoc::Dump(CDumpContext& dc) const
221 {
222   CDocument::Dump(dc);
223 }
224 #endif //_DEBUG
225
226 /////////////////////////////////////////////////////////////////////////////
227 // CGeometryDoc commands
228
229 //-----------------------------------------------------------------------------------------
230 //
231 //-----------------------------------------------------------------------------------------
232 void CGeometryDoc::DragEvent2D(const Standard_Integer /*x*/,
233                                const Standard_Integer /*y*/,
234                                const Standard_Integer /*TheState*/,
235                                const Handle(V3d_View)& /*aView*/)
236 {
237 }
238
239
240 //-----------------------------------------------------------------------------------------
241 //
242 //-----------------------------------------------------------------------------------------
243 void CGeometryDoc::InputEvent2D(const Standard_Integer /*x*/,
244                                 const Standard_Integer /*y*/,
245                                 const Handle(V3d_View)& /*aView*/)
246 {
247   myAISContext2D->SelectDetected();
248   myAISContext2D->UpdateCurrentViewer();
249 }
250
251 //-----------------------------------------------------------------------------------------
252 //
253 //-----------------------------------------------------------------------------------------
254 void CGeometryDoc::MoveEvent2D(const Standard_Integer x,
255                                const Standard_Integer y,
256                                const Handle(V3d_View)& aView)
257 {
258   if (aView->Viewer()->IsGridActive())
259   {
260     Standard_Real aGridX=0,aGridY=0,aGridZ=0;
261     aView->ConvertToGrid(x,y,aGridX,aGridY,aGridZ);
262     //View is not updated automatically in ConvertToGrid
263     aView->Update();
264   }
265   this->myAISContext2D->MoveTo (x, y, aView, Standard_True);
266 }
267
268 //-----------------------------------------------------------------------------------------
269 //
270 //-----------------------------------------------------------------------------------------
271 void CGeometryDoc::ShiftMoveEvent2D(const Standard_Integer /*x*/,
272                                     const Standard_Integer /*y*/,
273                                     const Handle(V3d_View)& /*aView*/)
274 {
275 }
276
277 //-----------------------------------------------------------------------------------------
278 //
279 //-----------------------------------------------------------------------------------------
280 void CGeometryDoc::ShiftDragEvent2D(const Standard_Integer /*x*/,
281                                     const Standard_Integer /*y*/,
282                                     const Standard_Integer /*TheState*/,
283                                     const Handle(V3d_View)& /*aView*/)
284 {
285 }
286
287
288 //-----------------------------------------------------------------------------------------
289 //
290 //-----------------------------------------------------------------------------------------
291 void CGeometryDoc::ShiftInputEvent2D(const Standard_Integer /*x*/,
292                                      const Standard_Integer /*y*/,
293                                      const Handle(V3d_View)& /*aView*/)
294 {
295 }
296
297 //-----------------------------------------------------------------------------------------
298 //
299 //-----------------------------------------------------------------------------------------
300 void  CGeometryDoc::Popup2D(const Standard_Integer x,
301                             const Standard_Integer y,
302                             const Handle(V3d_View)& aView)
303 {
304   CMenu menu;
305   VERIFY(menu.LoadMenu(IDR_Popup3D));
306   CMenu* pPopup; 
307
308   pPopup = menu.GetSubMenu(0);
309
310   ASSERT(pPopup != NULL);
311
312   POINT winCoord = { x , y };
313   Handle(WNT_Window) aWNTWindow=
314     Handle(WNT_Window)::DownCast(aView->Window());
315   ClientToScreen ( (HWND)(aWNTWindow->HWindow()),&winCoord);
316   pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON , winCoord.x, winCoord.y , 
317     AfxGetMainWnd());
318 }
319
320
321 //-----------------------------------------------------------------------------------------
322 //
323 //-----------------------------------------------------------------------------------------
324 void  CGeometryDoc::Popup (const Standard_Integer theMouseX,
325                            const Standard_Integer theMouseY,
326                            const Handle(V3d_View)& theView) 
327
328   Standard_Integer PopupMenuNumber=0;
329   GetAISContext()->InitSelected();
330   if (GetAISContext()->MoreSelected())
331   {
332     PopupMenuNumber=1;
333   }
334
335   CMenu menu;
336   VERIFY(menu.LoadMenu(IDR_Popup3D));
337   CMenu* pPopup = menu.GetSubMenu(PopupMenuNumber);
338   ASSERT(pPopup != NULL);
339
340   POINT winCoord = { theMouseX , theMouseY };
341   Handle(WNT_Window) aWNTWindow = Handle(WNT_Window)::DownCast(theView->Window());
342   ClientToScreen ( (HWND)(aWNTWindow->HWindow()), &winCoord );
343
344   pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON ,
345                          winCoord.x, winCoord.y , 
346                          AfxGetMainWnd());
347 }
348
349 //-----------------------------------------------------------------------------------------
350 //
351 //-----------------------------------------------------------------------------------------
352 void CGeometryDoc::InputEvent (const Standard_Integer /*theMouseX*/,
353                                 const Standard_Integer /*theMouseY*/,
354                                 const Handle(V3d_View)& /*theView*/)
355 {
356   myAISContext->SelectDetected();
357   myAISContext2D->UpdateCurrentViewer();
358 }
359
360 //-----------------------------------------------------------------------------------------
361 //
362 //-----------------------------------------------------------------------------------------
363 void CGeometryDoc::Put2DOnTop(bool isMax)
364 {
365   POSITION position = GetFirstViewPosition();
366   while (position != (POSITION)NULL)
367   {
368     CView* pCurrentView = (CView*)GetNextView(position);
369     if(pCurrentView->IsKindOf( RUNTIME_CLASS( CGeometryView2D ) ) )
370     {
371       ASSERT_VALID(pCurrentView);
372       CFrameWnd* pParentFrm = pCurrentView->GetParentFrame();
373       ASSERT(pParentFrm != (CFrameWnd *)NULL);
374       // simply make the frame window visible
375       if(isMax)
376       {
377         pParentFrm->ActivateFrame(SW_SHOWMAXIMIZED);
378       }
379       else
380       {
381         pParentFrm->ActivateFrame(SW_SHOW);
382       }
383     }
384   }
385 }
386
387 //-----------------------------------------------------------------------------------------
388 //
389 //-----------------------------------------------------------------------------------------
390 void CGeometryDoc::Minimize2D()
391 {
392   POSITION position = GetFirstViewPosition();
393   while (position != (POSITION)NULL)
394   {
395     CView* pCurrentView = (CView*)GetNextView(position);
396     if(pCurrentView->IsKindOf( RUNTIME_CLASS( CGeometryView2D ) ) )
397     {
398       ASSERT_VALID(pCurrentView);
399       CFrameWnd* pParentFrm = pCurrentView->GetParentFrame();
400       ASSERT(pParentFrm != (CFrameWnd *)NULL);
401       // simply make the frame window visible
402       pParentFrm->ActivateFrame(SW_HIDE);
403     }
404   }
405 }
406
407 //-----------------------------------------------------------------------------------------
408 //
409 //-----------------------------------------------------------------------------------------
410 void CGeometryDoc::Fit2DViews()
411 {
412   POSITION position = GetFirstViewPosition();
413   while (position != (POSITION)NULL)
414   {
415     CView* pCurrentView = (CView*)GetNextView(position);
416     if(pCurrentView->IsKindOf( RUNTIME_CLASS( CGeometryView2D ) ) )
417     {
418       ASSERT_VALID(pCurrentView);
419       CGeometryView2D* aCGeometryView2D = (CGeometryView2D*)pCurrentView;
420       aCGeometryView2D->FitAll();
421     }
422   }
423 }
424
425 //-----------------------------------------------------------------------------------------
426 //
427 //-----------------------------------------------------------------------------------------
428 void CGeometryDoc::Put3DOnTop(bool isMax)
429 {
430   POSITION position = GetFirstViewPosition();
431   while (position != (POSITION)NULL)
432   {
433     CView* pCurrentView = (CView*)GetNextView(position);
434     if(pCurrentView->IsKindOf( RUNTIME_CLASS( CGeometryView ) ) )
435     {
436       ASSERT_VALID(pCurrentView);
437       CFrameWnd* pParentFrm = pCurrentView->GetParentFrame();
438       ASSERT(pParentFrm != (CFrameWnd *)NULL);
439       // simply make the frame window visible
440       if(isMax)
441       {
442         pParentFrm->ActivateFrame(SW_SHOWMAXIMIZED);
443       }
444       else
445       {
446         pParentFrm->ActivateFrame(SW_SHOW);
447       }
448     }
449   }
450 }
451
452 //-----------------------------------------------------------------------------------------
453 //
454 //-----------------------------------------------------------------------------------------
455 void CGeometryDoc::Minimize3D()
456 {
457   POSITION position = GetFirstViewPosition();
458   while (position != (POSITION)NULL)
459   {
460     CView* pCurrentView = (CView*)GetNextView(position);
461     if(pCurrentView->IsKindOf( RUNTIME_CLASS( CGeometryView ) ) )
462     {
463       ASSERT_VALID(pCurrentView);
464       CFrameWnd* pParentFrm = pCurrentView->GetParentFrame();
465       ASSERT(pParentFrm != (CFrameWnd *)NULL);
466       // simply make the frame window visible
467       pParentFrm->ActivateFrame(SW_HIDE);
468     }
469   }
470 }
471
472 //-----------------------------------------------------------------------------------------
473 //
474 //-----------------------------------------------------------------------------------------
475 void CGeometryDoc::Fit3DViews(Standard_Real Coef)
476 {
477   POSITION position = GetFirstViewPosition();
478   while (position != (POSITION)NULL)
479   {
480     CView* pCurrentView = (CView*)GetNextView(position);
481     if(pCurrentView->IsKindOf( RUNTIME_CLASS( CGeometryView ) ) )
482     {
483       ASSERT_VALID(pCurrentView);
484       CGeometryView* aCGeometryView = (CGeometryView*)pCurrentView;
485       aCGeometryView->FitAll(Coef);
486     }
487   }
488 }
489
490 //-----------------------------------------------------------------------------------------
491 //
492 //-----------------------------------------------------------------------------------------
493 void CGeometryDoc::Set3DViewsZoom(const Standard_Real& Coef)
494 {
495   POSITION position = GetFirstViewPosition();
496   while (position != (POSITION)NULL)
497   {
498     CView* pCurrentView = (CView*)GetNextView(position);
499     if(pCurrentView->IsKindOf( RUNTIME_CLASS( CGeometryView ) ) )
500     {
501       ASSERT_VALID(pCurrentView);
502       CGeometryView* aCGeometryView = (CGeometryView*)pCurrentView;
503       aCGeometryView->SetZoom( Coef );
504     }
505   }
506 }
507
508 //-----------------------------------------------------------------------------------------
509 //                                Buttons event handlers
510 //-----------------------------------------------------------------------------------------
511 void CGeometryDoc::OnBUTTONTest1() 
512 {   Current = 1;
513 GeomSources::gpTest1(this);      }
514 void CGeometryDoc::OnBUTTONTest2() 
515 {   Current = 2;
516 GeomSources::gpTest2(this);      }
517 void CGeometryDoc::OnBUTTONTest3() 
518 {   Current = 3;
519 GeomSources::gpTest3(this);      }
520 void CGeometryDoc::OnBUTTONTest4() 
521 {   Current = 4;
522 GeomSources::gpTest4(this);      }
523 void CGeometryDoc::OnBUTTONTest5() 
524 {   Current = 5;
525 GeomSources::gpTest5(this);        }
526 void CGeometryDoc::OnBUTTONTest6() 
527 {   Current = 6;
528 GeomSources::gpTest6(this);      }
529 void CGeometryDoc::OnBUTTONTest7() 
530 {   Current = 7;
531 GeomSources::gpTest7(this);      }
532 void CGeometryDoc::OnBUTTONTest8() 
533 {   Current = 8;
534 GeomSources::gpTest8(this);      }
535 void CGeometryDoc::OnBUTTONTest9() 
536 {   Current = 9;
537 GeomSources::gpTest9(this);        }
538 void CGeometryDoc::OnBUTTONTest10() 
539 {   Current = 10;
540 GeomSources::gpTest10(this);    }
541 void CGeometryDoc::OnBUTTONTest11() 
542 {   Current = 11;
543 GeomSources::gpTest11(this);    }
544 void CGeometryDoc::OnBUTTONTest12() 
545 {   Current = 12;
546 GeomSources::gpTest12(this);    }
547 void CGeometryDoc::OnBUTTONTest13() 
548 {   Current = 13;
549 GeomSources::gpTest13(this);    }
550 void CGeometryDoc::OnBUTTONTest14() 
551 {   Current = 14;
552 GeomSources::gpTest14(this);    }
553 void CGeometryDoc::OnBUTTONTest15() 
554 {   Current = 15;
555 GeomSources::gpTest15(this);    }
556 void CGeometryDoc::OnBUTTONTest16() 
557 {   Current = 16;
558 GeomSources::gpTest16(this);    }
559 void CGeometryDoc::OnBUTTONTest17() 
560 {   Current = 17;
561 GeomSources::gpTest17(this);    }
562 void CGeometryDoc::OnBUTTONTest18() 
563 {   Current = 18;
564 GeomSources::gpTest18(this);    }
565 void CGeometryDoc::OnBUTTONTest19() 
566 {   Current = 19;
567 GeomSources::gpTest19(this);    }
568 void CGeometryDoc::OnBUTTONTest20() 
569 {   Current = 20;
570 GeomSources::gpTest20(this);    }
571 void CGeometryDoc::OnBUTTONTest21() 
572 {   Current = 21;
573 GeomSources::gpTest21(this);    }
574 void CGeometryDoc::OnBUTTONTest22() 
575 {   Current = 22;
576 GeomSources::gpTest22(this);    }
577 void CGeometryDoc::OnBUTTONTest23() 
578 {   Current = 23;
579 GeomSources::gpTest23(this);    }
580 void CGeometryDoc::OnBUTTONTest24() 
581 {   Current = 24;
582 GeomSources::gpTest24(this);    }
583 void CGeometryDoc::OnBUTTONTest25() 
584 {   Current = 25;
585 GeomSources::gpTest25(this);    }
586 void CGeometryDoc::OnBUTTONTest26() 
587 {   Current = 26;
588 GeomSources::gpTest26(this);    }
589 void CGeometryDoc::OnBUTTONTest27() 
590 {   Current = 27;
591 GeomSources::gpTest27(this);    }
592 void CGeometryDoc::OnBUTTONTest28() 
593 {   Current = 28;
594 GeomSources::gpTest28(this);    }
595 void CGeometryDoc::OnBUTTONTest29() 
596 {   Current = 29;
597 GeomSources::gpTest29(this);    }
598 void CGeometryDoc::OnBUTTONTest30() 
599 {   Current = 30;
600 GeomSources::gpTest30(this);    }
601 void CGeometryDoc::OnBUTTONTest31() 
602 {   Current = 31;
603 GeomSources::gpTest31(this);    }
604 void CGeometryDoc::OnBUTTONTest32() 
605 {   Current = 32;
606 GeomSources::gpTest32(this);  }
607 void CGeometryDoc::OnBUTTONTest33() 
608 {   Current = 33;
609 GeomSources::gpTest33(this);    }
610 void CGeometryDoc::OnBUTTONTest34() 
611 {   Current = 34;
612 GeomSources::gpTest34(this);    }
613 void CGeometryDoc::OnBUTTONTest35() 
614 {   Current = 35;
615 GeomSources::gpTest35(this);    }
616 void CGeometryDoc::OnBUTTONTest36() 
617 {   Current = 36;
618 GeomSources::gpTest36(this);    }
619
620 void CGeometryDoc::OnBUTTONTest37() 
621 {   Current = 37;
622 GeomSources::gpTest37(this);    }
623 void CGeometryDoc::OnBUTTONTest38() 
624 {   Current = 38;
625 GeomSources::gpTest38(this);    }
626 void CGeometryDoc::OnBUTTONTest39() 
627 {   Current = 39;
628 GeomSources::gpTest39(this);    }
629 void CGeometryDoc::OnBUTTONTest40() 
630 {   Current = 40;
631 GeomSources::gpTest40(this);    }
632 void CGeometryDoc::OnBUTTONTest41() 
633 {   Current = 41;
634 GeomSources::gpTest41(this);    }
635 void CGeometryDoc::OnBUTTONTest42() 
636 {   Current = 42;
637 GeomSources::gpTest42(this);    }
638 void CGeometryDoc::OnBUTTONTest43() 
639 {   Current = 43;
640 GeomSources::gpTest43(this);    }
641 void CGeometryDoc::OnBUTTONTest44() 
642 {   Current = 44;
643 GeomSources::gpTest44(this);    }
644 void CGeometryDoc::OnBUTTONTest45() 
645 {   Current = 45;
646 GeomSources::gpTest45(this);    }
647 void CGeometryDoc::OnBUTTONTest46() 
648 {   Current = 46;
649 GeomSources::gpTest46(this);    }
650 void CGeometryDoc::OnBUTTONTest47() 
651 {   Current = 47;
652 GeomSources::gpTest47(this);    }
653 void CGeometryDoc::OnBUTTONTest48() 
654 {   Current = 48;
655 GeomSources::gpTest48(this);    }
656 void CGeometryDoc::OnBUTTONTest49() 
657 {   Current = 49;
658 GeomSources::gpTest49(this);    }
659 void CGeometryDoc::OnBUTTONTest50() 
660 {   Current = 50;
661 GeomSources::gpTest50(this);    }
662
663 void CGeometryDoc::OnUpdateBUTTONTest1(CCmdUI* pCmdUI) 
664 {
665   if (Current == 1)
666     pCmdUI->SetCheck(true);
667   else
668     pCmdUI->SetCheck(false);
669 }
670
671 void CGeometryDoc::OnUpdateBUTTONTest2(CCmdUI* pCmdUI) 
672 {
673   if (Current == 2)
674     pCmdUI->SetCheck(true);
675   else 
676     pCmdUI->SetCheck(false);
677 }
678
679 void CGeometryDoc::OnUpdateBUTTONTest3(CCmdUI* pCmdUI) 
680 {
681   if (Current == 3)
682     pCmdUI->SetCheck(true);
683   else
684     pCmdUI->SetCheck(false);
685 }
686
687 void CGeometryDoc::OnUpdateBUTTONTest4(CCmdUI* pCmdUI) 
688 {
689   if (Current == 4)
690     pCmdUI->SetCheck(true);
691   else
692     pCmdUI->SetCheck(false);
693 }
694
695 void CGeometryDoc::OnUpdateBUTTONTest5(CCmdUI* pCmdUI) 
696 {
697   if (Current == 5)
698     pCmdUI->SetCheck(true);
699   else
700     pCmdUI->SetCheck(false);
701 }
702
703 void CGeometryDoc::OnUpdateBUTTONTest6(CCmdUI* pCmdUI) 
704 {
705   if (Current == 6)
706     pCmdUI->SetCheck(true);
707   else
708     pCmdUI->SetCheck(false);
709 }
710
711 void CGeometryDoc::OnUpdateBUTTONTest7(CCmdUI* pCmdUI) 
712 {
713   if (Current == 7)
714     pCmdUI->SetCheck(true);
715   else
716     pCmdUI->SetCheck(false);
717 }
718
719 void CGeometryDoc::OnUpdateBUTTONTest8(CCmdUI* pCmdUI) 
720 {
721   if (Current == 8)
722     pCmdUI->SetCheck(true);
723   else
724     pCmdUI->SetCheck(false);
725 }
726
727 void CGeometryDoc::OnUpdateBUTTONTest9(CCmdUI* pCmdUI) 
728 {
729   if (Current == 9)
730     pCmdUI->SetCheck(true);
731   else
732     pCmdUI->SetCheck(false);
733 }
734
735 void CGeometryDoc::OnUpdateBUTTONTest10(CCmdUI* pCmdUI) 
736 {
737   if (Current == 10)
738     pCmdUI->SetCheck(true);
739   else
740     pCmdUI->SetCheck(false);
741 }
742
743 void CGeometryDoc::OnUpdateBUTTONTest11(CCmdUI* pCmdUI) 
744 {
745   if (Current == 11)
746     pCmdUI->SetCheck(true);
747   else 
748     pCmdUI->SetCheck(false);
749 }
750
751 void CGeometryDoc::OnUpdateBUTTONTest12(CCmdUI* pCmdUI) 
752 {
753   if (Current == 12)
754     pCmdUI->SetCheck(true);
755   else pCmdUI->SetCheck(false);
756 }
757
758 void CGeometryDoc::OnUpdateBUTTONTest13(CCmdUI* pCmdUI) 
759 {
760   if (Current == 13)
761     pCmdUI->SetCheck(true);
762   else
763     pCmdUI->SetCheck(false);
764 }
765
766 void CGeometryDoc::OnUpdateBUTTONTest14(CCmdUI* pCmdUI) 
767 {
768   if (Current == 14)
769     pCmdUI->SetCheck(true);
770   else
771     pCmdUI->SetCheck(false);
772 }
773
774 void CGeometryDoc::OnUpdateBUTTONTest15(CCmdUI* pCmdUI)
775 {
776   if(Current == 15)
777     pCmdUI->SetCheck(true);
778   else
779     pCmdUI->SetCheck(false);
780 }
781
782 void CGeometryDoc::OnUpdateBUTTONTest16(CCmdUI* pCmdUI)
783 {
784   if (Current == 16)
785     pCmdUI->SetCheck(true);
786   else
787     pCmdUI->SetCheck(false);
788 }
789
790 void CGeometryDoc::OnUpdateBUTTONTest17(CCmdUI* pCmdUI)
791 {
792   if (Current == 17)
793     pCmdUI->SetCheck(true);
794   else
795     pCmdUI->SetCheck(false);
796 }
797
798 void CGeometryDoc::OnUpdateBUTTONTest18(CCmdUI* pCmdUI)
799 {
800   if (Current == 18)
801     pCmdUI->SetCheck(true);
802   else
803     pCmdUI->SetCheck(false);
804 }
805
806 void CGeometryDoc::OnUpdateBUTTONTest19(CCmdUI* pCmdUI)
807 {
808   if (Current == 19)
809     pCmdUI->SetCheck(true);
810   else
811     pCmdUI->SetCheck(false);
812 }
813
814 void CGeometryDoc::OnUpdateBUTTONTest20(CCmdUI* pCmdUI)
815 {
816   if (Current == 20)
817     pCmdUI->SetCheck(true);
818   else
819     pCmdUI->SetCheck(false);
820 }
821
822 void CGeometryDoc::OnUpdateBUTTONTest21(CCmdUI* pCmdUI)
823 {
824   if (Current == 21)
825     pCmdUI->SetCheck(true);
826   else
827     pCmdUI->SetCheck(false);
828 }
829
830 void CGeometryDoc::OnUpdateBUTTONTest22(CCmdUI* pCmdUI)
831 {
832   if (Current == 22)
833     pCmdUI->SetCheck(true);
834   else
835     pCmdUI->SetCheck(false);
836 }
837
838 void CGeometryDoc::OnUpdateBUTTONTest23(CCmdUI* pCmdUI)
839 {
840   if (Current == 23)
841     pCmdUI->SetCheck(true);
842   else
843     pCmdUI->SetCheck(false);
844 }
845
846 void CGeometryDoc::OnUpdateBUTTONTest24(CCmdUI* pCmdUI)
847 {
848   if (Current == 24)
849     pCmdUI->SetCheck(true);
850   else
851     pCmdUI->SetCheck(false);
852 }
853
854 void CGeometryDoc::OnUpdateBUTTONTest25(CCmdUI* pCmdUI)
855 {
856   if (Current == 25)
857     pCmdUI->SetCheck(true);
858   else
859     pCmdUI->SetCheck(false);
860 }
861
862 void CGeometryDoc::OnUpdateBUTTONTest26(CCmdUI* pCmdUI)
863 {
864   if (Current == 26)
865     pCmdUI->SetCheck(true);
866   else
867     pCmdUI->SetCheck(false);
868 }
869
870 void CGeometryDoc::OnUpdateBUTTONTest27(CCmdUI* pCmdUI)
871 {
872   if (Current == 27)
873     pCmdUI->SetCheck(true);
874   else
875     pCmdUI->SetCheck(false);
876 }
877
878 void CGeometryDoc::OnUpdateBUTTONTest28(CCmdUI* pCmdUI)
879 {
880   if (Current == 28)
881     pCmdUI->SetCheck(true);
882   else
883     pCmdUI->SetCheck(false);
884 }
885
886 void CGeometryDoc::OnUpdateBUTTONTest29(CCmdUI* pCmdUI)
887 {
888   if (Current == 29)
889     pCmdUI->SetCheck(true);
890   else
891     pCmdUI->SetCheck(false);
892 }
893
894 void CGeometryDoc::OnUpdateBUTTONTest30(CCmdUI* pCmdUI)
895 {
896   if (Current == 30)
897     pCmdUI->SetCheck(true);
898   else
899     pCmdUI->SetCheck(false);
900 }
901
902 void CGeometryDoc::OnUpdateBUTTONTest31(CCmdUI* pCmdUI)
903 {
904   if (Current == 31)
905     pCmdUI->SetCheck(true);
906   else
907     pCmdUI->SetCheck(false);
908 }
909
910 void CGeometryDoc::OnUpdateBUTTONTest32(CCmdUI* pCmdUI)
911 {
912   if (Current == 32)
913     pCmdUI->SetCheck(true);
914   else
915     pCmdUI->SetCheck(false);
916 }
917
918 void CGeometryDoc::OnUpdateBUTTONTest33(CCmdUI* pCmdUI)
919 {
920   if (Current == 33)
921     pCmdUI->SetCheck(true);
922   else
923     pCmdUI->SetCheck(false);
924 }
925
926 void CGeometryDoc::OnUpdateBUTTONTest34(CCmdUI* pCmdUI)
927 {
928   if (Current == 34)
929     pCmdUI->SetCheck(true);
930   else
931     pCmdUI->SetCheck(false);
932 }
933
934 void CGeometryDoc::OnUpdateBUTTONTest35(CCmdUI* pCmdUI)
935 {
936   if (Current == 35)
937     pCmdUI->SetCheck(true);
938   else
939     pCmdUI->SetCheck(false);
940 }
941
942 void CGeometryDoc::OnUpdateBUTTONTest36(CCmdUI* pCmdUI)
943 {
944   if (Current == 36)
945     pCmdUI->SetCheck(true);
946   else
947     pCmdUI->SetCheck(false);
948 }
949
950 void CGeometryDoc::OnUpdateBUTTONTest37(CCmdUI* pCmdUI)
951 {
952   if (Current == 37)
953     pCmdUI->SetCheck(true);
954   else
955     pCmdUI->SetCheck(false);
956 }
957
958 void CGeometryDoc::OnUpdateBUTTONTest38(CCmdUI* pCmdUI)
959 {
960   if (Current == 38)
961     pCmdUI->SetCheck(true);
962 else
963 pCmdUI->SetCheck(false);
964 }
965
966 void CGeometryDoc::OnUpdateBUTTONTest39(CCmdUI* pCmdUI)
967 {
968   if (Current == 39)
969     pCmdUI->SetCheck(true);
970   else
971     pCmdUI->SetCheck(false);
972 }
973
974 void CGeometryDoc::OnUpdateBUTTONTest40(CCmdUI* pCmdUI)
975 {
976   if (Current == 40)
977     pCmdUI->SetCheck(true);
978   else
979     pCmdUI->SetCheck(false);
980 }
981
982 void CGeometryDoc::OnUpdateBUTTONTest41(CCmdUI* pCmdUI)
983 {
984   if (Current == 41)
985     pCmdUI->SetCheck(true);
986   else
987     pCmdUI->SetCheck(false);
988 }
989
990 void CGeometryDoc::OnUpdateBUTTONTest42(CCmdUI* pCmdUI)
991 {
992   if (Current == 42)
993     pCmdUI->SetCheck(true);
994   else
995     pCmdUI->SetCheck(false);
996 }
997
998 void CGeometryDoc::OnUpdateBUTTONTest43(CCmdUI* pCmdUI)
999 {
1000   if (Current == 43)
1001     pCmdUI->SetCheck(true);
1002   else
1003     pCmdUI->SetCheck(false);
1004 }
1005
1006 void CGeometryDoc::OnUpdateBUTTONTest44(CCmdUI* pCmdUI)
1007 {
1008   if (Current == 44)
1009     pCmdUI->SetCheck(true);
1010   else
1011     pCmdUI->SetCheck(false);
1012 }
1013
1014 void CGeometryDoc::OnUpdateBUTTONTest45(CCmdUI* pCmdUI)
1015 {
1016   if (Current == 45)
1017     pCmdUI->SetCheck(true);
1018   else
1019     pCmdUI->SetCheck(false);
1020 }
1021
1022 void CGeometryDoc::OnUpdateBUTTONTest46(CCmdUI* pCmdUI)
1023 {
1024   if (Current == 46)
1025     pCmdUI->SetCheck(true);
1026   else
1027     pCmdUI->SetCheck(false);
1028 }
1029
1030 void CGeometryDoc::OnUpdateBUTTONTest47(CCmdUI* pCmdUI)
1031 {
1032   if (Current == 47)
1033     pCmdUI->SetCheck(true);
1034   else
1035     pCmdUI->SetCheck(false);
1036 }
1037
1038 void CGeometryDoc::OnUpdateBUTTONTest48(CCmdUI* pCmdUI)
1039 {
1040   if (Current == 48)
1041     pCmdUI->SetCheck(true);
1042   else
1043     pCmdUI->SetCheck(false);
1044 }
1045
1046 void CGeometryDoc::OnUpdateBUTTONTest49(CCmdUI* pCmdUI)
1047 {
1048   if (Current == 49)
1049     pCmdUI->SetCheck(true);
1050   else
1051     pCmdUI->SetCheck(false);
1052 }
1053
1054 void CGeometryDoc::OnUpdateBUTTONTest50(CCmdUI* pCmdUI)
1055 {
1056   if (Current == 50)
1057     pCmdUI->SetCheck(true);
1058   else
1059     pCmdUI->SetCheck(false);
1060 }
1061
1062 void CGeometryDoc::OnCloseDocument() 
1063 {
1064   // TODO: Add your specialized code here and/or call the base class
1065   CDocument::OnCloseDocument();
1066 }
1067
1068 void CGeometryDoc::OnCreateSol() 
1069 {
1070   // TODO: Add your command handler code here
1071   // Creation d'un sol
1072   CFileDialog dlg (TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
1073                    L"Points Files (*.dat)|*.dat; |All Files (*.*)|*.*||", NULL);
1074
1075   CString anOCCTDataPathValue;
1076   anOCCTDataPathValue.GetEnvironmentVariable(L"CSF_OCCTDataPath");
1077   CString initdir = (anOCCTDataPathValue + L"\\occ\\SurfaceFromPoints");
1078
1079   dlg.m_ofn.lpstrInitialDir = initdir;
1080
1081   if (dlg.DoModal() == IDOK) 
1082   {
1083     SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
1084     CString filename = dlg.GetPathName();
1085
1086     std::filebuf fic;
1087     std::istream in(&fic);  
1088     if (!fic.open (filename, std::ios::in))
1089       MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Error : Unable to open file", L"CasCade Error", MB_ICONERROR);
1090
1091     TColgp_SequenceOfXYZ seqOfXYZ;
1092     gp_XYZ pntXYZ;
1093     Standard_Integer nbPnt=0;
1094     Standard_Real x,y,z;
1095     BRep_Builder B;
1096     TopoDS_Compound C;
1097     B.MakeCompound(C);
1098     while (!in.fail()|| !in.eof())
1099     {
1100       if (in >> x && in >> y && in >> z){
1101         pntXYZ.SetX(x);
1102         pntXYZ.SetY(y);
1103         pntXYZ.SetZ(z);
1104         nbPnt++;
1105         seqOfXYZ.Append(pntXYZ);
1106         BRepBuilderAPI_MakeVertex V(gp_Pnt(x, y, z));
1107         B.Add(C,V.Vertex());        
1108       }
1109     }
1110     fic.close();
1111     Handle(AIS_Shape) anAISCompound = new AIS_Shape(C);
1112     myAISContext->Display(anAISCompound, Standard_False);
1113     Fit();
1114     Sleep(1000);
1115     GeoAlgo_Sol sol;
1116     sol.Build(seqOfXYZ);
1117
1118     if (sol.IsDone() == Standard_True)
1119     {
1120       Handle(Geom_BSplineSurface) GeomSol = sol.Surface();
1121       TopoDS_Face aface = BRepBuilderAPI_MakeFace(GeomSol, Precision::Confusion());
1122       if (!BRepAlgo::IsValid(aface))
1123         MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Error : The plate surface is not valid!", L"CasCade Error", MB_ICONERROR);
1124       Handle(AIS_Shape) anAISShape=new AIS_Shape(aface);
1125       myAISContext->Display(anAISShape, Standard_False);
1126       Fit();
1127     }   
1128     else
1129       MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Error : Computation has failed", L"CasCade Error", MB_ICONERROR);
1130   }
1131 }
1132
1133 /*********************************************************************************************
1134 **********************  S I M P L I F Y  *****************************************************
1135 *********************************************************************************************/
1136
1137 //================================================================
1138 // Function : fixParam
1139 // Purpose  : assigns a finite value to theParam if it's infinite
1140 //            (equal to +- Precision::Infinite())
1141 //================================================================
1142 static Standard_Boolean fixParam(Standard_Real& theParam)
1143 {
1144   Standard_Boolean aResult = Standard_False;
1145   if (Precision::IsNegativeInfinite(theParam))
1146   {
1147     theParam = -MAX_PARAM;
1148     aResult = Standard_True;
1149   }
1150   if (Precision::IsPositiveInfinite(theParam))
1151   {
1152     theParam = MAX_PARAM;
1153     aResult = Standard_True;
1154   }
1155   return aResult;
1156 }
1157
1158
1159 void CGeometryDoc::OnSimplify() 
1160 {
1161   CString anOCCTDataPathValue;
1162   anOCCTDataPathValue.GetEnvironmentVariable(L"CSF_OCCTDataPath");
1163   CString initfile = (anOCCTDataPathValue + L"\\occ\\shell1.brep");
1164
1165   std::filebuf aFileBuf;
1166   std::istream aStream (&aFileBuf);
1167   if (!aFileBuf.open (initfile, std::ios::in))
1168   {
1169     initfile += L" was not found. The sample can not be shown.";
1170     myCResultDialog.SetText (initfile);
1171     return;
1172   }
1173
1174   TopoDS_Shape aShape;
1175   BRep_Builder aBld;
1176   BRepTools::Read (aShape, aStream, aBld);
1177   if (aShape.IsNull())
1178   {
1179     initfile += L" is invalid file. The sample can not be shown.";
1180     myCResultDialog.SetText(initfile);
1181     return;
1182   }
1183   myAISContext->SetDisplayMode(AIS_Shaded, Standard_True);
1184   simplify(aShape);
1185 }
1186
1187 void CGeometryDoc::simplify(const TopoDS_Shape& aShape)
1188 {
1189     myCResultDialog.SetTitle("Simplify Face");
1190     myCResultDialog.SetText("  TopoDS_Shape aShape;\n"
1191     "\n"
1192     "  // initialize aShape\n"
1193     "  //aShape = ...\n"
1194     "\n"
1195     "  // define parameter triangulation\n"
1196     "  Standard_Real aDeflection = 0.1;\n"
1197     "  \n"
1198     "  // removes all the triangulations of the faces ,\n"
1199     "  //and all the polygons on the triangulations of the edges\n"
1200     "  BRepTools::Clean(aShape);\n"
1201     "  // adds a triangulation of the shape aShape with the deflection aDeflection\n"
1202     "  BRepMesh::Mesh(aShape,aDeflection);\n"
1203     "\n"
1204     "  Standard_Integer aIndex = 1, nbNodes = 0;\n"
1205     "  \n"
1206     "  // define two sequence of points\n"
1207     "  TColgp_SequenceOfPnt aPoints, aPoints1;\n"
1208     " \n"
1209     "  // triangulation\n"
1210     "  for(TopExp_Explorer aExpFace(aShape,TopAbs_FACE); aExpFace.More(); aExpFace.Next())\n"
1211     "  {  \n"
1212     "    TopoDS_Face aFace = TopoDS::Face(aExpFace.Current());\n"
1213     "    TopLoc_Location aLocation;\n"
1214     "\n"
1215     "    // takes the triangulation of the face aFace\n"
1216     "    Handle(Poly_Triangulation) aTr = BRep_Tool::Triangulation(aFace,aLocation);\n"
1217     "\n"
1218     "    if(!aTr.IsNull())\n"
1219     "    { \n"
1220     "      // takes the array of nodes for this triangulation\n"
1221     "      const TColgp_Array1OfPnt& aNodes = aTr->Nodes();    \n"
1222     "      nbNodes = aNodes.Length();\n"
1223     "\n"
1224     "      for( Standard_Integer i = 1; i <= nbNodes; i++)\n"
1225     "      {\n"
1226     "        // create seguence of node points in absolute coordinate system\n"
1227     "        gp_Pnt aPnt = aNodes(i).Transformed(aLocation);\n"
1228     "        aPoints.Append(aPnt);\n"
1229     "        \n"
1230     "      }\n"
1231     "    }\n"
1232     "  }\n"
1233     " \n"
1234     "  // remove double points\n"
1235     "  nbNodes = aPoints.Length();\n"
1236     "  for( Standard_Integer i = 1; i <= nbNodes; i++)\n"
1237     "  {\n"
1238     "    gp_Pnt aPi = aPoints(i);\n"
1239     "    for( Standard_Integer j = i + 1; j < nbNodes; j++)\n"
1240     "    {\n"
1241     "      gp_Pnt aPj = aPoints(j);\n"
1242     "      if(!aPi.IsEqual(aPj,0.9))\n"
1243     "        aIndex++;\n"
1244     "    }\n"
1245     "    if(aIndex == j - 1)\n"
1246     "      aPoints1.Append(aPi);\n"
1247     "\n"
1248     "    aIndex = i + 1;\n"
1249     "  }\n"
1250     "\n"
1251     "  // find max point\n"
1252     "  aIndex = 0;\n"
1253     "  gp_Pnt aPntMax = aPoints1(1);\n"
1254     "  nbNodes = aPoints1.Length();\n"
1255     "  for(i = 2; i <= nbNodes; i++)\n"
1256     "  {\n"
1257     "    if(aPoints1(i).X() > aPntMax.X())\n"
1258     "    {\n"
1259     "      aIndex = i;\n"
1260     "      aPntMax = aPoints1(aIndex);      \n"
1261     "    } \n"
1262     "  }\n"
1263     "\n"
1264     "  // clear seguence\n"
1265     "  aPoints.Clear();\n"
1266     "\n"
1267     "  Standard_Integer nbLeftNodes = nbNodes;\n"
1268     "\n"
1269     "  // ascending sort - fill aPoints with ascending \n"
1270     "  // by X coordinate points from aPoints1\n"
1271     "  for(i = 1; i < nbNodes; i++)\n"
1272     "  {\n"
1273     "    Standard_Real aMin = aPntMax.X();\n"
1274     "    aIndex = 1;\n"
1275     "    for( Standard_Integer j = 1; j <= nbLeftNodes; j++)\n"
1276     "    {\n"
1277     "      if(aPoints1(j).X() < aMin)\n"
1278     "      {\n"
1279     "        aMin = aPoints1(j).X();\n"
1280     "        aIndex = j;\n"
1281     "      } \n"
1282     "    }\n"
1283     "    aPoints.Append(aPoints1(aIndex));\n"
1284     "    aPoints1.Remove(aIndex);\n"
1285     "    nbLeftNodes = aPoints1.Length();\n"
1286     "  }\n"
1287     "  aPoints.Append(aPntMax);\n"
1288     "\n"
1289     "  // define parameters GeomPlate_BuildPlateSurface\n"
1290     "  Standard_Integer Degree = 3;\n"
1291     "  Standard_Integer NbPtsOnCur = 10;\n"
1292     "  Standard_Integer NbIter = 3;\n"
1293     "  Standard_Integer Order = 0;\n"
1294     "  Standard_Integer MaxSeg = 9;\n"
1295     "  Standard_Integer MaxDegree = 5;\n"
1296     "  Standard_Real dmax, anApproxTol = 0.001;\n"
1297     "  Standard_Real aConstrTol = Precision::Confusion();\n"
1298     "  \n"
1299     "  // define object BuildPlateSurface\n"
1300     "  GeomPlate_BuildPlateSurface BPSurf(Degree,NbPtsOnCur,NbIter);\n"
1301     "  \n"
1302     "  // add point constraints to GeomPlate_BuildPlateSurface object\n"
1303     "  nbNodes = aPoints.Length();\n"
1304     "  for (i = 1; i <= nbNodes; i++)\n"
1305     "    BPSurf.Add(new GeomPlate_PointConstraint(aPoints(i), Order, aConstrTol));\n"
1306     "\n"
1307     "  BPSurf.Perform();\n"
1308     "\n"
1309     "  // make PlateSurface\n"
1310     "  Handle(GeomPlate_Surface) PSurf;\n"
1311     "  Handle(Geom_Surface) aSurf;\n"
1312     "  \n"
1313     "  if (BPSurf.IsDone())\n"
1314     "  {\n"
1315     "    PSurf = BPSurf.Surface();\n"
1316     "\n"
1317     "    // define parameter approximation\n"
1318     "    dmax = Max(0.01,10*BPSurf.G0Error());\n"
1319     "\n"
1320     "    // make approximation\n"
1321     "    GeomPlate_MakeApprox Mapp(PSurf,anApproxTol, MaxSeg,MaxDegree,dmax);\n"
1322     "    aSurf = Mapp.Surface();\n"
1323     "  }\n"
1324     "  else \n"
1325     "    return;\n"
1326     "\n"
1327     "  ShapeAnalysis_FreeBounds aFreeBounds(aShape, Standard_False, Standard_True);\n"
1328     "  TopoDS_Compound aClosedWires = aFreeBounds.GetClosedWires();\n"
1329     "  TopTools_IndexedMapOfShape aWires;\n"
1330     "  TopExp::MapShapes(aClosedWires, TopAbs_WIRE, aWires);\n"
1331     "  TopoDS_Wire aWire;\n"
1332     "  Standard_Integer nbWires = aWires.Extent();\n"
1333     "  if (nbWires) \n"
1334     "    aWire = TopoDS::Wire(aWires(1));\n"
1335     "  else \n"
1336     "    return;\n"
1337     "\n"
1338     "  BRep_Builder B;\n"
1339     "  TopoDS_Face aFace;\n"
1340     "  B.MakeFace(aFace, aSurf, Precision::Confusion());\n"
1341     "  B.Add(aFace, aWire);\n"
1342     "  Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape(aFace);\n"
1343     "  sfs->Perform();\n"
1344     "  TopoDS_Shape aFixedFace = sfs->Shape();\n"
1345     "  if (aFixedFace.IsNull()) \n"
1346     "    return;\n");
1347
1348     // define parameter triangulation
1349     Standard_Real aDeflection = 0.1;
1350
1351     // removes all the triangulations of the faces ,
1352     //and all the polygons on the triangulations of the edges
1353     BRepTools::Clean(aShape);
1354     // adds a triangulation of the shape aShape with the deflection aDeflection
1355     BRepMesh_IncrementalMesh(aShape,aDeflection);
1356
1357     Standard_Integer aIndex = 1, nbNodes = 0;
1358
1359     // define two sequence of points
1360     TColgp_SequenceOfPnt aPoints, aPoints1;
1361
1362     // triangulation
1363     for(TopExp_Explorer aExpFace(aShape,TopAbs_FACE); aExpFace.More(); aExpFace.Next())
1364     {  
1365       TopoDS_Face aFace = TopoDS::Face(aExpFace.Current());
1366       TopLoc_Location aLocation;
1367
1368       // takes the triangulation of the face aFace
1369       Handle(Poly_Triangulation) aTr = BRep_Tool::Triangulation(aFace,aLocation);
1370
1371       if(!aTr.IsNull())
1372       { 
1373         // takes the array of nodes for this triangulation
1374         const TColgp_Array1OfPnt& aNodes = aTr->Nodes();    
1375         nbNodes = aNodes.Length();
1376
1377         for( Standard_Integer i = 1; i <= nbNodes; i++)
1378         {
1379           // create seguence of node points in absolute coordinate system
1380           gp_Pnt aPnt = aNodes(i).Transformed(aLocation);
1381           aPoints.Append(aPnt);
1382
1383         }
1384       }
1385     }
1386
1387     // remove double points
1388     nbNodes = aPoints.Length();
1389     Standard_Integer i;
1390     for( i = 1; i <= nbNodes; i++)
1391     {
1392       gp_Pnt aPi = aPoints(i);
1393       Standard_Integer j;
1394       for( j = i + 1; j < nbNodes; j++)
1395       {
1396         gp_Pnt aPj = aPoints(j);
1397         if(!aPi.IsEqual(aPj,0.9))
1398           aIndex++;
1399       }
1400       if(aIndex == j - 1)
1401         aPoints1.Append(aPi);
1402
1403       aIndex = i + 1;
1404     }
1405
1406     // find max point
1407     aIndex = 0;
1408     gp_Pnt aPntMax = aPoints1(1);
1409     nbNodes = aPoints1.Length();
1410     for(i = 2; i <= nbNodes; i++)
1411     {
1412       if(aPoints1(i).X() > aPntMax.X())
1413       {
1414         aIndex = i;
1415         aPntMax = aPoints1(aIndex);
1416       } 
1417     }
1418
1419     // clear seguence
1420     aPoints.Clear();
1421
1422     Standard_Integer nbLeftNodes = nbNodes;
1423
1424     // ascending sort - fill aPoints with ascending 
1425     // by X coordinate points from aPoints1
1426     for(i = 1; i < nbNodes; i++)
1427     {
1428       Standard_Real aMin = aPntMax.X();
1429       aIndex = 1;
1430       for( Standard_Integer j = 1; j <= nbLeftNodes; j++)
1431       {
1432         if(aPoints1(j).X() < aMin)
1433         {
1434           aMin = aPoints1(j).X();
1435           aIndex = j;
1436         } 
1437       }
1438       aPoints.Append(aPoints1(aIndex));
1439       aPoints1.Remove(aIndex);
1440       nbLeftNodes = aPoints1.Length();
1441     }
1442     aPoints.Append(aPntMax);
1443
1444     // define parameters GeomPlate_BuildPlateSurface
1445     Standard_Integer Degree = 3;
1446     Standard_Integer NbPtsOnCur = 10;
1447     Standard_Integer NbIter = 3;
1448     Standard_Integer Order = 0;
1449     Standard_Integer MaxSeg = 9;
1450     Standard_Integer MaxDegree = 5;
1451     Standard_Real dmax, anApproxTol = 0.001;
1452     Standard_Real aConstrTol = Precision::Confusion();
1453
1454     // define object BuildPlateSurface
1455     GeomPlate_BuildPlateSurface BPSurf(Degree,NbPtsOnCur,NbIter);
1456
1457     // add point constraints to GeomPlate_BuildPlateSurface object
1458     nbNodes = aPoints.Length();
1459     for (i = 1; i <= nbNodes; i++)
1460       BPSurf.Add(new GeomPlate_PointConstraint(aPoints(i), Order, aConstrTol));
1461
1462     BPSurf.Perform();
1463
1464     // make PlateSurface
1465     Handle(GeomPlate_Surface) PSurf;
1466     Handle(Geom_Surface) aSurf;
1467
1468     if (BPSurf.IsDone())
1469     {
1470       PSurf = BPSurf.Surface();
1471
1472       // define parameter approximation
1473       dmax = Max(0.01,10*BPSurf.G0Error());
1474
1475       // make approximation
1476       GeomPlate_MakeApprox Mapp(PSurf,anApproxTol, MaxSeg,MaxDegree,dmax);
1477       aSurf = Mapp.Surface();
1478     }
1479     else 
1480       return;
1481
1482     ShapeAnalysis_FreeBounds aFreeBounds(aShape, Standard_False, Standard_True);
1483     TopoDS_Compound aClosedWires = aFreeBounds.GetClosedWires();
1484     TopTools_IndexedMapOfShape aWires;
1485     TopExp::MapShapes(aClosedWires, TopAbs_WIRE, aWires);
1486     TopoDS_Wire aWire;
1487     Standard_Integer nbWires = aWires.Extent();
1488     if (nbWires) 
1489       aWire = TopoDS::Wire(aWires(1));
1490     else 
1491       return;
1492
1493     BRep_Builder B;
1494     TopoDS_Face aFace;
1495     B.MakeFace(aFace, aSurf, Precision::Confusion());
1496     B.Add(aFace, aWire);
1497     Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape(aFace);
1498     sfs->Perform();
1499     TopoDS_Shape aFixedFace = sfs->Shape();
1500     if (aFixedFace.IsNull()) 
1501       return;
1502
1503     // output surface, make it half transparent
1504     Handle(AIS_InteractiveObject) aSurfIO = drawSurface(
1505       aSurf, Quantity_NOC_LEMONCHIFFON3, Standard_False);
1506     aSurfIO->SetTransparency(0.5);
1507     myAISContext->Display(aSurfIO,Standard_False);
1508     Fit();
1509
1510     if(WAIT_A_LITTLE) return;
1511
1512     // output points
1513     for(i = 1; i <= nbNodes; i++)
1514       drawPoint(aPoints(i));
1515
1516     if(WAIT_A_LITTLE) return;
1517
1518     // output resulting face
1519     drawShape(aFixedFace);
1520 }
1521
1522 Handle(AIS_InteractiveObject) CGeometryDoc::drawSurface
1523                                   (const Handle(Geom_Surface)& theSurface,
1524                                    const Quantity_Color& theColor,
1525                                    const Standard_Boolean toDisplay)
1526 {
1527   Standard_Real u1, u2, v1, v2;
1528   theSurface->Bounds(u1,u2,v1,v2);
1529   fixParam(u1);
1530   fixParam(u2);
1531   fixParam(v1);
1532   fixParam(v2);
1533
1534   Handle(AIS_Shape) aGraphicSurface = 
1535     new AIS_Shape(BRepBuilderAPI_MakeFace (theSurface, u1, u2, v1, v2, Precision::Confusion()));
1536
1537   myAISContext->SetMaterial(aGraphicSurface, Graphic3d_NameOfMaterial_Plastified, toDisplay);
1538   myAISContext->SetColor(aGraphicSurface, theColor, toDisplay);
1539   if (toDisplay)
1540   {
1541     if (FitMode)
1542     {
1543       myAISContext->Display (aGraphicSurface, Standard_False);
1544       Fit();
1545     }
1546     else
1547       myAISContext->Display (aGraphicSurface, Standard_True);
1548   }
1549
1550   return aGraphicSurface;
1551 }
1552
1553 Standard_Boolean CGeometryDoc::WaitForInput (unsigned long aMilliSeconds)
1554 {
1555   //::WaitForSingleObject(::CreateEvent (NULL, FALSE, FALSE, NULL), aMilliSeconds);
1556   if (::MsgWaitForMultipleObjects(0, NULL, FALSE, aMilliSeconds,
1557     QS_KEY | QS_MOUSEBUTTON) != WAIT_TIMEOUT)
1558   {
1559     MSG msg;
1560     if (::PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
1561     {
1562       if (msg.message == WM_KEYUP)
1563       {
1564         ::PeekMessage (&msg, NULL, 0, 0, PM_REMOVE);
1565         return WaitForInput (aMilliSeconds);
1566       }
1567       else
1568         return Standard_True;
1569     }
1570   }
1571   return Standard_False;
1572 }
1573
1574 Handle(AIS_Point) CGeometryDoc::drawPoint
1575                                   (const gp_Pnt& aPnt,
1576                                    const Quantity_Color& theColor,
1577                                    const Standard_Boolean toDisplay)
1578 {
1579   Handle(AIS_Point) aGraphicPoint = new AIS_Point (new Geom_CartesianPoint(aPnt));
1580
1581   myAISContext->SetColor (aGraphicPoint, theColor, toDisplay);
1582   if (toDisplay)
1583   {
1584   myAISContext->Display (aGraphicPoint, Standard_True);
1585     //COCCDemoDoc::Fit();
1586   }
1587
1588   return aGraphicPoint;
1589 }
1590
1591 Handle(AIS_Shape) CGeometryDoc::drawShape
1592          (const TopoDS_Shape& theShape,
1593           const Graphic3d_NameOfMaterial theMaterial,
1594           const Standard_Boolean toDisplay)
1595 {
1596   Handle(AIS_Shape) aGraphicShape = new AIS_Shape(theShape);
1597
1598   myAISContext->SetMaterial(aGraphicShape, theMaterial, toDisplay);
1599   if (toDisplay)
1600   {
1601     if (FitMode)
1602     {
1603       myAISContext->Display (aGraphicShape, Standard_False);
1604       Fit();
1605     }
1606     else
1607       myAISContext->Display (aGraphicShape, Standard_True);
1608   }
1609
1610   return aGraphicShape;
1611 }