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

Graph program

Development Platform:

Visual C++

  1. #include "stdafx.h"
  2. #include "Box.h"
  3. #include "..//resource.h"
  4. #include <math.h>
  5. int     box_name_index = 1;
  6. BoxCommand::BoxCommand(IApplicationInterface* appI):
  7. m_app(appI)
  8. ,m_base_point_panel(NULL)
  9. ,m_x_size_panel(NULL)
  10. ,m_y_size_panel(NULL)
  11. , m_z_size_panel(NULL)
  12. , m_step(0)
  13. , size1(0.0)
  14. , size2(0.0)
  15. , size3(0.0)
  16. {
  17. ASSERT(m_app);
  18. m_matrix = new sgCMatrix;
  19. }
  20. BoxCommand::~BoxCommand()
  21. {
  22. if (m_matrix)
  23. delete m_matrix;
  24. m_app->GetCommandPanel()->RemoveAllDialogs();
  25. m_app->GetViewPort()->InvalidateViewPort();
  26. }
  27. bool    BoxCommand::PreTranslateMessage(MSG* pMsg)
  28. {
  29. /*if (pMsg->message==WM_KEYUP||
  30. pMsg->message==WM_CHAR)
  31. return false;*/
  32. if (pMsg->message==WM_KEYUP||pMsg->message==WM_KEYDOWN || 
  33. pMsg->message==WM_CHAR)
  34. {
  35. if (pMsg->wParam==VK_RETURN)
  36. {
  37. OnEnter();
  38. return true;
  39. }
  40. if (pMsg->wParam==VK_ESCAPE)
  41. {
  42. m_app->StopCommander();
  43. return true;
  44. }
  45. switch(m_step) {
  46. case 0:
  47. if (m_base_point_panel)
  48. m_base_point_panel->GetWindow()->SendMessage(pMsg->message,
  49. pMsg->wParam,
  50. pMsg->lParam);
  51. break;
  52. case 1:
  53. if (m_x_size_panel)
  54. m_x_size_panel->GetWindow()->SendMessage(pMsg->message,
  55. pMsg->wParam,
  56. pMsg->lParam);
  57. break;
  58. case 2:
  59. if (m_y_size_panel)
  60. m_y_size_panel->GetWindow()->SendMessage(pMsg->message,
  61. pMsg->wParam,
  62. pMsg->lParam);
  63. break;
  64. case 3:
  65. if (m_z_size_panel)
  66. m_z_size_panel->GetWindow()->SendMessage(pMsg->message,
  67. pMsg->wParam,
  68. pMsg->lParam);
  69. break;
  70. default:
  71. return false;
  72. }
  73. if (pMsg->message==WM_KEYDOWN)
  74. return false;
  75. else 
  76. return true;
  77. }
  78. else
  79. {
  80. if (pMsg->hwnd == m_app->GetViewPort()->GetWindow()->m_hWnd)
  81. {
  82. switch(pMsg->message) 
  83. {
  84. case WM_MOUSEMOVE:
  85. MouseMove(pMsg->wParam,GET_X_LPARAM(pMsg->lParam),GET_Y_LPARAM(pMsg->lParam));
  86. return true;
  87. case WM_LBUTTONDOWN:
  88. LeftClick(pMsg->wParam,GET_X_LPARAM(pMsg->lParam),GET_Y_LPARAM(pMsg->lParam));
  89. return true;
  90. default:
  91. return false;
  92. }
  93. }
  94. }
  95. return false;
  96. }
  97. void   BoxCommand::SendCommanderMessage(ICommander::COMMANDER_MESSAGE mes, void* params)
  98. {
  99. if (mes==ICommander::CM_SWITCH_ROLLUP_DIALOG)
  100. {
  101. ASSERT(params!=NULL);
  102. int   newActiveDlg = *(reinterpret_cast<int*>(params));
  103. ASSERT(newActiveDlg<=3);
  104. m_step = (unsigned int)newActiveDlg;
  105. for (unsigned int i=m_step+1;i<=3;i++)
  106. m_app->GetCommandPanel()->EnableRadio(i,false);
  107. SWITCH_RESOURCE
  108. switch(m_step) 
  109. {
  110. case 0:
  111. m_message.LoadString(IDS_BOX_ENTER_BASE_PNT);
  112. break;
  113. case 1:
  114. m_message.LoadString(IDS_BOX_X_SIZE);
  115. break;
  116. case 2:
  117. m_message.LoadString(IDS_BOX_Y_SIZE);
  118. break;
  119. case 3:
  120. m_message.LoadString(IDS_BOX_Z_SIZE);
  121. break;
  122. default:
  123. ASSERT(0);
  124. break;
  125. }
  126. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  127. }
  128. }
  129. void  BoxCommand::Start()
  130. {
  131. NewScenar();
  132. SWITCH_RESOURCE
  133. m_message.LoadString(IDS_TOOLTIP_ZERO);
  134. m_app->StartCommander(m_message);
  135. }
  136. void  BoxCommand::MouseMove(unsigned int nFlags,int pX,int pY)
  137. {
  138. SWITCH_RESOURCE
  139. switch(m_step) 
  140. {
  141. case 0:
  142. {
  143. ASSERT(m_base_point_panel);
  144. IViewPort::GET_SNAP_IN in_arg;
  145. in_arg.scrX = pX;
  146. in_arg.scrY = pY;
  147. in_arg.snapType = SNAP_SYSTEM;
  148. in_arg.XFix = m_base_point_panel->IsXFixed();
  149. in_arg.YFix = m_base_point_panel->IsYFixed();
  150. in_arg.ZFix = m_base_point_panel->IsZFixed();
  151. double tmpFl[3];
  152. m_base_point_panel->GetPoint(tmpFl[0],tmpFl[1],tmpFl[2]);
  153. in_arg.FixPoint.x = tmpFl[0];
  154. in_arg.FixPoint.y = tmpFl[1];
  155. in_arg.FixPoint.z = tmpFl[2];
  156. IViewPort::GET_SNAP_OUT out_arg;
  157. m_app->GetViewPort()->GetWorldPointAfterSnap(in_arg,out_arg);
  158. m_cur_pnt = out_arg.result_point;
  159. m_base_point_panel->SetPoint(m_cur_pnt.x,m_cur_pnt.y,m_cur_pnt.z);
  160. }
  161. break;
  162. case 1:
  163. {
  164. ASSERT(m_x_size_panel);
  165. SG_VECTOR dir; dir.x = 1.0; dir.y = 0.0; dir.z = 0.0;
  166. if (!m_app->GetViewPort()->ProjectScreenPointOnLine(pX,pY,m_first_pnt,dir,m_projection))
  167. {
  168. size1 = 0.0;
  169. m_x_size_panel->SetNumber(size1);
  170. break;
  171. }
  172. char sig = (((m_projection.x-m_first_pnt.x)*dir.x+
  173. (m_projection.y-m_first_pnt.y)*dir.y+
  174. (m_projection.z-m_first_pnt.z)*dir.z)>0)?1:-1;
  175. size1 = sgSpaceMath::PointsDistance(m_first_pnt,m_projection)*sig;
  176. size1 = m_app->ApplyPrecision(size1);
  177. m_projection.x = m_first_pnt.x+dir.x*size1;
  178. m_projection.y = m_first_pnt.y+dir.y*size1;
  179. m_projection.z = m_first_pnt.z+dir.z*size1;
  180. m_x_size_panel->SetNumber(size1);
  181. }
  182. break;
  183. case 2:
  184. {
  185. ASSERT(m_y_size_panel);
  186. SG_VECTOR dir; dir.y = 1.0; dir.x = 0.0; dir.z = 0.0;
  187. if (!m_app->GetViewPort()->ProjectScreenPointOnLine(pX,pY,m_first_pnt,dir,m_projection))
  188. {
  189. size2 = 0.0;
  190. m_y_size_panel->SetNumber(size2);
  191. break;
  192. }
  193. char sig = (((m_projection.x-m_first_pnt.x)*dir.x+
  194. (m_projection.y-m_first_pnt.y)*dir.y+
  195. (m_projection.z-m_first_pnt.z)*dir.z)>0)?1:-1;
  196. size2 = sgSpaceMath::PointsDistance(m_first_pnt,m_projection)*sig;
  197. size2 = m_app->ApplyPrecision(size2);
  198. m_projection.x = m_first_pnt.x+dir.x*size2;
  199. m_projection.y = m_first_pnt.y+dir.y*size2;
  200. m_projection.z = m_first_pnt.z+dir.z*size2;
  201. m_y_size_panel->SetNumber(size2);
  202. }
  203. break;
  204. case 3:
  205. {
  206. ASSERT(m_z_size_panel);
  207. SG_VECTOR dir; dir.z = 1.0; dir.y = 0.0; dir.x = 0.0;
  208. if (!m_app->GetViewPort()->ProjectScreenPointOnLine(pX,pY,m_first_pnt,dir,m_projection))
  209. {
  210. size3 = 0.0;
  211. m_z_size_panel->SetNumber(size3);
  212. break;
  213. }
  214. char sig = (((m_projection.x-m_first_pnt.x)*dir.x+
  215. (m_projection.y-m_first_pnt.y)*dir.y+
  216. (m_projection.z-m_first_pnt.z)*dir.z)>0)?1:-1;
  217. size3 = sgSpaceMath::PointsDistance(m_first_pnt,m_projection)*sig;
  218. size3 = m_app->ApplyPrecision(size3);
  219. m_projection.x = m_first_pnt.x+dir.x*size3;
  220. m_projection.y = m_first_pnt.y+dir.y*size3;
  221. m_projection.z = m_first_pnt.z+dir.z*size3;
  222. m_z_size_panel->SetNumber(size3);
  223. }
  224. break;
  225. default:
  226. ASSERT(0);
  227. }
  228. m_app->GetViewPort()->InvalidateViewPort();
  229. }
  230. void  BoxCommand::LeftClick(unsigned int nFlags,int pX,int pY)
  231. {
  232. SWITCH_RESOURCE
  233. switch(m_step) 
  234. {
  235. case 0:
  236. m_first_pnt=m_cur_pnt;
  237. m_matrix->Identity();
  238. m_matrix->Translate(m_first_pnt);
  239. m_step++;
  240. m_app->GetCommandPanel()->SetActiveRadio(m_step);
  241. m_message.LoadString(IDS_BOX_X_SIZE);
  242. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,
  243. m_message);
  244. break;
  245. case 1:
  246. if (fabs(size1)<0.0001)
  247. {
  248. m_message.LoadString(IDS_ERROR_ZERO_SIZE);
  249. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  250. m_message);
  251. return;
  252. }
  253. m_step++;
  254. m_app->GetCommandPanel()->SetActiveRadio(m_step);
  255. m_message.LoadString(IDS_BOX_Y_SIZE);
  256. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,
  257. m_message);
  258. break;
  259. case 2:
  260. if (fabs(size2)<0.0001)
  261. {
  262. m_message.LoadString(IDS_ERROR_ZERO_SIZE);
  263. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  264. m_message);
  265. return;
  266. }
  267. m_step++;
  268. m_app->GetCommandPanel()->SetActiveRadio(m_step);
  269. m_message.LoadString(IDS_BOX_Z_SIZE);
  270. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,
  271. m_message);
  272. break;
  273. case 3:
  274. {
  275. if (fabs(size3)<0.0001)
  276. {
  277. m_message.LoadString(IDS_ERROR_ZERO_SIZE);
  278. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  279. m_message);
  280. return;
  281. }
  282. SG_VECTOR vvv;memset(&vvv,0,sizeof(SG_VECTOR));
  283. if (size1<0) { vvv.x=size1; size1=-size1;}
  284. if (size2<0) { vvv.y=size2; size2=-size2;}
  285. if (size3<0) { vvv.z=size3; size3=-size3;}
  286. sgCBox* bx = sgCreateBox(size1,size2,size3);
  287. if (!bx)
  288. return;
  289. sgCMatrix* mtrx = bx->InitTempMatrix();
  290. mtrx->Translate(m_first_pnt);
  291. mtrx->Translate(vvv);
  292. bx->ApplyTempMatrix();
  293. bx->DestroyTempMatrix();
  294. CString nm;
  295. nm.LoadString(IDS_TOOLTIP_ZERO);
  296. CString nmInd;
  297. nmInd.Format("%i",box_name_index);
  298. nm+=nmInd;
  299. bx->SetName(nm);
  300. sgGetScene()->StartUndoGroup();
  301. sgGetScene()->AttachObject(bx);
  302. m_app->ApplyAttributes(bx);
  303. sgGetScene()->EndUndoGroup();
  304. box_name_index++;
  305. m_app->GetViewPort()->InvalidateViewPort();
  306. NewScenar();
  307. }
  308. break;
  309. default:
  310. ASSERT(0);
  311. break;
  312. }
  313. }
  314. void  BoxCommand::Draw()
  315. {
  316. switch(m_step) 
  317. {
  318. case 0:
  319. {
  320. float pC[3];
  321. m_app->GetViewPort()->GetPainter()->GetUserColorPoints(pC[0],pC[1],pC[2]);
  322. m_app->GetViewPort()->GetPainter()->SetCurColor(pC[0],pC[1],pC[2]);
  323. m_app->GetViewPort()->GetPainter()->DrawPoint(m_cur_pnt);
  324. }
  325. break;
  326. case 1:
  327. {
  328. float pC[3];
  329. m_app->GetViewPort()->GetPainter()->GetUserColorPoints(pC[0],pC[1],pC[2]);
  330. m_app->GetViewPort()->GetPainter()->SetCurColor(pC[0],pC[1],pC[2]);
  331. m_app->GetViewPort()->GetPainter()->DrawPoint(m_first_pnt);
  332. m_app->GetViewPort()->GetPainter()->DrawPoint(m_projection);
  333. SG_LINE ln;
  334. ln.p1 = m_first_pnt;
  335. ln.p2 = m_projection;
  336. m_app->GetViewPort()->GetPainter()->GetUserColorLines(pC[0],pC[1],pC[2]);
  337. m_app->GetViewPort()->GetPainter()->SetCurColor(pC[0],pC[1],pC[2]);
  338. m_app->GetViewPort()->GetPainter()->DrawLine(ln);
  339. }
  340. break;
  341. case 2:
  342. {
  343. float pC[3];
  344. m_app->GetViewPort()->GetPainter()->GetUserColorPoints(pC[0],pC[1],pC[2]);
  345. m_app->GetViewPort()->GetPainter()->SetCurColor(pC[0],pC[1],pC[2]);
  346. m_app->GetViewPort()->GetPainter()->DrawPoint(m_first_pnt);
  347. m_app->GetViewPort()->GetPainter()->DrawPoint(m_projection);
  348. m_app->GetViewPort()->GetPainter()->GetUserColorLines(pC[0],pC[1],pC[2]);
  349. m_app->GetViewPort()->GetPainter()->SetCurColor(pC[0],pC[1],pC[2]);
  350. m_app->GetViewPort()->GetPainter()->SetTransformMatrix(m_matrix);
  351. m_app->GetViewPort()->GetPainter()->DrawBox(size1,size2, 0.0);
  352. m_app->GetViewPort()->GetPainter()->SetTransformMatrix(NULL);
  353. }
  354. break;
  355. case 3:
  356. {
  357. float pC[3];
  358. m_app->GetViewPort()->GetPainter()->GetUserColorPoints(pC[0],pC[1],pC[2]);
  359. m_app->GetViewPort()->GetPainter()->SetCurColor(pC[0],pC[1],pC[2]);
  360. m_app->GetViewPort()->GetPainter()->DrawPoint(m_first_pnt);
  361. m_app->GetViewPort()->GetPainter()->DrawPoint(m_projection);
  362. m_app->GetViewPort()->GetPainter()->GetUserColorLines(pC[0],pC[1],pC[2]);
  363. m_app->GetViewPort()->GetPainter()->SetCurColor(pC[0],pC[1],pC[2]);
  364. m_app->GetViewPort()->GetPainter()->SetTransformMatrix(m_matrix);
  365. m_app->GetViewPort()->GetPainter()->DrawBox(size1,size2, size3);
  366. m_app->GetViewPort()->GetPainter()->SetTransformMatrix(NULL);
  367. }
  368. break;
  369. default:
  370. ASSERT(0);
  371. }
  372. }
  373. void  BoxCommand::OnEnter()
  374. {
  375. SWITCH_RESOURCE
  376. switch(m_step) 
  377. {
  378. case 0:
  379. {
  380. ASSERT(m_base_point_panel);
  381. m_base_point_panel->GetPoint(m_first_pnt.x,
  382. m_first_pnt.y,
  383. m_first_pnt.z);
  384. m_matrix->Identity();
  385. m_matrix->Translate(m_first_pnt);
  386. m_step++;
  387. m_app->GetCommandPanel()->SetActiveRadio(m_step);
  388. m_message.LoadString(IDS_BOX_X_SIZE);
  389. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,
  390. m_message);
  391. }
  392. break;
  393. case 1:
  394. {
  395. size1 = m_x_size_panel->GetNumber();
  396. if (fabs(size1)<0.0001)
  397. {
  398. m_message.LoadString(IDS_ERROR_ZERO_SIZE);
  399. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  400. m_message);
  401. return;
  402. }
  403. m_step++;
  404. m_app->GetCommandPanel()->SetActiveRadio(m_step);
  405. m_message.LoadString(IDS_BOX_Y_SIZE);
  406. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,
  407. m_message);
  408. }
  409. break;
  410. case 2:
  411. {
  412. size2 = m_y_size_panel->GetNumber();
  413. if (fabs(size2)<0.0001)
  414. {
  415. m_message.LoadString(IDS_ERROR_ZERO_SIZE);
  416. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  417. m_message);
  418. return;
  419. }
  420. m_step++;
  421. m_app->GetCommandPanel()->SetActiveRadio(m_step);
  422. m_message.LoadString(IDS_BOX_Z_SIZE);
  423. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,
  424. m_message);
  425. }
  426. break;
  427. case 3:
  428. {
  429. size3 = m_z_size_panel->GetNumber();
  430. if (fabs(size3)<0.0001)
  431. {
  432. m_message.LoadString(IDS_ERROR_ZERO_SIZE);
  433. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  434. m_message);
  435. return;
  436. }
  437. SG_VECTOR vvv;memset(&vvv,0,sizeof(SG_VECTOR));
  438. if (size1<0) { vvv.x=size1; size1=-size1;}
  439. if (size2<0) { vvv.y=size2; size2=-size2;}
  440. if (size3<0) { vvv.z=size3; size3=-size3;}
  441. sgCBox* bx = sgCreateBox(size1,size2,size3);
  442. if (!bx)
  443. return;
  444. sgCMatrix* mtrx = bx->InitTempMatrix();
  445. mtrx->Translate(m_first_pnt);
  446. mtrx->Translate(vvv);
  447. bx->ApplyTempMatrix();
  448. bx->DestroyTempMatrix();
  449. CString nm;
  450. nm.LoadString(IDS_TOOLTIP_ZERO);
  451. CString nmInd;
  452. nmInd.Format("%i",box_name_index);
  453. nm+=nmInd;
  454. bx->SetName(nm);
  455. sgGetScene()->StartUndoGroup();
  456. sgGetScene()->AttachObject(bx);
  457. m_app->ApplyAttributes(bx);
  458. sgGetScene()->EndUndoGroup();
  459. box_name_index++;
  460. m_app->GetViewPort()->InvalidateViewPort();
  461. NewScenar();
  462. }
  463. break;
  464. default:
  465. ASSERT(0);
  466. break;
  467. }
  468. }
  469. unsigned int  BoxCommand::GetItemsCount()
  470. {
  471. return 0;
  472. }
  473. void         BoxCommand::GetItem(unsigned int, CString&)
  474. {
  475. SWITCH_RESOURCE
  476. }
  477. void     BoxCommand::GetItemState(unsigned int, bool&, bool&)
  478. {
  479. }
  480. HBITMAP     BoxCommand::GetItemBitmap(unsigned int)
  481. {
  482. return NULL;
  483. }
  484. void         BoxCommand::Run(unsigned int)
  485. {
  486. }
  487. void  BoxCommand::NewScenar()
  488. {
  489. SWITCH_RESOURCE
  490. m_app->GetCommandPanel()->RemoveAllDialogs();
  491. CString lab;
  492. lab.LoadString(IDS_BASE_POINT);
  493. m_base_point_panel = reinterpret_cast<IGetPointPanel*>(m_app->
  494. GetCommandPanel()->
  495. AddDialog(IBaseInterfaceOfGetDialogs::GET_POINT_DLG,lab,true));
  496. lab.LoadString(IDS_X_SIZE);
  497. m_x_size_panel = reinterpret_cast<IGetNumberPanel*>(m_app->
  498. GetCommandPanel()->
  499. AddDialog(IBaseInterfaceOfGetDialogs::GET_NUMBER_DLG,lab,true));
  500. lab.LoadString(IDS_Y_SIZE);
  501. m_y_size_panel = reinterpret_cast<IGetNumberPanel*>(m_app->
  502. GetCommandPanel()->
  503. AddDialog(IBaseInterfaceOfGetDialogs::GET_NUMBER_DLG,lab,true));
  504. lab.LoadString(IDS_Z_SIZE);
  505. m_z_size_panel = reinterpret_cast<IGetNumberPanel*>(m_app->
  506. GetCommandPanel()->
  507. AddDialog(IBaseInterfaceOfGetDialogs::GET_NUMBER_DLG,lab,true));
  508. m_app->GetCommandPanel()->SetActiveRadio(0);
  509. lab.LoadString(IDS_BOX_ENTER_BASE_PNT);
  510. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,lab);
  511. m_step=0;
  512. }