0026936: Drawbacks of inlining in new type system in OCCT 7.0 -- automatic
[occt.git] / samples / mfc / standard / 04_Viewer3d / src / Viewer3dDoc.cpp
1 // Viewer3dDoc.cpp : implementation of the CViewer3dDoc class
2 //
3
4 #include "stdafx.h"
5 #include "Viewer3dApp.h"
6 #include "BoxRadius.h"
7 #include "DlgIsos.h"
8
9 #include "Viewer3dDoc.h"
10 #include "Viewer3dView.h"
11 #include "OffsetDlg.h"
12 #include "ResultDialog.h"
13 #include "User_Cylinder.hxx"
14
15 #include "AIS_Trihedron.hxx"
16 #include "Geom_Axis2Placement.hxx"
17
18 /////////////////////////////////////////////////////////////////////////////
19 // CViewer3dDoc
20
21 IMPLEMENT_DYNCREATE(CViewer3dDoc, CDocument)
22
23 BEGIN_MESSAGE_MAP(CViewer3dDoc, OCC_3dDoc)
24   //{{AFX_MSG_MAP(CViewer3dDoc)
25   ON_COMMAND(ID_BOX, OnBox)
26   ON_COMMAND(ID_Cylinder, OnCylinder)
27   ON_COMMAND(ID_SPHERE, OnSphere)
28   ON_COMMAND(ID_ERASEALL, OnRemoveAll)
29   ON_COMMAND(ID_NBISOS, OnNbisos)
30   ON_COMMAND(ID_FACES, OnFaces)
31   ON_COMMAND(ID_EDGES, OnEdges)
32   ON_COMMAND(ID_VERTICES, OnVertices)
33   ON_COMMAND(ID_NEUTRAL, OnNeutral)
34   ON_COMMAND(ID_USERCYLINDER_CHANGEFACECOLOR, OnUsercylinderChangefacecolor)
35   ON_COMMAND(ID_FILLET3D, OnFillet3d)
36   ON_COMMAND(ID_CIRCLE, OnCircle)
37   ON_COMMAND(ID_LINE, OnLine)
38   ON_COMMAND(ID_OVERLAPPED_BOX, OnOverlappedBox)
39   ON_COMMAND(ID_OVERLAPPED_CYLINDER, OnOverlappedCylinder)
40   ON_COMMAND(ID_OVERLAPPED_SPHERE, OnOverlappedSphere)
41   ON_COMMAND(ID_POLYGON_OFFSETS, OnPolygonOffsets)
42   ON_UPDATE_COMMAND_UI(ID_POLYGON_OFFSETS, OnUpdatePolygonOffsets)
43   ON_UPDATE_COMMAND_UI(ID_Cylinder, OnUpdateCylinder)
44   ON_UPDATE_COMMAND_UI(ID_SPHERE, OnUpdateSphere)
45   ON_UPDATE_COMMAND_UI(ID_BOX, OnUpdateBox)
46   ON_UPDATE_COMMAND_UI(ID_OVERLAPPED_CYLINDER, OnUpdateOverlappedCylinder)
47   ON_UPDATE_COMMAND_UI(ID_OVERLAPPED_SPHERE, OnUpdateOverlappedSphere)
48   ON_UPDATE_COMMAND_UI(ID_OVERLAPPED_BOX, OnUpdateOverlappedBox)
49   ON_COMMAND(ID_OBJECT_REMOVE, OnObjectRemove)
50   ON_COMMAND(ID_OBJECT_ERASE, OnObjectErase)
51   ON_COMMAND(ID_OBJECT_DISPLAYALL, OnObjectDisplayall)
52   ON_COMMAND(ID_OBJECT_COLORED_MESH, OnObjectColoredMesh)
53   ON_UPDATE_COMMAND_UI(ID_OBJECT_COLORED_MESH, OnUpdateObjectColoredMesh)
54   ON_UPDATE_COMMAND_UI(ID_OBJECT_SHADING, OnUpdateObjectShading)
55   ON_UPDATE_COMMAND_UI(ID_OBJECT_WIREFRAME, OnUpdateObjectWireframe)
56   ON_COMMAND(ID_OPTIONS_TRIHEDRON_DYNAMIC_TRIHEDRON, OnOptionsTrihedronDynamicTrihedron)
57   ON_UPDATE_COMMAND_UI(ID_OPTIONS_TRIHEDRON_DYNAMIC_TRIHEDRON, OnUpdateOptionsTrihedronDynamicTrihedron)
58   ON_UPDATE_COMMAND_UI(ID_OPTIONS_TRIHEDRON_STATIC_TRIHEDRON, OnUpdateOptionsTrihedronStaticTrihedron)
59   ON_COMMAND(ID_OBJECT_MATERIAL, OnObjectMaterial)
60   ON_COMMAND(ID_TEXTURE_ON, OnTextureOn)
61   ON_COMMAND(ID_BUTTONNext, OnBUTTONNext)
62   ON_COMMAND(ID_BUTTONStart, OnBUTTONStart)
63   ON_COMMAND(ID_BUTTONRepeat, OnBUTTONRepeat)
64   ON_COMMAND(ID_BUTTONPrev, OnBUTTONPrev)
65   ON_COMMAND(ID_BUTTONEnd, OnBUTTONEnd)
66   ON_UPDATE_COMMAND_UI(ID_BUTTONNext, OnUpdateBUTTONNext)
67   ON_UPDATE_COMMAND_UI(ID_BUTTONPrev, OnUpdateBUTTONPrev)
68   ON_UPDATE_COMMAND_UI(ID_BUTTONStart, OnUpdateBUTTONStart)
69   ON_UPDATE_COMMAND_UI(ID_BUTTONRepeat, OnUpdateBUTTONRepeat)
70   ON_UPDATE_COMMAND_UI(ID_BUTTONEnd, OnUpdateBUTTONEnd)
71   ON_COMMAND(ID_FILE_NEW, OnFileNew)
72   ON_COMMAND(ID_DUMP_VIEW, OnDumpView)
73   //}}AFX_MSG_MAP
74 END_MESSAGE_MAP()
75
76 /////////////////////////////////////////////////////////////////////////////
77 // CViewer3dDoc construction/destruction
78
79 CViewer3dDoc::CViewer3dDoc()
80 :OCC_3dDoc()
81 {
82   myCylinder.Nullify();
83   mySphere.Nullify();
84   myBox.Nullify();
85   myOverlappedCylinder.Nullify();
86   myOverlappedSphere.Nullify();
87   myOverlappedBox.Nullify();
88   myOffsetDlg = NULL;
89   myStaticTrihedronAxisIsDisplayed = FALSE;
90   myState = -1;
91
92   isTextureSampleStarted = FALSE;
93
94   myPresentation = OCCDemo_Presentation::Current;
95   myPresentation->SetDocument(this);
96
97
98   myAISContext->DefaultDrawer()->ShadingAspect()->SetColor(Quantity_NOC_CHARTREUSE1); 
99   myAISContext->DefaultDrawer()->ShadingAspect()->SetMaterial(Graphic3d_NOM_SILVER);
100
101   strcpy_s(myDataDir, "Data");
102   strcpy_s(myLastPath, ".");
103
104   /*
105   Handle(AIS_Trihedron) myTrihedron;
106   Handle(Geom_Axis2Placement) myTrihedronAxis=new Geom_Axis2Placement(gp::XOY());
107   myTrihedron=new AIS_Trihedron(myTrihedronAxis);
108   myAISContext->Display(myTrihedron);
109   */
110 }
111
112 CViewer3dDoc::~CViewer3dDoc()
113 {
114 }
115
116 /////////////////////////////////////////////////////////////////////////////
117 // CViewer3dDoc diagnostics
118
119 #ifdef _DEBUG
120 void CViewer3dDoc::AssertValid() const
121 {
122   CDocument::AssertValid();
123 }
124
125 void CViewer3dDoc::Dump(CDumpContext& dc) const
126 {
127   CDocument::Dump(dc);
128 }
129 #endif //_DEBUG
130
131 /////////////////////////////////////////////////////////////////////////////
132 // CViewer3dDoc commands
133
134 void CViewer3dDoc::UpdateResultMessageDlg (CString theTitle, const TCollection_AsciiString& theMessage)
135 {
136   CString aText (theMessage.ToCString());
137   myCResultDialog.SetText (aText);
138   myCResultDialog.SetTitle(theTitle);
139 }
140
141 void CViewer3dDoc::UpdateResultMessageDlg(CString theTitle, CString theMessage)
142 {
143   myCResultDialog.SetText (theMessage);
144   myCResultDialog.SetTitle(theTitle);
145 }
146
147 void CViewer3dDoc::OnBox()
148 {
149   if(myBox.IsNull())
150   {
151     BRepPrimAPI_MakeBox B(gp_Pnt(0,-400,-100), 200.,150.,100.);
152
153
154     myBox = new AIS_Shape(B.Shape());
155
156     myAISContext->SetMaterial(myBox,Graphic3d_NOM_PEWTER);
157     myAISContext->SetDisplayMode(myBox,1);
158
159     myAISContext->Display(myBox);
160     TCollection_AsciiString Message("\
161 BRepPrimAPI_MakeBox Box1(gp_Pnt(0,-400,-100), 200.,150.,100.);\n\
162     ");
163
164     UpdateResultMessageDlg("Create Box",Message);
165   }
166 }
167
168 void CViewer3dDoc::OnCylinder()
169 {
170   if(myCylinder.IsNull())
171   {
172     gp_Ax2 CylAx2(gp_Pnt(0,0,-100), gp_Dir(gp_Vec(gp_Pnt(0,0,-100),gp_Pnt(0,0,100))));
173     myCylinder = new User_Cylinder(CylAx2, 80.,200.);
174
175     myAISContext->SetDisplayMode(myCylinder,1);
176
177     myAISContext->Display(myCylinder);
178
179     TCollection_AsciiString Message("\
180 gp_Ax2 CylAx2(gp_Pnt(0,0,-100), gp_Dir(gp_Vec(gp_Pnt(0,0,-100),gp_Pnt(0,0,100))));\n\
181 C = new User_Cylinder(CylAx2, 80.,200.);;\n\
182     ");
183
184     UpdateResultMessageDlg("Create Cylinder",Message);
185   }
186 }
187
188 void CViewer3dDoc::OnSphere()
189 {
190   if(mySphere.IsNull())
191   {
192     BRepPrimAPI_MakeSphere S(gp_Pnt(0,300,0), 100.);
193
194     mySphere = new AIS_Shape(S.Shape());
195
196     myAISContext->SetMaterial(mySphere,Graphic3d_NOM_BRONZE);
197     myAISContext->SetDisplayMode(mySphere,1);
198
199     myAISContext->Display(mySphere);
200     TCollection_AsciiString Message("\
201 BRepPrimAPI_MakeSphere S(gp_Pnt(0,300,0), 100.);\n\
202     ");
203     UpdateResultMessageDlg("Create Sphere",Message);
204   }
205 }
206
207 void CViewer3dDoc::OnRemoveAll()
208
209 {
210   AIS_ListOfInteractive aListOfObjects;
211   myAISContext->ObjectsInside(aListOfObjects,AIS_KOI_Shape);
212
213   AIS_ListIteratorOfListOfInteractive aListIterator;
214   for(aListIterator.Initialize(aListOfObjects);aListIterator.More();aListIterator.Next()){
215     myAISContext->Remove(aListIterator.Value());
216   }
217
218   myAISContext->Remove(myCylinder);
219
220
221   myCylinder.Nullify();
222   mySphere.Nullify();
223   myBox.Nullify();
224   myOverlappedCylinder.Nullify();
225   myOverlappedSphere.Nullify();
226   myOverlappedBox.Nullify();
227   if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
228     myOffsetDlg->UpdateValues();
229 }
230
231 void CViewer3dDoc::OnOverlappedBox() 
232 {
233   OnBox();
234   if(myOverlappedBox.IsNull()){
235     BRepPrimAPI_MakeBox B(gp_Pnt(0,-400,-100), 200.,150.,100.);
236
237     BRepBuilderAPI_NurbsConvert aNurbsConvert(B.Shape());
238     TopoDS_Shape aBoxShape2 = aNurbsConvert.Shape();
239
240
241     myOverlappedBox = new AIS_Shape(aBoxShape2);
242
243     myAISContext->SetMaterial(myOverlappedBox,Graphic3d_NOM_GOLD);
244     myAISContext->SetDisplayMode(myOverlappedBox,1);
245
246     myAISContext->Display(myOverlappedBox);
247     TCollection_AsciiString Message("\
248 BRepPrimAPI_MakeBox Box1(gp_Pnt(0,-400,-100), 200.,150.,100.);\n\
249 \n\
250 BRepPrimAPI_MakeBox Box2(gp_Pnt(0,-400,-100), 200.,150.,100.);\n\
251 BRepBuilderAPI_NurbsConvert aNurbsConvert(Box2.Shape());\n\
252     ");
253     UpdateResultMessageDlg("Create overlapped boxes",Message);
254     if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
255       myOffsetDlg->UpdateValues();
256   }
257 }
258
259 void CViewer3dDoc::OnOverlappedCylinder()
260 {
261   OnCylinder();
262
263   if(myOverlappedCylinder.IsNull()){
264     gp_Ax2 CylAx2(gp_Pnt(0,0,-100), gp_Dir(gp_Vec(gp_Pnt(0,0,-100),gp_Pnt(0,0,100))));
265     BRepPrimAPI_MakeCylinder C(CylAx2, 80.,200.);
266
267     BRepBuilderAPI_NurbsConvert aNurbsConvert(C.Shape());
268     TopoDS_Shape aCylShape2 = aNurbsConvert.Shape();
269
270     myOverlappedCylinder = new AIS_Shape(aCylShape2);
271
272
273     myAISContext->SetMaterial(myOverlappedCylinder,Graphic3d_NOM_GOLD);    
274     myAISContext->SetDisplayMode(myOverlappedCylinder,1);
275
276     myAISContext->Display(myOverlappedCylinder);
277
278     TCollection_AsciiString Message("\
279 gp_Ax2 CylAx2(gp_Pnt(0,0,-100), gp_Dir(gp_Vec(gp_Pnt(0,0,-100),gp_Pnt(0,0,100))));\n\
280 Cylinder1 = new User_Cylinder(CylAx2, 80.,200.);\n\
281 \n\
282 BRepPrimAPI_MakeCylinder Cylinder2(CylAx2, 80.,200.);\n\
283 BRepBuilderAPI_NurbsConvert aNurbsConvert(Cylinder2.Shape());\n\
284     ");
285     UpdateResultMessageDlg("Create overlapped cylinders",Message);
286     if (myOffsetDlg && myOffsetDlg->IsWindowVisible())
287     {
288       myOffsetDlg->UpdateValues();
289     }
290   }
291 }
292
293 void CViewer3dDoc::OnOverlappedSphere()
294 {
295   OnSphere();
296   if(myOverlappedSphere.IsNull()){
297     BRepPrimAPI_MakeSphere S(gp_Pnt(0,300,0), 100.);
298
299     BRepBuilderAPI_NurbsConvert aNurbsConvert(S.Shape());
300     TopoDS_Shape aSphereShape2 = aNurbsConvert.Shape();
301
302     myOverlappedSphere = new AIS_Shape(aSphereShape2);
303
304     myAISContext->SetMaterial(myOverlappedSphere,Graphic3d_NOM_GOLD);    
305
306     myAISContext->SetDisplayMode(myOverlappedSphere,1);
307
308     myAISContext->Display(myOverlappedSphere);
309
310     TCollection_AsciiString Message("\
311 BRepPrimAPI_MakeSphere Sphere1(gp_Pnt(0,300,0), 100.);\n\
312 \n\
313 BRepPrimAPI_MakeSphere Sphere2(gp_Pnt(0,300,0), 100.);\n\
314 BRepBuilderAPI_NurbsConvert aNurbsConvert(Sphere2.Shape());\n\
315     ");
316     UpdateResultMessageDlg("Create overlapped spheres",Message);
317     if (myOffsetDlg && myOffsetDlg->IsWindowVisible())
318     {
319       myOffsetDlg->UpdateValues();
320     }
321   }
322 }
323
324 void CViewer3dDoc::OnPolygonOffsets()
325 {
326   myOffsetDlg = new COffsetDlg(this);
327   myOffsetDlg->Create(COffsetDlg::IDD,NULL);
328   myAISContext->UpdateCurrentViewer();
329 }
330
331 void CViewer3dDoc::OnUpdatePolygonOffsets(CCmdUI* pCmdUI) 
332 {
333   Standard_Integer aOffsetMode;
334   Standard_ShortReal aFactor;
335   Standard_ShortReal aCylUnits = 0;
336   Standard_ShortReal aSphereUnits = 0;
337   Standard_ShortReal aBoxUnits = 0;
338
339   BOOL IsOverlappedCylinderDisplayed = myAISContext->IsDisplayed(myOverlappedCylinder);
340   BOOL IsOverlappedSphereDisplayed = myAISContext->IsDisplayed(myOverlappedSphere);
341   BOOL IsOverlappedBoxDisplayed = myAISContext->IsDisplayed(myOverlappedBox);
342   BOOL IsAnyOverlappedObjectDisplayed = 
343     IsOverlappedCylinderDisplayed || IsOverlappedSphereDisplayed || IsOverlappedBoxDisplayed;
344
345   if(!myOverlappedCylinder.IsNull() && IsOverlappedCylinderDisplayed){
346     myOverlappedCylinder->PolygonOffsets(aOffsetMode,aFactor,aCylUnits);
347   }
348
349   if(!myOverlappedSphere.IsNull() && IsOverlappedSphereDisplayed){
350     myOverlappedSphere->PolygonOffsets(aOffsetMode,aFactor,aSphereUnits);
351   }
352
353   if(!myOverlappedBox.IsNull() && IsOverlappedBoxDisplayed){
354     myOverlappedBox->PolygonOffsets(aOffsetMode,aFactor,aBoxUnits);
355   }
356
357   if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
358     pCmdUI->SetCheck(TRUE);
359   else
360     pCmdUI->SetCheck(FALSE);
361   if (IsAnyOverlappedObjectDisplayed)
362     if(myOffsetDlg)
363       pCmdUI->Enable(!myOffsetDlg->IsWindowVisible());
364     else
365       pCmdUI->Enable(TRUE);
366   else
367     pCmdUI->Enable(FALSE);
368 }
369
370 void CViewer3dDoc::OnUpdateCylinder(CCmdUI* pCmdUI)
371 {
372   pCmdUI->Enable(myCylinder.IsNull());
373 }
374
375 void CViewer3dDoc::OnUpdateSphere(CCmdUI* pCmdUI)
376 {
377   pCmdUI->Enable(mySphere.IsNull()); 
378 }
379
380 void CViewer3dDoc::OnUpdateBox(CCmdUI* pCmdUI)
381 {
382   pCmdUI->Enable(myBox.IsNull()); 
383 }
384
385 void CViewer3dDoc::OnUpdateOverlappedCylinder(CCmdUI* pCmdUI)
386 {
387   pCmdUI->Enable   (myOverlappedCylinder.IsNull()
388     || myCylinder.IsNull());
389
390 }
391
392 void CViewer3dDoc::OnUpdateOverlappedSphere(CCmdUI* pCmdUI)
393 {
394   pCmdUI->Enable   (myOverlappedSphere.IsNull()
395     || mySphere.IsNull());
396 }
397
398 void CViewer3dDoc::OnUpdateOverlappedBox(CCmdUI* pCmdUI)
399 {
400   pCmdUI->Enable   (myOverlappedBox.IsNull()
401     || myBox.IsNull());
402 }
403
404 void CViewer3dDoc::OnObjectRemove()
405 {
406   if(myAISContext->IsCurrent(myBox))
407     myBox.Nullify();
408
409   if(myAISContext->IsCurrent(myCylinder))
410     myCylinder.Nullify();
411
412   if(myAISContext->IsCurrent(mySphere))
413     mySphere.Nullify();
414
415   if(myAISContext->IsCurrent(myOverlappedBox))
416     myOverlappedBox.Nullify();
417
418   if(myAISContext->IsCurrent(myOverlappedCylinder))
419     myOverlappedCylinder.Nullify();
420
421   if(myAISContext->IsCurrent(myOverlappedSphere))
422     myOverlappedSphere.Nullify();
423
424
425   for(myAISContext->InitCurrent();myAISContext->MoreCurrent();myAISContext->InitCurrent())
426     myAISContext->Remove(myAISContext->Current(),Standard_True);
427
428   if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
429     myOffsetDlg->UpdateValues();
430 }
431
432 void CViewer3dDoc::OnObjectErase()
433
434 {
435   OCC_3dBaseDoc::OnObjectErase();
436   if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
437     myOffsetDlg->UpdateValues();
438 }
439
440 void CViewer3dDoc::OnObjectDisplayall()
441
442 {
443   OCC_3dBaseDoc::OnObjectDisplayall();
444   if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
445     myOffsetDlg->UpdateValues();
446 }
447
448 Handle(User_Cylinder) CViewer3dDoc::GetCylinder()
449 {
450   return myCylinder;
451 }
452
453
454 Handle(AIS_Shape) CViewer3dDoc::GetSphere()
455 {
456   return mySphere;
457 }
458
459 Handle(AIS_Shape) CViewer3dDoc::GetBox()
460 {
461   return myBox;
462 }
463
464 Handle(AIS_Shape) CViewer3dDoc::GetOverlappedCylinder()
465 {
466   return myOverlappedCylinder;
467 }
468
469
470 Handle(AIS_Shape) CViewer3dDoc::GetOverlappedSphere()
471 {
472   return myOverlappedSphere;
473 }
474
475 Handle(AIS_Shape) CViewer3dDoc::GetOverlappedBox()
476 {
477   return myOverlappedBox;
478 }
479
480 void CViewer3dDoc::DragEvent (const Standard_Integer theX,
481                               const Standard_Integer theY,
482                               const Standard_Integer theState,
483                               const Handle(V3d_View)& theView)
484 {
485   OCC_3dBaseDoc::DragEvent (theX, theY, theState, theView);
486   if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
487     myOffsetDlg->UpdateValues();
488 }
489
490 //-----------------------------------------------------------------------------------------
491 //
492 //-----------------------------------------------------------------------------------------
493 void CViewer3dDoc::InputEvent(const Standard_Integer /*x*/,
494                               const Standard_Integer /*y*/,
495                               const Handle(V3d_View)& /*aView*/ )
496 {
497   if (myOffsetDlg && myOffsetDlg->IsWindowVisible())
498     myOffsetDlg->UpdateValues();
499
500   Quantity_Color CSFColor;
501   COLORREF MSColor;
502   myAISContext->Select(); 
503
504   // Change the color of a selected face in a user cylinder
505   if (myState == FACE_COLOR)
506   {
507     myAISContext->InitSelected();
508     if (myAISContext->MoreSelected())
509     {
510       Handle(AIS_InteractiveObject) Current = myAISContext->SelectedInteractive();
511       if (Current->HasColor())
512       {
513         CSFColor = myAISContext->Color (Current);
514         MSColor = RGB (CSFColor.Red()*255.0, CSFColor.Green()*255.0, CSFColor.Blue()*255.0);
515       }
516       else 
517       {
518         MSColor = RGB (255, 255, 255);
519       }
520
521       CColorDialog dlgColor(MSColor);
522       if (dlgColor.DoModal() == IDOK)
523       {
524         MSColor = dlgColor.GetColor();
525         CSFColor = Quantity_Color (GetRValue (MSColor)/255.0,
526                                    GetGValue (MSColor)/255.0,
527                                    GetBValue (MSColor)/255.0,
528                                    Quantity_TOC_RGB);
529
530         TopoDS_Shape S = myAISContext->SelectedShape();
531         Handle(Geom_Surface) Surface = BRep_Tool::Surface (TopoDS::Face(S));
532         if (Surface->IsKind (STANDARD_TYPE (Geom_Plane)))
533         {
534           Handle(User_Cylinder)::DownCast (myAISContext->SelectedInteractive())
535             ->SetPlanarFaceColor (CSFColor.Name());
536         }
537         else
538         {
539           Handle(User_Cylinder)::DownCast (myAISContext->SelectedInteractive())
540             ->SetCylindricalFaceColor (CSFColor.Name());
541         }
542
543         myAISContext->Redisplay (myAISContext->SelectedInteractive());
544         myState = -1;
545         myAISContext->CloseLocalContext();
546       }
547     }
548
549     myCResultDialog.SetTitle("Change face color");
550     myCResultDialog.SetText("  TopoDS_Shape S = myAISContext->SelectedShape(); \n"
551                                       "  \n"
552                                       "  Handle(Geom_Surface) Surface = BRep_Tool::Surface(TopoDS::Face(S));"
553                                       "  if (Surface->IsKind(STANDARD_TYPE(Geom_Plane))) \n"
554                                       "    Handle(User_Cylinder)::DownCast(myAISContext->Current())->SetPlanarFaceColor(CSFColor.Name()); \n"
555                                       "  else \n"
556                                       "    Handle(User_Cylinder)::DownCast(myAISContext->Current())->SetCylindricalFaceColor(CSFColor.Name()); \n"
557                                       "  \n"
558                                       "  myAISContext->Redisplay(myAISContext->Current()); \n"
559                                       "  \n"
560                                       "  myAISContext->CloseLocalContext(); \n"
561                                       "  \n"
562                                       "  \n"
563                                       "  NOTE: a User_Cylinder is an object defined by the user. \n"
564                                       "  The User_Cylinder class inherit from the AIS_InteractiveObject \n"
565                                       "  Cascade class, it's use is the same as an AIS_InteractiveObject. \n"
566                                       "  Methods SetPlanarFaceColor and SetCylindricalFaceColor are also \n"
567                                       "  defined in the User_Cylinder class. \n"
568                                       "  \n");
569     SetTitle (L"Change face color");
570   }
571 }
572
573 //-----------------------------------------------------------------------------------------
574 //
575 //-----------------------------------------------------------------------------------------
576 void CViewer3dDoc::ShiftDragEvent (const Standard_Integer theX,
577                                    const Standard_Integer  theY,
578                                    const Standard_Integer theState,
579                                    const Handle(V3d_View)& theView)
580 {
581   OCC_3dBaseDoc::ShiftDragEvent(theX, theY, theState, theView);
582   if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
583     myOffsetDlg->UpdateValues();
584 }
585
586
587 //-----------------------------------------------------------------------------------------
588 //
589 //-----------------------------------------------------------------------------------------
590 void CViewer3dDoc::ShiftInputEvent (const Standard_Integer theX,
591                                     const Standard_Integer theY,
592                                     const Handle(V3d_View)& theView)
593 {
594   OCC_3dBaseDoc::ShiftInputEvent (theX, theY, theView);
595   if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
596     myOffsetDlg->UpdateValues();
597 }
598
599 void CViewer3dDoc::OnObjectColoredMesh()
600 {
601   for(myAISContext->InitCurrent();myAISContext->MoreCurrent();myAISContext->NextCurrent())
602     if (myAISContext->Current()->IsKind(STANDARD_TYPE(User_Cylinder)))
603     {
604       myAISContext->ClearPrs(myAISContext->Current(),6,Standard_False);
605       myAISContext->RecomputePrsOnly(myAISContext->Current(),Standard_False);
606       myAISContext->SetDisplayMode(myAISContext->Current(),6);
607     }
608 }
609
610 void CViewer3dDoc::OnUpdateObjectColoredMesh(CCmdUI* pCmdUI)
611 {
612   bool CylinderIsCurrentAndDisplayed = false;
613   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
614     if(myAISContext->Current()->IsKind(STANDARD_TYPE(User_Cylinder)))
615       CylinderIsCurrentAndDisplayed=true;
616   pCmdUI->Enable (CylinderIsCurrentAndDisplayed);
617 }
618
619 void CViewer3dDoc::OnUpdateObjectWireframe(CCmdUI* pCmdUI)
620 {
621   bool OneOrMoreInShadingOrColoredMesh = false;
622   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
623     if (myAISContext->IsDisplayed(myAISContext->Current(),1) || myAISContext->IsDisplayed(myAISContext->Current(),6)) 
624       OneOrMoreInShadingOrColoredMesh=true;
625   pCmdUI->Enable (OneOrMoreInShadingOrColoredMesh);
626 }
627
628
629 void CViewer3dDoc::OnUpdateObjectShading(CCmdUI* pCmdUI)
630 {
631   bool OneOrMoreInWireframeOrColoredMesh = false;
632   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
633     if (myAISContext->IsDisplayed(myAISContext->Current(),0) || myAISContext->IsDisplayed(myAISContext->Current(),6))
634       OneOrMoreInWireframeOrColoredMesh=true;
635   pCmdUI->Enable (OneOrMoreInWireframeOrColoredMesh);
636 }
637
638 void CViewer3dDoc::OnOptionsTrihedronDynamicTrihedron()
639 {
640   if (myAISContext -> IsDisplayed(myTrihedron))
641   {
642     myAISContext->Remove(myTrihedron);
643   }
644   else
645   {
646     Handle(Geom_Axis2Placement) myTrihedronAxis=new Geom_Axis2Placement(gp::XOY());
647     myTrihedron=new AIS_Trihedron(myTrihedronAxis);
648     myAISContext->SetTrihedronSize(200, Standard_True);
649     myAISContext->Display(myTrihedron);
650   }
651 }
652
653 void CViewer3dDoc::OnUpdateOptionsTrihedronDynamicTrihedron(CCmdUI* pCmdUI)
654 {
655   if (myAISContext->IsDisplayed(myTrihedron))
656     pCmdUI->SetCheck(1);
657   else
658     pCmdUI->SetCheck(0);
659 }
660
661 void CViewer3dDoc::SetMyStaticTrihedronAxisIsDisplayed(BOOL IsDisplayed)
662 {
663   myStaticTrihedronAxisIsDisplayed = IsDisplayed;
664 }
665
666 void CViewer3dDoc::OnUpdateOptionsTrihedronStaticTrihedron(CCmdUI* pCmdUI) 
667 {
668   if (myStaticTrihedronAxisIsDisplayed)
669     pCmdUI->SetCheck(1);
670   else
671     pCmdUI->SetCheck(0);
672
673 }
674
675 void  CViewer3dDoc::Popup (const Standard_Integer  x,
676                            const Standard_Integer  y ,
677                            const Handle(V3d_View)& aView)
678 {
679   myPopupMenuNumber=0;
680   // Specified check for context menu number to call
681   myAISContext->InitCurrent();
682   if (myAISContext->MoreCurrent())
683   {
684     if (myAISContext->Current()->IsKind(STANDARD_TYPE(User_Cylinder)))
685     {
686       myPopupMenuNumber = 2;
687       //return;
688     }
689   }
690   OCC_3dBaseDoc::Popup(x,y, aView);
691 }
692
693 //Set faces selection mode
694 void CViewer3dDoc::OnFaces() 
695 {
696   myAISContext->CloseAllContexts();
697   myAISContext->OpenLocalContext();
698   myAISContext->ActivateStandardMode (TopAbs_FACE);
699
700   myCResultDialog.SetTitle("Standard mode: TopAbs_FACE");
701   myCResultDialog.SetText("  myAISContext->OpenLocalContext(); \n"
702                                     "  \n"
703                                     "  myAISContext->ActivateStandardMode(TopAbs_FACE); \n"
704                                     "  \n");
705   SetTitle (L"Standard mode: TopAbs_FACE");
706 }
707
708 //Set edges selection mode
709 void CViewer3dDoc::OnEdges() 
710 {
711   myAISContext->CloseAllContexts();
712   myAISContext->OpenLocalContext();
713   myAISContext->ActivateStandardMode(TopAbs_EDGE);
714
715   myCResultDialog.SetTitle("Standard mode: TopAbs_EDGE");
716   myCResultDialog.SetText("  myAISContext->OpenLocalContext(); \n"
717                                     "  \n"
718                                     "  myAISContext->ActivateStandardMode(TopAbs_EDGE); \n"
719                                     "  \n");
720   SetTitle (L"Standard mode: TopAbs_EDGE");
721 }
722
723 // Set vertices selection mode
724 void CViewer3dDoc::OnVertices() 
725 {
726   myAISContext->CloseAllContexts();
727   myAISContext->OpenLocalContext();
728   myAISContext->ActivateStandardMode (TopAbs_VERTEX);
729
730   myCResultDialog.SetTitle("Standard mode: TopAbs_VERTEX");
731   myCResultDialog.SetText("  myAISContext->OpenLocalContext(); \n"
732                                     "  \n"
733                                     "  myAISContext->ActivateStandardMode(TopAbs_VERTEX); \n"
734                                     "  \n");
735   SetTitle (L"Standard mode: TopAbs_VERTEX");
736 }
737
738 //Neutral selection mode
739 void CViewer3dDoc::OnNeutral() 
740 {
741   myAISContext->CloseAllContexts();
742
743   myCResultDialog.SetTitle("Standard mode: Neutral");
744   myCResultDialog.SetText("  myAISContext->CloseAllContexts(); \n"
745                                     "  \n");
746   SetTitle (L"Standard mode: Neutral");
747 }
748
749 // Change the color of faces on a user cylinder
750 void CViewer3dDoc::OnUsercylinderChangefacecolor() 
751 {
752   myAISContext->OpenLocalContext();
753   myAISContext->Activate(myAISContext->Current(),4);
754   myState = FACE_COLOR;
755   // see the following of treatment in inputevent
756 }
757
758 // Make 3d fillets on solids
759 // It is necessary to activate the edges selection mode and select edges on an object
760 // before running this function
761 void CViewer3dDoc::OnFillet3d() 
762 {
763   if (!myAISContext->HasOpenedContext())
764   {
765     AfxMessageBox (L"It is necessary to activate the edges selection mode\n"
766                    L"and select edges on an object before \nrunning this function");
767     return;
768   }
769
770   myAISContext->InitSelected();
771   if (myAISContext->MoreSelected()) 
772   {
773     Handle(AIS_Shape) aSelInteractive (Handle(AIS_Shape)::DownCast (myAISContext->SelectedInteractive()));
774     if (aSelInteractive.IsNull())
775     {
776       AfxMessageBox (L"It is necessary to activate the edges selection mode\n"
777                      L"and select edges on an object before \nrunning this function");
778       return;
779     }
780
781     BRepFilletAPI_MakeFillet aFillet (aSelInteractive->Shape());
782
783     for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
784     {
785       const TopoDS_Shape& aSelShape = myAISContext->SelectedShape();
786       if (aSelShape.ShapeType() != TopAbs_EDGE)
787       {
788         AfxMessageBox (L"It is necessary to activate the edges selection mode\n"
789                        L"and select edges on an object before \nrunning this function");
790         return;
791       }
792     }
793
794     BoxRadius aDialog (NULL,10.);
795     if (aDialog.DoModal() == IDOK)
796     {
797       for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
798       {
799         // Selected shape has its own location, and sub-shapes in the solid shape for fillet building
800         // don't have own locations. Fillet builder needs to know that input edge is just the same one as
801         // some sub-shape in our target solid shape, so no location is to be in input edge for fillet builder.
802         TopoDS_Shape aSelShape = myAISContext->SelectedShape().Located (TopLoc_Location());
803         aFillet.Add (aDialog.m_radius, TopoDS::Edge (aSelShape) );
804       }
805     }
806     else
807     {
808       return;
809     }
810
811     TopoDS_Shape aNewShape;
812     try
813     {
814       aNewShape = aFillet.Shape();
815     }
816     catch (Standard_Failure)
817     {
818       AfxMessageBox (L"Error During Fillet computation");
819       return;
820     }
821
822     aSelInteractive ->Set (aNewShape);
823     myAISContext->Redisplay (aSelInteractive);
824   }
825
826   myCResultDialog.SetTitle("Make a fillet");
827   myCResultDialog.SetText("  Handle(AIS_Shape) S = Handle(AIS_Shape)::DownCast(myAISContext->Interactive()); \n"
828                                     "  \n"
829                                     "  BRepAPI_MakeFillet aFillet(S->Shape()); \n"
830                                     "  \n"
831                                     "  TopoDS_Edge anEdge=TopoDS::Edge(myAISContext->SelectedShape()); \n"
832                                     "  \n"
833                                     "  aFillet.Add(dlg.m_radius,anEdge); \n"
834                                     "  \n"
835                                     "  TopoDS_Shape aNewShape = aFillet.Shape(); \n"
836                                     "  \n"
837                                     "  S->Set(aNewShape); \n"
838                                     "  \n"
839                                     "  myAISContext->Redisplay(S); \n"
840                                     "  \n");
841   SetTitle (L"Make a fillet");
842 }
843
844 // Create and display a circle with standard tools
845 void CViewer3dDoc::OnCircle() 
846 {
847   gp_Ax2 anAx2 (gp_Pnt (0., 0., 0.), gp_Dir(0., 0., -1.));
848   Handle(Geom_Circle) aGeomCircle = new Geom_Circle (anAx2, 300);
849
850   // the lines above substitute
851   // GC_MakeCircle C(gp_Pnt(-100.,-300.,0.),gp_Pnt(-50.,-200.,0.),gp_Pnt(-10.,-250.,0.));
852   // Handle(AIS_Circle) anAISCirc = new AIS_Circle(C.Value());
853
854   Handle(AIS_Circle) anAISCirc = new AIS_Circle(aGeomCircle);
855   myAISContext->Display (anAISCirc);
856
857   myCResultDialog.SetTitle("Create a circle");
858   myCResultDialog.SetText("  GC_MakeCircle C(gp_Pnt(-100.,-300.,0.),gp_Pnt(-50.,-200.,0.),gp_Pnt(-10.,-250.,0.)); \n"
859                                     "  \n"
860                                     "  Handle(AIS_Circle) anAISCirc = new AIS_Circle(C.Value()); \n"
861                                     "  \n"
862                                     "  myAISContext->Display(anAISCirc); \n"
863                                     "  \n");
864   SetTitle (L"Create a circle");
865 }
866
867 void CViewer3dDoc::OnLine() 
868 {
869   // TODO: Add your command handler code here
870   gp_Lin aGpLin (gp_Pnt (0., 0., 0.), gp_Dir(1., 0., 0.));
871   Handle(Geom_Line) aGeomLin = new Geom_Line (aGpLin);
872   Handle(AIS_Line) anAISLine = new AIS_Line (aGeomLin);
873   myAISContext->Display (anAISLine);
874
875   myCResultDialog.SetTitle("Create a line");
876   myCResultDialog.SetText("  gp_Lin L(gp_Pnt(0.,0.,0.),gp_Dir(1.,0.,0.)); \n"
877                                     "  \n"
878                                     "  Handle(Geom_Line) aLine = new Geom_Line(L); \n"
879                                     "  \n"
880                                     "  Handle(AIS_Line) anAISLine = new AIS_Line(aLine); \n"
881                                     "  \n"
882                                     "  myAISContext->Display(anAISLine); \n"
883                                     "  \n");
884   SetTitle (L"Create a line");
885 }
886
887 void CViewer3dDoc::OnNbisos() 
888 {
889   int aNumU = myAISContext->DefaultDrawer()->UIsoAspect()->Number();
890   int aNumV = myAISContext->DefaultDrawer()->VIsoAspect()->Number();
891
892   DlgIsos aDlg (NULL, aNumU, aNumV);
893
894   if (aDlg.DoModal() == IDOK)
895   {
896     myAISContext->DefaultDrawer()->UIsoAspect()->SetNumber (aDlg.m_isou);
897     myAISContext->DefaultDrawer()->VIsoAspect()->SetNumber (aDlg.m_isov);
898
899     myCResultDialog.SetTitle("Iso Aspect");
900     myCResultDialog.SetText("  myAISContext->DefaultDrawer()->UIsoAspect()->SetNumber(dlg.m_isou); \n"
901                                       "  \n"
902                                       "  myAISContext->DefaultDrawer()->VIsoAspect()->SetNumber(dlg.m_isov); \n"
903                                       "  \n");
904     SetTitle (L"Iso Aspect");
905   }
906 }
907
908 BOOL CViewer3dDoc::OnNewDocument()
909 {
910   if (!CDocument::OnNewDocument())
911     return FALSE;
912
913   // TODO: add reinitialization code here
914   // (SDI documents will reuse this document)
915   SetTitle(myPresentation->GetName());
916
917   myAISContext->EraseAll();
918   myAISContext->SetDisplayMode(AIS_Shaded);
919
920   POSITION pos = GetFirstViewPosition();
921   while (pos != NULL)
922   {
923     CViewer3dView* pView = (CViewer3dView*) GetNextView(pos);
924     pView->Reset();
925   }  
926
927   return TRUE;
928 }
929
930 void CViewer3dDoc::OnFileNew()
931 {
932   OnNewDocument();
933 }
934
935 void CViewer3dDoc::InitViewButtons()
936 {
937   POSITION pos = GetFirstViewPosition();
938   while (pos != NULL)
939   {
940     CViewer3dView* pView = (CViewer3dView*) GetNextView(pos);
941     pView->InitButtons();
942   }
943 }
944
945 void CViewer3dDoc::OnTextureOn()
946 {
947         isTextureSampleStarted = TRUE;
948         Start();
949 }
950
951 void CViewer3dDoc::DoSample()
952 {
953   InitViewButtons();
954
955   HCURSOR hOldCursor = ::GetCursor();
956   HCURSOR hNewCursor = AfxGetApp()->LoadStandardCursor(IDC_APPSTARTING);
957
958   SetCursor(hNewCursor);
959   {
960     try
961     {
962       myPresentation->DoSample();
963     }
964     catch (Standard_Failure)
965     {
966       Standard_SStream aSStream;
967       aSStream << "An exception was caught: " << Standard_Failure::Caught() << ends;
968       CString aMsg = aSStream.str().c_str();
969       AfxMessageBox (aMsg);
970     }
971   }
972   SetCursor(hOldCursor);
973 }
974
975 void CViewer3dDoc::OnBUTTONStart() 
976 {
977   myAISContext->EraseAll();
978   myPresentation->FirstSample();
979   DoSample();
980 }
981
982 void CViewer3dDoc::OnBUTTONEnd()
983 {
984   myAISContext->EraseAll();
985   myPresentation->LastSample();
986   DoSample();
987 }
988
989 void CViewer3dDoc::OnBUTTONRepeat() 
990 {
991   DoSample();
992 }
993
994 void CViewer3dDoc::OnBUTTONNext() 
995 {
996   if (!myPresentation->AtLastSample())
997   {
998     myPresentation->NextSample();
999     DoSample();
1000   }
1001 }
1002
1003 void CViewer3dDoc::OnBUTTONPrev() 
1004 {
1005   if (!myPresentation->AtFirstSample())
1006   {
1007     myPresentation->PrevSample();
1008     DoSample();
1009   }
1010 }
1011
1012 void CViewer3dDoc::OnUpdateBUTTONNext(CCmdUI* pCmdUI) 
1013 {
1014         if ( isTextureSampleStarted )
1015                 pCmdUI->Enable (!myPresentation->AtLastSample());
1016         else
1017                 pCmdUI->Enable (FALSE);
1018 }
1019
1020 void CViewer3dDoc::OnUpdateBUTTONPrev(CCmdUI* pCmdUI) 
1021 {
1022         if ( isTextureSampleStarted )
1023                 pCmdUI->Enable (!myPresentation->AtFirstSample());
1024         else
1025                 pCmdUI->Enable (FALSE);
1026 }
1027
1028 void CViewer3dDoc::OnUpdateBUTTONStart(CCmdUI* pCmdUI) 
1029 {
1030         pCmdUI->Enable (isTextureSampleStarted);
1031 }
1032
1033 void CViewer3dDoc::OnUpdateBUTTONRepeat(CCmdUI* pCmdUI) 
1034 {
1035         pCmdUI->Enable (isTextureSampleStarted);
1036 }
1037
1038 void CViewer3dDoc::OnUpdateBUTTONEnd(CCmdUI* pCmdUI) 
1039 {
1040         pCmdUI->Enable (isTextureSampleStarted);
1041 }
1042
1043 void CViewer3dDoc::OnDumpView()
1044 {
1045   for (POSITION aPos = GetFirstViewPosition(); aPos != NULL;)
1046   {
1047     CViewer3dView* pView = (CViewer3dView* )GetNextView (aPos);
1048     pView->UpdateWindow();
1049   }
1050
1051   myViewer->InitActiveViews();
1052   Handle(V3d_View) aView = myViewer->ActiveView();
1053   ExportView (aView);
1054 }
1055
1056 void CViewer3dDoc::Start()
1057 {
1058   myPresentation->Init();
1059   OnBUTTONStart();
1060 }
1061
1062 void CViewer3dDoc::Fit()
1063 {
1064         CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
1065         CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
1066         CViewer3dView *pView = (CViewer3dView *) pChild->GetActiveView();
1067         pView->FitAll();
1068 }