0029367: Visualization - simplify interface of V3d_View and V3d_Viewer
[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, Standard_False);
157     myAISContext->SetDisplayMode (myBox, 1, Standard_False);
158
159     myAISContext->Display (myBox, Standard_True);
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, Standard_False);
176
177     myAISContext->Display (myCylinder, Standard_True);
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, Standard_False);
197     myAISContext->SetDisplayMode (mySphere, 1, Standard_False);
198
199     myAISContext->Display (mySphere, Standard_True);
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(), Standard_False);
216   }
217
218   myAISContext->Remove (myCylinder, Standard_True);
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, Standard_False);
244     myAISContext->SetDisplayMode (myOverlappedBox, 1, Standard_False);
245
246     myAISContext->Display (myOverlappedBox, Standard_True);
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, Standard_False);
274     myAISContext->SetDisplayMode (myOverlappedCylinder, 1, Standard_False);
275
276     myAISContext->Display (myOverlappedCylinder, Standard_True);
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, Standard_False);
305
306     myAISContext->SetDisplayMode (myOverlappedSphere, 1, Standard_False);
307
308     myAISContext->Display (myOverlappedSphere, Standard_True);
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   // A small trick to avoid complier error (C2668).
407   const Handle(AIS_InteractiveObject)& aBox = myBox;
408   if(myAISContext->IsSelected (aBox))
409     myBox.Nullify();
410
411   const Handle(AIS_InteractiveObject)& aCylinder = myCylinder;
412   if(myAISContext->IsSelected (aCylinder))
413     myCylinder.Nullify();
414
415   const Handle(AIS_InteractiveObject)& aSphere = mySphere;
416   if(myAISContext->IsSelected (aSphere))
417     mySphere.Nullify();
418
419   const Handle(AIS_InteractiveObject)& anOverlappedBox = myOverlappedBox;
420   if(myAISContext->IsSelected (anOverlappedBox))
421     myOverlappedBox.Nullify();
422
423   const Handle(AIS_InteractiveObject)& anOverlappedCylinder = myOverlappedCylinder;
424   if(myAISContext->IsSelected (anOverlappedCylinder))
425     myOverlappedCylinder.Nullify();
426
427   const Handle(AIS_InteractiveObject)& anOverlappedSphere = myOverlappedSphere;
428   if(myAISContext->IsSelected (anOverlappedSphere))
429     myOverlappedSphere.Nullify();
430
431
432   for(myAISContext->InitSelected();myAISContext->MoreSelected();myAISContext->InitSelected())
433     myAISContext->Remove(myAISContext->SelectedInteractive(),Standard_True);
434
435   if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
436     myOffsetDlg->UpdateValues();
437 }
438
439 void CViewer3dDoc::OnObjectErase()
440
441 {
442   OCC_3dBaseDoc::OnObjectErase();
443   if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
444     myOffsetDlg->UpdateValues();
445 }
446
447 void CViewer3dDoc::OnObjectDisplayall()
448
449 {
450   OCC_3dBaseDoc::OnObjectDisplayall();
451   if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
452     myOffsetDlg->UpdateValues();
453 }
454
455 Handle(User_Cylinder) CViewer3dDoc::GetCylinder()
456 {
457   return myCylinder;
458 }
459
460
461 Handle(AIS_Shape) CViewer3dDoc::GetSphere()
462 {
463   return mySphere;
464 }
465
466 Handle(AIS_Shape) CViewer3dDoc::GetBox()
467 {
468   return myBox;
469 }
470
471 Handle(AIS_Shape) CViewer3dDoc::GetOverlappedCylinder()
472 {
473   return myOverlappedCylinder;
474 }
475
476
477 Handle(AIS_Shape) CViewer3dDoc::GetOverlappedSphere()
478 {
479   return myOverlappedSphere;
480 }
481
482 Handle(AIS_Shape) CViewer3dDoc::GetOverlappedBox()
483 {
484   return myOverlappedBox;
485 }
486
487 void CViewer3dDoc::DragEvent (const Standard_Integer theX,
488                               const Standard_Integer theY,
489                               const Standard_Integer theState,
490                               const Handle(V3d_View)& theView)
491 {
492   OCC_3dBaseDoc::DragEvent (theX, theY, theState, theView);
493   if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
494     myOffsetDlg->UpdateValues();
495 }
496
497 //-----------------------------------------------------------------------------------------
498 //
499 //-----------------------------------------------------------------------------------------
500 void CViewer3dDoc::InputEvent(const Standard_Integer /*x*/,
501                               const Standard_Integer /*y*/,
502                               const Handle(V3d_View)& /*aView*/ )
503 {
504   if (myOffsetDlg && myOffsetDlg->IsWindowVisible())
505     myOffsetDlg->UpdateValues();
506
507   Quantity_Color CSFColor;
508   COLORREF MSColor;
509   myAISContext->Select (Standard_True);
510
511   // Change the color of a selected face in a user cylinder
512   if (myState == FACE_COLOR)
513   {
514     myAISContext->InitSelected();
515     if (myAISContext->MoreSelected())
516     {
517       Handle(AIS_InteractiveObject) Current = myAISContext->SelectedInteractive();
518       if (Current->HasColor())
519       {
520         myAISContext->Color (Current, CSFColor);
521         MSColor = RGB (CSFColor.Red()*255.0, CSFColor.Green()*255.0, CSFColor.Blue()*255.0);
522       }
523       else 
524       {
525         MSColor = RGB (255, 255, 255);
526       }
527
528       CColorDialog dlgColor(MSColor);
529       if (dlgColor.DoModal() == IDOK)
530       {
531         MSColor = dlgColor.GetColor();
532         CSFColor = Quantity_Color (GetRValue (MSColor)/255.0,
533                                    GetGValue (MSColor)/255.0,
534                                    GetBValue (MSColor)/255.0,
535                                    Quantity_TOC_RGB);
536
537         Handle(AIS_InteractiveObject) aSelectedObject = myAISContext->SelectedInteractive();
538         Handle(User_Cylinder)::DownCast (aSelectedObject)->SetColor (CSFColor);
539
540         myAISContext->Redisplay (aSelectedObject, Standard_True);
541         myState = -1;
542       }
543     }
544
545     myCResultDialog.SetTitle("Change face color");
546     myCResultDialog.SetText(          "  Handle(AIS_InteractiveObject) aSelectedObject = myAISContext->SelectedInteractive(); \n"
547                                       "  Handle(User_Cylinder)::DownCast(aSelectedObject)->SetColor (CSFColor); \n"
548                                       "  myAISContext->Redisplay (myAISContext->Current(), Standard_True); \n"
549                                       "  \n"
550                                       "  \n"
551                                       "  NOTE: a User_Cylinder is an object defined by the user. \n"
552                                       "  The User_Cylinder class inherit from the AIS_InteractiveObject \n"
553                                       "  Cascade class, it's use is the same as an AIS_InteractiveObject. \n"
554                                       "  \n");
555     SetTitle (L"Change face color");
556   }
557 }
558
559 //-----------------------------------------------------------------------------------------
560 //
561 //-----------------------------------------------------------------------------------------
562 void CViewer3dDoc::ShiftDragEvent (const Standard_Integer theX,
563                                    const Standard_Integer  theY,
564                                    const Standard_Integer theState,
565                                    const Handle(V3d_View)& theView)
566 {
567   OCC_3dBaseDoc::ShiftDragEvent(theX, theY, theState, theView);
568   if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
569     myOffsetDlg->UpdateValues();
570 }
571
572
573 //-----------------------------------------------------------------------------------------
574 //
575 //-----------------------------------------------------------------------------------------
576 void CViewer3dDoc::ShiftInputEvent (const Standard_Integer theX,
577                                     const Standard_Integer theY,
578                                     const Handle(V3d_View)& theView)
579 {
580   OCC_3dBaseDoc::ShiftInputEvent (theX, theY, theView);
581   if(myOffsetDlg && myOffsetDlg->IsWindowVisible())
582     myOffsetDlg->UpdateValues();
583 }
584
585 void CViewer3dDoc::OnObjectColoredMesh()
586 {
587   for(myAISContext->InitSelected();myAISContext->MoreSelected();myAISContext->NextSelected())
588     if (myAISContext->SelectedInteractive()->IsKind(STANDARD_TYPE(User_Cylinder)))
589     {
590       myAISContext->ClearPrs(myAISContext->SelectedInteractive(),6,Standard_False);
591       myAISContext->RecomputePrsOnly(myAISContext->SelectedInteractive(), Standard_False);
592       myAISContext->SetDisplayMode(myAISContext->SelectedInteractive(), 6, Standard_False);
593     }
594
595   myAISContext->UpdateCurrentViewer();
596 }
597
598 void CViewer3dDoc::OnUpdateObjectColoredMesh(CCmdUI* pCmdUI)
599 {
600   bool CylinderIsCurrentAndDisplayed = false;
601   for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ())
602     if(myAISContext->SelectedInteractive()->IsKind(STANDARD_TYPE(User_Cylinder)))
603       CylinderIsCurrentAndDisplayed=true;
604   pCmdUI->Enable (CylinderIsCurrentAndDisplayed);
605 }
606
607 void CViewer3dDoc::OnUpdateObjectWireframe(CCmdUI* pCmdUI)
608 {
609   bool OneOrMoreInShadingOrColoredMesh = false;
610   for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ())
611     if (myAISContext->IsDisplayed(myAISContext->SelectedInteractive(), 1) || myAISContext->IsDisplayed(myAISContext->SelectedInteractive(), 6))
612       OneOrMoreInShadingOrColoredMesh=true;
613   pCmdUI->Enable (OneOrMoreInShadingOrColoredMesh);
614 }
615
616
617 void CViewer3dDoc::OnUpdateObjectShading(CCmdUI* pCmdUI)
618 {
619   bool OneOrMoreInWireframeOrColoredMesh = false;
620   for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ())
621     if (myAISContext->IsDisplayed(myAISContext->SelectedInteractive(),0) || myAISContext->IsDisplayed(myAISContext->SelectedInteractive(),6))
622       OneOrMoreInWireframeOrColoredMesh=true;
623   pCmdUI->Enable (OneOrMoreInWireframeOrColoredMesh);
624 }
625
626 void CViewer3dDoc::OnOptionsTrihedronDynamicTrihedron()
627 {
628   if (myAISContext -> IsDisplayed(myTrihedron))
629   {
630     myAISContext->Remove(myTrihedron, Standard_True);
631   }
632   else
633   {
634     Handle(Geom_Axis2Placement) myTrihedronAxis=new Geom_Axis2Placement(gp::XOY());
635     myTrihedron=new AIS_Trihedron(myTrihedronAxis);
636     myAISContext->SetTrihedronSize(200, Standard_False);
637     myAISContext->Display(myTrihedron, Standard_True);
638   }
639 }
640
641 void CViewer3dDoc::OnUpdateOptionsTrihedronDynamicTrihedron(CCmdUI* pCmdUI)
642 {
643   if (myAISContext->IsDisplayed(myTrihedron))
644     pCmdUI->SetCheck(1);
645   else
646     pCmdUI->SetCheck(0);
647 }
648
649 void CViewer3dDoc::SetMyStaticTrihedronAxisIsDisplayed(BOOL IsDisplayed)
650 {
651   myStaticTrihedronAxisIsDisplayed = IsDisplayed;
652 }
653
654 void CViewer3dDoc::OnUpdateOptionsTrihedronStaticTrihedron(CCmdUI* pCmdUI) 
655 {
656   if (myStaticTrihedronAxisIsDisplayed)
657     pCmdUI->SetCheck(1);
658   else
659     pCmdUI->SetCheck(0);
660
661 }
662
663 void  CViewer3dDoc::Popup (const Standard_Integer  x,
664                            const Standard_Integer  y ,
665                            const Handle(V3d_View)& aView)
666 {
667   myPopupMenuNumber=0;
668   // Specified check for context menu number to call
669   myAISContext->InitSelected();
670   if (myAISContext->MoreSelected())
671   {
672     if (myAISContext->SelectedInteractive()->IsKind(STANDARD_TYPE(User_Cylinder)))
673     {
674       myPopupMenuNumber = 2;
675       //return;
676     }
677   }
678   OCC_3dBaseDoc::Popup(x,y, aView);
679 }
680
681 //Set faces selection mode
682 void CViewer3dDoc::OnFaces() 
683 {
684   myAISContext->Deactivate();
685   myAISContext->Activate (AIS_Shape::SelectionMode (TopAbs_FACE));
686
687   myCResultDialog.SetTitle("Standard mode: TopAbs_FACE");
688   myCResultDialog.SetText("  myAISContext->Deactivate();\n"
689                           "  myAISContext->Activate (AIS_Shape::SelectionMode (TopAbs_FACE));\n\n");
690   SetTitle (L"Standard mode: TopAbs_FACE");
691 }
692
693 //Set edges selection mode
694 void CViewer3dDoc::OnEdges() 
695 {
696   myAISContext->Deactivate();
697   myAISContext->Activate (AIS_Shape::SelectionMode (TopAbs_EDGE));
698
699   myCResultDialog.SetTitle("Standard mode: TopAbs_EDGE");
700   myCResultDialog.SetText("  myAISContext->Deactivate();\n"
701                           "  myAISContext->Activate (AIS_Shape::SelectionMode (TopAbs_EDGE));\n\n");
702   SetTitle (L"Standard mode: TopAbs_EDGE");
703 }
704
705 // Set vertices selection mode
706 void CViewer3dDoc::OnVertices() 
707 {
708   myAISContext->Deactivate();
709   myAISContext->Activate (AIS_Shape::SelectionMode (TopAbs_VERTEX));
710
711   myCResultDialog.SetTitle("Standard mode: TopAbs_VERTEX");
712   myCResultDialog.SetText("  myAISContext->Deactivate();\n"
713                           "  myAISContext->Activate (AIS_Shape::SelectionMode (TopAbs_VERTEX));\n\n");
714   SetTitle (L"Standard mode: TopAbs_VERTEX");
715 }
716
717 //Neutral selection mode
718 void CViewer3dDoc::OnNeutral() 
719 {
720   myAISContext->Deactivate();
721   myAISContext->Activate (0);
722
723   myCResultDialog.SetTitle("Standard mode: Neutral");
724   myCResultDialog.SetText("  myAISContext->Deactivate();\n"
725                           "  myAISContext->Activate (0);\n\n");
726   SetTitle (L"Standard mode: Neutral");
727 }
728
729 // Change the color of faces on a user cylinder
730 void CViewer3dDoc::OnUsercylinderChangefacecolor() 
731 {
732   myAISContext->Activate (myAISContext->SelectedInteractive(), AIS_Shape::SelectionMode (TopAbs_FACE));
733   myState = FACE_COLOR;
734   // see the following of treatment in inputevent
735 }
736
737 // Make 3d fillets on solids
738 // It is necessary to activate the edges selection mode and select edges on an object
739 // before running this function
740 void CViewer3dDoc::OnFillet3d() 
741 {
742   myAISContext->InitSelected();
743   if (myAISContext->MoreSelected()) 
744   {
745     Handle(AIS_Shape) aSelInteractive (Handle(AIS_Shape)::DownCast (myAISContext->SelectedInteractive()));
746     if (aSelInteractive.IsNull())
747     {
748       AfxMessageBox (L"It is necessary to activate the edges selection mode\n"
749                      L"and select edges on an object before \nrunning this function");
750       return;
751     }
752
753     BRepFilletAPI_MakeFillet aFillet (aSelInteractive->Shape());
754
755     for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
756     {
757       const TopoDS_Shape& aSelShape = myAISContext->SelectedShape();
758       if (aSelShape.ShapeType() != TopAbs_EDGE)
759       {
760         AfxMessageBox (L"It is necessary to activate the edges selection mode\n"
761                        L"and select edges on an object before \nrunning this function");
762         return;
763       }
764     }
765
766     BoxRadius aDialog (NULL,10.);
767     if (aDialog.DoModal() == IDOK)
768     {
769       for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
770       {
771         // Selected shape has its own location, and sub-shapes in the solid shape for fillet building
772         // don't have own locations. Fillet builder needs to know that input edge is just the same one as
773         // some sub-shape in our target solid shape, so no location is to be in input edge for fillet builder.
774         TopoDS_Shape aSelShape = myAISContext->SelectedShape().Located (TopLoc_Location());
775         aFillet.Add (aDialog.m_radius, TopoDS::Edge (aSelShape) );
776       }
777     }
778     else
779     {
780       return;
781     }
782
783     TopoDS_Shape aNewShape;
784     try
785     {
786       aNewShape = aFillet.Shape();
787     }
788     catch (Standard_Failure)
789     {
790       AfxMessageBox (L"Error During Fillet computation");
791       return;
792     }
793
794     aSelInteractive ->Set (aNewShape);
795     myAISContext->Redisplay (aSelInteractive, Standard_True);
796   }
797
798   myCResultDialog.SetTitle("Make a fillet");
799   myCResultDialog.SetText("  Handle(AIS_Shape) S = Handle(AIS_Shape)::DownCast(myAISContext->Interactive()); \n"
800                                     "  \n"
801                                     "  BRepAPI_MakeFillet aFillet(S->Shape()); \n"
802                                     "  \n"
803                                     "  TopoDS_Edge anEdge=TopoDS::Edge(myAISContext->SelectedShape()); \n"
804                                     "  \n"
805                                     "  aFillet.Add(dlg.m_radius,anEdge); \n"
806                                     "  \n"
807                                     "  TopoDS_Shape aNewShape = aFillet.Shape(); \n"
808                                     "  \n"
809                                     "  S->Set(aNewShape); \n"
810                                     "  \n"
811                                     "  myAISContext->Redisplay(S); \n"
812                                     "  \n");
813   SetTitle (L"Make a fillet");
814 }
815
816 // Create and display a circle with standard tools
817 void CViewer3dDoc::OnCircle() 
818 {
819   gp_Ax2 anAx2 (gp_Pnt (0., 0., 0.), gp_Dir(0., 0., -1.));
820   Handle(Geom_Circle) aGeomCircle = new Geom_Circle (anAx2, 300);
821
822   // the lines above substitute
823   // GC_MakeCircle C(gp_Pnt(-100.,-300.,0.),gp_Pnt(-50.,-200.,0.),gp_Pnt(-10.,-250.,0.));
824   // Handle(AIS_Circle) anAISCirc = new AIS_Circle(C.Value());
825
826   Handle(AIS_Circle) anAISCirc = new AIS_Circle(aGeomCircle);
827   myAISContext->Display (anAISCirc, Standard_True);
828
829   myCResultDialog.SetTitle("Create a circle");
830   myCResultDialog.SetText("  GC_MakeCircle C(gp_Pnt(-100.,-300.,0.),gp_Pnt(-50.,-200.,0.),gp_Pnt(-10.,-250.,0.)); \n"
831                                     "  \n"
832                                     "  Handle(AIS_Circle) anAISCirc = new AIS_Circle(C.Value()); \n"
833                                     "  \n"
834                                     "  myAISContext->Display(anAISCirc); \n"
835                                     "  \n");
836   SetTitle (L"Create a circle");
837 }
838
839 void CViewer3dDoc::OnLine() 
840 {
841   // TODO: Add your command handler code here
842   gp_Lin aGpLin (gp_Pnt (0., 0., 0.), gp_Dir(1., 0., 0.));
843   Handle(Geom_Line) aGeomLin = new Geom_Line (aGpLin);
844   Handle(AIS_Line) anAISLine = new AIS_Line (aGeomLin);
845   myAISContext->Display (anAISLine, Standard_True);
846
847   myCResultDialog.SetTitle("Create a line");
848   myCResultDialog.SetText("  gp_Lin L(gp_Pnt(0.,0.,0.),gp_Dir(1.,0.,0.)); \n"
849                                     "  \n"
850                                     "  Handle(Geom_Line) aLine = new Geom_Line(L); \n"
851                                     "  \n"
852                                     "  Handle(AIS_Line) anAISLine = new AIS_Line(aLine); \n"
853                                     "  \n"
854                                     "  myAISContext->Display(anAISLine); \n"
855                                     "  \n");
856   SetTitle (L"Create a line");
857 }
858
859 void CViewer3dDoc::OnNbisos() 
860 {
861   int aNumU = myAISContext->DefaultDrawer()->UIsoAspect()->Number();
862   int aNumV = myAISContext->DefaultDrawer()->VIsoAspect()->Number();
863
864   DlgIsos aDlg (NULL, aNumU, aNumV);
865
866   if (aDlg.DoModal() == IDOK)
867   {
868     myAISContext->DefaultDrawer()->UIsoAspect()->SetNumber (aDlg.m_isou);
869     myAISContext->DefaultDrawer()->VIsoAspect()->SetNumber (aDlg.m_isov);
870
871     myCResultDialog.SetTitle("Iso Aspect");
872     myCResultDialog.SetText("  myAISContext->DefaultDrawer()->UIsoAspect()->SetNumber(dlg.m_isou); \n"
873                                       "  \n"
874                                       "  myAISContext->DefaultDrawer()->VIsoAspect()->SetNumber(dlg.m_isov); \n"
875                                       "  \n");
876     SetTitle (L"Iso Aspect");
877   }
878 }
879
880 BOOL CViewer3dDoc::OnNewDocument()
881 {
882   if (!CDocument::OnNewDocument())
883     return FALSE;
884
885   // TODO: add reinitialization code here
886   // (SDI documents will reuse this document)
887   SetTitle(myPresentation->GetName());
888
889   myAISContext->EraseAll (Standard_False);
890   myAISContext->SetDisplayMode(AIS_Shaded, Standard_True);
891
892   POSITION pos = GetFirstViewPosition();
893   while (pos != NULL)
894   {
895     CViewer3dView* pView = (CViewer3dView*) GetNextView(pos);
896     pView->Reset();
897   }  
898
899   return TRUE;
900 }
901
902 void CViewer3dDoc::OnFileNew()
903 {
904   OnNewDocument();
905 }
906
907 void CViewer3dDoc::InitViewButtons()
908 {
909   POSITION pos = GetFirstViewPosition();
910   while (pos != NULL)
911   {
912     CViewer3dView* pView = (CViewer3dView*) GetNextView(pos);
913     pView->InitButtons();
914   }
915 }
916
917 void CViewer3dDoc::OnTextureOn()
918 {
919         isTextureSampleStarted = TRUE;
920         Start();
921 }
922
923 void CViewer3dDoc::DoSample()
924 {
925   InitViewButtons();
926
927   HCURSOR hOldCursor = ::GetCursor();
928   HCURSOR hNewCursor = AfxGetApp()->LoadStandardCursor(IDC_APPSTARTING);
929
930   SetCursor(hNewCursor);
931   {
932     try
933     {
934       myPresentation->DoSample();
935     }
936     catch (Standard_Failure const& anException)
937     {
938       Standard_SStream aSStream;
939       aSStream << "An exception was caught: " << anException << std::ends;
940       CString aMsg = aSStream.str().c_str();
941       AfxMessageBox (aMsg);
942     }
943   }
944   SetCursor(hOldCursor);
945 }
946
947 void CViewer3dDoc::OnBUTTONStart() 
948 {
949   myAISContext->EraseAll (Standard_True);
950   myPresentation->FirstSample();
951   DoSample();
952 }
953
954 void CViewer3dDoc::OnBUTTONEnd()
955 {
956   myAISContext->EraseAll (Standard_True);
957   myPresentation->LastSample();
958   DoSample();
959 }
960
961 void CViewer3dDoc::OnBUTTONRepeat() 
962 {
963   DoSample();
964 }
965
966 void CViewer3dDoc::OnBUTTONNext() 
967 {
968   if (!myPresentation->AtLastSample())
969   {
970     myPresentation->NextSample();
971     DoSample();
972   }
973 }
974
975 void CViewer3dDoc::OnBUTTONPrev() 
976 {
977   if (!myPresentation->AtFirstSample())
978   {
979     myPresentation->PrevSample();
980     DoSample();
981   }
982 }
983
984 void CViewer3dDoc::OnUpdateBUTTONNext(CCmdUI* pCmdUI) 
985 {
986         if ( isTextureSampleStarted )
987                 pCmdUI->Enable (!myPresentation->AtLastSample());
988         else
989                 pCmdUI->Enable (FALSE);
990 }
991
992 void CViewer3dDoc::OnUpdateBUTTONPrev(CCmdUI* pCmdUI) 
993 {
994         if ( isTextureSampleStarted )
995                 pCmdUI->Enable (!myPresentation->AtFirstSample());
996         else
997                 pCmdUI->Enable (FALSE);
998 }
999
1000 void CViewer3dDoc::OnUpdateBUTTONStart(CCmdUI* pCmdUI) 
1001 {
1002         pCmdUI->Enable (isTextureSampleStarted);
1003 }
1004
1005 void CViewer3dDoc::OnUpdateBUTTONRepeat(CCmdUI* pCmdUI) 
1006 {
1007         pCmdUI->Enable (isTextureSampleStarted);
1008 }
1009
1010 void CViewer3dDoc::OnUpdateBUTTONEnd(CCmdUI* pCmdUI) 
1011 {
1012         pCmdUI->Enable (isTextureSampleStarted);
1013 }
1014
1015 void CViewer3dDoc::OnDumpView()
1016 {
1017   for (POSITION aPos = GetFirstViewPosition(); aPos != NULL;)
1018   {
1019     CViewer3dView* pView = (CViewer3dView* )GetNextView (aPos);
1020     pView->UpdateWindow();
1021   }
1022
1023   Handle(V3d_View) aView = myViewer->ActiveViews().First();
1024   ExportView (aView);
1025 }
1026
1027 void CViewer3dDoc::Start()
1028 {
1029   myPresentation->Init();
1030   OnBUTTONStart();
1031 }
1032
1033 void CViewer3dDoc::Fit()
1034 {
1035         CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
1036         CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
1037         CViewer3dView *pView = (CViewer3dView *) pChild->GetActiveView();
1038         pView->FitAll();
1039 }