appmodel.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #include "appmodel.h"
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "ha_part.h"
  6. #include "ha_part_util.h"
  7. #include "roll_hm.hxx"
  8. // HOOPS includes
  9. #include "hc.h"
  10. #include "HStream.h"
  11. #include "HOpcodeShell.h"
  12. #include "HUtility.h"
  13. #include <part_api.hxx>
  14. #include "HTools.h"
  15. #include "ha_rend_options.h"
  16. #include "HIOManager.h"
  17. #include "varray.h"
  18. #include "HBaseView.h"
  19. #include "kernapi.hxx"
  20. #include <QtCore/QDebug>
  21. AppModel::AppModel()
  22. {
  23. m_bSolidModel = true;
  24. SetBRepGeometry(true);
  25. m_mi=0;
  26. // initialize_ha_part();
  27. m_pHAPart = ACIS_NEW HA_Part();
  28. api_part_set_distribution_mode( TRUE );//设置历史分布打开或关闭。
  29. // distributed_history(TRUE,TRUE);
  30. char buffer[1024];
  31. char pbuffer[POINTER_BUFFER_SIZE];
  32. PART* pPart = m_pHAPart->GetPart();
  33. sprintf(buffer,"?Include Library/%s",ptoax(pPart->history_stream(), pbuffer));
  34. qDebug() << "appmodde buffer" << buffer;
  35. m_ModelKey = HA_KCreate_Segment(0, buffer);
  36. HA_Set_Rendering_Options("segment pattern = ?Include Library/history/entity");//提供对如何通过HOOPS/ACIS网格管理器将ACIS对象渲染为HOOPS对象的控制
  37. }
  38. AppModel::~AppModel()
  39. {
  40. // Model cleanup : delete all the entities from the
  41. // partition associated with this model
  42. DeleteAllEntities();
  43. if(m_pHAPart)
  44. {
  45. ACIS_DELETE m_pHAPart;
  46. m_pHAPart = NULL;
  47. }
  48. // terminate_ha_part();
  49. if(m_mi)
  50. {
  51. delete m_mi;
  52. m_mi = 0;
  53. }
  54. }
  55. // Delete all the entities in the current model from the
  56. // modeler and associated HOOPS geometry
  57. void AppModel::DeleteAllEntities()
  58. {
  59. // HA_Delete_Entity_Geometry(m_entityList);
  60. // api_del_entity_list(m_entityList);
  61. // m_entityList.clear();
  62. ENTITY_LIST elist;
  63. m_pHAPart->GetPart()->get_entities(elist);
  64. elist.init();
  65. ENTITY* pEnt = NULL;
  66. while((pEnt= elist.next())!= NULL)
  67. {
  68. m_pHAPart->RemoveEntity(pEnt);
  69. }
  70. api_del_entity_list(elist);
  71. elist.clear();
  72. }
  73. void AppModel::AddAcisEntity( ENTITY* entity )
  74. {
  75. if(m_pHAPart)
  76. {
  77. api_facet_entity( entity);
  78. m_pHAPart->AddEntity(entity);
  79. }
  80. }
  81. void AppModel::AddAcisEntitiesToPart( ENTITY_LIST& list)
  82. {
  83. if(m_pHAPart)
  84. {
  85. api_facet_bodies( list);
  86. ENTITY* entity = list.first();
  87. while (entity)
  88. {
  89. m_pHAPart->AddEntity(entity);
  90. entity = list.next();
  91. }
  92. }
  93. }
  94. void AppModel::RemoveAcisEntity( ENTITY* entity )
  95. {
  96. if(m_pHAPart)
  97. {
  98. m_pHAPart->RemoveEntity(entity);
  99. }
  100. }
  101. void AppModel::UpdateAcisEntity( ENTITY* entity )
  102. {
  103. if(m_pHAPart)
  104. {
  105. m_pHAPart->UpdateEntity(entity);
  106. }
  107. }
  108. void AppModel::AddAcisEntities( const ENTITY_LIST& entityList )
  109. {
  110. if(m_pHAPart)
  111. {
  112. m_pHAPart->AddEntities(entityList);
  113. }
  114. }
  115. void AppModel::DeleteAcisEntity( ENTITY* entity)
  116. {
  117. // delete from ACIS
  118. if (m_pHAPart)
  119. {
  120. m_pHAPart->RemoveEntity(entity);
  121. }
  122. api_del_entity(entity);
  123. }
  124. void AppModel::DeleteAcisEntities( ENTITY_LIST& elist)
  125. {
  126. // delete from HOOPS
  127. // HA_Delete_Entity_Geometry(elist);
  128. // delete from ACIS
  129. elist.init();
  130. ENTITY* pEnt = NULL;
  131. while((pEnt = elist.next()) != NULL)
  132. {
  133. m_pHAPart->RemoveEntity(pEnt);
  134. }
  135. api_del_entity_list(elist);
  136. }
  137. // our application-specific read function
  138. HFileInputResult AppModel::Read(const char * FileName)
  139. {
  140. unsigned int i;
  141. HFileInputResult success = InputOK;
  142. // get the file extension
  143. char extension[120];
  144. HUtility::FindFileNameExtension(FileName, extension);
  145. // read the file into the model object's model segment
  146. HC_Open_Segment_By_Key(m_ModelKey);
  147. HC_Open_Segment("main");
  148. if (streq(extension,"sat"))
  149. {
  150. m_bSolidModel = true;
  151. SetBRepGeometry(true);
  152. // m_pHAPart->LoadFile(FileName,true);
  153. int depth = 0;
  154. outcome result;
  155. api_part_start_state(depth);
  156. {
  157. API_BEGIN
  158. {
  159. ENTITY_LIST elist;
  160. FILE * fp = NULL;
  161. logical res = TRUE;
  162. fp = fopen(FileName, "r");
  163. res = (fp != NULL);
  164. if (res)
  165. {
  166. result = api_restore_entity_list(fp, TRUE, elist);
  167. res = result.ok();
  168. }
  169. if (fp)
  170. fclose(fp);
  171. if (res)
  172. {
  173. ENTITY* entity = 0;
  174. elist.init();
  175. while ((entity = elist.next()) != 0)
  176. AddAcisEntity(entity);
  177. }
  178. }
  179. API_END
  180. }
  181. api_part_note_state(result, depth);
  182. }
  183. else if (streq(extension,"sab"))
  184. {
  185. m_bSolidModel = true;
  186. SetBRepGeometry(true);
  187. // m_pHAPart->LoadFile(FileName,false);
  188. int depth = 0;
  189. outcome result;
  190. api_part_start_state(depth);
  191. {
  192. API_BEGIN
  193. {
  194. ENTITY_LIST elist;
  195. FILE * fp = NULL;
  196. logical res = TRUE;
  197. fp = fopen(FileName, "r");
  198. res = (fp != NULL);
  199. if (res)
  200. {
  201. result = api_restore_entity_list(fp, FALSE, elist);
  202. res = result.ok();
  203. }
  204. if (fp)
  205. fclose(fp);
  206. if (res)
  207. {
  208. ENTITY* entity = 0;
  209. elist.init();
  210. while ((entity = elist.next()) != 0)
  211. AddAcisEntity(entity);
  212. }
  213. }
  214. API_END
  215. }
  216. api_part_note_state(result, depth);
  217. }
  218. else if (streq(extension,"asf"))
  219. {
  220. m_bSolidModel = true;
  221. SetBRepGeometry(true);
  222. HStreamFileToolkit tk;
  223. // tk.SetOpcodeHandler (TKE_Comment, new TK_PSComment(this));
  224. TK_Status status = HTK_Read_Stream_File( FileName, &tk );
  225. if( status != TK_Complete )
  226. success = InputFail;
  227. }
  228. else
  229. {
  230. // No special read - let the base class handle
  231. m_bSolidModel = false;
  232. success = HBaseModel::Read(FileName);
  233. }
  234. HC_Close_Segment();
  235. HC_Close_Segment();
  236. return success;
  237. }
  238. // our application-specific write function
  239. bool AppModel::Write(const char * FileName, HBaseView * view,
  240. int version, int width, int height)
  241. {
  242. unsigned int i;
  243. bool success = true;
  244. // get the file extension
  245. char extension[120];
  246. HUtility::FindFileNameExtension(FileName, extension);
  247. if (streq(extension,"sat"))
  248. {
  249. ENTITY_LIST savelist;
  250. GetEntityList(savelist);
  251. if (savelist.count() > 0)
  252. HA_Write_Sat_File(FileName, savelist);
  253. }
  254. else if (streq(extension,"png"))
  255. {
  256. HOutputHandlerOptions options;
  257. VCharArray width_str(128), height_str(128), temp(128), xpixels_str(128);
  258. HC_Open_Segment_By_Key(view->GetViewKey());
  259. /* Get the size in milimeters. */
  260. HC_Show_Device_Info(".", "size", temp);
  261. HC_Parse_String(temp, ",", 0, width_str);
  262. HC_Parse_String(temp, ",", 1, height_str);
  263. /* Get the pixels in the x direction so we can find DPI. */
  264. HC_Show_Device_Info(".", "pixels", temp);
  265. HC_Parse_String(temp, ",", 0, xpixels_str);
  266. HC_Close_Segment();
  267. /* Save off the window width and height in inches. */
  268. options.WindowWidth(atof(width_str)/2.54f);
  269. options.WindowHeight(atof(height_str)/2.54f);
  270. /* DPI = xpixels / width in inches; */
  271. options.ImageDpi(atoi(xpixels_str) / options.WindowWidth());
  272. HBaseModel::WriteWithOptions(FileName, view, &options);
  273. }
  274. else
  275. {
  276. if(!HBaseModel::Write(FileName, view, width, height))
  277. success = false;
  278. }
  279. return success;
  280. }
  281. logical AppModel::Undo()
  282. {
  283. return m_pHAPart->RollBack();
  284. }
  285. logical AppModel::Redo()
  286. {
  287. return m_pHAPart->RollForward();
  288. }
  289. logical AppModel::CanUndo()
  290. {
  291. PART* pPart = m_pHAPart->GetPart();
  292. HISTORY_STREAM* hs = pPart->history_stream();
  293. if (hs)
  294. return (hs->can_roll_back()!=0);
  295. return false;
  296. }
  297. logical AppModel::CanRedo()
  298. {
  299. PART* pPart = m_pHAPart->GetPart();
  300. HISTORY_STREAM* hs = pPart->history_stream();
  301. if (hs)
  302. return (hs->can_roll_forward()!=0);
  303. return false;
  304. }
  305. void AppModel::GetEntityList(ENTITY_LIST& elist)
  306. {
  307. if (m_pHAPart != NULL)
  308. {
  309. PART* pPart = m_pHAPart->GetPart();
  310. if(pPart != NULL)
  311. {
  312. pPart->get_entities(elist);
  313. }
  314. }
  315. }