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