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