FaceCommand.cpp
上传用户:kairuinn
上传日期:2009-02-07
资源大小:2922k
文件大小:17k
源码类别:

图形图象

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "FaceCommand.h"
  3. #include "..//resource.h"
  4. #include <algorithm>
  5. int     face_name_index = 1;
  6. FaceCommand::FaceCommand(IApplicationInterface* appI):
  7. m_app(appI)
  8. ,m_get_main_object_panel(NULL)
  9. ,m_get_sub_objects_panel(NULL)
  10. {
  11. ASSERT(m_app);
  12. m_step = 0;
  13. m_otv.clear();
  14. }
  15. FaceCommand::~FaceCommand()
  16. {
  17. m_otv.clear();
  18. if (!m_object_lines.empty())
  19. m_object_lines.clear();
  20. if (sgGetScene()->GetSelectedObjectsList()->GetCount()>0)
  21. {
  22. sgCObject*  curObj = sgGetScene()->GetSelectedObjectsList()->GetHead();
  23. while (curObj) 
  24. {
  25. curObj->Select(false);
  26. curObj = sgGetScene()->GetSelectedObjectsList()->GetNext(curObj);
  27. }
  28. }
  29. m_app->GetCommandPanel()->RemoveAllDialogs();
  30. m_app->GetViewPort()->InvalidateViewPort();
  31. }
  32. bool    FaceCommand::PreTranslateMessage(MSG* pMsg)
  33. {
  34. /*if (pMsg->message==WM_KEYUP||
  35. pMsg->message==WM_CHAR)
  36. return false;*/
  37. if (pMsg->message==WM_KEYUP||pMsg->message==WM_KEYDOWN || 
  38. pMsg->message==WM_CHAR)
  39. {
  40. if (pMsg->wParam==VK_RETURN)
  41. {
  42. OnEnter();
  43. return true;
  44. }
  45. if (pMsg->wParam==VK_ESCAPE)
  46. {
  47. m_app->StopCommander();
  48. return true;
  49. }
  50. switch(m_step) 
  51. {
  52. case 0:
  53. if (m_get_main_object_panel)
  54. m_get_main_object_panel->GetWindow()->SendMessage(pMsg->message,
  55. pMsg->wParam,
  56. pMsg->lParam);
  57. break;
  58. case 1:
  59. if (m_get_sub_objects_panel)
  60. m_get_sub_objects_panel->GetWindow()->SendMessage(pMsg->message,
  61. pMsg->wParam,
  62. pMsg->lParam);
  63. break;
  64. }
  65. if (pMsg->message==WM_KEYDOWN)
  66. return false;
  67. else 
  68. return true;
  69. }
  70. else
  71. {
  72. if (pMsg->hwnd == m_app->GetViewPort()->GetWindow()->m_hWnd)
  73. {
  74. switch(pMsg->message) 
  75. {
  76. case WM_MOUSEMOVE:
  77. MouseMove(pMsg->wParam,GET_X_LPARAM(pMsg->lParam),GET_Y_LPARAM(pMsg->lParam));
  78. return true;
  79. case WM_LBUTTONDOWN:
  80. LeftClick(pMsg->wParam,GET_X_LPARAM(pMsg->lParam),GET_Y_LPARAM(pMsg->lParam));
  81. return true;
  82. default:
  83. return false;
  84. }
  85. }
  86. }
  87. return false;
  88. }
  89. void     FaceCommand::SendCommanderMessage(ICommander::COMMANDER_MESSAGE mes, 
  90.    void* params) 
  91. {
  92. if (mes==ICommander::CM_SWITCH_ROLLUP_DIALOG)
  93. {
  94. ASSERT(params!=NULL);
  95. int   newActiveDlg = *(reinterpret_cast<int*>(params));
  96. ASSERT(newActiveDlg<=1);
  97. m_step = (unsigned int)newActiveDlg;
  98. for (unsigned int i=m_step+1;i<=1;i++)
  99. m_app->GetCommandPanel()->EnableRadio(i,false);
  100. SWITCH_RESOURCE
  101. switch(m_step) 
  102. {
  103. case 0:
  104. m_message.LoadString(IDS_SEL_MAIN_CONT);
  105. m_get_main_object_panel->SelectObject(NULL,false);
  106. if (m_first_obj)
  107. {
  108. m_get_sub_objects_panel->AddObject(m_first_obj,false);
  109. m_first_obj->Select(false);
  110. }
  111. m_first_obj = NULL;
  112. m_get_sub_objects_panel->SelectObject(NULL,false);
  113. for (size_t i=0;i<m_otv.size();i++)
  114. m_otv[i]->Select(false);
  115. m_otv.clear();
  116. m_object_lines.clear();
  117. break;
  118. case 1:
  119. m_get_sub_objects_panel->SelectObject(NULL,false);
  120. for (size_t i=0;i<m_otv.size();i++)
  121. m_otv[i]->Select(false);
  122. m_otv.clear();
  123. m_message.LoadString(IDS_SEL_OTV);
  124. break;
  125. default:
  126. ASSERT(0);
  127. break;
  128. }
  129. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  130. return;
  131. }
  132. if (mes==ICommander::CM_SELECT_OBJECT)
  133. {
  134. ASSERT(params!=NULL);
  135. if (m_step>1)
  136. return;
  137. sgCObject* so = (sgCObject*)params;
  138. if (so!=m_app->GetViewPort()->GetHotObject())
  139. m_app->GetViewPort()->SetHotObject(so);
  140. if (m_step==0)
  141. {
  142. m_first_obj = so;
  143. }
  144. else
  145. {
  146. if (!so->IsSelect())
  147. {
  148. SWITCH_RESOURCE
  149. sgC2DObject* curH = reinterpret_cast<sgC2DObject*>(m_app->GetViewPort()->GetHotObject());
  150. HO_CORRECT_PATHS_RES testRes = GoodObjectForHole(curH);
  151. if (testRes!=HO_SUCCESS)
  152. {
  153. switch(testRes) 
  154. {
  155. case HO_NOT_IN_ONE_PLANE:
  156. m_message.LoadString(IDS_NOT_IN_ONE_PLANE);
  157. break;
  158. case HO_INTERSECTS_WITH_OUT:
  159. m_message.LoadString(IDS_INTERS_OUT);
  160. break;
  161. case HO_IN_NOT_INSIDE_OUT:
  162. m_message.LoadString(IDS_NOT_INSIDE_OUT);
  163. break;
  164. case HO_INTERSECTS_WITH_OTHER_HOLE:
  165. m_message.LoadString(IDS_INTERS_WITH_OTHERS);
  166. break;
  167. case HO_INSIDE_EXIST_HOLE:
  168. m_message.LoadString(IDS_INSIDE_EXIST_HOLE);
  169. break;
  170. case HO_CONTAIN_EXIST_HOLE:
  171. m_message.LoadString(IDS_CONSIST_EXIST_HOLE);
  172. break;
  173. default:
  174. m_message.LoadString(IDS_ERROR_OBJ_NOT_SEL);
  175. break;
  176. }
  177. m_app->PutMessage(IApplicationInterface::MT_ERROR,m_message);
  178. m_get_sub_objects_panel->SelectObject(so,false);
  179. return;
  180. }
  181. }
  182. so->Select(!so->IsSelect());
  183. if (so->IsSelect())
  184. m_otv.push_back(so);
  185. else
  186. {
  187. std::vector<sgCObject*>::iterator resultIt;
  188. resultIt = std::find(m_otv.begin(),m_otv.end(),so);
  189. if (resultIt!=m_otv.end())
  190. m_otv.erase(resultIt);
  191. }
  192. }
  193. m_app->GetViewPort()->InvalidateViewPort();
  194. }
  195. }
  196. static   bool isObjAddToList(sgCObject* obj)
  197. {
  198. SG_OBJECT_TYPE ot = obj->GetType();
  199. if (ot==SG_OT_CIRCLE)
  200. return true;
  201. if (ot==SG_OT_SPLINE)
  202. {
  203. sgCSpline* spl = reinterpret_cast<sgCSpline*>(obj);
  204. if (spl->IsPlane(NULL,NULL) && spl->IsClosed() && !spl->IsSelfIntersecting())
  205. return true;
  206. else
  207. return false;
  208. }
  209. if (ot==SG_OT_CONTOUR)
  210. {
  211. sgCContour* cntr = reinterpret_cast<sgCContour*>(obj);
  212. if (cntr->IsPlane(NULL,NULL) && cntr->IsClosed() && !cntr->IsSelfIntersecting())
  213. return true;
  214. else
  215. return false;
  216. }
  217. return false;
  218. }
  219. void  FaceCommand::Start()
  220. {
  221. SWITCH_RESOURCE
  222. m_app->GetCommandPanel()->RemoveAllDialogs();
  223. m_message.LoadString(IDS_TOOLTIP_ZERO);
  224. m_app->StartCommander(m_message);
  225. m_message.LoadString(IDS_MAIN_CON);
  226. m_get_main_object_panel = reinterpret_cast<IGetObjectsPanel*>(m_app->
  227. GetCommandPanel()->
  228. AddDialog(IBaseInterfaceOfGetDialogs::GET_OBJECTS_DLG,m_message,true));
  229. m_message.LoadString(IDS_OTV);
  230. m_get_sub_objects_panel = reinterpret_cast<IGetObjectsPanel*>(m_app->
  231. GetCommandPanel()->
  232. AddDialog(IBaseInterfaceOfGetDialogs::GET_OBJECTS_DLG,m_message,true));
  233. m_get_main_object_panel->SetMultiselectMode(false);
  234. m_get_main_object_panel->FillList(isObjAddToList);
  235. m_get_sub_objects_panel->SetMultiselectMode(true);
  236. m_get_sub_objects_panel->FillList(isObjAddToList);
  237. m_step=0;
  238. m_app->GetCommandPanel()->SetActiveRadio(0);
  239. m_message.LoadString(IDS_SEL_MAIN_CONT);
  240. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  241. }
  242. void  FaceCommand::MouseMove(unsigned int nFlags,int pX,int pY)
  243. {
  244. if (m_step==0)
  245. {
  246. if (!(nFlags & MK_LBUTTON))
  247. {
  248. int snapSz = m_app->GetViewPort()->GetSnapSize();
  249. sgCObject* ho = m_app->GetViewPort()->GetTopObject(
  250. m_app->GetViewPort()->GetHitsInRect(CRect(pX-snapSz, pY-snapSz,
  251. pX+snapSz, pY+snapSz)));
  252. if (ho && isObjAddToList(ho))
  253. {
  254. m_app->GetViewPort()->SetHotObject(ho);
  255. if (m_get_main_object_panel)
  256. m_get_main_object_panel->SelectObject(ho,true);
  257. }
  258. else
  259. {
  260. m_app->GetViewPort()->SetHotObject(NULL);
  261. if (m_get_main_object_panel)
  262. m_get_main_object_panel->SelectObject(NULL,true);
  263. }
  264. m_app->GetViewPort()->InvalidateViewPort();
  265. }
  266. return;
  267. }
  268. else
  269. if (m_step==1)
  270. {
  271. if (!(nFlags & MK_LBUTTON))
  272. {
  273. int snapSz = m_app->GetViewPort()->GetSnapSize();
  274. sgCObject* ho = m_app->GetViewPort()->GetTopObject(
  275. m_app->GetViewPort()->GetHitsInRect(CRect(pX-snapSz, pY-snapSz,
  276. pX+snapSz, pY+snapSz)));
  277. if (ho && isObjAddToList(ho))
  278. {
  279. m_app->GetViewPort()->SetHotObject(ho);
  280. //if (m_get_sub_objects_panel)
  281. // m_get_sub_objects_panel->SelectObject(ho,true);
  282. }
  283. else
  284. {
  285. m_app->GetViewPort()->SetHotObject(NULL);
  286. //if (m_get_sub_objects_panel)
  287. // m_get_sub_objects_panel->SelectObject(NULL,true);
  288. }
  289. m_app->GetViewPort()->InvalidateViewPort();
  290. }
  291. return;
  292. }
  293. else
  294. ASSERT(0);
  295. }
  296. void   FaceCommand::FillObjectLinesList(sgCObject* obj)
  297. {
  298. if (!obj)
  299. {
  300. ASSERT(0);
  301. return;
  302. }
  303. SG_OBJECT_TYPE ot = obj->GetType();
  304. SG_LINE  tmpLn;
  305. switch(ot) 
  306. {
  307. case SG_OT_LINE:
  308. {
  309. sgCLine* ln = reinterpret_cast<sgCLine*>(obj);
  310. memcpy(&tmpLn,ln->GetGeometry(),sizeof(SG_LINE));
  311. m_object_lines.push_back(tmpLn);
  312. }
  313. break;
  314. case SG_OT_ARC:
  315. {
  316. sgCArc* arcc = reinterpret_cast<sgCArc*>(obj);
  317. const int pc = arcc->GetPointsCount();
  318. const SG_POINT* pnts = arcc->GetPoints();
  319. if (pc>2)
  320. {
  321. for (int i=0;i<pc-1;i++)
  322. {
  323. tmpLn.p1 = pnts[i];
  324. tmpLn.p2 = pnts[i+1];
  325. m_object_lines.push_back(tmpLn);
  326. }
  327. }
  328. }
  329. break;
  330. case SG_OT_CIRCLE:
  331. {
  332. sgCCircle* ccir = reinterpret_cast<sgCCircle*>(obj);
  333. const int pc = ccir->GetPointsCount();
  334. const SG_POINT* pnts = ccir->GetPoints();
  335. if (pc>2)
  336. {
  337. for (int i=0;i<pc-1;i++)
  338. {
  339. tmpLn.p1 = pnts[i];
  340. tmpLn.p2 = pnts[i+1];
  341. m_object_lines.push_back(tmpLn);
  342. }
  343. }
  344. }
  345. break;
  346. case SG_OT_SPLINE:
  347. {
  348. sgCSpline* spl = reinterpret_cast<sgCSpline*>(obj);
  349. const int pc = spl->GetGeometry()->GetPointsCount();
  350. const SG_POINT* pnts = spl->GetGeometry()->GetPoints();
  351. if (pc>2)
  352. {
  353. for (int i=0;i<pc-1;i++)
  354. {
  355. tmpLn.p1 = pnts[i];
  356. tmpLn.p2 = pnts[i+1];
  357. m_object_lines.push_back(tmpLn);
  358. }
  359. }
  360. }
  361. break;
  362. case SG_OT_CONTOUR:
  363. {
  364. sgCContour* cntr = reinterpret_cast<sgCContour*>(obj);
  365. sgCObject*  curObj = cntr->GetChildrenList()->GetHead();
  366. while (curObj) 
  367. {
  368. FillObjectLinesList(curObj);
  369. curObj = cntr->GetChildrenList()->GetNext(curObj);
  370. }
  371. }
  372. break;
  373. default:
  374. ASSERT(0);
  375. }
  376. }
  377. bool   FaceCommand::IsIntersect(SG_LINE* ln)
  378. {
  379. ASSERT(ln);
  380. std::list<SG_LINE>::iterator Iter;
  381. SG_POINT tP;
  382. for ( Iter = m_object_lines.begin( ); Iter != m_object_lines.end( ); Iter++ )
  383. if (sgSpaceMath::IsSegmentsIntersecting((*Iter),false,*ln,true,tP))
  384. return true;
  385. return false;
  386. }
  387. HO_CORRECT_PATHS_RES  FaceCommand::GoodObjectForHole(const sgC2DObject* try_hole)
  388. {
  389. if (m_first_obj==NULL)
  390. return HO_UNKNOWN_ERROR;
  391. sgC2DObject::SG_2D_OBJECT_ORIENT out_or;
  392. SG_VECTOR out_Norm;
  393. double    out_D;
  394. sgC2DObject* out_obj = reinterpret_cast<sgC2DObject*>(m_first_obj);
  395. if (!out_obj->IsPlane(&out_Norm,&out_D))
  396. return HO_OUT_NOT_FLAT;
  397. out_or = out_obj->GetOrient(out_Norm);
  398. if (out_or == sgC2DObject::OO_ERROR) 
  399. {
  400. // 纤握紊 峦咆腿