systemdatautils.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. """PipeNetwork Data .
  2. """
  3. import test
  4. from systemrelation import systemutils
  5. from systemrelation import graph_model
  6. def get_dicdata(sql,dic):
  7. """
  8. Request db for data through sql.
  9. Convert data to dict.
  10. :param sql:
  11. :param dic:
  12. :return:
  13. """
  14. data=test.get_data(sql)
  15. dic_data=[dict(zip(dic,item)) for item in data]
  16. return list(dic_data)
  17. def get_project_models(project_id):
  18. '''
  19. Get all the models in the project.
  20. project-->folder-->model.
  21. :param project_id:
  22. :return:
  23. '''
  24. element_data = []
  25. sql ="select " \
  26. "file.id as file_id" \
  27. ",file.original_name as file_name" \
  28. ",file.status as file_status" \
  29. ",floor.id as floor_id" \
  30. ",floor.floor_name as floor_name" \
  31. ",folder.id as folder_id" \
  32. ",folder.name as folder_name" \
  33. ",folder.project_id as project_id " \
  34. "from revit.model_file as file " \
  35. "inner join " \
  36. "revit.model_floor as floor " \
  37. "on ( file.id=floor.current_model_id) " \
  38. "inner join " \
  39. "revit.model_folder as folder " \
  40. "on (floor.folder_id=folder.id )" \
  41. "where folder.project_id='%s'" \
  42. "and folder.id='bbe510dbe26011e999b69b669ea08505'"\
  43. % (project_id)
  44. #"and file.status=4 "\
  45. COMPONENT_KEYS = [
  46. 'file_id',
  47. 'file_name',
  48. 'file_status',
  49. 'floor_id',
  50. 'floor_name',
  51. 'folder_id',
  52. 'folder_name',
  53. 'project_id'
  54. ]
  55. element_data.extend(get_dicdata(sql, COMPONENT_KEYS))
  56. element_data = list(map(systemutils.dic2obj, element_data))
  57. return element_data
  58. def get_element_data(model_id):
  59. """
  60. Cache all Element from Db
  61. :return:
  62. """
  63. element_data=[]
  64. component_sql="select * from revit.component where model_id='%s'"%(model_id)
  65. COMPONENT_KEYS = [
  66. 'id',
  67. 'model_id',
  68. 'local_id',
  69. 'local_name',
  70. 'family',
  71. 'location',
  72. 'outline',
  73. 'source_id',
  74. 'name',
  75. 'revit_id',
  76. 'parameters',
  77. 'type',
  78. 'tag',
  79. 'owner',
  80. 'last_update',
  81. 'create_time',
  82. 'owner_id',
  83. 'family_name',
  84. 'owner_source_id'
  85. ]
  86. element_data.extend(get_dicdata(component_sql,COMPONENT_KEYS))
  87. equipment_sql="select * from revit.equipment where model_id='%s'"%(model_id)
  88. EQUIPMENT_KEYS = [
  89. 'id',
  90. 'model_id',
  91. 'local_id',
  92. 'local_name',
  93. 'family',
  94. 'location',
  95. 'outline',
  96. 'last_update',
  97. 'create_time',
  98. 'source_id',
  99. 'name',
  100. 'parameters',
  101. 'revit_id',
  102. 'type',
  103. 'tag',
  104. 'family_name',
  105. ]
  106. element_data.extend(get_dicdata(equipment_sql,EQUIPMENT_KEYS))
  107. other_sql="select * from revit.other where model_id='%s'"%(model_id)
  108. OTHER_KEYS = [
  109. 'id',
  110. 'model_id',
  111. 'connected_ids',
  112. 'source_id',
  113. 'name',
  114. 'revit_id',
  115. 'type',
  116. 'create_time',
  117. 'last_update',
  118. 'location',
  119. 'outline'
  120. ]
  121. element_data.extend(get_dicdata(other_sql,OTHER_KEYS))
  122. pipe_sql = "select * from revit.pipe where model_id='%s'" % (model_id)
  123. PIPE_KEYS = [
  124. 'id',
  125. 'model_id',
  126. 'location',
  127. 'outline',
  128. 'connected_ids',
  129. 'mep_system_type',
  130. 'diameter',
  131. 'source_id',
  132. 'name',
  133. 'revit_id',
  134. 'type',
  135. 'last_update',
  136. 'create_time',
  137. 'parameters'
  138. ]
  139. element_data.extend(get_dicdata(pipe_sql, PIPE_KEYS))
  140. duct_sql="select * from revit.duct where model_id='%s'"%(model_id)
  141. DUCT_KEYS = [
  142. 'id',
  143. 'model_id',
  144. 'location',
  145. 'outline',
  146. 'connected_ids',
  147. 'mep_system_type',
  148. 'shape',
  149. 'diameter',
  150. 'width',
  151. 'height',
  152. 'source_id',
  153. 'revit_id',
  154. 'name',
  155. 'type',
  156. 'last_update',
  157. 'create_time',
  158. 'parameters'
  159. ]
  160. element_data.extend(get_dicdata(duct_sql,DUCT_KEYS))
  161. element_data=list(map(systemutils.dic2obj, element_data))
  162. return element_data
  163. def get_connectors_data(model_id, system_name, domain):
  164. """Cache all connector from Db。"""
  165. connector_data=[]
  166. connector_sql = "SELECT * FROM revit.connector where model_id='%s' and mep_system_type='%s' and domain='%s'" % (model_id,system_name,domain)
  167. print(connector_sql)
  168. CONNECTOR_KEYS = [
  169. 'id',
  170. 'model_id',
  171. 'belong',
  172. 'belong_type',
  173. 'origin',
  174. 'domain',
  175. 'description',
  176. 'connected',
  177. 'owner',
  178. 'connected_ids',
  179. 'mep_system_type',
  180. 'source_id',
  181. 'revit_id',
  182. 'type',
  183. 'create_time',
  184. 'last_update',
  185. 'connect_id_json'
  186. ]
  187. connector_data.extend(get_dicdata(connector_sql, CONNECTOR_KEYS))
  188. connector_data=list(map(systemutils.dic2obj,connector_data))
  189. return connector_data
  190. def save_project_data(project_graph,project_id):
  191. """
  192. Sava project graph data.
  193. :param floor_graph:
  194. :return:
  195. """
  196. del_project_data(project_id)
  197. # Create
  198. block_id=0
  199. for g in project_graph.groupedsystemgraphs:
  200. for s in g.systemgraphs:
  201. for e in s.system_edges:
  202. save_edge_data(e, block_id)
  203. for e in g.connectedges:
  204. save_edge_data(e,block_id)
  205. block_id+=1
  206. def save_edge_data(edge,block_id):
  207. """
  208. Save edge info using sql
  209. :param edge:
  210. :param block_id:
  211. :return:
  212. """
  213. vertex1=edge.start_vertex
  214. vertex2=edge.end_vertex
  215. c1=vertex1.system_data
  216. c2=vertex2.system_data
  217. model1=systemutils.get_specify_type_parent(vertex1,graph_model.FloorGraph).model
  218. model2 = systemutils.get_specify_type_parent(vertex2, graph_model.FloorGraph).model
  219. sql="insert into revit_calc.connected_block" \
  220. "(id1,type1,model_id1,id2,type2,model_id2,block_id,project_id) values " \
  221. "('%s','%s','%s','%s','%s','%s','%s','%s')"\
  222. %(c1.id,c1.type,model1.file_id,c2.id,c2.type,model2.file_id,block_id,model1.project_id)
  223. data = test.save_data(sql)
  224. def del_project_data(project_id):
  225. """
  226. Before save.delete old data.
  227. :param project_id:
  228. :return:
  229. """
  230. sql = "delete from revit_calc.connected_block where project_id='%s'"% (project_id)
  231. data = test.save_data(sql)
  232. def get_connected_block_data(project_id,block_id):
  233. '''
  234. Get all the connected_block .
  235. :param project_id:
  236. :param block_id:
  237. :return:
  238. '''
  239. element_data = []
  240. sql = "select * from revit_calc.connected_block " \
  241. "where project_id='%s'" \
  242. "and block_id='%s'" \
  243. % (project_id,block_id)
  244. sql = "select * from revit_calc.connected_block " \
  245. "where project_id='%s'" \
  246. "order by cast(block_id as int) asc"\
  247. % (project_id)
  248. KEYS = [
  249. 'id',
  250. 'id1',
  251. 'type1',
  252. 'model_id1',
  253. 'id2',
  254. 'type2',
  255. 'model_id2',
  256. 'direction',
  257. 'block_id',
  258. 'project_id',
  259. ]
  260. element_data.extend(get_dicdata(sql, KEYS))
  261. element_data = list(map(systemutils.dic2obj, element_data))
  262. return element_data
  263. def get_connected_block_source_data(project_id,block_id):
  264. '''
  265. Get all the connected_block source .
  266. :param project_id:
  267. :param block_id:
  268. :return:
  269. '''
  270. element_data = []
  271. sql = "select * from revit_calc.connected_block_source " \
  272. "where project_id='%s'" \
  273. "and block_id='%s'" \
  274. % (project_id,block_id)
  275. KEYS = [
  276. 'id',
  277. 'project_id',
  278. 'block_id',
  279. 'source_id',
  280. ]
  281. element_data.extend(get_dicdata(sql, KEYS))
  282. element_data = list(map(systemutils.dic2obj, element_data))
  283. return element_data
  284. def update_connected_block_data(block_datas):
  285. '''
  286. update connected_block direction by id.
  287. :param block_datas:
  288. :return:
  289. '''
  290. for block_data in block_datas:
  291. sql="update revit_calc.connected_block set direction='%s' ,block_id='%s' where id='%s'"%(block_data.direction,block_data.block_id,block_data.id)
  292. data = test.save_data(sql)
  293. if '__main__'==__name__:
  294. # model_id="cf4e11a0e25811e999b6dbc4c75fa946"
  295. # system_name="喷淋管"
  296. # domain="DomainPiping"
  297. # g=get_connectors_data(model_id,system_name,domain)
  298. # print (g)
  299. project_id='Pj1101010015'
  300. models=get_project_models(project_id)
  301. print(models)