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

Graph program

Development Platform:

Visual C++

  1. #include "stdafx.h"
  2. #include "Contour.h"
  3. #include "..//Dialogs//ContScenarDlg.h"
  4. #include "..//Dialogs//ContObjDlg.h"
  5. #include "..//resource.h"
  6. #include <math.h>
  7. int     contour_name_index = 1;
  8. Contour::Contour(IApplicationInterface* appI):
  9. m_app(appI)
  10. , m_start_object(NULL)
  11. , m_scenar(-1)
  12. , m_step(0)
  13. , m_lines(false)
  14. , m_scenar_panel(NULL)
  15. , m_get_object_panel(NULL)
  16. , m_select_obj_type_panel(NULL)
  17. , m_get_first_point(NULL)
  18. , m_get_second_point(NULL)
  19. , m_get_third_point(NULL)
  20. , m_isFirstPoint(true)
  21. , m_isSecondPoint(false)
  22. , m_isLastPointOnArc(false)
  23. , m_exist_arc_data(false)
  24. , m_can_close(false)
  25. {
  26. ASSERT(m_app);
  27. }
  28. Contour::~Contour()
  29. {
  30. size_t sz =m_objects.size();
  31. for (size_t i=0;i<sz;i++)
  32. sgCObject::DeleteObject(m_objects[i]);
  33. m_objects.clear();
  34. m_app->GetCommandPanel()->RemoveAllDialogs();
  35. if (m_scenar_panel)
  36. {
  37. m_scenar_panel->DestroyWindow();
  38. delete m_scenar_panel;
  39. m_scenar_panel = NULL;
  40. }
  41. if (m_select_obj_type_panel)
  42. {
  43. m_select_obj_type_panel->DestroyWindow();
  44. delete m_select_obj_type_panel;
  45. m_select_obj_type_panel = NULL;
  46. }
  47. m_app->GetViewPort()->InvalidateViewPort();
  48. }
  49. bool    Contour::PreTranslateMessage(MSG* pMsg)
  50. {
  51. /*if (pMsg->message==WM_KEYUP||
  52. pMsg->message==WM_CHAR)
  53. return false;*/
  54. if (pMsg->message==WM_KEYUP||pMsg->message==WM_KEYDOWN || 
  55. pMsg->message==WM_CHAR)
  56. {
  57. if (pMsg->wParam==VK_RETURN)
  58. {
  59. OnEnter();
  60. return true;
  61. }
  62. if (pMsg->wParam==VK_ESCAPE)
  63. {
  64. m_app->StopCommander();
  65. return true;
  66. }
  67. switch(m_step) 
  68. {
  69. case 0:
  70. if (m_get_first_point)
  71. m_get_first_point->GetWindow()->SendMessage(pMsg->message,
  72. pMsg->wParam,
  73. pMsg->lParam);
  74. break;
  75. case 1:
  76. if (m_get_second_point)
  77. m_get_second_point->GetWindow()->SendMessage(pMsg->message,
  78. pMsg->wParam,
  79. pMsg->lParam);
  80. break;
  81. case 2:
  82. if (m_get_third_point)
  83. m_get_third_point->GetWindow()->SendMessage(pMsg->message,
  84. pMsg->wParam,
  85. pMsg->lParam);
  86. break;
  87. }
  88. if (pMsg->message==WM_KEYDOWN)
  89. return false;
  90. else 
  91. return true;
  92. }
  93. else
  94. {
  95. if (pMsg->hwnd == m_app->GetViewPort()->GetWindow()->m_hWnd)
  96. {
  97. switch(pMsg->message) 
  98. {
  99. case WM_MOUSEMOVE:
  100. MouseMove(pMsg->wParam,GET_X_LPARAM(pMsg->lParam),GET_Y_LPARAM(pMsg->lParam));
  101. return true;
  102. case WM_LBUTTONDOWN:
  103. LeftClick(pMsg->wParam,GET_X_LPARAM(pMsg->lParam),GET_Y_LPARAM(pMsg->lParam));
  104. return true;
  105. default:
  106. return false;
  107. }
  108. }
  109. }
  110. return false;
  111. }
  112. static   bool isObjAddToList(sgCObject* o)
  113. {
  114. if (o->GetType()==SG_OT_LINE ||
  115. o->GetType()==SG_OT_ARC)
  116. return true;
  117. if (o->GetType()==SG_OT_CONTOUR)
  118. {
  119. sgCContour* cc = reinterpret_cast<sgCContour*>(o);
  120. if (cc->IsClosed())
  121. return false;
  122. else
  123. return true;
  124. }
  125. return false;
  126. }
  127. void  Contour::SwitchScenario(int newScen)
  128. {
  129. SWITCH_RESOURCE
  130. switch(newScen) {
  131. case 1:
  132. m_lines = false;
  133. if (m_scenar==0)
  134. break;
  135. m_scenar = 0;
  136. if (m_select_obj_type_panel!=NULL)
  137. {
  138. m_app->GetCommandPanel()->RemoveDialog(m_select_obj_type_panel);
  139. m_select_obj_type_panel->DestroyWindow();
  140. delete m_select_obj_type_panel;
  141. m_select_obj_type_panel = NULL;
  142. }
  143. if (m_get_first_point)
  144. {
  145. m_app->GetCommandPanel()->RemoveDialog(m_get_first_point);
  146. m_get_first_point = NULL;
  147. }
  148. if (m_get_second_point)
  149. {
  150. m_app->GetCommandPanel()->RemoveDialog(m_get_second_point);
  151. m_get_second_point = NULL;
  152. }
  153. if (m_get_third_point)
  154. {
  155. m_app->GetCommandPanel()->RemoveDialog(m_get_third_point);
  156. m_get_third_point = NULL;
  157. }
  158. if (m_get_object_panel==NULL)
  159. {
  160. CString lab;
  161. lab.LoadString(IDS_OBJECT);
  162. m_get_object_panel = 
  163. reinterpret_cast<IGetObjectsPanel*>(m_app->GetCommandPanel()->
  164. AddDialog(IBaseInterfaceOfGetDialogs::GET_OBJECTS_DLG,
  165. lab,false));
  166. m_get_object_panel->EnableControls(true);
  167. m_get_object_panel->SetMultiselectMode(false);
  168. m_get_object_panel->FillList(isObjAddToList);
  169. m_app->GetCommandPanel()->EnableRadio(0,true);
  170. m_app->GetCommandPanel()->EnableRadio(1,true);
  171. }
  172. m_message.LoadString(IDS_CHOISE_FIRST_OBJ_OF_PATH);
  173. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  174. {
  175. size_t sz =m_objects.size();
  176. for (size_t i=0;i<sz;i++)
  177. sgCObject::DeleteObject(m_objects[i]);
  178. }
  179. break;
  180. case 0:
  181. if (m_scenar==1)
  182. break;
  183. m_scenar = 1;
  184. if (m_get_object_panel!=NULL)
  185. {
  186. m_app->GetCommandPanel()->RemoveDialog(m_get_object_panel);
  187. m_get_object_panel = NULL;
  188. }
  189. if (m_select_obj_type_panel==NULL)
  190. {
  191. m_select_obj_type_panel = new CContObjDlg;
  192. m_select_obj_type_panel->Create(IDD_CONT_OBJ_TYPE,
  193. m_app->GetCommandPanel()->GetDialogsContainerWindow());
  194. m_select_obj_type_panel->SetCommander(this);
  195. m_message.LoadString(IDS_CONT_OBJ_TYPE);
  196. m_app->GetCommandPanel()->AddDialog(m_select_obj_type_panel,m_message,false);
  197. m_select_obj_type_panel->EnableControls(true);
  198. m_app->GetCommandPanel()->EnableRadio(1,true);
  199. m_select_obj_type_panel->OnBnClickedContObjRadio1();
  200. }
  201. m_message.LoadString(IDS_ENTER_POINT_COORDS);
  202. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  203. break;
  204. default:
  205. ASSERT(0);
  206. break;
  207. }
  208. }
  209. void Contour::SwitchObjectType(int newObjType)
  210. {
  211. SWITCH_RESOURCE
  212. switch(newObjType) 
  213. {
  214. case 0:
  215. if (m_lines)
  216. break;
  217. if (m_step==1)
  218. {
  219. if (m_get_third_point)
  220. {
  221. m_app->GetCommandPanel()->RemoveDialog(m_get_third_point);
  222. m_get_third_point = NULL;
  223. }
  224. m_lines = true;
  225. break;
  226. }
  227. m_lines = true;
  228. if (m_get_first_point)
  229. {
  230. m_app->GetCommandPanel()->RemoveDialog(m_get_first_point);
  231. m_get_first_point = NULL;
  232. }
  233. if (m_get_second_point)
  234. {
  235. m_app->GetCommandPanel()->RemoveDialog(m_get_second_point);
  236. m_get_second_point = NULL;
  237. }
  238. if (m_get_third_point)
  239. {
  240. m_app->GetCommandPanel()->RemoveDialog(m_get_third_point);
  241. m_get_third_point = NULL;
  242. }
  243. if (m_get_first_point==NULL)
  244. {
  245. m_message.LoadString(IDS_FIRST_POINT);
  246. m_get_first_point = 
  247. reinterpret_cast<IGetPointPanel*>(m_app->GetCommandPanel()->
  248. AddDialog(IBaseInterfaceOfGetDialogs::GET_POINT_DLG,
  249. m_message,true));
  250. }
  251. if (m_get_second_point==NULL)
  252. {
  253. m_message.LoadString(IDS_SECOND_POINT);
  254. m_get_second_point = 
  255. reinterpret_cast<IGetPointPanel*>(m_app->GetCommandPanel()->
  256. AddDialog(IBaseInterfaceOfGetDialogs::GET_POINT_DLG,
  257. m_message,true));
  258. }
  259. m_app->GetCommandPanel()->SetActiveRadio(2);
  260. break;
  261. case 1:
  262. if (!m_lines)
  263. break;
  264. if (m_step==1)
  265. {
  266. if (m_get_third_point==NULL)
  267. {
  268. m_message.LoadString(IDS_POINT_ON_ARC);
  269. m_get_third_point = 
  270. reinterpret_cast<IGetPointPanel*>(m_app->GetCommandPanel()->
  271. AddDialog(IBaseInterfaceOfGetDialogs::GET_POINT_DLG,
  272. m_message,true));
  273. }
  274. m_lines = false;
  275. break;
  276. }
  277. m_lines = false;
  278. if (m_get_first_point)
  279. {
  280. m_app->GetCommandPanel()->RemoveDialog(m_get_first_point);
  281. m_get_first_point = NULL;
  282. }
  283. if (m_get_second_point)
  284. {
  285. m_app->GetCommandPanel()->RemoveDialog(m_get_second_point);
  286. m_get_second_point = NULL;
  287. }
  288. if (m_get_third_point)
  289. {
  290. m_app->GetCommandPanel()->RemoveDialog(m_get_third_point);
  291. m_get_third_point = NULL;
  292. }
  293. if (m_get_first_point==NULL)
  294. {
  295. m_message.LoadString(IDS_BEG_POINT);
  296. m_get_first_point = 
  297. reinterpret_cast<IGetPointPanel*>(m_app->GetCommandPanel()->
  298. AddDialog(IBaseInterfaceOfGetDialogs::GET_POINT_DLG,
  299. m_message,true));
  300. }
  301. if (m_get_second_point==NULL)
  302. {
  303. m_message.LoadString(IDS_END_POINT);
  304. m_get_second_point = 
  305. reinterpret_cast<IGetPointPanel*>(m_app->GetCommandPanel()->
  306. AddDialog(IBaseInterfaceOfGetDialogs::GET_POINT_DLG,
  307. m_message,true));
  308. }
  309. if (m_get_third_point==NULL)
  310. {
  311. m_message.LoadString(IDS_POINT_ON_ARC);
  312. m_get_third_point = 
  313. reinterpret_cast<IGetPointPanel*>(m_app->GetCommandPanel()->
  314. AddDialog(IBaseInterfaceOfGetDialogs::GET_POINT_DLG,
  315. m_message,true));
  316. }
  317. m_app->GetCommandPanel()->SetActiveRadio(2);
  318. break;
  319. default:
  320. ASSERT(0);
  321. break;
  322. }
  323. }
  324. void  Contour::SendCommanderMessage(ICommander::COMMANDER_MESSAGE mes, void* params)
  325. {
  326. if (mes==ICommander::CM_SWITCH_ROLLUP_DIALOG)
  327. {
  328. ASSERT(params!=NULL);
  329. int   newActiveDlg = *(reinterpret_cast<int*>(params));
  330. ASSERT(newActiveDlg<=1);
  331. m_step = (unsigned int)newActiveDlg;
  332. /*if (newActiveDlg==0)
  333. {
  334. m_app->GetCommandPanel()->EnableRadio(1,false);
  335. m_message.LoadString(IDS_CHOISE_TRANS_OBJ);
  336. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  337. }*/
  338. //m_app->GetViewPort()->InvalidateViewPort();
  339. return;
  340. }
  341. if (mes==ICommander::CM_SELECT_OBJECT)
  342. {
  343. ASSERT(params!=NULL);
  344. ASSERT(m_scenar==0);
  345. sgCObject* so = (sgCObject*)params;
  346. if (so!=m_start_object)
  347. {
  348. m_app->GetViewPort()->SetHotObject(so);
  349. m_start_object = so;
  350. }
  351. m_app->GetViewPort()->InvalidateViewPort();
  352. }
  353. }
  354. void  Contour::Start()
  355. {
  356. SWITCH_RESOURCE
  357. m_app->GetCommandPanel()->RemoveAllDialogs();
  358. CString lab;
  359. lab.LoadString(IDS_TOOLTIP_FIVETH);
  360. m_app->StartCommander(lab);
  361. m_scenar_panel = new CContScenarDlg;
  362. m_scenar_panel->Create(IDD_CONT_SCENARS_DLG,
  363. m_app->GetCommandPanel()->GetDialogsContainerWindow());
  364. m_scenar_panel->SetCommander(this);
  365. m_message.LoadString(IDS_CONT_SCENAR);
  366. m_app->GetCommandPanel()->AddDialog(m_scenar_panel,m_message,false);
  367. m_app->GetCommandPanel()->EnableRadio(0,true);
  368. }
  369. static  void get_end_points_of_object(sgCObject* ob, SG_POINT& bP, SG_POINT& eP)
  370. {
  371. if (ob->GetType()==SG_OT_ARC)
  372. {
  373. sgCArc* arcO = reinterpret_cast<sgCArc*>(ob);
  374. const SG_ARC* arcG = arcO->GetGeometry();
  375. bP = arcG->begin;
  376. eP = arcG->end;
  377. }
  378. else
  379. if (ob->GetType()==SG_OT_LINE)
  380. {
  381. sgCLine* lineO = reinterpret_cast<sgCLine*>(ob);
  382. const SG_LINE* lineG = lineO->GetGeometry();
  383. bP = lineG->p1;
  384. eP = lineG->p2;
  385. }
  386. else
  387. if (ob->GetType()==SG_OT_CONTOUR)
  388. {
  389. sgCContour* cO = reinterpret_cast<sgCContour*>(ob);
  390. //cO->GetEndPoints(bP,eP);
  391. bP = cO->GetPointFromCoefficient(0.0);
  392. eP = cO->GetPointFromCoefficient(1.0);
  393. }
  394. else
  395. {
  396. ASSERT(0);
  397. }
  398. }
  399. void  Contour::CreateContour(sgCObject* stObj)
  400. {
  401. std::vector<sgCObject*>   ArcAndLines;
  402. std::vector<sgCObject*>   goodOnPath;
  403. goodOnPath.push_back(stObj);
  404. SG_POINT  zeroBegP;
  405. SG_POINT  zeroEndP;
  406. get_end_points_of_object(stObj, zeroBegP, zeroEndP);
  407. sgCObject*  curObj = sgGetScene()->GetObjectsList()->GetHead();
  408. while (curObj) 
  409. {
  410. if (curObj->GetType()==SG_OT_ARC ||
  411. curObj->GetType()==SG_OT_LINE )
  412. {
  413. if (curObj!=stObj)
  414. ArcAndLines.push_back(curObj);
  415. }
  416. else
  417. {
  418. if (curObj->GetType()==SG_OT_CONTOUR)
  419. {
  420. sgCContour* ccont = reinterpret_cast<sgCContour*>(curObj);
  421. if (!ccont->IsClosed())
  422. {
  423. if (curObj!=stObj)
  424. ArcAndLines.push_back(curObj);
  425. }
  426. }
  427. }
  428. curObj = sgGetScene()->GetObjectsList()->GetNext(curObj);
  429. }
  430. #define MIN_DIST   0.0000001
  431. for (size_t i=0; i<ArcAndLines.size(); i++)
  432. {
  433. if (sgSpaceMath::PointsDistance(zeroEndP,zeroBegP)<MIN_DIST)
  434. break;
  435. if (ArcAndLines[i]==NULL)
  436. continue;
  437. SG_POINT begP;
  438. SG_POINT endP;
  439. get_end_points_of_object(ArcAndLines[i], begP, endP);
  440. bool  yes = false;
  441. if (sgSpaceMath::PointsDistance(begP,zeroBegP)<MIN_DIST)
  442. {
  443. zeroBegP = endP;
  444. yes = true;
  445. goto metlab;
  446. }
  447. if (sgSpaceMath::PointsDistance(endP,zeroBegP)<MIN_DIST)
  448. {
  449. zeroBegP = begP;
  450. yes = true;
  451. goto metlab;
  452. }
  453. if (sgSpaceMath::PointsDistance(begP,zeroEndP)<MIN_DIST)
  454. {
  455. zeroEndP = endP;
  456. yes = true;
  457. goto metlab;
  458. }
  459. if (sgSpaceMath::PointsDistance(endP,zeroEndP)<MIN_DIST)
  460. {
  461. zeroEndP = begP;
  462. yes = true;
  463. goto metlab;
  464. }
  465. metlab:
  466. if (yes)
  467. {
  468. goodOnPath.push_back(ArcAndLines[i]);
  469. //ArcAndLines.erase(&ArcAndLines[i]);
  470. ArcAndLines[i] = NULL;
  471. i = -1;
  472. continue;
  473. }
  474. yes = false;
  475. }
  476. if (goodOnPath.size()>1)
  477. {
  478. sgGetScene()->StartUndoGroup();
  479. sgCContour* cont = sgCContour::CreateContour(&goodOnPath[0],goodOnPath.size());
  480. if (!cont)
  481. return;
  482. SWITCH_RESOURCE
  483. m_message.LoadString(IDS_TOOLTIP_FIVETH);
  484. CString nmInd;
  485. nmInd.Format("%i",contour_name_index);
  486. m_message+=nmInd;
  487. cont->SetName(m_message);
  488. contour_name_index++;
  489. sgGetScene()->AttachObject(cont);
  490. sgGetScene()->EndUndoGroup();
  491. }
  492. else
  493. {
  494. if (goodOnPath.size()==0)
  495. {
  496. m_message.LoadString(IDS_ERR_PATH_NO_OBJ);
  497. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  498. m_message);
  499. return;
  500. }
  501. if (goodOnPath.size()==1)
  502. {
  503. m_message.LoadString(IDS_ERR_PATH_ONE_OBJ);
  504. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  505. m_message);
  506. return;
  507. }
  508. }
  509. }
  510. void  Contour::CreateContourFromObjects()
  511. {
  512. size_t sz = m_objects.size();
  513. if (sz==0)
  514. {
  515. m_message.LoadString(IDS_ERR_PATH_NO_OBJ);
  516. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  517. m_message);
  518. return;
  519. }
  520. if (sz==1)
  521. {
  522. m_message.LoadString(IDS_ERR_PATH_ONE_OBJ);
  523. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  524. m_message);
  525. return;
  526. }
  527. if (sz>1)
  528. {
  529. sgGetScene()->StartUndoGroup();
  530. sgCContour* cont = sgCContour::CreateContour(&m_objects[0],sz);
  531. if (!cont)
  532. return;
  533. SWITCH_RESOURCE
  534. m_message.LoadString(IDS_TOOLTIP_FIVETH);
  535. CString nmInd;
  536. nmInd.Format("%i",contour_name_index);
  537. m_message+=nmInd;
  538. cont->SetName(m_message);
  539. contour_name_index++;
  540. sgGetScene()->AttachObject(cont);
  541. sgGetScene()->EndUndoGroup();
  542. m_objects.clear();
  543. m_isFirstPoint = true;
  544. m_isSecondPoint = false;
  545. m_isLastPointOnArc = false;
  546. m_exist_arc_data = false;
  547. m_can_close =false;
  548. m_message.LoadString(IDS_FIRST_POINT);
  549. m_app->GetCommandPanel()->RenameRadio(2,m_message);
  550. m_app->GetCommandPanel()->SetActiveRadio(2);
  551. m_app->GetCommandPanel()->EnableRadio(3,false);
  552. m_step = 0;
  553. }
  554. }
  555. void  Contour::MouseMove(unsigned int nFlags,int pX,int pY)
  556. {
  557. if (m_scenar==0)
  558. {
  559. if (!(nFlags & MK_LBUTTON))
  560. {
  561. int snapSz = m_app->GetViewPort()->GetSnapSize();
  562. sgCObject* ho = m_app->GetViewPort()->GetTopObject(
  563. m_app->GetViewPort()->GetHitsInRect(CRect(pX-snapSz, pY-snapSz,
  564. pX+snapSz, pY+snapSz)));
  565. if (ho && isObjAddToList(ho))
  566. {
  567. m_app->GetViewPort()->SetHotObject(ho);
  568. if (m_get_object_panel)
  569. m_get_object_panel->SelectObject(ho,true);
  570. }
  571. else
  572. {
  573. m_app->GetViewPort()->SetHotObject(NULL);
  574. if (m_get_object_panel)
  575. m_get_object_panel->SelectObject(NULL,true);
  576. }
  577. m_app->GetViewPort()->InvalidateViewPort();
  578. }
  579. return;
  580. }
  581. if (m_scenar==1)
  582. {
  583. IViewPort::GET_SNAP_IN in_arg;
  584. in_arg.scrX = pX;
  585. in_arg.scrY = pY;
  586. in_arg.snapType = SNAP_SYSTEM;
  587. switch(m_step) 
  588. {
  589. case 0:
  590. if (m_get_first_point)
  591. {
  592. in_arg.XFix = m_get_first_point->IsXFixed();
  593. in_arg.YFix = m_get_first_point->IsYFixed();
  594. in_arg.ZFix = m_get_first_point->IsZFixed();
  595. m_get_first_point->GetPoint(in_arg.FixPoint.x,in_arg.FixPoint.y,in_arg.FixPoint.z);
  596. IViewPort::GET_SNAP_OUT out_arg;
  597. m_app->GetViewPort()->GetWorldPointAfterSnap(in_arg,out_arg);
  598. m_cur_point = out_arg.result_point;
  599. m_get_first_point->SetPoint((float)(m_cur_point.x),(float)(m_cur_point.y),(float)(m_cur_point.z));
  600. }
  601. break;
  602. case 1:
  603. if (m_get_second_point)
  604. {
  605. in_arg.XFix = m_get_second_point->IsXFixed();
  606. in_arg.YFix = m_get_second_point->IsYFixed();
  607. in_arg.ZFix = m_get_second_point->IsZFixed();
  608. m_get_second_point->GetPoint(in_arg.FixPoint.x,in_arg.FixPoint.y,in_arg.FixPoint.z);
  609. IViewPort::GET_SNAP_OUT out_arg;
  610. m_app->GetViewPort()->GetWorldPointAfterSnap(in_arg,out_arg);
  611. m_cur_point = out_arg.result_point;
  612. m_get_second_point->SetPoint((float)(m_cur_point.x),(float)(m_cur_point.y),(float)(m_cur_point.z));
  613. }
  614. break;
  615. case 2:
  616. if (m_lines)
  617. {
  618. ASSERT(0);
  619. return;
  620. }
  621. if (m_get_third_point)
  622. {
  623. in_arg.XFix = m_get_third_point->IsXFixed();
  624. in_arg.YFix = m_get_third_point->IsYFixed();
  625. in_arg.ZFix = m_get_third_point->IsZFixed();
  626. m_get_third_point->GetPoint(in_arg.FixPoint.x,in_arg.FixPoint.y,in_arg.FixPoint.z);
  627. IViewPort::GET_SNAP_OUT out_arg;
  628. m_app->GetViewPort()->GetWorldPointAfterSnap(in_arg,out_arg);
  629. m_cur_point = out_arg.result_point;
  630. m_get_third_point->SetPoint((float)(m_cur_point.x),(float)(m_cur_point.y),(float)(m_cur_point.z));
  631. }
  632. if (!m_lines)
  633. {
  634. m_exist_arc_data = m_arc_geo.FromThreePoints(m_tmp_first_point,m_tmp_second_point,
  635. m_cur_point,false);
  636. }
  637. break;
  638. default:
  639. ASSERT(0);
  640. return;
  641. }
  642. m_app->GetViewPort()->InvalidateViewPort();
  643. }
  644. }
  645. void  Contour::LeftClick(unsigned int nFlags,int pX,int pY)
  646. {
  647. SWITCH_RESOURCE
  648. if (m_scenar==0)
  649. {
  650. m_start_object = m_app->GetViewPort()->GetHotObject();
  651. if (m_start_object==NULL)
  652. {
  653. m_message.LoadString(IDS_ERR_PATH_NO_OBJ);
  654. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  655. m_message);
  656. return;
  657. }
  658. CreateContour(m_start_object);
  659. m_get_object_panel->RemoveAllObjects();
  660. m_get_object_panel->FillList(isObjAddToList);
  661. return;
  662. }
  663. if (m_scenar==1)
  664. {
  665. if (m_isFirstPoint)
  666. {
  667. m_tmp_first_point = m_cur_point;
  668. m_isFirstPoint = false;
  669. m_isSecondPoint = true;
  670. if (m_objects.size()==0)
  671. m_first_point = m_cur_point;
  672. m_step=1;
  673. m_app->GetCommandPanel()->SetActiveRadio(m_step+2);
  674. m_app->GetCommandPanel()->EnableRadio(m_step+1,false);
  675. m_message.LoadString(IDS_PREV);
  676. m_app->GetCommandPanel()->RenameRadio(m_step+1,m_message);
  677. }
  678. else
  679. if (m_isSecondPoint)
  680. {
  681. if (sgSpaceMath::PointsDistance(m_tmp_first_point,m_cur_point)<0.00001)
  682. {
  683. m_message.LoadString(IDS_ERR_PNT_AS_PREV);
  684. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  685. m_message);
  686. return;
  687. }
  688. if (m_lines)
  689. {
  690. if (m_get_first_point)
  691. m_get_first_point->SetPoint(m_cur_point.x,
  692. m_cur_point.y,
  693. m_cur_point.z);
  694. sgCLine* ll = sgCreateLine(m_tmp_first_point.x,
  695. m_tmp_first_point.y,
  696. m_tmp_first_point.z,
  697. m_cur_point.x,
  698. m_cur_point.y,
  699. m_cur_point.z);
  700. m_objects.push_back(ll);
  701. m_tmp_first_point = m_cur_point;
  702. m_isFirstPoint = false;
  703. m_isSecondPoint = true;
  704. }
  705. else
  706. {
  707. m_tmp_second_point = m_cur_point;
  708. m_isFirstPoint = false;
  709. m_isSecondPoint = false;
  710. m_isLastPointOnArc = true;
  711. m_step=2;
  712. m_app->GetCommandPanel()->SetActiveRadio(m_step+2);
  713. m_app->GetCommandPanel()->EnableRadio(m_step+1,false);
  714. if (m_select_obj_type_panel)
  715. m_select_obj_type_panel->EnableLineType(false);
  716. }
  717. int snSz = m_app->GetViewPort()->GetSnapSize();
  718. double  coords[3];
  719. m_app->GetViewPort()->ProjectWorldPoint(m_first_point,coords[0],coords[1],coords[2]);
  720. if (sqrt((coords[0]-pX)*(coords[0]-pX)+
  721. (coords[1]-pY)*(coords[1]-pY))<=snSz ||
  722. sgSpaceMath::PointsDistance(m_first_point,m_cur_point)<0.00001)
  723. m_can_close = true;
  724. if (m_lines && m_can_close)
  725. {
  726. m_message.LoadString(IDS_CLOSE_CONTOUR);
  727. if (AfxMessageBox(m_message,MB_YESNO)==IDYES)
  728. {
  729. sgCObject* lastO = m_objects[m_objects.size()-1];
  730. ASSERT(lastO->GetType()==SG_OT_LINE);
  731. sgCLine* lastL = reinterpret_cast<sgCLine*>(lastO);
  732. m_tmp_first_point = lastL->GetGeometry()->p1;
  733. sgCObject::DeleteObject(lastO);
  734. m_objects.pop_back();
  735. sgCLine* ll = sgCreateLine(m_tmp_first_point.x,
  736. m_tmp_first_point.y,
  737. m_tmp_first_point.z,
  738. m_first_point.x,
  739. m_first_point.y,
  740. m_first_point.z);
  741. m_objects.push_back(ll);
  742. CreateContourFromObjects();
  743. }
  744. else
  745. m_can_close = false;
  746. }
  747. }
  748. else
  749. if (m_isLastPointOnArc)
  750. {
  751. if (sgSpaceMath::IsPointsOnOneLine(m_tmp_first_point,m_tmp_second_point,m_cur_point))
  752. {
  753. m_message.LoadString(IDS_ARC_ERR_ON_LINE);
  754. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  755. m_message);
  756. return;
  757. }
  758. ASSERT(!m_lines);
  759. ASSERT(m_exist_arc_data);
  760. sgCArc* aa = sgCreateArc(m_arc_geo);
  761. m_objects.push_back(aa);
  762. m_tmp_first_point = m_tmp_second_point;
  763. m_isFirstPoint = false;
  764. m_isSecondPoint = true;
  765. m_isLastPointOnArc = false;
  766. m_exist_arc_data = false;
  767. m_step =1;
  768. m_app->GetCommandPanel()->SetActiveRadio(m_step+2);
  769. m_app->GetCommandPanel()->EnableRadio(m_step+3,false);
  770. if (m_get_first_point)
  771. m_get_first_point->SetPoint(m_tmp_first_point.x,
  772. m_tmp_first_point.y,
  773. m_tmp_first_point.z);
  774. if (m_select_obj_type_panel)
  775. m_select_obj_type_panel->EnableLineType(true);
  776. if (m_can_close)
  777. {
  778. m_message.LoadString(IDS_CLOSE_CONTOUR);
  779. if (AfxMessageBox(m_message,MB_YESNO)==IDYES)
  780. {
  781. sgCObject* lastO = m_objects[m_objects.size()-1];
  782. ASSERT(lastO->GetType()==SG_OT_ARC);
  783. sgCArc* lastA = reinterpret_cast<sgCArc*>(lastO);
  784. m_tmp_first_point = lastA->GetGeometry()->begin;
  785. sgCObject::DeleteObject(lastO);
  786. m_objects.pop_back();
  787. m_arc_geo.FromThreePoints(m_tmp_first_point,m_first_point,
  788. m_cur_point,false);
  789. sgCArc* aa = sgCreateArc(m_arc_geo);
  790. m_objects.push_back(aa);
  791. CreateContourFromObjects();
  792. }
  793. else
  794. m_can_close = false;
  795. }
  796. }
  797. else
  798. {
  799. ASSERT(0);
  800. }
  801. }
  802.     
  803. }
  804. void  Contour::Draw()
  805. {
  806. if (m_scenar==1)
  807. {
  808. float pC[3];
  809. m_app->GetViewPort()->GetPainter()->GetUserColorPoints(pC[0],pC[1],pC[2]);
  810. m_app->GetViewPort()->GetPainter()->SetCurColor(pC[0],pC[1],pC[2]);
  811. m_app->GetViewPort()->GetPainter()->DrawPoint(m_cur_point);
  812. m_app->GetViewPort()->GetPainter()->GetUserColorLines(pC[0],pC[1],pC[2]);
  813. m_app->GetViewPort()->GetPainter()->SetCurColor(pC[0],pC[1],pC[2]);
  814. size_t sz = m_objects.size();
  815. for(size_t i=0;i<sz;i++)
  816. {
  817. if (m_objects[i]->GetType()==SG_OT_LINE)
  818. {
  819. sgCLine* tmpl = reinterpret_cast<sgCLine*>(m_objects[i]);
  820. m_app->GetViewPort()->GetPainter()->DrawLine(*tmpl->GetGeometry());
  821. continue;
  822. }
  823. if (m_objects[i]->GetType()==SG_OT_ARC)
  824. {
  825. sgCArc* tmpa = reinterpret_cast<sgCArc*>(m_objects[i]);
  826. m_app->GetViewPort()->GetPainter()->DrawArc(*tmpa->GetGeometry());
  827. continue;
  828. }
  829. }
  830. if (m_exist_arc_data)
  831. {
  832. m_app->GetViewPort()->GetPainter()->DrawArc(m_arc_geo);
  833. }
  834. if (m_isSecondPoint)
  835. {
  836. SG_LINE lll;
  837. lll.p1 = m_tmp_first_point;
  838. lll.p2 = m_cur_point;
  839. m_app->GetViewPort()->GetPainter()->DrawLine(lll);
  840. }
  841. }
  842. }
  843. void  Contour::OnEnter()
  844. {
  845. SWITCH_RESOURCE
  846. if (m_scenar==0)
  847. {
  848. m_start_object = m_app->GetViewPort()->GetHotObject();
  849. if (m_start_object==NULL)
  850. {
  851. m_message.LoadString(IDS_ERR_PATH_NO_OBJ);
  852. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  853. m_message);
  854. return;
  855. }
  856. CreateContour(m_start_object);
  857. m_get_object_panel->RemoveAllObjects();
  858. m_get_object_panel->FillList(isObjAddToList);
  859. return;
  860. }
  861. if (m_scenar==1)
  862. {
  863. switch(m_step) 
  864. {
  865. case 0:
  866. if (m_get_first_point)
  867. m_get_first_point->GetPoint(m_cur_point.x,
  868. m_cur_point.y,
  869. m_cur_point.z);
  870. break;
  871. case 1:
  872. if (m_get_second_point)
  873. m_get_second_point->GetPoint(m_cur_point.x,
  874. m_cur_point.y,
  875. m_cur_point.z);
  876. break;
  877. case 2:
  878. if (m_get_third_point)
  879. m_get_third_point->GetPoint(m_cur_point.x,
  880. m_cur_point.y,
  881. m_cur_point.z);
  882. break;
  883. }
  884. if (m_isFirstPoint)
  885. {
  886. m_tmp_first_point = m_cur_point;
  887. m_isFirstPoint = false;
  888. m_isSecondPoint = true;
  889. if (m_objects.size()==0)
  890. m_first_point = m_cur_point;
  891. m_step=1;
  892. m_app->GetCommandPanel()->SetActiveRadio(m_step+2);
  893. m_app->GetCommandPanel()->EnableRadio(m_step+1,false);
  894. m_message.LoadString(IDS_PREV);
  895. m_app->GetCommandPanel()->RenameRadio(m_step+1,m_message);
  896. }
  897. else
  898. if (m_isSecondPoint)
  899. {
  900. if (sgSpaceMath::PointsDistance(m_tmp_first_point,m_cur_point)<0.00001)
  901. {
  902. m_message.LoadString(IDS_ERR_PNT_AS_PREV);
  903. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  904. m_message);
  905. goto unfix;
  906. }
  907. if (m_lines)
  908. {
  909. if (m_get_first_point)
  910. m_get_first_point->SetPoint(m_cur_point.x,
  911. m_cur_point.y,
  912. m_cur_point.z);
  913. sgCLine* ll = sgCreateLine(m_tmp_first_point.x,
  914. m_tmp_first_point.y,
  915. m_tmp_first_point.z,
  916. m_cur_point.x,
  917. m_cur_point.y,
  918. m_cur_point.z);
  919. m_objects.push_back(ll);
  920. m_tmp_first_point = m_cur_point;
  921. m_isFirstPoint = false;
  922. m_isSecondPoint = true;
  923. }
  924. else
  925. {
  926. m_tmp_second_point = m_cur_point;
  927. m_isFirstPoint = false;
  928. m_isSecondPoint = false;
  929. m_isLastPointOnArc = true;
  930. m_step=2;
  931. m_app->GetCommandPanel()->SetActiveRadio(m_step+2);
  932. m_app->GetCommandPanel()->EnableRadio(m_step+1,false);
  933. if (m_select_obj_type_panel)
  934. m_select_obj_type_panel->EnableLineType(false);
  935. }
  936. if (sgSpaceMath::PointsDistance(m_first_point,m_cur_point)<0.00001)
  937. m_can_close = true;
  938. if (m_lines && m_can_close)
  939. {
  940. m_message.LoadString(IDS_CLOSE_CONTOUR);
  941. if (AfxMessageBox(m_message,MB_YESNO)==IDYES)
  942. {
  943. sgCObject* lastO = m_objects[m_objects.size()-1];
  944. ASSERT(lastO->GetType()==SG_OT_LINE);
  945. sgCLine* lastL = reinterpret_cast<sgCLine*>(lastO);
  946. m_tmp_first_point = lastL->GetGeometry()->p1;
  947. sgCObject::DeleteObject(lastO);
  948. m_objects.pop_back();
  949. sgCLine* ll = sgCreateLine(m_tmp_first_point.x,
  950. m_tmp_first_point.y,
  951. m_tmp_first_point.z,
  952. m_first_point.x,
  953. m_first_point.y,
  954. m_first_point.z);
  955. m_objects.push_back(ll);
  956. CreateContourFromObjects();
  957. }
  958. else
  959. m_can_close = false;
  960. }
  961. }
  962. else
  963. if (m_isLastPointOnArc)
  964. {
  965. if (sgSpaceMath::IsPointsOnOneLine(m_tmp_first_point,m_tmp_second_point,m_cur_point))
  966. {
  967. m_message.LoadString(IDS_ARC_ERR_ON_LINE);
  968. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  969. m_message);
  970. goto unfix;
  971. }
  972. ASSERT(!m_lines);
  973. if (m_arc_geo.FromThreePoints(m_tmp_first_point,
  974. m_tmp_second_point,
  975. m_cur_point,
  976. false))
  977. {
  978. sgCArc* aa = sgCreateArc(m_arc_geo);
  979. m_objects.push_back(aa);
  980. m_tmp_first_point = m_tmp_second_point;
  981. m_isFirstPoint = false;
  982. m_isSecondPoint = true;
  983. m_isLastPointOnArc = false;
  984. m_exist_arc_data = false;
  985. m_step =1;
  986. m_app->GetCommandPanel()->SetActiveRadio(m_step+2);
  987. m_app->GetCommandPanel()->EnableRadio(m_step+3,false);
  988. if (m_get_first_point)
  989. m_get_first_point->SetPoint(m_tmp_first_point.x,
  990. m_tmp_first_point.y,
  991. m_tmp_first_point.z);
  992. if (m_select_obj_type_panel)
  993. m_select_obj_type_panel->EnableLineType(true);
  994. if (m_can_close)
  995. {
  996. m_message.LoadString(IDS_CLOSE_CONTOUR);
  997. if (AfxMessageBox(m_message,MB_YESNO)==IDYES)
  998. {
  999. sgCObject* lastO = m_objects[m_objects.size()-1];
  1000. ASSERT(lastO->GetType()==SG_OT_ARC);
  1001. sgCArc* lastA = reinterpret_cast<sgCArc*>(lastO);
  1002. m_tmp_first_point = lastA->GetGeometry()->begin;
  1003. sgCObject::DeleteObject(lastO);
  1004. m_objects.pop_back();
  1005. m_arc_geo.FromThreePoints(m_tmp_first_point,m_first_point,
  1006. m_cur_point,false);
  1007. sgCArc* aa = sgCreateArc(m_arc_geo);
  1008. m_objects.push_back(aa);
  1009. CreateContourFromObjects();
  1010. }
  1011. else
  1012. m_can_close = false;
  1013. }
  1014. }
  1015. else
  1016. {
  1017. m_message.LoadString(IDS_ARC_ERR_ON_LINE);
  1018. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  1019. m_message);
  1020. goto unfix;
  1021. }
  1022. }
  1023. else
  1024. {
  1025. ASSERT(0);
  1026. }
  1027. }
  1028. unfix:
  1029. if (m_get_first_point)
  1030. {
  1031. m_get_first_point->XFix(false);
  1032. m_get_first_point->YFix(false);
  1033. m_get_first_point->ZFix(false);
  1034. }
  1035. if (m_get_second_point)
  1036. {
  1037. m_get_second_point->XFix(false);
  1038. m_get_second_point->YFix(false);
  1039. m_get_second_point->ZFix(false);
  1040. }
  1041. if (m_get_third_point)
  1042. {
  1043. m_get_third_point->XFix(false);
  1044. m_get_third_point->YFix(false);
  1045. m_get_third_point->ZFix(false);
  1046. }
  1047. m_app->GetViewPort()->InvalidateViewPort();
  1048. }
  1049. unsigned int  Contour::GetItemsCount()
  1050. {
  1051. return 1;
  1052. }
  1053. void         Contour::GetItem(unsigned int itemID, CString& itSrt)
  1054. {
  1055. SWITCH_RESOURCE
  1056. if (itemID==0) 
  1057. {
  1058. itSrt.LoadString(IDS_END_OPER);
  1059. }
  1060. else
  1061. {
  1062. ASSERT(0);
  1063. }
  1064. }
  1065. void     Contour::GetItemState(unsigned int itemID, bool& enbl, bool& checked)
  1066. {
  1067. enbl = true;
  1068. checked = false;
  1069. }
  1070. HBITMAP   Contour::GetItemBitmap(unsigned int)
  1071. {
  1072. return NULL;
  1073. }
  1074. void         Contour::Run(unsigned int itemID)
  1075. {
  1076. switch(itemID) 
  1077. {
  1078. case 0:
  1079. if (m_objects.size()==0)
  1080. {
  1081. m_message.LoadString(IDS_ERR_PATH_NO_OBJ);
  1082. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  1083. m_message);
  1084. return;
  1085. }
  1086. if (m_objects.size()==1)
  1087. {
  1088. m_message.LoadString(IDS_ERR_PATH_ONE_OBJ);
  1089. m_app->PutMessage(IApplicationInterface::MT_ERROR,
  1090. m_message);
  1091. return;
  1092. }
  1093. CreateContourFromObjects();
  1094. break;
  1095. default:
  1096. ASSERT(0);
  1097. }
  1098. }