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_GraphicDriver) aGraphicDriver =
53 ((CAnimationApp*)AfxGetApp())->GetGraphicDriver();
55 myViewer = new V3d_Viewer(aGraphicDriver);
56 myViewer->SetDefaultLights();
57 myViewer->SetLightOn();
59 myAISContext =new AIS_InteractiveContext(myViewer);
66 TopoDS_Shape CrankArm;
67 TopoDS_Shape CylinderHead;
68 TopoDS_Shape Propeller;
70 TopoDS_Shape EngineBlock;
72 /*wchar_t AbloluteExecutableFileName[200];
73 HMODULE hModule = GetModuleHandleW (NULL);
74 GetModuleFileNameW (hModule, AbloluteExecutableFileName, 200);
75 CString aDataDirPath (AbloluteExecutableFileName);
76 int index = aDataDirPath.ReverseFind ('\\');
77 aDataDirPath.Delete (index + 1, aDataDirPath.GetLength() - index - 1);*/
79 CString SHAREPATHValue;
80 SHAREPATHValue.GetEnvironmentVariable (L"CSF_OCCTDataPath");
81 CString aDataDirPath = (SHAREPATHValue + "\\occ");
83 std::filebuf aFileBuf;
84 std::istream aStream (&aFileBuf);
85 CString aPathCrankArm = aDataDirPath + "\\CrankArm.rle";
86 if (aFileBuf.open (aPathCrankArm, std::ios::in))
88 BRepTools::Read (CrankArm, aStream, B);
92 CString aPathCylinderHead = aDataDirPath + "\\CylinderHead.rle";
93 if (aFileBuf.open (aPathCylinderHead, std::ios::in))
95 BRepTools::Read (CylinderHead, aStream, B);
99 CString aPathPropeller = aDataDirPath + "\\Propeller.rle";
100 if (aFileBuf.open (aPathPropeller, std::ios::in))
102 BRepTools::Read (Propeller, aStream, B);
106 CString aPathPiston = aDataDirPath + "\\Piston.rle";
107 if (aFileBuf.open (aPathPiston, std::ios::in))
109 BRepTools::Read (Piston, aStream, B);
113 CString aPathEngineBlock = aDataDirPath + "\\EngineBlock.rle";
114 if (aFileBuf.open (aPathEngineBlock, std::ios::in))
116 BRepTools::Read (EngineBlock, aStream, B);
120 if (CrankArm.IsNull() || CylinderHead.IsNull() ||
121 Propeller.IsNull() || Piston.IsNull() || EngineBlock.IsNull())
123 int rep = MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Shape(s) not found.\nCheck the Data directory path!", L"Error", MB_OK | MB_ICONERROR);
127 myAISContext->SetDeviationCoefficient(myDeviation);
129 myAisCylinderHead = new AIS_Shape (CylinderHead);
130 myAISContext->SetColor (myAisCylinderHead, Quantity_NOC_WHITE, Standard_False);
131 myAISContext->SetMaterial (myAisCylinderHead, Graphic3d_NOM_PLASTIC, Standard_False);
132 myAisEngineBlock = new AIS_Shape (EngineBlock);
133 myAISContext->SetColor(myAisEngineBlock, Quantity_NOC_WHITE, Standard_False);
134 myAISContext->SetMaterial(myAisEngineBlock,Graphic3d_NOM_PLASTIC, Standard_False);
136 myAISContext->Display(myAisCylinderHead ,1,-1,Standard_False);
137 myAISContext->Display(myAisEngineBlock ,1,-1,Standard_False);
139 myAisCrankArm = new AIS_Shape (CrankArm);
140 myAISContext->SetColor (myAisCrankArm, Quantity_NOC_HOTPINK, Standard_False);
141 myAISContext->SetMaterial(myAisCrankArm, Graphic3d_NOM_PLASTIC, Standard_False);
142 myAisPiston = new AIS_Shape (Piston);
143 myAISContext->SetColor (myAisPiston , Quantity_NOC_WHITE, Standard_False);
144 myAISContext->SetMaterial(myAisPiston , Graphic3d_NOM_PLASTIC, Standard_False);
145 myAisPropeller = new AIS_Shape (Propeller);
146 myAISContext->SetColor (myAisPropeller, Quantity_NOC_RED, Standard_False);
147 myAISContext->SetMaterial(myAisPropeller, Graphic3d_NOM_PLASTIC, Standard_False);
149 myAISContext->Display(myAisCrankArm, 1,-1,Standard_False);
150 myAISContext->Display(myAisPropeller, 1,-1,Standard_False);
151 myAISContext->Display(myAisPiston, 1,-1,Standard_True);
160 m_bIsGridLoaded = FALSE;
163 CAnimationDoc::~CAnimationDoc()
167 /////////////////////////////////////////////////////////////////////////////
168 // CAnimationDoc diagnostics
171 void CAnimationDoc::AssertValid() const
173 CDocument::AssertValid();
176 void CAnimationDoc::Dump(CDumpContext& dc) const
182 /////////////////////////////////////////////////////////////////////////////
183 // CAnimationDoc commands
184 //-----------------------------------------------------------------------------------------
186 //-----------------------------------------------------------------------------------------
187 void CAnimationDoc::DragEvent(const Standard_Integer x ,
188 const Standard_Integer y ,
189 const Standard_Integer TheState ,
190 const Handle(V3d_View)& aView )
193 // TheState == -1 button down
194 // TheState == 0 move
195 // TheState == 1 button up
197 static Standard_Integer theButtonDownX=0;
198 static Standard_Integer theButtonDownY=0;
207 myAISContext->Select (theButtonDownX, theButtonDownY, x, y, aView, Standard_True);
210 //-----------------------------------------------------------------------------------------
212 //-----------------------------------------------------------------------------------------
213 void CAnimationDoc::InputEvent(const Standard_Integer /*x*/,
214 const Standard_Integer /*y*/,
215 const Handle(V3d_View)& /*aView*/ )
217 myAISContext->Select (Standard_True);
220 //-----------------------------------------------------------------------------------------
222 //-----------------------------------------------------------------------------------------
223 void CAnimationDoc::MoveEvent(const Standard_Integer x ,
224 const Standard_Integer y ,
225 const Handle(V3d_View)& aView )
227 myAISContext->MoveTo (x, y, aView, Standard_True);
230 //-----------------------------------------------------------------------------------------
232 //-----------------------------------------------------------------------------------------
233 void CAnimationDoc::ShiftMoveEvent(const Standard_Integer x ,
234 const Standard_Integer y ,
235 const Handle(V3d_View)& aView )
237 myAISContext->MoveTo (x, y, aView, Standard_True);
240 //-----------------------------------------------------------------------------------------
242 //-----------------------------------------------------------------------------------------
243 void CAnimationDoc::ShiftDragEvent(const Standard_Integer x ,
244 const Standard_Integer y ,
245 const Standard_Integer TheState ,
246 const Handle(V3d_View)& aView )
248 static Standard_Integer theButtonDownX=0;
249 static Standard_Integer theButtonDownY=0;
258 myAISContext->ShiftSelect (theButtonDownX, theButtonDownY, x, y, aView, Standard_True);
262 //-----------------------------------------------------------------------------------------
264 //-----------------------------------------------------------------------------------------
265 void CAnimationDoc::ShiftInputEvent(const Standard_Integer /*x*/,
266 const Standard_Integer /*y*/,
267 const Handle(V3d_View)& /*aView*/)
269 myAISContext->ShiftSelect (Standard_True);
272 //-----------------------------------------------------------------------------------------
274 //-----------------------------------------------------------------------------------------
275 void CAnimationDoc::Popup(const Standard_Integer /*x*/,
276 const Standard_Integer /*y*/ ,
277 const Handle(V3d_View)& /*aView*/)
281 void CAnimationDoc::OnMyTimer()
283 // TODO: Add your message handler code here and/or call default
285 Standard_Real angleA;
286 Standard_Real angleB;
288 gp_Ax1 Ax1(gp_Pnt(0,0,0),gp_Vec(0,0,1));
292 angleA = thread*myAngle*M_PI/180;
294 angleB = atan(X / Sqrt(-X * X + 1));
295 Standard_Real decal(25*0.6);
298 //Build a transformation on the display
299 gp_Trsf aPropellerTrsf;
300 aPropellerTrsf.SetRotation(Ax1,angleA);
301 myAISContext->SetLocation(myAisPropeller,aPropellerTrsf);
303 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));
304 gp_Trsf aCrankArmTrsf;
305 aCrankArmTrsf.SetTransformation( base.Rotated(gp_Ax1(gp_Pnt(3*decal,0,0),gp_Dir(0,0,1)),angleB));
306 myAISContext->SetLocation(myAisCrankArm,aCrankArmTrsf);
309 aPistonTrsf.SetTranslation(gp_Vec(-3*decal*(1-Cos(angleA))-8*decal*(1-Cos(angleB)),0,0));
310 myAISContext->SetLocation(myAisPiston,aPistonTrsf);
312 myAISContext->UpdateCurrentViewer();
315 void CAnimationDoc::OnShading()
318 POSITION position = GetFirstViewPosition();
319 CView* pCurrentView = (CView*)GetNextView(position);
320 ((CAnimationView3D *)pCurrentView) ->OnStop();
322 CShadingDialog aDial(NULL);
324 aDial.myvalue=int((myDeviation-0.00003)/0.00003);
326 if (aDial.DoModal()==IDOK) {
327 Standard_Real dev(aDial.myvalue);
328 myDeviation = 0.00003+0.00003*dev;
330 myAISContext->SetDeviationCoefficient(myDeviation);
331 TopoDS_Shape Propeller=myAisPropeller->Shape();
332 BRepTools::Clean(Propeller);
334 myAisPropeller->Set(Propeller);
335 myAISContext->Deactivate(myAisPropeller);
336 myAISContext->Redisplay (myAisPropeller, Standard_True);
338 ((CAnimationView3D *)pCurrentView)->OnRestart();
342 void CAnimationDoc::OnThread()
344 POSITION position = GetFirstViewPosition();
345 CView* pCurrentView = (CView*)GetNextView(position);
346 ((CAnimationView3D *)pCurrentView) ->OnStop();
347 CThreadDialog aThreadDial(NULL);
348 if (aThreadDial.DoModal()==IDOK) {
349 thread = aThreadDial.m_Angle;
352 ((CAnimationView3D *)pCurrentView)->OnRestart();
356 void CAnimationDoc::OnFileLoadgrid()
358 // TODO: Add your command handler code here
360 CFileDialog dlg(TRUE,
363 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
364 L"Points Files (*.pnt;*.pnts)|*.pnt; *.pnts|All Files (*.*)|*.*||",
368 initdir.GetEnvironmentVariable (L"CSF_OCCTDataPath");
369 initdir += L"\\occ\\";
371 dlg.m_ofn.lpstrInitialDir = initdir;
374 if (dlg.DoModal() == IDOK)
376 CString C = dlg.GetPathName();
377 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
379 Handle(Geom_BSplineSurface) mySurface;
381 if(grid2surf(C,mySurface ))
383 //To set the minimum value of the surface to Z=0
384 Standard_Real Tolerance = 0.0;
387 GeomAdaptor_Surface GAS(mySurface);
390 BndLib_AddSurface::Add(GAS,Tolerance,B);
392 Standard_Real Xmin,Xmax,Ymin,Ymax,Zmin,Zmax;
393 B.Get(Xmin,Ymin,Zmin,Xmax,Ymax,Zmax);
394 TopoDS_Solid Box = BRepPrimAPI_MakeBox(gp_Pnt(Xmin,Ymin,0),Xmax-Xmin,Ymax-Ymin,50).Solid();
396 gp_Vec V(gp_Pnt(0,0,Zmin),gp_Pnt(0,0,0));
400 mySurface->Transform(T);
401 BRepBuilderAPI_MakeFace aMkFace(mySurface, Precision::Confusion());
402 myFace = aMkFace.Face();
404 // Remove all other shapes
405 myAISContext->RemoveAll (Standard_False);
407 Handle(AIS_Shape) myAISSurface = new AIS_Shape(myFace);
409 myAISContext->Display(myAISSurface, Standard_False);
410 myAISContext->Deactivate(myAISSurface,Standard_False);
411 myAISContext->SetColor (myAISSurface,Quantity_NOC_WHITE,Standard_False);
412 myAISContext->SetMaterial (myAISSurface,Graphic3d_NOM_STONE,Standard_False);
413 myAISContext->SetDisplayMode (myAISSurface,1,Standard_False);
414 myAISContext->SetDeviationCoefficient (0.001);
415 CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;
416 CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
417 CAnimationView3D *pView = (CAnimationView3D *) pChild->GetActiveView();
421 BRepBndLib::Add(myFace, Boite);
422 Boite.Get(m_Xmin, m_Ymin, m_Zmin, m_Xmax, m_Ymax, m_Zmax);
424 m_bIsGridLoaded = TRUE;
426 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
432 void CAnimationDoc::OnUpdateWalkWalkthru(CCmdUI* pCmdUI)
434 // TODO: Add your command update UI handler code here
436 pCmdUI->SetCheck (aValue.GetEnvironmentVariable (L"CSF_WALKTHROUGH") ? 1 : 0);