appmodel.cpp 9.1 KB

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