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

Graph program

Development Platform:

Visual C++

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