0028105: HLR rendering crash in MFC sample
[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         Handle(AIS_InteractiveObject) aSelectedObject = myAISContext->SelectedInteractive();
531         Handle(User_Cylinder)::DownCast (aSelectedObject)->SetColor (CSFColor.Name());
532
533         myAISContext->Redisplay (aSelectedObject);
534         myState = -1;
535         myAISContext->CloseLocalContext();
536       }
537     }
538
539     myCResultDialog.SetTitle("Change face color");
540     myCResultDialog.SetText("  TopoDS_Shape S = myAISContext->SelectedShape(); \n"
541                                       "  \n"
542                                       "  Handle(Geom_Surface) Surface = BRep_Tool::Surface(TopoDS::Face(S));"
543                                       "  if (Surface->IsKind(STANDARD_TYPE(Geom_Plane))) \n"
544                                       "    Handle(User_Cylinder)::DownCast(myAISContext->Current())->SetPlanarFaceColor(CSFColor.Name()); \n"
545                                       "  else \n"
546                                       "    Handle(User_Cylinder)::DownCast(myAISContext->Current())->SetCylindricalFaceColor(CSFColor.Name()); \n"
547                                       "  \n"
548                                       "  myAISContext->Redisplay(myAISContext->Current()); \n"
549                                       "  \n"
550                                       "  myAISContext->CloseLocalContext(); \n"
551                                       "  \n"
552                                       "  \n"
553                                       "  NOTE: a User_Cylinder is an object defined by the user. \n"
554                                       "  The User_Cylinder class inherit from the AIS_InteractiveObject \n"
555                                       "  Cascade class, it's use is the same as an AIS_InteractiveObject. \n"
556                                       "  Methods SetPlanarFaceColor and SetCylindricalFaceColor are also \n"
557                                       "  defined in the User_Cylinder class. \n"
558                                       "  \n");
559     SetTitle (L"Change face color");
560   }
561 }
562
563 //-----------------------------------------------------------------------------------------
564 //
565 //-----------------------------------------------------------------------------------------
566 void CViewer3dDoc::ShiftDragEvent (const Standard_Integer theX,
567                                    const Standard_Integer  theY,
568                                    const Standard_Integer theState,
569                                    const Handle(V3d_View)& theView)
570 {
571   OCC_3dBaseDoc::ShiftDragEvent(theX, theY, theState, theView);
572   if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
573     myOffsetDlg->UpdateValues();
574 }
575
576
577 //-----------------------------------------------------------------------------------------
578 //
579 //-----------------------------------------------------------------------------------------
580 void CViewer3dDoc::ShiftInputEvent (const Standard_Integer theX,
581                                     const Standard_Integer theY,
582                                     const Handle(V3d_View)& theView)
583 {
584   OCC_3dBaseDoc::ShiftInputEvent (theX, theY, theView);
585   if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
586     myOffsetDlg->UpdateValues();
587 }
588
589 void CViewer3dDoc::OnObjectColoredMesh()
590 {
591   for(myAISContext->InitCurrent();myAISContext->MoreCurrent();myAISContext->NextCurrent())
592     if (myAISContext->Current()->IsKind(STANDARD_TYPE(User_Cylinder)))
593     {
594       myAISContext->ClearPrs(myAISContext->Current(),6,Standard_False);
595       myAISContext->RecomputePrsOnly(myAISContext->Current(),Standard_False);
596       myAISContext->SetDisplayMode(myAISContext->Current(),6);
597     }
598 }
599
600 void CViewer3dDoc::OnUpdateObjectColoredMesh(CCmdUI* pCmdUI)
601 {
602   bool CylinderIsCurrentAndDisplayed = false;
603   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
604     if(myAISContext->Current()->IsKind(STANDARD_TYPE(User_Cylinder)))
605       CylinderIsCurrentAndDisplayed=true;
606   pCmdUI->Enable (CylinderIsCurrentAndDisplayed);
607 }
608
609 void CViewer3dDoc::OnUpdateObjectWireframe(CCmdUI* pCmdUI)
610 {
611   bool OneOrMoreInShadingOrColoredMesh = false;
612   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
613     if (myAISContext->IsDisplayed(myAISContext->Current(),1) || myAISContext->IsDisplayed(myAISContext->Current(),6)) 
614       OneOrMoreInShadingOrColoredMesh=true;
615   pCmdUI->Enable (OneOrMoreInShadingOrColoredMesh);
616 }
617
618
619 void CViewer3dDoc::OnUpdateObjectShading(CCmdUI* pCmdUI)
620 {
621   bool OneOrMoreInWireframeOrColoredMesh = false;
622   for (myAISContext->InitCurrent();myAISContext->MoreCurrent ();myAISContext->NextCurrent ())
623     if (myAISContext->IsDisplayed(myAISContext->Current(),0) || myAISContext->IsDisplayed(myAISContext->Current(),6))
624       OneOrMoreInWireframeOrColoredMesh=true;
625   pCmdUI->Enable (OneOrMoreInWireframeOrColoredMesh);
626 }
627
628 void CViewer3dDoc::OnOptionsTrihedronDynamicTrihedron()
629 {
630   if (myAISContext -> IsDisplayed(myTrihedron))
631   {
632     myAISContext->Remove(myTrihedron);
633   }
634   else
635   {
636     Handle(Geom_Axis2Placement) myTrihedronAxis=new Geom_Axis2Placement(gp::XOY());
637     myTrihedron=new AIS_Trihedron(myTrihedronAxis);
638     myAISContext->SetTrihedronSize(200, Standard_True);
639     myAISContext->Display(myTrihedron);
640   }
641 }
642
643 void CViewer3dDoc::OnUpdateOptionsTrihedronDynamicTrihedron(CCmdUI* pCmdUI)
644 {
645   if (myAISContext->IsDisplayed(myTrihedron))
646     pCmdUI->SetCheck(1);
647   else
648     pCmdUI->SetCheck(0);
649 }
650
651 void CViewer3dDoc::SetMyStaticTrihedronAxisIsDisplayed(BOOL IsDisplayed)
652 {
653   myStaticTrihedronAxisIsDisplayed = IsDisplayed;
654 }
655
656 void CViewer3dDoc::OnUpdateOptionsTrihedronStaticTrihedron(CCmdUI* pCmdUI) 
657 {
658   if (myStaticTrihedronAxisIsDisplayed)
659     pCmdUI->SetCheck(1);
660   else
661     pCmdUI->SetCheck(0);
662
663 }
664
665 void  CViewer3dDoc::Popup (const Standard_Integer  x,
666                            const Standard_Integer  y ,
667                            const Handle(V3d_View)& aView)
668 {
669   myPopupMenuNumber=0;
670   // Specified check for context menu number to call
671   myAISContext->InitCurrent();
672   if (myAISContext->MoreCurrent())
673   {
674     if (myAISContext->Current()->IsKind(STANDARD_TYPE(User_Cylinder)))
675     {
676       myPopupMenuNumber = 2;
677       //return;
678     }
679   }
680   OCC_3dBaseDoc::Popup(x,y, aView);
681 }
682
683 //Set faces selection mode
684 void CViewer3dDoc::OnFaces() 
685 {
686   myAISContext->CloseAllContexts();
687   myAISContext->OpenLocalContext();
688   myAISContext->ActivateStandardMode (TopAbs_FACE);
689
690   myCResultDialog.SetTitle("Standard mode: TopAbs_FACE");
691   myCResultDialog.SetText("  myAISContext->OpenLocalContext(); \n"
692                                     "  \n"
693                                     "  myAISContext->ActivateStandardMode(TopAbs_FACE); \n"
694                                     "  \n");
695   SetTitle (L"Standard mode: TopAbs_FACE");
696 }
697
698 //Set edges selection mode
699 void CViewer3dDoc::OnEdges() 
700 {
701   myAISContext->CloseAllContexts();
702   myAISContext->OpenLocalContext();
703   myAISContext->ActivateStandardMode(TopAbs_EDGE);
704
705   myCResultDialog.SetTitle("Standard mode: TopAbs_EDGE");
706   myCResultDialog.SetText("  myAISContext->OpenLocalContext(); \n"
707                                     "  \n"
708                                     "  myAISContext->ActivateStandardMode(TopAbs_EDGE); \n"
709                                     "  \n");
710   SetTitle (L"Standard mode: TopAbs_EDGE");
711 }
712
713 // Set vertices selection mode
714 void CViewer3dDoc::OnVertices() 
715 {
716   myAISContext->CloseAllContexts();
717   myAISContext->OpenLocalContext();
718   myAISContext->ActivateStandardMode (TopAbs_VERTEX);
719
720   myCResultDialog.SetTitle("Standard mode: TopAbs_VERTEX");
721   myCResultDialog.SetText("  myAISContext->OpenLocalContext(); \n"
722                                     "  \n"
723                                     "  myAISContext->ActivateStandardMode(TopAbs_VERTEX); \n"
724                                     "  \n");
725   SetTitle (L"Standard mode: TopAbs_VERTEX");
726 }
727
728 //Neutral selection mode
729 void CViewer3dDoc::OnNeutral() 
730 {
731   myAISContext->CloseAllContexts();
732
733   myCResultDialog.SetTitle("Standard mode: Neutral");
734   myCResultDialog.SetText("  myAISContext->CloseAllContexts(); \n"
735                                     "  \n");
736   SetTitle (L"Standard mode: Neutral");
737 }
738
739 // Change the color of faces on a user cylinder
740 void CViewer3dDoc::OnUsercylinderChangefacecolor() 
741 {
742   myAISContext->OpenLocalContext();
743   myAISContext->Activate(myAISContext->Current(),4);
744   myState = FACE_COLOR;
745   // see the following of treatment in inputevent
746 }
747
748 // Make 3d fillets on solids
749 // It is necessary to activate the edges selection mode and select edges on an object
750 // before running this function
751 void CViewer3dDoc::OnFillet3d() 
752 {
753   if (!myAISContext->HasOpenedContext())
754   {
755     AfxMessageBox (L"It is necessary to activate the edges selection mode\n"
756                    L"and select edges on an object before \nrunning this function");
757     return;
758   }
759
760   myAISContext->InitSelected();
761   if (myAISContext->MoreSelected()) 
762   {
763     Handle(AIS_Shape) aSelInteractive (Handle(AIS_Shape)::DownCast (myAISContext->SelectedInteractive()));
764     if (aSelInteractive.IsNull())
765     {
766       AfxMessageBox (L"It is necessary to activate the edges selection mode\n"
767                      L"and select edges on an object before \nrunning this function");
768       return;
769     }
770
771     BRepFilletAPI_MakeFillet aFillet (aSelInteractive->Shape());
772
773     for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
774     {
775       const TopoDS_Shape& aSelShape = myAISContext->SelectedShape();
776       if (aSelShape.ShapeType() != TopAbs_EDGE)
777       {
778         AfxMessageBox (L"It is necessary to activate the edges selection mode\n"
779                        L"and select edges on an object before \nrunning this function");
780         return;
781       }
782     }
783
784     BoxRadius aDialog (NULL,10.);
785     if (aDialog.DoModal() == IDOK)
786     {
787       for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
788       {
789         // Selected shape has its own location, and sub-shapes in the solid shape for fillet building
790         // don't have own locations. Fillet builder needs to know that input edge is just the same one as
791         // some sub-shape in our target solid shape, so no location is to be in input edge for fillet builder.
792         TopoDS_Shape aSelShape = myAISContext->SelectedShape().Located (TopLoc_Location());
793         aFillet.Add (aDialog.m_radius, TopoDS::Edge (aSelShape) );
794       }
795     }
796     else
797     {
798       return;
799     }
800
801     TopoDS_Shape aNewShape;
802     try
803     {
804       aNewShape = aFillet.Shape();
805     }
806     catch (Standard_Failure)
807     {
808       AfxMessageBox (L"Error During Fillet computation");
809       return;
810     }
811
812     aSelInteractive ->Set (aNewShape);
813     myAISContext->Redisplay (aSelInteractive);
814   }
815
816   myCResultDialog.SetTitle("Make a fillet");
817   myCResultDialog.SetText("  Handle(AIS_Shape) S = Handle(AIS_Shape)::DownCast(myAISContext->Interactive()); \n"
818                                     "  \n"
819                                     "  BRepAPI_MakeFillet aFillet(S->Shape()); \n"
820                                     "  \n"
821                                     "  TopoDS_Edge anEdge=TopoDS::Edge(myAISContext->SelectedShape()); \n"
822                                     "  \n"
823                                     "  aFillet.Add(dlg.m_radius,anEdge); \n"
824                                     "  \n"
825                                     "  TopoDS_Shape aNewShape = aFillet.Shape(); \n"
826                                     "  \n"
827                                     "  S->Set(aNewShape); \n"
828                                     "  \n"
829                                     "  myAISContext->Redisplay(S); \n"
830                                     "  \n");
831   SetTitle (L"Make a fillet");
832 }
833
834 // Create and display a circle with standard tools
835 void CViewer3dDoc::OnCircle() 
836 {
837   gp_Ax2 anAx2 (gp_Pnt (0., 0., 0.), gp_Dir(0., 0., -1.));
838   Handle(Geom_Circle) aGeomCircle = new Geom_Circle (anAx2, 300);
839
840   // the lines above substitute
841   // GC_MakeCircle C(gp_Pnt(-100.,-300.,0.),gp_Pnt(-50.,-200.,0.),gp_Pnt(-10.,-250.,0.));
842   // Handle(AIS_Circle) anAISCirc = new AIS_Circle(C.Value());
843
844   Handle(AIS_Circle) anAISCirc = new AIS_Circle(aGeomCircle);
845   myAISContext->Display (anAISCirc);
846
847   myCResultDialog.SetTitle("Create a circle");
848   myCResultDialog.SetText("  GC_MakeCircle C(gp_Pnt(-100.,-300.,0.),gp_Pnt(-50.,-200.,0.),gp_Pnt(-10.,-250.,0.)); \n"
849                                     "  \n"
850                                     "  Handle(AIS_Circle) anAISCirc = new AIS_Circle(C.Value()); \n"
851                                     "  \n"
852                                     "  myAISContext->Display(anAISCirc); \n"
853                                     "  \n");
854   SetTitle (L"Create a circle");
855 }
856
857 void CViewer3dDoc::OnLine() 
858 {
859   // TODO: Add your command handler code here
860   gp_Lin aGpLin (gp_Pnt (0., 0., 0.), gp_Dir(1., 0., 0.));
861   Handle(Geom_Line) aGeomLin = new Geom_Line (aGpLin);
862   Handle(AIS_Line) anAISLine = new AIS_Line (aGeomLin);
863   myAISContext->Display (anAISLine);
864
865   myCResultDialog.SetTitle("Create a line");
866   myCResultDialog.SetText("  gp_Lin L(gp_Pnt(0.,0.,0.),gp_Dir(1.,0.,0.)); \n"
867                                     "  \n"
868                                     "  Handle(Geom_Line) aLine = new Geom_Line(L); \n"
869                                     "  \n"
870                                     "  Handle(AIS_Line) anAISLine = new AIS_Line(aLine); \n"
871                                     "  \n"
872                                     "  myAISContext->Display(anAISLine); \n"
873                                     "  \n");
874   SetTitle (L"Create a line");
875 }
876
877 void CViewer3dDoc::OnNbisos() 
878 {
879   int aNumU = myAISContext->DefaultDrawer()->UIsoAspect()->Number();
880   int aNumV = myAISContext->DefaultDrawer()->VIsoAspect()->Number();
881
882   DlgIsos aDlg (NULL, aNumU, aNumV);
883
884   if (aDlg.DoModal() == IDOK)
885   {
886     myAISContext->DefaultDrawer()->UIsoAspect()->SetNumber (aDlg.m_isou);
887     myAISContext->DefaultDrawer()->VIsoAspect()->SetNumber (aDlg.m_isov);
888
889     myCResultDialog.SetTitle("Iso Aspect");
890     myCResultDialog.SetText("  myAISContext->DefaultDrawer()->UIsoAspect()->SetNumber(dlg.m_isou); \n"
891                                       "  \n"
892                                       "  myAISContext->DefaultDrawer()->VIsoAspect()->SetNumber(dlg.m_isov); \n"
893                                       "  \n");
894     SetTitle (L"Iso Aspect");
895   }
896 }
897
898 BOOL CViewer3dDoc::OnNewDocument()
899 {
900   if (!CDocument::OnNewDocument())
901     return FALSE;
902
903   // TODO: add reinitialization code here
904   // (SDI documents will reuse this document)
905   SetTitle(myPresentation->GetName());
906
907   myAISContext->EraseAll();
908   myAISContext->SetDisplayMode(AIS_Shaded);
909
910   POSITION pos = GetFirstViewPosition();
911   while (pos != NULL)
912   {
913     CViewer3dView* pView = (CViewer3dView*) GetNextView(pos);
914     pView->Reset();
915   }  
916
917   return TRUE;
918 }
919
920 void CViewer3dDoc::OnFileNew()
921 {
922   OnNewDocument();
923 }
924
925 void CViewer3dDoc::InitViewButtons()
926 {
927   POSITION pos = GetFirstViewPosition();
928   while (pos != NULL)
929   {
930     CViewer3dView* pView = (CViewer3dView*) GetNextView(pos);
931     pView->InitButtons();
932   }
933 }
934
935 void CViewer3dDoc::OnTextureOn()
936 {
937         isTextureSampleStarted = TRUE;
938         Start();
939 }
940
941 void CViewer3dDoc::DoSample()
942 {
943   InitViewButtons();
944
945   HCURSOR hOldCursor = ::GetCursor();
946   HCURSOR hNewCursor = AfxGetApp()->LoadStandardCursor(IDC_APPSTARTING);
947
948   SetCursor(hNewCursor);
949   {
950     try
951     {
952       myPresentation->DoSample();
953     }
954     catch (Standard_Failure)
955     {
956       Standard_SStream aSStream;
957       aSStream << "An exception was caught: " << Standard_Failure::Caught() << ends;
958       CString aMsg = aSStream.str().c_str();
959       AfxMessageBox (aMsg);
960     }
961   }
962   SetCursor(hOldCursor);
963 }
964
965 void CViewer3dDoc::OnBUTTONStart() 
966 {
967   myAISContext->EraseAll();
968   myPresentation->FirstSample();
969   DoSample();
970 }
971
972 void CViewer3dDoc::OnBUTTONEnd()
973 {
974   myAISContext->EraseAll();
975   myPresentation->LastSample();
976   DoSample();
977 }
978
979 void CViewer3dDoc::OnBUTTONRepeat() 
980 {
981   DoSample();
982 }
983
984 void CViewer3dDoc::OnBUTTONNext() 
985 {
986   if (!myPresentation->AtLastSample())
987   {
988     myPresentation->NextSample();
989     DoSample();
990   }
991 }
992
993 void CViewer3dDoc::OnBUTTONPrev() 
994 {
995   if (!myPresentation->AtFirstSample())
996   {
997     myPresentation->PrevSample();
998     DoSample();
999   }
1000 }
1001
1002 void CViewer3dDoc::OnUpdateBUTTONNext(CCmdUI* pCmdUI) 
1003 {
1004         if ( isTextureSampleStarted )
1005                 pCmdUI->Enable (!myPresentation->AtLastSample());
1006         else
1007                 pCmdUI->Enable (FALSE);
1008 }
1009
1010 void CViewer3dDoc::OnUpdateBUTTONPrev(CCmdUI* pCmdUI) 
1011 {
1012         if ( isTextureSampleStarted )
1013                 pCmdUI->Enable (!myPresentation->AtFirstSample());
1014         else
1015                 pCmdUI->Enable (FALSE);
1016 }
1017
1018 void CViewer3dDoc::OnUpdateBUTTONStart(CCmdUI* pCmdUI) 
1019 {
1020         pCmdUI->Enable (isTextureSampleStarted);
1021 }
1022
1023 void CViewer3dDoc::OnUpdateBUTTONRepeat(CCmdUI* pCmdUI) 
1024 {
1025         pCmdUI->Enable (isTextureSampleStarted);
1026 }
1027
1028 void CViewer3dDoc::OnUpdateBUTTONEnd(CCmdUI* pCmdUI) 
1029 {
1030         pCmdUI->Enable (isTextureSampleStarted);
1031 }
1032
1033 void CViewer3dDoc::OnDumpView()
1034 {
1035   for (POSITION aPos = GetFirstViewPosition(); aPos != NULL;)
1036   {
1037     CViewer3dView* pView = (CViewer3dView* )GetNextView (aPos);
1038     pView->UpdateWindow();
1039   }
1040
1041   myViewer->InitActiveViews();
1042   Handle(V3d_View) aView = myViewer->ActiveView();
1043   ExportView (aView);
1044 }
1045
1046 void CViewer3dDoc::Start()
1047 {
1048   myPresentation->Init();
1049   OnBUTTONStart();
1050 }
1051
1052 void CViewer3dDoc::Fit()
1053 {
1054         CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
1055         CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
1056         CViewer3dView *pView = (CViewer3dView *) pChild->GetActiveView();
1057         pView->FitAll();
1058 }