SolidgraphView.cpp
Upload User: kairuinn
Upload Date: 2009-02-07
Package Size: 2922k
Code Size: 14k
Category:

Graph program

Development Platform:

Visual C++

  1. // SolidgraphView.cpp : implementation of the CSolidgraphView class
  2. //
  3. #include "stdafx.h"
  4. #include "Solidgraph.h"
  5. #include "SolidgraphDoc.h"
  6. #include "SolidgraphView.h"
  7. #include "ChildFrm.h"
  8. #include "Drawer.h" 
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #endif
  12. CSolidgraphView*       global_opengl_view = NULL;
  13. static GLfloat g_fMouseZMult   = 0.01f; // Mouse 'Z'-Axis multiplier
  14. // CSolidgraphView
  15. IMPLEMENT_DYNCREATE(CSolidgraphView, COpenGLView)
  16. BEGIN_MESSAGE_MAP(CSolidgraphView, COpenGLView)
  17. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  18. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  19. //ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  20. ON_WM_LBUTTONDOWN()
  21. ON_WM_LBUTTONUP()
  22. ON_WM_MOUSEMOVE()
  23. ON_WM_SETCURSOR()
  24. ON_WM_RBUTTONDOWN()
  25. ON_COMMAND(ID_FILE_PRINT_PREVIEW, OnFilePrintPreview)
  26. END_MESSAGE_MAP()
  27. // CSolidgraphView construction/destruction
  28. CSolidgraphView::CSolidgraphView()
  29. {
  30. global_opengl_view = this;
  31. m_isPrinting = false;
  32. }
  33. CSolidgraphView::~CSolidgraphView()
  34. {
  35. global_opengl_view = NULL;
  36. }
  37. BOOL CSolidgraphView::PreCreateWindow(CREATESTRUCT& cs)
  38. {
  39. cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN|CS_OWNDC;
  40. return COpenGLView::PreCreateWindow(cs);
  41. }
  42. void CSolidgraphView::OnInitialUpdate()
  43. {
  44. COpenGLView::OnInitialUpdate();
  45. static_cast<CChildFrame*>(GetParentFrame())->SetView(this);
  46. m_Camera.ReInit();
  47. if (sgGetScene()->GetObjectsList()->GetCount()>0)
  48. {
  49. SG_POINT a1,a2;
  50. sgGetScene()->GetGabarits(a1,a2);
  51. m_Camera.FitBounds(a1.x,a1.y,a1.z,a2.x,a2.y,a2.z);
  52. }
  53. Invalidate();
  54. }
  55. #include "Tools//TranslateCommand.h"
  56. #include "Tools//RotateCommand.h"
  57. void  CSolidgraphView::DrawScene(GLenum mode, bool selSubObj)
  58. {
  59. sgCObject*  curObj = sgGetScene()->GetObjectsList()->GetHead();
  60. while (curObj) 
  61. {
  62. if (/*sgGetScene()->GetLayerVisible(curObj->GetAttribute(SG_OA_LAYER))*/true)
  63. {
  64. Drawer::DrawObject(mode,curObj,selSubObj);
  65. if ((curObj->GetAttribute(SG_OA_DRAW_STATE) & SG_DS_GABARITE) &&
  66. curObj!=Drawer::CurrentEditableObject)
  67. {
  68. SG_POINT a1,a2;
  69. curObj->GetGabarits(a1,a2);
  70. Drawer::DrawGabariteBox(a1,a2,Drawer::HotObjectColor);
  71. }
  72. /*if (curObj->GetType()==SG_OT_3D)
  73. {
  74. sgC3DObject* ooo = reinterpret_cast<sgC3DObject*>(curObj);
  75. for (unsigned int i=0;i<ooo->GetBRep()->GetPiecesCount();i++)
  76. {
  77. SG_POINT s1,s2;
  78. ooo->GetBRep()->GetPiece(i)->GetLocalGabarits(s1,s2);
  79. Drawer::DrawGabariteBox(s1,s2,Drawer::HotObjectColor);
  80. }
  81. }*/
  82. }
  83. curObj = sgGetScene()->GetObjectsList()->GetNext(curObj);
  84. }
  85. /*SG_POINT s1,s2;
  86. sgGetScene()->GetGabarits(s1,s2);
  87. Drawer::DrawGabariteBox(s1,s2,Drawer::HotObjectColor);*/
  88. }
  89. void CSolidgraphView::DrawFromCommander()
  90. {
  91. CChildFrame* pFrame = static_cast<CChildFrame*>(GetParentFrame());
  92. if (pFrame->m_commander)
  93. pFrame->m_commander->Draw();
  94. }
  95. void CSolidgraphView::OnDraw(CDC* pDC)
  96. {
  97. if (pDC->IsPrinting()) 
  98. {
  99. CRect rcDIB;
  100. GetClientRect(&rcDIB);
  101. rcDIB.right = rcDIB.Width();
  102. rcDIB.bottom = rcDIB.Height();
  103. // get size of printer page (in pixels)
  104. int cxPage = pDC->GetDeviceCaps(HORZRES);
  105. int cyPage = pDC->GetDeviceCaps(VERTRES);
  106. // get printer pixels per inch
  107. int cxInch = pDC->GetDeviceCaps(LOGPIXELSX);
  108. int cyInch = pDC->GetDeviceCaps(LOGPIXELSY);
  109. CRect rcDest;
  110. rcDest.top = rcDest.left = 0;
  111. rcDest.bottom = (int)(((double)rcDIB.Height() * cxPage * cyInch)
  112. / ((double)rcDIB.Width() * cxInch));
  113. rcDest.right = cxPage;
  114. m_CapturedImage.OnDraw(pDC->m_hDC, &rcDest, &rcDIB);
  115. }
  116. }
  117. BOOL CSolidgraphView::OnPreparePrinting(CPrintInfo* pInfo)
  118. {
  119. if(!pInfo->m_bPreview)
  120. {
  121. CRect rcDIB;
  122. GetClientRect(&rcDIB);
  123. CDC* sss = CDC::FromHandle(m_hDC);
  124. OnDraw(sss);
  125. m_CapturedImage.Capture(sss, rcDIB);
  126. }
  127. return DoPreparePrinting(pInfo);
  128. }
  129. void CSolidgraphView::OnFilePrintPreview()
  130. {
  131. AfxGetMainWnd()->SendMessage(WM_CANCELMODE, 0, 0);
  132. CRect rcDIB;
  133. GetClientRect(&rcDIB);
  134. CDC* sss = CDC::FromHandle(m_hDC);
  135. OnDraw(sss);
  136. m_CapturedImage.Capture(sss, rcDIB);
  137. CView::OnFilePrintPreview();
  138. }
  139. void CSolidgraphView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  140. {
  141. m_isPrinting = true;
  142. CChildFrame* pFrame = static_cast<CChildFrame*>(GetParentFrame());
  143. pFrame->FreeCommander();
  144. }
  145. void CSolidgraphView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  146. {
  147. m_CapturedImage.Release();
  148. m_isPrinting = false;
  149. }
  150. // CSolidgraphView diagnostics
  151. #ifdef _DEBUG
  152. void CSolidgraphView::AssertValid() const
  153. {
  154. COpenGLView::AssertValid();
  155. }
  156. void CSolidgraphView::Dump(CDumpContext& dc) const
  157. {
  158. COpenGLView::Dump(dc);
  159. }
  160. CSolidgraphDoc* CSolidgraphView::GetDocument() const // non-debug version is inline
  161. {
  162. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSolidgraphDoc)));
  163. return (CSolidgraphDoc*)m_pDocument;
  164. }
  165. #endif //_DEBUG
  166. void CSolidgraphView::OnLButtonDown(UINT nFlags, CPoint point)
  167. {
  168. CChildFrame* pFrame = static_cast<CChildFrame*>(GetParentFrame());
  169. // Save the mouse left button down screen position
  170. m_ScreenLeftButtonDownPoint = point;
  171. SetCapture();
  172. COpenGLView::OnLButtonDown(nFlags, point);
  173. }
  174. void CSolidgraphView::OnLButtonUp(UINT nFlags, CPoint point)
  175. {
  176. m_ScreenLeftButtonDownPoint.x = 0;
  177. m_ScreenLeftButtonDownPoint.y = 0;
  178. if (GetCapture()==this)
  179. ReleaseCapture();
  180. COpenGLView::OnLButtonUp(nFlags, point);
  181. }
  182. void CSolidgraphView::OnMouseMove(UINT nFlags, CPoint point)
  183. {
  184. CChildFrame* pFrame = static_cast<CChildFrame*>(GetParentFrame());
  185. SetFocus();
  186. if (!pFrame->m_commander)
  187. {
  188. switch (m_hand_action )
  189. {
  190. case HA_ROTATE:
  191. if (nFlags & MK_LBUTTON)
  192. {
  193. SG_VECTOR downPnt;
  194. SG_VECTOR curPnt;
  195. // Convert the mouse left button down position to world
  196. m_Camera.GetWorldCoord(m_ScreenLeftButtonDownPoint.x, 
  197. m_ScreenLeftButtonDownPoint.y, 
  198. 0.0, 
  199. downPnt);
  200. // Convert the mouse point into world coordinates
  201. m_Camera.GetWorldCoord(point.x, point.y, 0.0,curPnt);
  202. //VecSubf(curPnt, downPnt, curPnt);
  203. curPnt = sgSpaceMath::VectorsSub(curPnt, downPnt);
  204. CSize deltaPos;
  205. deltaPos = m_ScreenLeftButtonDownPoint - point;
  206. m_ScreenLeftButtonDownPoint = point;
  207. RotateCamera(deltaPos);
  208. Invalidate(FALSE);
  209. }
  210. break;
  211. case HA_MOVE:
  212. if (nFlags & MK_LBUTTON)
  213. {
  214. SG_VECTOR downPnt;
  215. SG_VECTOR curPnt;
  216. // Convert the mouse left button down position to world
  217. m_Camera.GetWorldCoord(m_ScreenLeftButtonDownPoint.x, 
  218. m_ScreenLeftButtonDownPoint.y, 
  219. 0.0, 
  220. downPnt);
  221. // Convert the mouse point into world coordinates
  222. m_Camera.GetWorldCoord(point.x, point.y, 0.0,curPnt);
  223. //VecSubf(curPnt, downPnt, curPnt);
  224. curPnt = sgSpaceMath::VectorsSub(curPnt, downPnt);
  225. m_ScreenLeftButtonDownPoint = point;
  226. TranslateCamera(curPnt);
  227. Invalidate(FALSE);
  228. }
  229. break;
  230. case HA_ZOOM:
  231. if (nFlags & MK_LBUTTON)
  232. {
  233. SG_VECTOR downPnt;
  234. SG_VECTOR curPnt;
  235. // Convert the mouse left button down position to world
  236. m_Camera.GetWorldCoord(m_ScreenLeftButtonDownPoint.x, 
  237. m_ScreenLeftButtonDownPoint.y, 
  238. 0.0, 
  239. downPnt);
  240. // Convert the mouse point into world coordinates
  241. m_Camera.GetWorldCoord(point.x, point.y, 0.0,curPnt);
  242. //VecSubf(curPnt, downPnt, curPnt);
  243. curPnt = sgSpaceMath::VectorsSub(curPnt, downPnt);
  244. CSize deltaPos;
  245. deltaPos = m_ScreenLeftButtonDownPoint - point;
  246. m_ScreenLeftButtonDownPoint = point;
  247. ZoomCamera(deltaPos);
  248. Invalidate(FALSE);
  249. }
  250. break;
  251. default:
  252. break;
  253. }
  254. }
  255. if (!pFrame->m_commander)
  256. /*pFrame->m_commander->MouseMove(nFlags,point.x,point.y);
  257. else*/
  258. if (!(nFlags & MK_LBUTTON))
  259. {
  260. int snapSz = pFrame->GetSnapSize();
  261. const sgCObject* oldHotObj = Drawer::CurrentHotObject;
  262. Drawer::CurrentHotObject = GetTopObject(GetHitsInRect(CRect(point.x-snapSz, point.y-snapSz,
  263. point.x+snapSz, point.y+snapSz),true));
  264. Drawer::TopParentOfHotObject = GetObjectTopParent(Drawer::CurrentHotObject);
  265.   if (Drawer::TopParentOfHotObject)
  266. Drawer::CurrentHotObject = Drawer::TopParentOfHotObject;
  267. /**************************************/
  268. if (Drawer::CurrentHotObject &&
  269. (Drawer::CurrentHotObject->GetType()==SG_OT_LINE ||
  270.  Drawer::CurrentHotObject->GetType()==SG_OT_CIRCLE ||
  271.  Drawer::CurrentHotObject->GetType()==SG_OT_SPLINE ||
  272.  Drawer::CurrentHotObject->GetType()==SG_OT_ARC ||
  273.  Drawer::CurrentHotObject->GetType()==SG_OT_CONTOUR)
  274. )
  275. {
  276. sgC2DObject* spl = reinterpret_cast<sgC2DObject*>(Drawer::CurrentHotObject);
  277. CString messs = "2DObject ";
  278. if (spl->IsClosed())
  279. messs += " Closed; ";
  280. else
  281. messs += " No closed; ";
  282. if (spl->IsLinear())
  283. {
  284. messs += " Linear;";
  285. }
  286. else
  287. {
  288. messs += " No linear - ";
  289. SG_VECTOR plN;
  290. double    plD;
  291. if (spl->IsPlane(&plN,&plD))
  292. {
  293. CString aaaa;
  294. aaaa.Format("Normal: X=%f,Y=%f,Z=%f D: %f",plN.x,plN.y,plN.z,plD);
  295. messs += " Plane; ";
  296. messs+=aaaa;
  297. }
  298. else
  299. messs += " No Plane; ";
  300. }
  301. if (spl->IsSelfIntersecting())
  302. messs += "Self intersecing; ";
  303. else
  304. messs += "No Self intersecing; ";
  305. pFrame->PutMessage(IApplicationInterface::MT_MESSAGE,
  306. messs);
  307. }
  308. else
  309. if (Drawer::CurrentHotObject && (Drawer::CurrentHotObject->GetType()==SG_OT_3D))
  310. {
  311. sgC3DObject* ddd = reinterpret_cast<sgC3DObject*>(Drawer::CurrentHotObject);
  312. CString messs;
  313. messs.Format("Volume = %f",ddd->GetVolume());
  314. messs+="  Square = ";
  315. CString aaaa;
  316. aaaa.Format("%f",ddd->GetSquare());
  317. messs+=aaaa;
  318. pFrame->PutMessage(IApplicationInterface::MT_MESSAGE,
  319. messs);
  320. }
  321. else
  322.                         pFrame->PutMessage(IApplicationInterface::MT_MESSAGE,
  323. "                        ");
  324. /**************************************/
  325. if (oldHotObj!=Drawer::CurrentHotObject)
  326. Invalidate();
  327. }
  328. COpenGLView::OnMouseMove(nFlags, point);
  329. }
  330. BOOL CSolidgraphView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  331. {
  332. CMainFrame* mFr = static_cast<CMainFrame*>(theApp.m_pMainWnd);
  333. if (!mFr)
  334. return COpenGLView::OnSetCursor(pWnd, nHitTest, message);
  335. HCURSOR   curs = mFr->GetCursorer()->GetCursor();
  336. if (!curs)
  337. return COpenGLView::OnSetCursor(pWnd, nHitTest, message);
  338. ::SetCursor(curs);
  339. return TRUE;
  340. }
  341. static WORD cm_icons[] = { IDB_PROJECTION_TOOLBAR_TC, 
  342. 16,16,
  343. ID_ALL_SCENE_VIEW,
  344. ID_PARALL_PROJ,
  345. ID_PERSPECT_PROJ,
  346. ID_FRONT_VIEW,
  347. ID_BACK_VIEW,
  348. ID_TOP_VIEW,
  349. ID_BOTTOM_VIEW,
  350. ID_LEFT_VIEW,
  351. ID_RIGHT_VIEW,
  352. ID_ISO_FRONT_VIEW,
  353. ID_ISO_BACK_VIEW,
  354. NULL
  355. };
  356. void CSolidgraphView::OnRButtonDown(UINT nFlags, CPoint point)
  357. {
  358. CChildFrame* pFrame = static_cast<CChildFrame*>(GetParentFrame());
  359. if (!pFrame->m_commander && !Drawer::CurrentHotObject)
  360. {
  361. CEGMenu menu;
  362. menu.CreatePopupMenu();
  363. int nItem=0;
  364. UINT  chs=0;
  365. chs = (sgGetScene()->GetObjectsList()->GetCount()!=0)?MF_ENABLED:MF_GRAYED;
  366. menu.InsertMenu(nItem++, MF_BYPOSITION|chs, ID_ALL_SCENE_VIEW, GetLeftHalfOfString(ID_ALL_SCENE_VIEW));
  367. menu.InsertMenu(nItem++, MF_BYPOSITION|MF_SEPARATOR);
  368. chs = (!m_Camera.m_bPerspective)?MF_CHECKED:0;
  369. menu.InsertMenu(nItem++, MF_BYPOSITION|chs , ID_PARALL_PROJ, GetLeftHalfOfString(ID_PARALL_PROJ));
  370. chs = (m_Camera.m_bPerspective)?MF_CHECKED:0;
  371. menu.InsertMenu(nItem++, MF_BYPOSITION|chs, ID_PERSPECT_PROJ, GetLeftHalfOfString(ID_PERSPECT_PROJ));
  372. menu.InsertMenu(nItem++, MF_BYPOSITION|MF_SEPARATOR);
  373. chs = (m_Camera.m_enumCameraPosition==CP_FRONT)?MF_CHECKED:0;
  374. menu.InsertMenu(nItem++, MF_BYPOSITION|chs, ID_FRONT_VIEW, GetLeftHalfOfString(ID_FRONT_VIEW));
  375. chs = (m_Camera.m_enumCameraPosition==CP_BACK)?MF_CHECKED:0;
  376. menu.InsertMenu(nItem++, MF_BYPOSITION|chs,  ID_BACK_VIEW, GetLeftHalfOfString(ID_BACK_VIEW));
  377. chs = (m_Camera.m_enumCameraPosition==CP_TOP)?MF_CHECKED:0;
  378. menu.InsertMenu(nItem++, MF_BYPOSITION|chs , ID_TOP_VIEW, GetLeftHalfOfString(ID_TOP_VIEW));
  379. chs = (m_Camera.m_enumCameraPosition==CP_BOTTOM)?MF_CHECKED:0;
  380. menu.InsertMenu(nItem++, MF_BYPOSITION|chs , ID_BOTTOM_VIEW, GetLeftHalfOfString(ID_BOTTOM_VIEW));
  381. chs = (m_Camera.m_enumCameraPosition==CP_LEFT)?MF_CHECKED:0;
  382. menu.InsertMenu(nItem++, MF_BYPOSITION|chs,  ID_LEFT_VIEW, GetLeftHalfOfString(ID_LEFT_VIEW));
  383. chs = (m_Camera.m_enumCameraPosition==CP_RIGHT)?MF_CHECKED:0;
  384. menu.InsertMenu(nItem++, MF_BYPOSITION|chs , ID_RIGHT_VIEW, GetLeftHalfOfString(ID_RIGHT_VIEW));
  385. menu.InsertMenu(nItem++, MF_BYPOSITION|MF_SEPARATOR);
  386. chs = (m_Camera.m_enumCameraPosition==CP_ISO_FRONT)?MF_CHECKED:0;
  387. menu.InsertMenu(nItem++, MF_BYPOSITION|chs,  ID_ISO_FRONT_VIEW, GetLeftHalfOfString(ID_ISO_FRONT_VIEW));
  388. chs = (m_Camera.m_enumCameraPosition==CP_ISO_BACK)?MF_CHECKED:0;
  389. menu.InsertMenu(nItem++, MF_BYPOSITION|chs , ID_ISO_BACK_VIEW, GetLeftHalfOfString(ID_ISO_BACK_VIEW));
  390. //menu.InsertMenu(nItem++, MF_BYPOSITION|MF_SEPARATOR);
  391. menu.LoadToolBar( cm_icons, RGB(0,0,0) ); 
  392. //menu.SetDefaultItem(ID_EDIT_PASTE);
  393. CRect clR;
  394. GetWindowRect(&clR);
  395. menu.TrackPopupMenu( TPM_LEFTALIGN | TPM_LEFTBUTTON | 
  396. TPM_RIGHTBUTTON|0x0000, point.x+clR.left,point.y+clR.top, this);
  397. }
  398. else
  399. {
  400. if (pFrame->m_commander)
  401. {
  402. CRect clR;
  403. GetWindowRect(&clR);
  404. pFrame->CommanderContextMenu(point.x+clR.left,point.y+clR.top);
  405. }
  406. else
  407. if (Drawer::CurrentHotObject)
  408. {
  409. CRect clR;
  410. GetWindowRect(&clR);
  411. pFrame->EditCommanderContextMenu(point.x+clR.left,point.y+clR.top);
  412. }
  413. }
  414. COpenGLView::OnRButtonDown(nFlags, point);
  415. }