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

Graph program

Development Platform:

Visual C++

  1. #include "stdafx.h"
  2. #include "BooleanOperation.h"
  3. #include "..//resource.h"
  4. int     bool_name_index = 1;
  5. BooleanCommand::BooleanCommand(IApplicationInterface* appI,BO_TYPE bt):
  6. m_app(appI)
  7. ,m_bo_type(bt)
  8. ,m_get_first_obj_panel(NULL)
  9. ,m_get_second_obj_panel(NULL)
  10. ,m_step(0)
  11. , m_first_obj(NULL)
  12. , m_second_obj(NULL)
  13. , m_sec_obj_sel(false)
  14. {
  15. ASSERT(m_app);
  16. }
  17. BooleanCommand::~BooleanCommand()
  18. {
  19. if (sgGetScene()->GetSelectedObjectsList()->GetCount()>0)
  20. {
  21. sgCObject*  curObj = sgGetScene()->GetSelectedObjectsList()->GetHead();
  22. while (curObj) 
  23. {
  24. curObj->Select(false);
  25. curObj = sgGetScene()->GetSelectedObjectsList()->GetNext(curObj);
  26. }
  27. }
  28. m_app->GetCommandPanel()->RemoveAllDialogs();
  29. m_app->GetViewPort()->InvalidateViewPort();
  30. }
  31. bool    BooleanCommand::PreTranslateMessage(MSG* pMsg)
  32. {
  33. if (pMsg->message==WM_KEYUP)
  34. return true;
  35. if (pMsg->message==WM_KEYDOWN || 
  36. pMsg->message==WM_CHAR)
  37. {
  38. if (pMsg->wParam==VK_RETURN)
  39. {
  40. OnEnter();
  41. return true;
  42. }
  43. if (pMsg->wParam==VK_ESCAPE)
  44. {
  45. m_app->StopCommander();
  46. return true;
  47. }
  48. if (m_step==0 && m_get_first_obj_panel)
  49. {
  50. m_get_first_obj_panel->GetWindow()->SendMessage(WM_CHAR,
  51. pMsg->wParam,
  52. pMsg->lParam);
  53. return true;
  54. }
  55. if (m_step==1 && m_get_second_obj_panel)
  56. {
  57. m_get_second_obj_panel->GetWindow()->SendMessage(WM_CHAR,
  58. pMsg->wParam,
  59. pMsg->lParam);
  60. return true;
  61. }
  62. }
  63. else
  64. {
  65. if (pMsg->hwnd == m_app->GetViewPort()->GetWindow()->m_hWnd)
  66. {
  67. switch(pMsg->message) 
  68. {
  69. case WM_MOUSEMOVE:
  70. MouseMove(pMsg->wParam,GET_X_LPARAM(pMsg->lParam),GET_Y_LPARAM(pMsg->lParam));
  71. return true;
  72. case WM_LBUTTONDOWN:
  73. LeftClick(pMsg->wParam,GET_X_LPARAM(pMsg->lParam),GET_Y_LPARAM(pMsg->lParam));
  74. return true;
  75. default:
  76. return false;
  77. }
  78. }
  79. }
  80. return false;
  81. }
  82. void   BooleanCommand::SendCommanderMessage(ICommander::COMMANDER_MESSAGE mes, void* params)
  83. {
  84. if (mes==ICommander::CM_SWITCH_ROLLUP_DIALOG)
  85. {
  86. ASSERT(params!=NULL);
  87. int   newActiveDlg = *(reinterpret_cast<int*>(params));
  88. ASSERT(newActiveDlg<=1);
  89. m_step = (unsigned int)newActiveDlg;
  90. if (newActiveDlg==0)
  91. {
  92. m_app->GetCommandPanel()->EnableRadio(1,false);
  93. m_message.LoadString(IDS_SEL_F_O);
  94. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  95. }
  96. else
  97. {
  98. m_message.LoadString(IDS_SEL_S_O);
  99. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  100. }
  101. m_app->GetViewPort()->InvalidateViewPort();
  102. return;
  103. }
  104. if (mes==ICommander::CM_SELECT_OBJECT)
  105. {
  106. ASSERT(params!=NULL);
  107. sgCObject* so = (sgCObject*)params;
  108. if(so->GetType()!=SG_OT_3D)
  109. {
  110. ASSERT(0);
  111. return;
  112. }
  113. so->Select(true);
  114. if (m_step==0)
  115. {
  116. if (m_first_obj!=so)
  117. {
  118. if (m_first_obj)
  119. {
  120. m_first_obj->Select(false);
  121. m_get_second_obj_panel->AddObject(m_first_obj,false);
  122. }
  123. m_first_obj = reinterpret_cast<sgC3DObject*>(so);
  124. m_get_second_obj_panel->RemoveObject(m_first_obj);
  125. }
  126. }
  127. else
  128. {
  129. if (m_second_obj!=so)
  130. {
  131. if (m_second_obj)
  132. m_second_obj->Select(false);
  133. m_second_obj = reinterpret_cast<sgC3DObject*>(so);
  134. }
  135. }
  136. m_app->GetViewPort()->InvalidateViewPort();
  137. }
  138. }
  139. static   bool isObjAddToList(sgCObject* obj)
  140. {
  141. if (obj->GetType()==SG_OT_3D)
  142. return true;
  143. return false;
  144. }
  145. void  BooleanCommand::Start()
  146. {
  147. SWITCH_RESOURCE
  148. m_app->GetCommandPanel()->RemoveAllDialogs();
  149. switch(m_bo_type) {
  150. case BO_INTSCT:
  151. m_message.LoadString(IDS_TOOLTIP_ZERO);
  152. break;
  153. case BO_UNION:
  154. m_message.LoadString(IDS_TOOLTIP_FIRST);
  155. break;
  156. case BO_SUB:
  157. m_message.LoadString(IDS_TOOLTIP_SECOND);
  158. break;
  159. case BO_IL:
  160. m_message.LoadString(IDS_TOOLTIP_THIRD);
  161. break;
  162. default:
  163. ASSERT(0);
  164. return;
  165. }
  166. m_app->StartCommander(m_message);
  167. CString lab;
  168. lab.LoadString(IDS_FIRST_OBJECT);
  169. m_get_first_obj_panel = reinterpret_cast<IGetObjectsPanel*>(m_app->
  170. GetCommandPanel()->
  171. AddDialog(IBaseInterfaceOfGetDialogs::GET_OBJECTS_DLG,lab,true));
  172. lab.LoadString(IDS_SECOND_OBJECT);
  173. m_get_second_obj_panel = reinterpret_cast<IGetObjectsPanel*>(m_app->
  174. GetCommandPanel()->
  175. AddDialog(IBaseInterfaceOfGetDialogs::GET_OBJECTS_DLG,lab,true));
  176. m_get_first_obj_panel->SetMultiselectMode(false);
  177. m_get_second_obj_panel->SetMultiselectMode(false);
  178. m_get_first_obj_panel->FillList(isObjAddToList);
  179. m_get_second_obj_panel->FillList(isObjAddToList);
  180. m_step=0;
  181. m_app->GetCommandPanel()->SetActiveRadio(0);
  182. m_message.LoadString(IDS_SEL_F_O);
  183. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  184. }
  185. void    BooleanCommand::BuildBoolean()
  186. {
  187. ASSERT(m_first_obj);
  188. ASSERT(m_second_obj);
  189. sgCGroup*  resGr = NULL;
  190. switch(m_bo_type) {
  191. case BO_INTSCT:
  192. resGr = sgBoolean::Intersection(*m_first_obj,*m_second_obj);
  193. break;
  194. case BO_UNION:
  195. resGr = sgBoolean::Union(*m_first_obj,*m_second_obj);
  196. break;
  197. case BO_SUB:
  198. resGr = sgBoolean::Sub(*m_first_obj,*m_second_obj);
  199. break;
  200. case BO_IL:
  201. resGr = sgBoolean::IntersectionContour(*m_first_obj,*m_second_obj);
  202. break;
  203. default:
  204. return;
  205. }
  206. if (!resGr)
  207. {
  208. m_message.LoadString(IDS_ERR_CANT_CREATE);
  209. m_app->PutMessage(IApplicationInterface::MT_ERROR,m_message);
  210. return;
  211. }
  212. const int ChCnt = resGr->GetChildrenList()->GetCount();
  213. sgCObject**  allChilds = (sgCObject**)malloc(ChCnt*sizeof(sgCObject*));
  214. if (!resGr->BreakGroup(allChilds))
  215. {
  216. ASSERT(0);
  217. }
  218. const int sz = resGr->GetChildrenList()->GetCount();
  219. ASSERT(sz==0);
  220. sgCObject::DeleteObject(resGr);
  221. SWITCH_RESOURCE
  222. CString nm;
  223. nm.LoadString(IDS_BOOL_NAME);
  224. CString nmInd;
  225. CString curName;
  226. sgGetScene()->StartUndoGroup();
  227. for (int i=0;i<ChCnt;i++)
  228. {
  229. sgGetScene()->AttachObject(allChilds[i]);
  230. nmInd.Format("%i",bool_name_index);
  231. curName = nm+nmInd;
  232. allChilds[i]->SetName(curName);
  233. if (allChilds[i]->GetType()==SG_OT_3D)
  234. {
  235. sgC3DObject* cccO = reinterpret_cast<sgC3DObject*>(allChilds[i]);
  236. const SG_MATERIAL* mmm = m_first_obj->GetMaterial();
  237. if (mmm)
  238. cccO->SetMaterial(*mmm);
  239. }
  240. bool_name_index++;
  241. m_app->ApplyAttributes(allChilds[i]);
  242. }
  243. free(allChilds);
  244. sgGetScene()->DetachObject(m_first_obj);
  245. sgGetScene()->DetachObject(m_second_obj);
  246. sgGetScene()->EndUndoGroup();
  247. if (sgGetScene()->GetSelectedObjectsList()->GetCount()>0)
  248. {
  249. sgCObject*  curObj = sgGetScene()->GetSelectedObjectsList()->GetHead();
  250. while (curObj) 
  251. {
  252. curObj->Select(false);
  253. curObj = sgGetScene()->GetSelectedObjectsList()->GetNext(curObj);
  254. }
  255. }
  256. }
  257. void  BooleanCommand::MouseMove(unsigned int nFlags,int pX,int pY)
  258. {
  259. //if (m_step==0)
  260. {
  261. if (!(nFlags & MK_LBUTTON))
  262. {
  263. int snapSz = m_app->GetViewPort()->GetSnapSize();
  264. m_app->GetViewPort()->SetHotObject(m_app->GetViewPort()->GetTopObjectByType(
  265. m_app->GetViewPort()->GetHitsInRect(CRect(pX-snapSz, pY-snapSz,
  266. pX+snapSz, pY+snapSz)),SG_OT_3D));
  267. m_app->GetViewPort()->InvalidateViewPort();
  268. }
  269. }
  270. /*else
  271. {
  272. }*/
  273. }
  274. void  BooleanCommand::LeftClick(unsigned int nFlags,int pX,int pY)
  275. {
  276. if (m_step==0)
  277. {
  278. if (m_app->GetViewPort()->GetHotObject())
  279. {
  280. m_app->GetViewPort()->GetHotObject()->Select(true);
  281. if (m_first_obj!=m_app->GetViewPort()->GetHotObject())
  282. {
  283. if (m_first_obj)
  284. {
  285. m_first_obj->Select(false);
  286. m_get_second_obj_panel->AddObject(m_first_obj,false);
  287. }
  288. m_first_obj = reinterpret_cast<sgC3DObject*>(m_app->
  289. GetViewPort()->GetHotObject());
  290. m_get_second_obj_panel->RemoveObject(m_first_obj);
  291. }
  292. m_step++;
  293. m_app->GetCommandPanel()->SetActiveRadio(1);
  294. m_app->GetViewPort()->InvalidateViewPort();
  295. }
  296. }
  297. else
  298. {
  299. sgCObject* co = m_app->GetViewPort()->GetHotObject();
  300. if (co && co!=m_first_obj)
  301. {
  302. co->Select(true);
  303. if (m_second_obj!=co)
  304. {
  305. if (m_second_obj)
  306. m_second_obj->Select(false);
  307. m_second_obj = reinterpret_cast<sgC3DObject*>(co);
  308. BuildBoolean();
  309. m_step=0;
  310. m_app->GetCommandPanel()->SetActiveRadio(0);
  311. m_message.LoadString(IDS_SEL_F_O);
  312. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  313. m_app->GetViewPort()->InvalidateViewPort();
  314. }
  315. }
  316. }
  317. }
  318. void  BooleanCommand::Draw()
  319. {
  320. }
  321. void  BooleanCommand::OnEnter()
  322. {
  323. if (m_step==0)
  324. {
  325. if (!m_first_obj)
  326. return;
  327. m_step++;
  328. m_app->GetCommandPanel()->SetActiveRadio(1);
  329. m_app->GetViewPort()->InvalidateViewPort();
  330. }
  331. else
  332. {
  333. if (!m_first_obj || !m_second_obj)
  334. return;
  335. BuildBoolean();
  336. m_step=0;
  337. m_app->GetCommandPanel()->SetActiveRadio(0);
  338. m_message.LoadString(IDS_SEL_F_O);
  339. m_app->PutMessage(IApplicationInterface::MT_MESSAGE,m_message);
  340. m_app->GetViewPort()->InvalidateViewPort();
  341. }
  342. }
  343. unsigned int  BooleanCommand::GetItemsCount()
  344. {
  345. return 0;
  346. }
  347. void         BooleanCommand::GetItem(unsigned int, CString&)
  348. {
  349. }
  350. void     BooleanCommand::GetItemState(unsigned int, bool&, bool&)
  351. {
  352. }
  353. HBITMAP   BooleanCommand::GetItemBitmap(unsigned int)
  354. {
  355. return NULL;
  356. }
  357. void         BooleanCommand::Run(unsigned int)
  358. {
  359. }