1 // AnimationDocApp.cpp : implementation of the CAnimationDoc class
6 #include "AnimationApp.h"
7 #include "AnimationDoc.h"
8 #include "AnimationView3D.h"
9 #include "AISDialogs.h"
10 #include "ShadingDialog.h"
11 #include "ThreadDialog.h"
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
21 //#define new DEBUG_NEW // by cascade
23 static char THIS_FILE[] = __FILE__;
26 /////////////////////////////////////////////////////////////////////////////
29 IMPLEMENT_DYNCREATE(CAnimationDoc, CDocument)
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)
41 /////////////////////////////////////////////////////////////////////////////
42 // CAnimationDoc construction/destruction
44 CAnimationDoc::CAnimationDoc()
46 // TODO: add one-time construction code here
48 static Standard_Integer StaticCount=1;
50 myCount = StaticCount;
52 Handle(Graphic3d_WNTGraphicDevice) theGraphicDevice =
53 ((CAnimationApp*)AfxGetApp())->GetGraphicDevice();
55 TCollection_ExtendedString a3DName("Visu3D");
56 myViewer = new V3d_Viewer(theGraphicDevice,a3DName.ToExtString(),"", 1000.0,
57 V3d_XposYnegZpos, Quantity_NOC_GRAY30,
58 V3d_ZBUFFER,V3d_GOURAUD,V3d_WAIT,
59 Standard_True, Standard_False);
61 myViewer->SetDefaultLights();
62 myViewer->SetLightOn();
64 myAISContext =new AIS_InteractiveContext(myViewer);
71 TopoDS_Shape CrankArm;
72 TopoDS_Shape CylinderHead;
73 TopoDS_Shape Propeller;
75 TopoDS_Shape EngineBlock;
77 char AbloluteExecutableFileName[200];
78 HMODULE hModule = GetModuleHandle(NULL);
79 GetModuleFileName (hModule, AbloluteExecutableFileName, 200);
81 CString aString(AbloluteExecutableFileName);
82 int index = aString.ReverseFind('\\');
84 aString.Delete(index+1, aString.GetLength() - index - 1);
88 CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);
89 aString = (CASROOTValue + "\\..\\data\\occ");
91 char DataDirPath[200];
92 strcpy_s(DataDirPath, aString);
94 strcpy_s(temp, DataDirPath);
96 strcat_s(temp,"\\CrankArm.rle");
97 BRepTools::Read(CrankArm, temp, B);
99 strcpy_s(temp, DataDirPath);
100 strcat_s(temp,"\\CylinderHead.rle");
101 BRepTools::Read(CylinderHead, temp, B);
103 strcpy_s(temp, DataDirPath);
104 strcat_s(temp,"\\Propeller.rle");
105 BRepTools::Read(Propeller, temp, B);
107 strcpy_s(temp, DataDirPath);
108 strcat_s(temp,"\\Piston.rle");
109 BRepTools::Read(Piston, temp, B);
111 strcpy_s(temp, DataDirPath);
112 strcat_s(temp,"\\EngineBlock.rle");
113 BRepTools::Read(EngineBlock, temp, B);
115 if (CrankArm.IsNull() || CylinderHead.IsNull() ||
116 Propeller.IsNull() || Piston.IsNull() || EngineBlock.IsNull())
118 int rep = MessageBox(NULL, "Shape(s) not found.\nCheck the Data directory path!", "Error",MB_OK | MB_ICONERROR);
122 myAISContext->SetDeviationCoefficient(myDeviation);
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);
131 myAISContext->Display(myAisCylinderHead ,1,-1,Standard_False,Standard_False);
132 myAISContext->Display(myAisEngineBlock ,1,-1,Standard_False,Standard_False);
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);
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);
155 m_bIsGridLoaded = FALSE;
158 CAnimationDoc::~CAnimationDoc()
162 /////////////////////////////////////////////////////////////////////////////
163 // CAnimationDoc diagnostics
166 void CAnimationDoc::AssertValid() const
168 CDocument::AssertValid();
171 void CAnimationDoc::Dump(CDumpContext& dc) const
177 /////////////////////////////////////////////////////////////////////////////
178 // CAnimationDoc commands
179 //-----------------------------------------------------------------------------------------
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 )
188 // TheState == -1 button down
189 // TheState == 0 move
190 // TheState == 1 button up
192 static Standard_Integer theButtonDownX=0;
193 static Standard_Integer theButtonDownY=0;
202 myAISContext->Select(theButtonDownX,theButtonDownY,x,y,aView);
205 //-----------------------------------------------------------------------------------------
207 //-----------------------------------------------------------------------------------------
208 void CAnimationDoc::InputEvent(const Standard_Integer x ,
209 const Standard_Integer y ,
210 const Handle(V3d_View)& aView )
212 myAISContext->Select();
215 //-----------------------------------------------------------------------------------------
217 //-----------------------------------------------------------------------------------------
218 void CAnimationDoc::MoveEvent(const Standard_Integer x ,
219 const Standard_Integer y ,
220 const Handle(V3d_View)& aView )
222 myAISContext->MoveTo(x,y,aView);
225 //-----------------------------------------------------------------------------------------
227 //-----------------------------------------------------------------------------------------
228 void CAnimationDoc::ShiftMoveEvent(const Standard_Integer x ,
229 const Standard_Integer y ,
230 const Handle(V3d_View)& aView )
232 myAISContext->MoveTo(x,y,aView);
235 //-----------------------------------------------------------------------------------------
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 )
243 static Standard_Integer theButtonDownX=0;
244 static Standard_Integer theButtonDownY=0;
253 myAISContext->ShiftSelect(theButtonDownX,theButtonDownY,x,y,aView);
257 //-----------------------------------------------------------------------------------------
259 //-----------------------------------------------------------------------------------------
260 void CAnimationDoc::ShiftInputEvent(const Standard_Integer x ,
261 const Standard_Integer y ,
262 const Handle(V3d_View)& aView )
264 myAISContext->ShiftSelect();
267 //-----------------------------------------------------------------------------------------
269 //-----------------------------------------------------------------------------------------
270 void CAnimationDoc::Popup(const Standard_Integer x,
271 const Standard_Integer y ,
272 const Handle(V3d_View)& aView )
276 void CAnimationDoc::OnMyTimer()
278 // TODO: Add your message handler code here and/or call default
280 Standard_Real angleA;
281 Standard_Real angleB;
283 gp_Ax1 Ax1(gp_Pnt(0,0,0),gp_Vec(0,0,1));
287 angleA = thread*myAngle*PI/180;
289 angleB = atan(X / Sqrt(-X * X + 1));
290 Standard_Real decal(25*0.6);
293 //Build a transformation on the display
294 gp_Trsf aPropellerTrsf;
295 aPropellerTrsf.SetRotation(Ax1,angleA);
296 myAISContext->SetLocation(myAisPropeller,aPropellerTrsf);
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);
304 aPistonTrsf.SetTranslation(gp_Vec(-3*decal*(1-Cos(angleA))-8*decal*(1-Cos(angleB)),0,0));
305 myAISContext->SetLocation(myAisPiston,aPistonTrsf);
307 myAISContext->UpdateCurrentViewer();
310 void CAnimationDoc::OnShading()
313 POSITION position = GetFirstViewPosition();
314 CView* pCurrentView = (CView*)GetNextView(position);
315 ((CAnimationView3D *)pCurrentView) ->OnStop();
317 CShadingDialog aDial(NULL);
319 aDial.myvalue=int((myDeviation-0.00003)/0.00003);
321 if (aDial.DoModal()==IDOK) {
322 Standard_Real dev(aDial.myvalue);
323 myDeviation = 0.00003+0.00003*dev;
325 myAISContext->SetDeviationCoefficient(myDeviation);
326 TopoDS_Shape Propeller=myAisPropeller->Shape();
327 BRepTools::Clean(Propeller);
329 myAisPropeller->Set(Propeller);
330 myAISContext->Deactivate(myAisPropeller);
331 myAISContext->Redisplay(myAisPropeller);
333 ((CAnimationView3D *)pCurrentView)->OnRestart();
337 void CAnimationDoc::OnThread()
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;
347 ((CAnimationView3D *)pCurrentView)->OnRestart();
351 void CAnimationDoc::OnFileLoadgrid()
353 // TODO: Add your command handler code here
355 CFileDialog dlg(TRUE,
358 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
359 "Points Files (*.pnt;*.pnts)|*.pnt; *.pnts|All Files (*.*)|*.*||",
362 CString initdir(((OCC_BaseApp*) AfxGetApp())->GetInitDataDir());
363 initdir += "\\..\\..\\Data\\";
365 dlg.m_ofn.lpstrInitialDir = initdir;
368 if (dlg.DoModal() == IDOK)
370 CString C = dlg.GetPathName();
371 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
373 Handle_Geom_BSplineSurface mySurface;
375 if(grid2surf(C,mySurface ))
377 //To set the minimum value of the surface to Z=0
378 Standard_Real Tolerance = 0.0;
381 GeomAdaptor_Surface GAS(mySurface);
384 BndLib_AddSurface::Add(GAS,Tolerance,B);
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();
390 gp_Vec V(gp_Pnt(0,0,Zmin),gp_Pnt(0,0,0));
394 mySurface->Transform(T);
395 BRepBuilderAPI_MakeFace aMkFace(mySurface);
396 myFace = aMkFace.Face();
398 // Remove all other shapes
399 myAISContext->RemoveAll();
401 Handle(AIS_Shape) myAISSurface = new AIS_Shape(myFace);
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();
415 BRepBndLib::Add(myFace, Boite);
416 Boite.Get(m_Xmin, m_Ymin, m_Zmin, m_Xmax, m_Ymax, m_Zmax);
418 m_bIsGridLoaded = TRUE;
420 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
426 void CAnimationDoc::OnUpdateWalkWalkthru(CCmdUI* pCmdUI)
428 // TODO: Add your command update UI handler code here
432 i = GetEnvironmentVariable( "CSF_WALKTHROUGH" , szMsg , sizeof szMsg ) ;
434 pCmdUI->SetCheck ( 1 ) ;
436 pCmdUI->SetCheck ( 0 ) ;