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

Graph program

Development Platform:

Visual C++

  1. #include "stdafx.h"
  2. #include "Coons2Command.h"
  3. #include "..//resource.h"
  4. int     coons_name_index = 1;
  5. Coons2Command::Coons2Command(IApplicationInterface* appI):
  6. m_app(appI)
  7. {
  8. ASSERT(m_app);
  9. m_step = 0;
  10. m_cur_obj = NULL;
  11. m_get_object_panels[0] = 
  12. m_get_object_panels[1] = NULL;
  13. m_objs[0] = m_objs[1] = m_objs[2] = m_objs[3] = NULL;
  14. }
  15. Coons2Command::~Coons2Command()
  16. {
  17. if (sgGetScene()->GetSelectedObjectsList()->GetCount()>0)
  18. {
  19. sgCObject*  curObj = sgGetScene()->GetSelectedObjectsList()->GetHead();
  20. while (curObj) 
  21. {
  22. curObj->Select(false);
  23. curObj = sgGetScene()->GetSelectedObjectsList()->GetNext(curObj);
  24. }
  25. }
  26. m_app->GetCommandPanel()->RemoveAllDialogs();
  27. m_app->GetViewPort()->InvalidateViewPort();
  28. }
  29. bool    Coons2Command::PreTranslateMessage(MSG* pMsg)
  30. {
  31. /*if (pMsg->message==WM_KEYUP||
  32. pMsg->message==WM_CHAR)
  33. return false;*/
  34. if (pMsg->message==WM_KEYUP||pMsg->message==WM_KEYDOWN || 
  35. pMsg->message==WM_CHAR)
  36. {
  37. if (pMsg->wParam==VK_RETURN)
  38. {
  39. OnEnter();
  40. return true;
  41. }
  42. if (pMsg->wParam==VK_ESCAPE)
  43. {
  44. m_app->StopCommander();
  45. return true;
  46. }
  47. if(m_step>=0 && m_step<2) 
  48. {
  49. if (m_get_object_panels[m_step])
  50. m_get_object_panels[m_step]->GetWindow()->SendMessage(pMsg->message,
  51. pMsg->wParam,
  52. pMsg->lParam);
  53. }
  54. if (pMsg->message==WM_KEYDOWN)
  55. return false;
  56. else 
  57. return true;
  58. }
  59. else
  60. {
  61. if (pMsg->hwnd == m_app->GetViewPort()->GetWindow()->m_hWnd)
  62. {
  63. switch(pMsg->message) 
  64. {
  65. case WM_MOUSEMOVE:
  66. MouseMove(pMsg->wParam,GET_X_LPARAM(pMsg->lParam),GET_Y_LPARAM(pMsg->lParam));
  67. return true;
  68. case WM_LBUTTONDOWN:
  69. LeftClick(pMsg->wParam,GET_X_LPARAM(pMsg->lParam),GET_Y_LPARAM(pMsg->lParam));
  70. return true;
  71. default:
  72. return false;
  73. }
  74. }
  75. }
  76. return false;
  77. }
  78. void     Coons2Command::SendCommanderMessage(ICommander::COMMANDER_MESSAGE mes, 
  79.    void* params) 
  80. {
  81. if (mes==ICommander::CM_SWITCH_ROLLUP_DIALOG)
  82. {
  83. ASSERT(params!=NULL);
  84. int   newActiveDlg = *(reinterpret_cast<int*>(params));
  85. ASSERT(newActiveDlg<=1);
  86. m_step = (unsigned int)newActiveDlg;
  87. for (unsigned int i=m_step+1;i<=1;i++)
  88. {
  89. m_app->GetCommandPanel()->EnableRadio(i,false);
  90. m_objs[i]=NULL;
  91. }
  92. SWITCH_RESOURCE
  93. switch(m_step) 
  94. {
  95. case 0:
  96. m_message.LoadString(IDS_COON_SEL_1_CONT);
  97. break;
  98. case 1:
  99. m_message.LoadString(IDS_COON_SEL_2_CONT);
  100. break;
  101. default:
  102. ASSERT(0);
  103. break;
  104. }
  105. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  106. return;
  107. }
  108. if (mes==ICommander::CM_SELECT_OBJECT)
  109. {
  110. ASSERT(params!=NULL);
  111. ASSERT(m_step==0 || m_step==1);
  112. sgCObject* so = (sgCObject*)params;
  113. if (so!=m_app->GetViewPort()->GetHotObject())
  114. {
  115. m_app->GetViewPort()->SetHotObject(so);
  116. m_cur_obj = so;
  117. }
  118. m_app->GetViewPort()->InvalidateViewPort();
  119. }
  120. }
  121. static   bool isObjAddToList(sgCObject* obj)
  122. {
  123. SG_OBJECT_TYPE ot = obj->GetType();
  124. if (ot==SG_OT_LINE ||
  125. ot==SG_OT_ARC)
  126. return true;
  127. if (ot==SG_OT_SPLINE)
  128. {
  129. sgCSpline* spl = reinterpret_cast<sgCSpline*>(obj);
  130. if (!spl->IsClosed() && !spl->IsSelfIntersecting())
  131. return true;
  132. else
  133. return false;
  134. }
  135. if (ot==SG_OT_CONTOUR)
  136. {
  137. sgCContour* cntr = reinterpret_cast<sgCContour*>(obj);
  138. if (!cntr->IsClosed() && !cntr->IsSelfIntersecting())
  139. return true;
  140. else
  141. return false;
  142. }
  143. return false;
  144. }
  145. void  Coons2Command::Start()
  146. {
  147. SWITCH_RESOURCE
  148. m_app->GetCommandPanel()->RemoveAllDialogs();
  149. m_message.LoadString(IDS_COONS_NAME);
  150. m_app->StartCommander(m_message);
  151. m_message.LoadString(IDS_CONT);
  152. m_get_object_panels[0] = reinterpret_cast<IGetObjectsPanel*>(m_app->
  153. GetCommandPanel()->
  154. AddDialog(IBaseInterfaceOfGetDialogs::GET_OBJECTS_DLG,m_message,true));
  155. m_get_object_panels[1] = reinterpret_cast<IGetObjectsPanel*>(m_app->
  156. GetCommandPanel()->
  157. AddDialog(IBaseInterfaceOfGetDialogs::GET_OBJECTS_DLG,m_message,true));
  158. for (int i=0;i<2;i++)
  159. {
  160. if (m_get_object_panels[i])
  161. {
  162. m_get_object_panels[i]->SetMultiselectMode(false);
  163. m_get_object_panels[i]->FillList(isObjAddToList);
  164. }
  165. }
  166. m_step=0;
  167. m_app->GetCommandPanel()->SetActiveRadio(0);
  168. m_message.LoadString(IDS_COON_SEL_1_CONT);
  169. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  170. }
  171. void  Coons2Command::MouseMove(unsigned int nFlags,int pX,int pY)
  172. {
  173. //if (m_step==0)
  174. {
  175. if (!(nFlags & MK_LBUTTON))
  176. {
  177. int snapSz = m_app->GetViewPort()->GetSnapSize();
  178. sgCObject* ho = m_app->GetViewPort()->GetTopObject(
  179. m_app->GetViewPort()->GetHitsInRect(CRect(pX-snapSz, pY-snapSz,
  180. pX+snapSz, pY+snapSz)));
  181. if (ho && isObjAddToList(ho))
  182. {
  183. m_app->GetViewPort()->SetHotObject(ho);
  184. if (m_get_object_panels[m_step])
  185. m_get_object_panels[m_step]->SelectObject(ho,true);
  186. }
  187. else
  188. {
  189. m_app->GetViewPort()->SetHotObject(NULL);
  190. if (m_get_object_panels[m_step])
  191. m_get_object_panels[m_step]->SelectObject(NULL,true);
  192. }
  193. m_app->GetViewPort()->InvalidateViewPort();
  194. }
  195. return;
  196. }
  197. }
  198. bool Coons2Command::SetFourthAndFivethByFirstAndSecond()
  199. {
  200. m_objs[2] = m_objs[3] = NULL;
  201. if (m_objs[0]==NULL || m_objs[1]==NULL)
  202. return false;
  203. SG_POINT begFir, endFir, begSec, endSec;
  204. begFir = m_objs[0]->GetPointFromCoefficient(0.0);
  205. endFir = m_objs[0]->GetPointFromCoefficient(1.0);
  206. begSec = m_objs[1]->GetPointFromCoefficient(0.0);
  207. endSec = m_objs[1]->GetPointFromCoefficient(1.0);
  208. SG_VECTOR transVec;
  209. if (sgSpaceMath::PointsDistance(begFir,begSec)<0.00001)
  210. {
  211. m_objs[2] = (sgC2DObject*)m_objs[1]->Clone();
  212. transVec.x = endFir.x - begFir.x;
  213. transVec.y = endFir.y - begFir.y;
  214. transVec.z = endFir.z - begFir.z;
  215. m_objs[2]->InitTempMatrix()->Translate(transVec);
  216. m_objs[2]->ApplyTempMatrix();
  217. m_objs[2]->DestroyTempMatrix();
  218. m_objs[3] = (sgC2DObject*)m_objs[0]->Clone();
  219. transVec.x = endSec.x - begSec.x;
  220. transVec.y = endSec.y - begSec.y;
  221. transVec.z = endSec.z - begSec.z;
  222. m_objs[3]->InitTempMatrix()->Translate(transVec);
  223. m_objs[3]->ApplyTempMatrix();
  224. m_objs[3]->DestroyTempMatrix();
  225. return true;
  226. }
  227. else
  228. if (sgSpaceMath::PointsDistance(begFir,endSec)<0.00001)
  229. {
  230. m_objs[2] = (sgC2DObject*)m_objs[1]->Clone();
  231. transVec.x = endFir.x - begFir.x;
  232. transVec.y = endFir.y - begFir.y;
  233. transVec.z = endFir.z - begFir.z;
  234. m_objs[2]->InitTempMatrix()->Translate(transVec);
  235. m_objs[2]->ApplyTempMatrix();
  236. m_objs[2]->DestroyTempMatrix();
  237. m_objs[3] = (sgC2DObject*)m_objs[0]->Clone();
  238. transVec.x = begSec.x - endSec.x;
  239. transVec.y = begSec.y - endSec.y;
  240. transVec.z = begSec.z - endSec.z;
  241. m_objs[3]->InitTempMatrix()->Translate(transVec);
  242. m_objs[3]->ApplyTempMatrix();
  243. m_objs[3]->DestroyTempMatrix();
  244. return true;
  245. }
  246. else
  247. if (sgSpaceMath::PointsDistance(endFir,begSec)<0.00001)
  248. {
  249. m_objs[2] = (sgC2DObject*)m_objs[1]->Clone();
  250. transVec.x = begFir.x - endFir.x;
  251. transVec.y = begFir.y - endFir.y;
  252. transVec.z = begFir.z - endFir.z;
  253. m_objs[2]->InitTempMatrix()->Translate(transVec);
  254. m_objs[2]->ApplyTempMatrix();
  255. m_objs[2]->DestroyTempMatrix();
  256. m_objs[3] = (sgC2DObject*)m_objs[0]->Clone();
  257. transVec.x = endSec.x - begSec.x;
  258. transVec.y = endSec.y - begSec.y;
  259. transVec.z = endSec.z - begSec.z;
  260. m_objs[3]->InitTempMatrix()->Translate(transVec);
  261. m_objs[3]->ApplyTempMatrix();
  262. m_objs[3]->DestroyTempMatrix();
  263. return true;
  264. }
  265. else
  266. if (sgSpaceMath::PointsDistance(endFir,endSec)<0.00001)
  267. {
  268. m_objs[2] = (sgC2DObject*)m_objs[1]->Clone();
  269. transVec.x = begFir.x-endFir.x;
  270. transVec.y = begFir.y-endFir.y;
  271. transVec.z = begFir.z-endFir.z;
  272. m_objs[2]->InitTempMatrix()->Translate(transVec);
  273. m_objs[2]->ApplyTempMatrix();
  274. m_objs[2]->DestroyTempMatrix();
  275. m_objs[3] = (sgC2DObject*)m_objs[0]->Clone();
  276. transVec.x = begSec.x - endSec.x;
  277. transVec.y = begSec.y - endSec.y;
  278. transVec.z = begSec.z - endSec.z;
  279. m_objs[3]->InitTempMatrix()->Translate(transVec);
  280. m_objs[3]->ApplyTempMatrix();
  281. m_objs[3]->DestroyTempMatrix();
  282. return true;
  283. }
  284. else
  285. {
  286. return false;
  287. }
  288. }
  289. void  Coons2Command::LeftClick(unsigned int nFlags,int pX,int pY)
  290. {
  291. SWITCH_RESOURCE
  292. m_cur_obj=m_app->GetViewPort()->GetHotObject();
  293. if (m_cur_obj)
  294. {
  295. if (m_app->GetViewPort()->GetHotObject()->IsSelect())
  296. {
  297. m_message.LoadString(IDS_CONT_ALREADY_SEL);
  298. m_app->PutMessage(IApplicationInterface::MT_ERROR,m_message);
  299. return;
  300. }
  301. m_cur_obj->Select(true);
  302. m_objs[m_step++] = reinterpret_cast<sgC2DObject*>(m_cur_obj);
  303. if (m_step==1)
  304. {
  305. m_message.LoadString(IDS_COON_SEL_2_CONT);
  306. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  307. }
  308. if (m_step==2)
  309. {
  310. if (SetFourthAndFivethByFirstAndSecond())
  311. {
  312. sgCObject* aaa = sgSurfaces::Coons((*m_objs[0]),
  313. (*m_objs[1]),
  314. (*m_objs[2]),
  315. (m_objs[3]),36,36,6,6);
  316. if (!aaa)
  317. {
  318. m_message.LoadString(IDS_ERR_CANT_CREATE);
  319. m_app->PutMessage(IApplicationInterface::MT_ERROR,m_message);
  320. return;
  321. }
  322. CString nm;
  323. nm.LoadString(IDS_COONS_NAME);
  324. CString nmInd;
  325. nmInd.Format("%i",coons_name_index);
  326. nm+=nmInd;
  327. aaa->SetName(nm);
  328. aaa->SetUserGeometry("{F7325996-1640-4cf8-BA6A-F18415B512CB}",0,NULL);
  329. sgGetScene()->StartUndoGroup();
  330. sgGetScene()->AttachObject(aaa);
  331. m_app->ApplyAttributes(aaa);
  332. sgGetScene()->EndUndoGroup();
  333. coons_name_index++;
  334. m_message.LoadString(IDS_COON_SEL_1_CONT);
  335. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  336. if (sgGetScene()->GetSelectedObjectsList()->GetCount()>0)
  337. {
  338. sgCObject*  curObj = sgGetScene()->GetSelectedObjectsList()->GetHead();
  339. while (curObj) 
  340. {
  341. curObj->Select(false);
  342. curObj = sgGetScene()->GetSelectedObjectsList()->GetNext(curObj);
  343. }
  344. }
  345. sgDeleteObject(m_objs[2]);
  346. sgDeleteObject(m_objs[3]);
  347. m_step=0;
  348. }
  349. else
  350. {
  351. m_message.LoadString(IDS_COONS_ER_MUST_PRIMICAT);
  352. m_app->PutMessage(IApplicationInterface::MT_ERROR,m_message);
  353. m_objs[1]->Select(false);
  354. m_step = 1;
  355. }
  356. }
  357. m_app->GetCommandPanel()->SetActiveRadio(m_step);
  358. m_app->GetViewPort()->InvalidateViewPort();
  359. }
  360. else
  361. {
  362. m_message.LoadString(IDS_ERROR_OBJ_NOT_SEL);
  363. m_app->PutMessage(IApplicationInterface::MT_ERROR,m_message);
  364. return;
  365. }
  366. }
  367. void  Coons2Command::Draw()
  368. {
  369. }
  370. void  Coons2Command::OnEnter()
  371. {
  372. SWITCH_RESOURCE
  373. m_cur_obj=m_app->GetViewPort()->GetHotObject();
  374. if (m_cur_obj)
  375. {
  376. if (m_app->GetViewPort()->GetHotObject()->IsSelect())
  377. {
  378. m_message.LoadString(IDS_CONT_ALREADY_SEL);
  379. m_app->PutMessage(IApplicationInterface::MT_ERROR,m_message);
  380. return;
  381. }
  382. m_cur_obj->Select(true);
  383. m_objs[m_step++] = reinterpret_cast<sgC2DObject*>(m_cur_obj);
  384. if (m_step==1)
  385. {
  386. m_message.LoadString(IDS_COON_SEL_2_CONT);
  387. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  388. }
  389. if (m_step==2)
  390. {
  391. if (SetFourthAndFivethByFirstAndSecond())
  392. {
  393. sgCObject* aaa = sgSurfaces::Coons((*m_objs[0]),
  394. (*m_objs[1]),
  395. (*m_objs[2]),
  396. (m_objs[3]),36,36,6,6);
  397. if (!aaa)
  398. {
  399. m_message.LoadString(IDS_ERR_CANT_CREATE);
  400. m_app->PutMessage(IApplicationInterface::MT_ERROR,m_message);
  401. return;
  402. }
  403. CString nm;
  404. nm.LoadString(IDS_COONS_NAME);
  405. CString nmInd;
  406. nmInd.Format("%i",coons_name_index);
  407. nm+=nmInd;
  408. aaa->SetName(nm);
  409. aaa->SetUserGeometry("{F7325996-1640-4cf8-BA6A-F18415B512CB}",0,NULL);
  410. sgGetScene()->StartUndoGroup();
  411. sgGetScene()->AttachObject(aaa);
  412. m_app->ApplyAttributes(aaa);
  413. sgGetScene()->EndUndoGroup();
  414. coons_name_index++;
  415. m_message.LoadString(IDS_COON_SEL_1_CONT);
  416. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  417. if (sgGetScene()->GetSelectedObjectsList()->GetCount()>0)
  418. {
  419. sgCObject*  curObj = sgGetScene()->GetSelectedObjectsList()->GetHead();
  420. while (curObj) 
  421. {
  422. curObj->Select(false);
  423. curObj = sgGetScene()->GetSelectedObjectsList()->GetNext(curObj);
  424. }
  425. }
  426. sgDeleteObject(m_objs[2]);
  427. sgDeleteObject(m_objs[3]);
  428. m_step=0;
  429. }
  430. else
  431. {
  432. m_message.LoadString(IDS_COONS_ER_MUST_PRIMICAT);
  433. m_app->PutMessage(IApplicationInterface::MT_ERROR,m_message);
  434. m_objs[1]->Select(false);
  435. m_step = 1;
  436. }
  437. }
  438. m_app->GetCommandPanel()->SetActiveRadio(m_step);
  439. m_app->GetViewPort()->InvalidateViewPort();
  440. }
  441. else
  442. {
  443. m_message.LoadString(IDS_ERROR_OBJ_NOT_SEL);
  444. m_app->PutMessage(IApplicationInterface::MT_ERROR,m_message);
  445. return;
  446. }
  447. }
  448. unsigned int  Coons2Command::GetItemsCount()
  449. {
  450. return 0;
  451. }
  452. void         Coons2Command::GetItem(unsigned int itemID, CString& itSrt)
  453. {
  454. }
  455. void     Coons2Command::GetItemState(unsigned int itemID, bool& enbl, bool& checked)
  456. {
  457. }
  458. HBITMAP   Coons2Command::GetItemBitmap(unsigned int)
  459. {
  460. return NULL;
  461. }
  462. void         Coons2Command::Run(unsigned int itemID)
  463. {
  464. }