0031431: Visualization, PrsMgr_PresentableObject - simplify HLR computing interface
[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     Standard_Real 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, Standard_True);
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()->InitSelected();
329   if (GetAISContext()->MoreSelected())
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 (Standard_True);
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(Standard_Real 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 Standard_Real& 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 anOCCTDataPathValue;
1074   anOCCTDataPathValue.GetEnvironmentVariable(L"CSF_OCCTDataPath");
1075   CString initdir = (anOCCTDataPathValue + L"\\occ\\SurfaceFromPoints");
1076
1077   dlg.m_ofn.lpstrInitialDir = initdir;
1078
1079   if (dlg.DoModal() == IDOK) 
1080   {
1081     SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
1082     CString filename = dlg.GetPathName();
1083
1084     std::filebuf fic;
1085     std::istream in(&fic);  
1086     if (!fic.open (filename, std::ios::in))
1087       MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Error : Unable to open file", L"CasCade Error", MB_ICONERROR);
1088
1089     TColgp_SequenceOfXYZ seqOfXYZ;
1090     gp_XYZ pntXYZ;
1091     Standard_Integer nbPnt=0;
1092     Standard_Real x,y,z;
1093     BRep_Builder B;
1094     TopoDS_Compound C;
1095     B.MakeCompound(C);
1096     while (!in.fail()|| !in.eof())
1097     {
1098       if (in >> x && in >> y && in >> z){
1099         pntXYZ.SetX(x);
1100         pntXYZ.SetY(y);
1101         pntXYZ.SetZ(z);
1102         nbPnt++;
1103         seqOfXYZ.Append(pntXYZ);
1104         BRepBuilderAPI_MakeVertex V(gp_Pnt(x, y, z));
1105         B.Add(C,V.Vertex());        
1106       }
1107     }
1108     fic.close();
1109     Handle(AIS_Shape) anAISCompound = new AIS_Shape(C);
1110     myAISContext->Display(anAISCompound, Standard_False);
1111     Fit();
1112     Sleep(1000);
1113     GeoAlgo_Sol sol;
1114     sol.Build(seqOfXYZ);
1115
1116     if (sol.IsDone() == Standard_True)
1117     {
1118       Handle(Geom_BSplineSurface) GeomSol = sol.Surface();
1119       TopoDS_Face aface = BRepBuilderAPI_MakeFace(GeomSol, Precision::Confusion());
1120       if (!BRepAlgo::IsValid(aface))
1121         MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Error : The plate surface is not valid!", L"CasCade Error", MB_ICONERROR);
1122       Handle(AIS_Shape) anAISShape=new AIS_Shape(aface);
1123       myAISContext->Display(anAISShape, Standard_False);
1124       Fit();
1125     }   
1126     else
1127       MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Error : Computation has failed", L"CasCade Error", MB_ICONERROR);
1128   }
1129 }
1130
1131 /*********************************************************************************************
1132 **********************  S I M P L I F Y  *****************************************************
1133 *********************************************************************************************/
1134
1135 //================================================================
1136 // Function : fixParam
1137 // Purpose  : assings a finite value to theParam if it intinite
1138 //            (equal to +- Precision::Infinite())
1139 //================================================================
1140 static Standard_Boolean fixParam(Standard_Real& theParam)
1141 {
1142   Standard_Boolean aResult = Standard_False;
1143   if (Precision::IsNegativeInfinite(theParam))
1144   {
1145     theParam = -MAX_PARAM;
1146     aResult = Standard_True;
1147   }
1148   if (Precision::IsPositiveInfinite(theParam))
1149   {
1150     theParam = MAX_PARAM;
1151     aResult = Standard_True;
1152   }
1153   return aResult;
1154 }
1155
1156
1157 void CGeometryDoc::OnSimplify() 
1158 {
1159   CString anOCCTDataPathValue;
1160   anOCCTDataPathValue.GetEnvironmentVariable(L"CSF_OCCTDataPath");
1161   CString initfile = (anOCCTDataPathValue + L"\\occ\\shell1.brep");
1162
1163   std::filebuf aFileBuf;
1164   std::istream aStream (&aFileBuf);
1165   if (!aFileBuf.open (initfile, std::ios::in))
1166   {
1167     initfile += L" was not found. The sample can not be shown.";
1168     myCResultDialog.SetText (initfile);
1169     return;
1170   }
1171
1172   TopoDS_Shape aShape;
1173   BRep_Builder aBld;
1174   BRepTools::Read (aShape, aStream, aBld);
1175   if (aShape.IsNull())
1176   {
1177     initfile += L" is invalid file. The sample can not be shown.";
1178     myCResultDialog.SetText(initfile);
1179     return;
1180   }
1181   myAISContext->SetDisplayMode(AIS_Shaded, Standard_True);
1182   simplify(aShape);
1183 }
1184
1185 void CGeometryDoc::simplify(const TopoDS_Shape& aShape)
1186 {
1187     myCResultDialog.SetTitle("Simplify Face");
1188     myCResultDialog.SetText("  TopoDS_Shape aShape;\n"
1189     "\n"
1190     "  // initialize aShape\n"
1191     "  //aShape = ...\n"
1192     "\n"
1193     "  // define parameter triangulation\n"
1194     "  Standard_Real aDeflection = 0.1;\n"
1195     "  \n"
1196     "  // removes all the triangulations of the faces ,\n"
1197     "  //and all the polygons on the triangulations of the edges\n"
1198     "  BRepTools::Clean(aShape);\n"
1199     "  // adds a triangulation of the shape aShape with the deflection aDeflection\n"
1200     "  BRepMesh::Mesh(aShape,aDeflection);\n"
1201     "\n"
1202     "  Standard_Integer aIndex = 1, nbNodes = 0;\n"
1203     "  \n"
1204     "  // define two sequence of points\n"
1205     "  TColgp_SequenceOfPnt aPoints, aPoints1;\n"
1206     " \n"
1207     "  // triangulation\n"
1208     "  for(TopExp_Explorer aExpFace(aShape,TopAbs_FACE); aExpFace.More(); aExpFace.Next())\n"
1209     "  {  \n"
1210     "    TopoDS_Face aFace = TopoDS::Face(aExpFace.Current());\n"
1211     "    TopLoc_Location aLocation;\n"
1212     "\n"
1213     "    // takes the triangulation of the face aFace\n"
1214     "    Handle(Poly_Triangulation) aTr = BRep_Tool::Triangulation(aFace,aLocation);\n"
1215     "\n"
1216     "    if(!aTr.IsNull())\n"
1217     "    { \n"
1218     "      // takes the array of nodes for this triangulation\n"
1219     "      const TColgp_Array1OfPnt& aNodes = aTr->Nodes();    \n"
1220     "      nbNodes = aNodes.Length();\n"
1221     "\n"
1222     "      for( Standard_Integer i = 1; i <= nbNodes; i++)\n"
1223     "      {\n"
1224     "        // create seguence of node points in absolute coordinate system\n"
1225     "        gp_Pnt aPnt = aNodes(i).Transformed(aLocation);\n"
1226     "        aPoints.Append(aPnt);\n"
1227     "        \n"
1228     "      }\n"
1229     "    }\n"
1230     "  }\n"
1231     " \n"
1232     "  // remove double points\n"
1233     "  nbNodes = aPoints.Length();\n"
1234     "  for( Standard_Integer i = 1; i <= nbNodes; i++)\n"
1235     "  {\n"
1236     "    gp_Pnt aPi = aPoints(i);\n"
1237     "    for( Standard_Integer j = i + 1; j < nbNodes; j++)\n"
1238     "    {\n"
1239     "      gp_Pnt aPj = aPoints(j);\n"
1240     "      if(!aPi.IsEqual(aPj,0.9))\n"
1241     "        aIndex++;\n"
1242     "    }\n"
1243     "    if(aIndex == j - 1)\n"
1244     "      aPoints1.Append(aPi);\n"
1245     "\n"
1246     "    aIndex = i + 1;\n"
1247     "  }\n"
1248     "\n"
1249     "  // find max point\n"
1250     "  aIndex = 0;\n"
1251     "  gp_Pnt aPntMax = aPoints1(1);\n"
1252     "  nbNodes = aPoints1.Length();\n"
1253     "  for(i = 2; i <= nbNodes; i++)\n"
1254     "  {\n"
1255     "    if(aPoints1(i).X() > aPntMax.X())\n"
1256     "    {\n"
1257     "      aIndex = i;\n"
1258     "      aPntMax = aPoints1(aIndex);      \n"
1259     "    } \n"
1260     "  }\n"
1261     "\n"
1262     "  // clear seguence\n"
1263     "  aPoints.Clear();\n"
1264     "\n"
1265     "  Standard_Integer nbLeftNodes = nbNodes;\n"
1266     "\n"
1267     "  // ascending sort - fill aPoints with ascending \n"
1268     "  // by X coordinate points from aPoints1\n"
1269     "  for(i = 1; i < nbNodes; i++)\n"
1270     "  {\n"
1271     "    Standard_Real aMin = aPntMax.X();\n"
1272     "    aIndex = 1;\n"
1273     "    for( Standard_Integer j = 1; j <= nbLeftNodes; j++)\n"
1274     "    {\n"
1275     "      if(aPoints1(j).X() < aMin)\n"
1276     "      {\n"
1277     "        aMin = aPoints1(j).X();\n"
1278     "        aIndex = j;\n"
1279     "      } \n"
1280     "    }\n"
1281     "    aPoints.Append(aPoints1(aIndex));\n"
1282     "    aPoints1.Remove(aIndex);\n"
1283     "    nbLeftNodes = aPoints1.Length();\n"
1284     "  }\n"
1285     "  aPoints.Append(aPntMax);\n"
1286     "\n"
1287     "  // define parameters GeomPlate_BuildPlateSurface\n"
1288     "  Standard_Integer Degree = 3;\n"
1289     "  Standard_Integer NbPtsOnCur = 10;\n"
1290     "  Standard_Integer NbIter = 3;\n"
1291     "  Standard_Integer Order = 0;\n"
1292     "  Standard_Integer MaxSeg = 9;\n"
1293     "  Standard_Integer MaxDegree = 5;\n"
1294     "  Standard_Real dmax, anApproxTol = 0.001;\n"
1295     "  Standard_Real aConstrTol = Precision::Confusion();\n"
1296     "  \n"
1297     "  // define object BuildPlateSurface\n"
1298     "  GeomPlate_BuildPlateSurface BPSurf(Degree,NbPtsOnCur,NbIter);\n"
1299     "  \n"
1300     "  // add point constraints to GeomPlate_BuildPlateSurface object\n"
1301     "  nbNodes = aPoints.Length();\n"
1302     "  for (i = 1; i <= nbNodes; i++)\n"
1303     "    BPSurf.Add(new GeomPlate_PointConstraint(aPoints(i), Order, aConstrTol));\n"
1304     "\n"
1305     "  BPSurf.Perform();\n"
1306     "\n"
1307     "  // make PlateSurface\n"
1308     "  Handle(GeomPlate_Surface) PSurf;\n"
1309     "  Handle(Geom_Surface) aSurf;\n"
1310     "  \n"
1311     "  if (BPSurf.IsDone())\n"
1312     "  {\n"
1313     "    PSurf = BPSurf.Surface();\n"
1314     "\n"
1315     "    // define parameter approximation\n"
1316     "    dmax = Max(0.01,10*BPSurf.G0Error());\n"
1317     "\n"
1318     "    // make approximation\n"
1319     "    GeomPlate_MakeApprox Mapp(PSurf,anApproxTol, MaxSeg,MaxDegree,dmax);\n"
1320     "    aSurf = Mapp.Surface();\n"
1321     "  }\n"
1322     "  else \n"
1323     "    return;\n"
1324     "\n"
1325     "  ShapeAnalysis_FreeBounds aFreeBounds(aShape, Standard_False, Standard_True);\n"
1326     "  TopoDS_Compound aClosedWires = aFreeBounds.GetClosedWires();\n"
1327     "  TopTools_IndexedMapOfShape aWires;\n"
1328     "  TopExp::MapShapes(aClosedWires, TopAbs_WIRE, aWires);\n"
1329     "  TopoDS_Wire aWire;\n"
1330     "  Standard_Integer nbWires = aWires.Extent();\n"
1331     "  if (nbWires) \n"
1332     "    aWire = TopoDS::Wire(aWires(1));\n"
1333     "  else \n"
1334     "    return;\n"
1335     "\n"
1336     "  BRep_Builder B;\n"
1337     "  TopoDS_Face aFace;\n"
1338     "  B.MakeFace(aFace, aSurf, Precision::Confusion());\n"
1339     "  B.Add(aFace, aWire);\n"
1340     "  Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape(aFace);\n"
1341     "  sfs->Perform();\n"
1342     "  TopoDS_Shape aFixedFace = sfs->Shape();\n"
1343     "  if (aFixedFace.IsNull()) \n"
1344     "    return;\n");
1345
1346     // define parameter triangulation
1347     Standard_Real aDeflection = 0.1;
1348
1349     // removes all the triangulations of the faces ,
1350     //and all the polygons on the triangulations of the edges
1351     BRepTools::Clean(aShape);
1352     // adds a triangulation of the shape aShape with the deflection aDeflection
1353     BRepMesh_IncrementalMesh(aShape,aDeflection);
1354
1355     Standard_Integer aIndex = 1, nbNodes = 0;
1356
1357     // define two sequence of points
1358     TColgp_SequenceOfPnt aPoints, aPoints1;
1359
1360     // triangulation
1361     for(TopExp_Explorer aExpFace(aShape,TopAbs_FACE); aExpFace.More(); aExpFace.Next())
1362     {  
1363       TopoDS_Face aFace = TopoDS::Face(aExpFace.Current());
1364       TopLoc_Location aLocation;
1365
1366       // takes the triangulation of the face aFace
1367       Handle(Poly_Triangulation) aTr = BRep_Tool::Triangulation(aFace,aLocation);
1368
1369       if(!aTr.IsNull())
1370       { 
1371         // takes the array of nodes for this triangulation
1372         const TColgp_Array1OfPnt& aNodes = aTr->Nodes();    
1373         nbNodes = aNodes.Length();
1374
1375         for( Standard_Integer i = 1; i <= nbNodes; i++)
1376         {
1377           // create seguence of node points in absolute coordinate system
1378           gp_Pnt aPnt = aNodes(i).Transformed(aLocation);
1379           aPoints.Append(aPnt);
1380
1381         }
1382       }
1383     }
1384
1385     // remove double points
1386     nbNodes = aPoints.Length();
1387     Standard_Integer i;
1388     for( i = 1; i <= nbNodes; i++)
1389     {
1390       gp_Pnt aPi = aPoints(i);
1391       Standard_Integer j;
1392       for( j = i + 1; j < nbNodes; j++)
1393       {
1394         gp_Pnt aPj = aPoints(j);
1395         if(!aPi.IsEqual(aPj,0.9))
1396           aIndex++;
1397       }
1398       if(aIndex == j - 1)
1399         aPoints1.Append(aPi);
1400
1401       aIndex = i + 1;
1402     }
1403
1404     // find max point
1405     aIndex = 0;
1406     gp_Pnt aPntMax = aPoints1(1);
1407     nbNodes = aPoints1.Length();
1408     for(i = 2; i <= nbNodes; i++)
1409     {
1410       if(aPoints1(i).X() > aPntMax.X())
1411       {
1412         aIndex = i;
1413         aPntMax = aPoints1(aIndex);
1414       } 
1415     }
1416
1417     // clear seguence
1418     aPoints.Clear();
1419
1420     Standard_Integer nbLeftNodes = nbNodes;
1421
1422     // ascending sort - fill aPoints with ascending 
1423     // by X coordinate points from aPoints1
1424     for(i = 1; i < nbNodes; i++)
1425     {
1426       Standard_Real aMin = aPntMax.X();
1427       aIndex = 1;
1428       for( Standard_Integer j = 1; j <= nbLeftNodes; j++)
1429       {
1430         if(aPoints1(j).X() < aMin)
1431         {
1432           aMin = aPoints1(j).X();
1433           aIndex = j;
1434         } 
1435       }
1436       aPoints.Append(aPoints1(aIndex));
1437       aPoints1.Remove(aIndex);
1438       nbLeftNodes = aPoints1.Length();
1439     }
1440     aPoints.Append(aPntMax);
1441
1442     // define parameters GeomPlate_BuildPlateSurface
1443     Standard_Integer Degree = 3;
1444     Standard_Integer NbPtsOnCur = 10;
1445     Standard_Integer NbIter = 3;
1446     Standard_Integer Order = 0;
1447     Standard_Integer MaxSeg = 9;
1448     Standard_Integer MaxDegree = 5;
1449     Standard_Real dmax, anApproxTol = 0.001;
1450     Standard_Real aConstrTol = Precision::Confusion();
1451
1452     // define object BuildPlateSurface
1453     GeomPlate_BuildPlateSurface BPSurf(Degree,NbPtsOnCur,NbIter);
1454
1455     // add point constraints to GeomPlate_BuildPlateSurface object
1456     nbNodes = aPoints.Length();
1457     for (i = 1; i <= nbNodes; i++)
1458       BPSurf.Add(new GeomPlate_PointConstraint(aPoints(i), Order, aConstrTol));
1459
1460     BPSurf.Perform();
1461
1462     // make PlateSurface
1463     Handle(GeomPlate_Surface) PSurf;
1464     Handle(Geom_Surface) aSurf;
1465
1466     if (BPSurf.IsDone())
1467     {
1468       PSurf = BPSurf.Surface();
1469
1470       // define parameter approximation
1471       dmax = Max(0.01,10*BPSurf.G0Error());
1472
1473       // make approximation
1474       GeomPlate_MakeApprox Mapp(PSurf,anApproxTol, MaxSeg,MaxDegree,dmax);
1475       aSurf = Mapp.Surface();
1476     }
1477     else 
1478       return;
1479
1480     ShapeAnalysis_FreeBounds aFreeBounds(aShape, Standard_False, Standard_True);
1481     TopoDS_Compound aClosedWires = aFreeBounds.GetClosedWires();
1482     TopTools_IndexedMapOfShape aWires;
1483     TopExp::MapShapes(aClosedWires, TopAbs_WIRE, aWires);
1484     TopoDS_Wire aWire;
1485     Standard_Integer nbWires = aWires.Extent();
1486     if (nbWires) 
1487       aWire = TopoDS::Wire(aWires(1));
1488     else 
1489       return;
1490
1491     BRep_Builder B;
1492     TopoDS_Face aFace;
1493     B.MakeFace(aFace, aSurf, Precision::Confusion());
1494     B.Add(aFace, aWire);
1495     Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape(aFace);
1496     sfs->Perform();
1497     TopoDS_Shape aFixedFace = sfs->Shape();
1498     if (aFixedFace.IsNull()) 
1499       return;
1500
1501     // output surface, make it half transparent
1502     Handle(AIS_InteractiveObject) aSurfIO = drawSurface(
1503       aSurf, Quantity_NOC_LEMONCHIFFON3, Standard_False);
1504     aSurfIO->SetTransparency(0.5);
1505     myAISContext->Display(aSurfIO,Standard_False);
1506     Fit();
1507
1508     if(WAIT_A_LITTLE) return;
1509
1510     // output points
1511     for(i = 1; i <= nbNodes; i++)
1512       drawPoint(aPoints(i));
1513
1514     if(WAIT_A_LITTLE) return;
1515
1516     // output resulting face
1517     drawShape(aFixedFace);
1518 }
1519
1520 Handle(AIS_InteractiveObject) CGeometryDoc::drawSurface
1521                                   (const Handle(Geom_Surface)& theSurface,
1522                                    const Quantity_Color& theColor,
1523                                    const Standard_Boolean toDisplay)
1524 {
1525   Standard_Real u1, u2, v1, v2;
1526   theSurface->Bounds(u1,u2,v1,v2);
1527   fixParam(u1);
1528   fixParam(u2);
1529   fixParam(v1);
1530   fixParam(v2);
1531
1532   Handle(AIS_Shape) aGraphicSurface = 
1533     new AIS_Shape(BRepBuilderAPI_MakeFace (theSurface, u1, u2, v1, v2, Precision::Confusion()));
1534
1535   myAISContext->SetMaterial(aGraphicSurface, Graphic3d_NOM_PLASTIC, toDisplay);
1536   myAISContext->SetColor(aGraphicSurface, theColor, toDisplay);
1537   if (toDisplay)
1538   {
1539     if (FitMode)
1540     {
1541       myAISContext->Display (aGraphicSurface, Standard_False);
1542       Fit();
1543     }
1544     else
1545       myAISContext->Display (aGraphicSurface, Standard_True);
1546   }
1547
1548   return aGraphicSurface;
1549 }
1550
1551 Standard_Boolean CGeometryDoc::WaitForInput (unsigned long aMilliSeconds)
1552 {
1553   //::WaitForSingleObject(::CreateEvent (NULL, FALSE, FALSE, NULL), aMilliSeconds);
1554   if (::MsgWaitForMultipleObjects(0, NULL, FALSE, aMilliSeconds,
1555     QS_KEY | QS_MOUSEBUTTON) != WAIT_TIMEOUT)
1556   {
1557     MSG msg;
1558     if (::PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
1559     {
1560       if (msg.message == WM_KEYUP)
1561       {
1562         ::PeekMessage (&msg, NULL, 0, 0, PM_REMOVE);
1563         return WaitForInput (aMilliSeconds);
1564       }
1565       else
1566         return Standard_True;
1567     }
1568   }
1569   return Standard_False;
1570 }
1571
1572 Handle(AIS_Point) CGeometryDoc::drawPoint
1573                                   (const gp_Pnt& aPnt,
1574                                    const Quantity_Color& theColor,
1575                                    const Standard_Boolean toDisplay)
1576 {
1577   Handle(AIS_Point) aGraphicPoint = new AIS_Point (new Geom_CartesianPoint(aPnt));
1578
1579   myAISContext->SetColor (aGraphicPoint, theColor, toDisplay);
1580   if (toDisplay)
1581   {
1582   myAISContext->Display (aGraphicPoint, Standard_True);
1583     //COCCDemoDoc::Fit();
1584   }
1585
1586   return aGraphicPoint;
1587 }
1588
1589 Handle(AIS_Shape) CGeometryDoc::drawShape
1590          (const TopoDS_Shape& theShape,
1591           const Graphic3d_NameOfMaterial theMaterial,
1592           const Standard_Boolean toDisplay)
1593 {
1594   Handle(AIS_Shape) aGraphicShape = new AIS_Shape(theShape);
1595
1596   myAISContext->SetMaterial(aGraphicShape, theMaterial, toDisplay);
1597   if (toDisplay)
1598   {
1599     if (FitMode)
1600     {
1601       myAISContext->Display (aGraphicShape, Standard_False);
1602       Fit();
1603     }
1604     else
1605       myAISContext->Display (aGraphicShape, Standard_True);
1606   }
1607
1608   return aGraphicShape;
1609 }