0023776: Redesign of MFC samples after V2d viewer removing
[occt.git] / samples / mfc / standard / 09_Animation / src / AnimationDoc.cpp
1 // AnimationDocApp.cpp : implementation of the CAnimationDoc class
2 //
3
4 #include "stdafx.h"
5
6 #include "AnimationApp.h"
7 #include "AnimationDoc.h"
8 #include "AnimationView3D.h"
9 #include "AISDialogs.h"
10 #include "ShadingDialog.h"
11 #include "ThreadDialog.h"
12 #include "Fonc.hxx"
13
14 #define DEFAULT_COLOR Quantity_NOC_CYAN1
15 #define DEFAULT_MATERIAL Graphic3d_NOM_PLASTER
16 #define DEFAULT_DEVIATIONCOEFFICIENT 0.001
17 #define DEFAULT_HILIGHTCOLOR Quantity_NOC_YELLOW
18
19
20 #ifdef _DEBUG
21 //#define new DEBUG_NEW  // by cascade
22 #undef THIS_FILE
23 static char THIS_FILE[] = __FILE__;
24 #endif
25
26 /////////////////////////////////////////////////////////////////////////////
27 // CAnimationDoc
28
29 IMPLEMENT_DYNCREATE(CAnimationDoc, CDocument)
30
31 BEGIN_MESSAGE_MAP(CAnimationDoc, CDocument)
32         //{{AFX_MSG_MAP(CAnimationDoc)
33         ON_COMMAND(ID_SHADING, OnShading)
34         ON_COMMAND(ID_Thread, OnThread)
35         ON_COMMAND(ID_FILE_LOADGRID, OnFileLoadgrid)
36         ON_UPDATE_COMMAND_UI(ID_WALK_WALKTHRU, OnUpdateWalkWalkthru)
37         //}}AFX_MSG_MAP
38
39 END_MESSAGE_MAP()
40
41 /////////////////////////////////////////////////////////////////////////////
42 // CAnimationDoc construction/destruction
43
44 CAnimationDoc::CAnimationDoc()
45 {
46         // TODO: add one-time construction code here
47
48         static Standard_Integer StaticCount=1;
49         StaticCount++;
50         myCount = StaticCount;
51
52         Handle(Graphic3d_GraphicDriver) aGraphicDriver = 
53                 ((CAnimationApp*)AfxGetApp())->GetGraphicDriver();
54
55     TCollection_ExtendedString a3DName("Visu3D");
56         myViewer = new V3d_Viewer(aGraphicDriver,a3DName.ToExtString(),"", 1000.0, 
57                               V3d_XposYnegZpos, Quantity_NOC_GRAY30,
58                               V3d_ZBUFFER,V3d_GOURAUD,V3d_WAIT, 
59                               Standard_True, Standard_False);
60
61         myViewer->SetDefaultLights();
62         myViewer->SetLightOn();
63
64         myAISContext =new AIS_InteractiveContext(myViewer);
65
66         myDeviation = 0.0008;
67         thread = 4;
68         myAngle = 0;
69
70         BRep_Builder B;
71         TopoDS_Shape CrankArm;
72         TopoDS_Shape CylinderHead;
73         TopoDS_Shape Propeller;
74         TopoDS_Shape Piston;
75         TopoDS_Shape EngineBlock;
76         
77         char AbloluteExecutableFileName[200];
78         HMODULE hModule = GetModuleHandle(NULL);
79         GetModuleFileName (hModule, AbloluteExecutableFileName, 200);
80
81         CString aString(AbloluteExecutableFileName);
82         int index = aString.ReverseFind('\\');
83
84         aString.Delete(index+1, aString.GetLength() - index - 1);
85
86         TCHAR tchBuf[80];
87
88         CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL); 
89         aString = (CASROOTValue + "\\data\\occ");       
90
91         char DataDirPath[200];
92         strcpy_s(DataDirPath, aString);
93         char temp[200];
94         strcpy_s(temp, DataDirPath);
95
96     strcat_s(temp,"\\CrankArm.rle");
97         BRepTools::Read(CrankArm, temp, B);
98         
99     strcpy_s(temp, DataDirPath);
100     strcat_s(temp,"\\CylinderHead.rle");
101         BRepTools::Read(CylinderHead, temp, B);
102
103         strcpy_s(temp, DataDirPath);
104     strcat_s(temp,"\\Propeller.rle");
105         BRepTools::Read(Propeller, temp, B);
106
107         strcpy_s(temp, DataDirPath);
108     strcat_s(temp,"\\Piston.rle");
109         BRepTools::Read(Piston, temp, B);
110
111         strcpy_s(temp, DataDirPath);
112     strcat_s(temp,"\\EngineBlock.rle");
113         BRepTools::Read(EngineBlock, temp, B);
114         
115         if (CrankArm.IsNull() || CylinderHead.IsNull() || 
116         Propeller.IsNull() || Piston.IsNull() || EngineBlock.IsNull())
117         {
118                 int rep = MessageBox(NULL, "Shape(s) not found.\nCheck the Data directory path!", "Error",MB_OK | MB_ICONERROR);
119                 if (rep == IDOK)
120                         exit(0);
121         }
122         myAISContext->SetDeviationCoefficient(myDeviation);
123
124         myAisCylinderHead = new AIS_Shape (CylinderHead);
125         myAISContext->SetColor    (myAisCylinderHead, Quantity_NOC_WHITE);
126         myAISContext->SetMaterial (myAisCylinderHead, Graphic3d_NOM_PLASTIC);
127         myAisEngineBlock  = new AIS_Shape (EngineBlock);
128         myAISContext->SetColor(myAisEngineBlock,   Quantity_NOC_WHITE);
129         myAISContext->SetMaterial(myAisEngineBlock,Graphic3d_NOM_PLASTIC);
130
131         myAISContext->Display(myAisCylinderHead ,1,-1,Standard_False,Standard_False);
132         myAISContext->Display(myAisEngineBlock  ,1,-1,Standard_False,Standard_False);
133
134         myAisCrankArm     = new AIS_Shape (CrankArm);
135         myAISContext->SetColor   (myAisCrankArm, Quantity_NOC_HOTPINK);
136         myAISContext->SetMaterial(myAisCrankArm, Graphic3d_NOM_PLASTIC);
137         myAisPiston       = new AIS_Shape (Piston);
138         myAISContext->SetColor   (myAisPiston  , Quantity_NOC_WHITE);
139         myAISContext->SetMaterial(myAisPiston  , Graphic3d_NOM_PLASTIC);
140         myAisPropeller    = new AIS_Shape (Propeller);
141         myAISContext->SetColor   (myAisPropeller, Quantity_NOC_RED);
142         myAISContext->SetMaterial(myAisPropeller, Graphic3d_NOM_PLASTIC);
143
144         myAISContext->Display(myAisCrankArm     ,1,-1,Standard_False,Standard_False);
145         myAISContext->Display(myAisPropeller    ,1,-1,Standard_False,Standard_False);
146         myAISContext->Display(myAisPiston       ,1,-1,Standard_True,Standard_False);
147
148         m_Xmin = -300. ;
149         m_Ymin = -300. ;
150         m_Zmin = -300. ;
151         m_Xmax = +300. ;
152         m_Ymax = +300. ;
153         m_Zmax = +300. ;
154
155         m_bIsGridLoaded = FALSE;
156 }
157
158 CAnimationDoc::~CAnimationDoc()
159 {
160 }
161
162 /////////////////////////////////////////////////////////////////////////////
163 // CAnimationDoc diagnostics
164
165 #ifdef _DEBUG
166 void CAnimationDoc::AssertValid() const
167 {
168         CDocument::AssertValid();
169 }
170
171 void CAnimationDoc::Dump(CDumpContext& dc) const
172 {
173         CDocument::Dump(dc);
174 }
175 #endif //_DEBUG
176
177 /////////////////////////////////////////////////////////////////////////////
178 // CAnimationDoc commands
179 //-----------------------------------------------------------------------------------------
180 //
181 //-----------------------------------------------------------------------------------------
182 void CAnimationDoc::DragEvent(const Standard_Integer  x        ,
183                                                   const Standard_Integer  y        ,
184                                                   const Standard_Integer  TheState ,
185                                   const Handle(V3d_View)& aView    )
186 {
187
188     // TheState == -1  button down
189         // TheState ==  0  move
190         // TheState ==  1  button up
191
192     static Standard_Integer theButtonDownX=0;
193     static Standard_Integer theButtonDownY=0;
194
195         if (TheState == -1)
196     {
197       theButtonDownX=x;
198       theButtonDownY=y;
199     }
200
201         if (TheState == 1)
202           myAISContext->Select(theButtonDownX,theButtonDownY,x,y,aView);  
203 }
204
205 //-----------------------------------------------------------------------------------------
206 //
207 //-----------------------------------------------------------------------------------------
208 void CAnimationDoc::InputEvent(const Standard_Integer  x     ,
209                                                    const Standard_Integer  y     ,
210                                    const Handle(V3d_View)& aView ) 
211 {
212     myAISContext->Select(); 
213 }
214
215 //-----------------------------------------------------------------------------------------
216 //
217 //-----------------------------------------------------------------------------------------
218 void CAnimationDoc::MoveEvent(const Standard_Integer  x       ,
219                                   const Standard_Integer  y       ,
220                                   const Handle(V3d_View)& aView   ) 
221 {
222       myAISContext->MoveTo(x,y,aView);
223 }
224
225 //-----------------------------------------------------------------------------------------
226 //
227 //-----------------------------------------------------------------------------------------
228 void CAnimationDoc::ShiftMoveEvent(const Standard_Integer  x       ,
229                                   const Standard_Integer  y       ,
230                                   const Handle(V3d_View)& aView   ) 
231 {
232       myAISContext->MoveTo(x,y,aView);
233 }
234
235 //-----------------------------------------------------------------------------------------
236 //
237 //-----------------------------------------------------------------------------------------
238 void CAnimationDoc::ShiftDragEvent(const Standard_Integer  x        ,
239                                                                            const Standard_Integer  y        ,
240                                                                            const Standard_Integer  TheState ,
241                                        const Handle(V3d_View)& aView    ) 
242 {
243     static Standard_Integer theButtonDownX=0;
244     static Standard_Integer theButtonDownY=0;
245
246         if (TheState == -1)
247     {
248       theButtonDownX=x;
249       theButtonDownY=y;
250     }
251
252         if (TheState == 0)
253           myAISContext->ShiftSelect(theButtonDownX,theButtonDownY,x,y,aView);  
254 }
255
256
257 //-----------------------------------------------------------------------------------------
258 //
259 //-----------------------------------------------------------------------------------------
260 void CAnimationDoc::ShiftInputEvent(const Standard_Integer  x       ,
261                                                                             const Standard_Integer  y       ,
262                                         const Handle(V3d_View)& aView   ) 
263 {
264         myAISContext->ShiftSelect(); 
265 }
266
267 //-----------------------------------------------------------------------------------------
268 //
269 //-----------------------------------------------------------------------------------------
270 void  CAnimationDoc::Popup(const Standard_Integer  x,
271                                                            const Standard_Integer  y ,
272                                const Handle(V3d_View)& aView   ) 
273 {
274 }
275
276 void CAnimationDoc::OnMyTimer() 
277 {
278         // TODO: Add your message handler code here and/or call default
279         
280         Standard_Real angleA;
281         Standard_Real angleB;
282         Standard_Real X;
283         gp_Ax1 Ax1(gp_Pnt(0,0,0),gp_Vec(0,0,1));
284
285         myAngle++;
286
287         angleA = thread*myAngle*M_PI/180;
288         X = Sin(angleA)*3/8;
289         angleB = atan(X / Sqrt(-X * X + 1));
290         Standard_Real decal(25*0.6);
291
292         
293         //Build a transformation on the display
294     gp_Trsf aPropellerTrsf;
295     aPropellerTrsf.SetRotation(Ax1,angleA);
296         myAISContext->SetLocation(myAisPropeller,aPropellerTrsf);
297         
298         gp_Ax3 base(gp_Pnt(3*decal*(1-Cos(angleA)),-3*decal*Sin(angleA),0),gp_Vec(0,0,1),gp_Vec(1,0,0));
299     gp_Trsf aCrankArmTrsf;
300     aCrankArmTrsf.SetTransformation(   base.Rotated(gp_Ax1(gp_Pnt(3*decal,0,0),gp_Dir(0,0,1)),angleB));
301         myAISContext->SetLocation(myAisCrankArm,aCrankArmTrsf);
302
303     gp_Trsf aPistonTrsf;
304     aPistonTrsf.SetTranslation(gp_Vec(-3*decal*(1-Cos(angleA))-8*decal*(1-Cos(angleB)),0,0));
305         myAISContext->SetLocation(myAisPiston,aPistonTrsf);
306
307     myAISContext->UpdateCurrentViewer();
308 }
309
310 void CAnimationDoc::OnShading() 
311 {
312         
313         POSITION position = GetFirstViewPosition();
314         CView* pCurrentView = (CView*)GetNextView(position);
315         ((CAnimationView3D *)pCurrentView)              ->OnStop();
316
317         CShadingDialog aDial(NULL);
318
319         aDial.myvalue=int((myDeviation-0.00003)/0.00003);
320         
321         if (aDial.DoModal()==IDOK) {
322                 Standard_Real dev(aDial.myvalue);
323                 myDeviation = 0.00003+0.00003*dev;
324
325                 myAISContext->SetDeviationCoefficient(myDeviation);
326         TopoDS_Shape Propeller=myAisPropeller->Shape();
327                 BRepTools::Clean(Propeller);
328                 
329         myAisPropeller->Set(Propeller);
330         myAISContext->Deactivate(myAisPropeller);
331         myAISContext->Redisplay(myAisPropeller);
332         }
333         ((CAnimationView3D *)pCurrentView)->OnRestart();
334 }
335
336
337 void CAnimationDoc::OnThread() 
338 {
339         POSITION position = GetFirstViewPosition();
340     CView* pCurrentView = (CView*)GetNextView(position);
341         ((CAnimationView3D *)pCurrentView)              ->OnStop();
342         CThreadDialog aThreadDial(NULL);
343         if (aThreadDial.DoModal()==IDOK) {
344                 thread = aThreadDial.m_Angle;
345         }
346
347         ((CAnimationView3D *)pCurrentView)->OnRestart();
348 }
349
350
351 void CAnimationDoc::OnFileLoadgrid() 
352 {
353         // TODO: Add your command handler code here
354
355   CFileDialog dlg(TRUE,
356                   NULL,
357                   NULL,
358                   OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
359                   "Points Files (*.pnt;*.pnts)|*.pnt; *.pnts|All Files (*.*)|*.*||", 
360                   NULL );
361
362         CString initdir(((OCC_App*) AfxGetApp())->GetInitDataDir());
363         initdir += "\\..\\..\\Data\\";
364
365         dlg.m_ofn.lpstrInitialDir = initdir;
366
367
368   if (dlg.DoModal() == IDOK)
369     {
370       CString C = dlg.GetPathName();
371       SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
372
373       Handle_Geom_BSplineSurface mySurface;
374
375       if(grid2surf(C,mySurface ))
376         {
377           //To set the minimum value of the surface to Z=0 
378           Standard_Real Tolerance = 0.0;
379           Bnd_Box B;
380       TopoDS_Face myFace;
381           GeomAdaptor_Surface GAS(mySurface);
382
383           
384           BndLib_AddSurface::Add(GAS,Tolerance,B);
385
386           Standard_Real Xmin,Xmax,Ymin,Ymax,Zmin,Zmax;
387           B.Get(Xmin,Ymin,Zmin,Xmax,Ymax,Zmax);
388           TopoDS_Solid Box = BRepPrimAPI_MakeBox(gp_Pnt(Xmin,Ymin,0),Xmax-Xmin,Ymax-Ymin,50).Solid();
389
390           gp_Vec V(gp_Pnt(0,0,Zmin),gp_Pnt(0,0,0));
391           
392           gp_Trsf T;
393           T.SetTranslation(V);
394           mySurface->Transform(T);
395           BRepBuilderAPI_MakeFace aMkFace(mySurface, Precision::Confusion());
396           myFace = aMkFace.Face();
397
398           // Remove all other shapes
399           myAISContext->RemoveAll();
400
401           Handle(AIS_Shape) myAISSurface = new AIS_Shape(myFace);
402
403           myAISContext->Display(myAISSurface, Standard_False);
404           myAISContext->Deactivate(myAISSurface,Standard_False);
405           myAISContext->SetColor (myAISSurface,Quantity_NOC_WHITE,Standard_False);
406           myAISContext->SetMaterial (myAISSurface,Graphic3d_NOM_STONE,Standard_False);
407           myAISContext->SetDisplayMode (myAISSurface,1,Standard_False);
408           myAISContext->SetDeviationCoefficient (0.001);
409         CMDIFrameWnd *pFrame =  (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
410         CMDIChildWnd *pChild =  (CMDIChildWnd *) pFrame->GetActiveFrame();
411         CAnimationView3D *pView = (CAnimationView3D *) pChild->GetActiveView();
412         pView->FitAll();
413
414       Bnd_Box Boite;
415       BRepBndLib::Add(myFace, Boite);
416       Boite.Get(m_Xmin, m_Ymin, m_Zmin, m_Xmax, m_Ymax, m_Zmax);
417           
418           m_bIsGridLoaded = TRUE;
419         }       
420       SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
421 }
422
423
424 }
425
426 void CAnimationDoc::OnUpdateWalkWalkthru(CCmdUI* pCmdUI) 
427 {
428         // TODO: Add your command update UI handler code here
429
430         int i ;
431         char szMsg [256] ;
432         i = GetEnvironmentVariable( "CSF_WALKTHROUGH" , szMsg , sizeof szMsg ) ;
433         if ( i )
434                 pCmdUI->SetCheck ( 1 ) ;
435         else
436                 pCmdUI->SetCheck ( 0 ) ;
437 }