Browse Source

functions

jxing 4 years ago
parent
commit
d4e120b2af

+ 306 - 0
src/affected_space/function.py

@@ -927,3 +927,309 @@ except Exception as e:
 $BODY$
   LANGUAGE plpython3u VOLATILE
   COST 100
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+CREATE OR REPLACE FUNCTION "public"."sys_block"("project_id" varchar, "building_id" varchar, "system_name" varchar, "domain" varchar)
+RETURNS "pg_catalog"."bool"
+AS $BODY$
+
+from relations.src.system_relation import systemutils, graph_model
+
+# 获取楼层上的ModelId信息点
+def get_models(project_id, building_id):
+    sql = "select " \
+          "id,name,local_id,local_name,sequence_id,model_id,building_id,project_id " \
+          "from floor " \
+          "where project_id=$1 " \
+          "and ($2 is null or building_id=$2) " \
+          "order by sequence_id desc"
+    join_plan = plpy.prepare(sql, ["text", "text"])
+    data = join_plan.execute([project_id, building_id])
+    models = systemutils.sqldata2objlist(data)
+    return models
+
+
+def get_connectors(model_id, system_name, domain):
+    sql = "SELECT * FROM revit.connector where model_id=$1 and mep_system_type=$2 and domain=$3"
+    join_plan = plpy.prepare(sql, ["text", "text", "text"])
+    data = join_plan.execute([model_id, system_name, domain])
+    floor_connectors = systemutils.sqldata2objlist(data)
+    return floor_connectors
+
+
+def get_elements(model_id):
+    floor_elements = []
+    sql = "select * from revit.equipment where model_id=$1"
+    join_plan = plpy.prepare(sql, ["text"])
+    data = join_plan.execute([model_id])
+    floor_elements.extend(systemutils.sqldata2objlist(data))
+
+    sql = "select * from revit.component where model_id=$1"
+    join_plan = plpy.prepare(sql, ["text"])
+    data = join_plan.execute([model_id])
+    floor_elements.extend(systemutils.sqldata2objlist(data))
+
+    sql = "select * from revit.other where model_id=$1"
+    join_plan = plpy.prepare(sql, ["text"])
+    data = join_plan.execute([model_id])
+    floor_elements.extend(systemutils.sqldata2objlist(data))
+
+    sql = "select * from revit.join_object where model_id=$1"
+    join_plan = plpy.prepare(sql, ["text"])
+    data = join_plan.execute([model_id])
+    floor_elements.extend(systemutils.sqldata2objlist(data))
+
+    sql = "select * from revit.pipe where model_id=$1"
+    join_plan = plpy.prepare(sql, ["text"])
+    data = join_plan.execute([model_id])
+    floor_elements.extend(systemutils.sqldata2objlist(data))
+
+    sql = "select * from revit.duct where model_id=$1"
+    join_plan = plpy.prepare(sql, ["text"])
+    data = join_plan.execute([model_id])
+    floor_elements.extend(systemutils.sqldata2objlist(data))
+    return floor_elements
+
+
+def delete_block_data(project_id, building_id, system_name, domain):
+    sql = "delete from revit_calc.connected_block " \
+          "where project_id=$1" \
+          "and mep_system_type=$2" \
+          "and domain=$3" \
+          "and ($4 is null or building_id=$4)"
+    join_plan = plpy.prepare(sql, ["text", "text", "text", "text"])
+    join_plan.execute([project_id, system_name, domain, building_id])
+
+
+def save_edge_data(edge, block_id, system_name, domain, building_id):
+    vertex1 = edge.start_vertex
+    vertex2 = edge.end_vertex
+    c1 = vertex1.system_data
+    c2 = vertex2.system_data
+    floor1 = vertex1.get_parent_floor()
+    floor2 = vertex2.get_parent_floor()
+    model1 = floor1.model
+    model2 = floor2.model
+    sql = "insert into revit_calc.connected_block" \
+          "(id1,type1,model_id1,id2,type2,model_id2,block_id,project_id,mep_system_type,domain,building_id) values " \
+          "($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)"
+    join_plan = plpy.prepare(sql,
+                             ["text", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text"])
+    join_plan.execute([c1.id, c1.type, model1.model_id, c2.id, c2.type, model2.model_id, block_id, model1.project_id,
+                       floor1.system_name, floor1.domain, building_id])
+
+
+def get_real_model_id(models):
+    sql_str = ""
+    for model in models:
+        sql_str += '\'' + model.model_id + '\','
+    if sql_str.endswith(','):
+        sql_str = sql_str[0:-1]
+    real_model_id_plan = plpy.prepare("select mid.id, file.id fid, file.status from (select id, current_model_id from revit.model_floor where id in ({0})) mid left join revit.model_file file on mid.current_model_id = file.id".format(sql_str), [])
+    real_model_ids = real_model_id_plan.execute([])
+    floor_model_dict = dict()
+    for row in real_model_ids:
+        floor_id = row.get('id')
+        model_id = row.get('fid')
+        status = row.get('status')
+        if status == 4:
+            floor_model_dict[floor_id] = model_id
+    for model in models:
+        if model.model_id not in floor_model_dict:
+            del models[model.model_id]
+        else:
+            model.model_id = floor_model_dict[model.model_id]
+
+
+try:
+    # 将下面对数据库的操作作为一个事务, 出异常则自动rollback
+    with plpy.subtransaction():
+        models = get_models(project_id, building_id)
+        floorgraphs = []
+        plpy.info(len(models))
+        get_real_model_id(models)
+        for model in models:
+            floor_connectors = get_connectors(model.model_id, system_name, domain)
+            floor_elements = get_elements(model.model_id)
+            g = graph_model.FloorGraph(model, system_name, domain, floor_connectors, floor_elements)
+            plpy.info("reached")
+            g.get_floor_graphs()
+            floorgraphs.append(g)
+        projectgraph = graph_model.combine_systemgraph(project_id, floorgraphs)
+
+        delete_block_data(project_id, building_id, system_name, domain)
+        block_id = 0
+        for g in projectgraph.groupedsystemgraphs:
+            for s in g.systemgraphs:
+                for e in s.system_edges:
+                    save_edge_data(e, block_id, system_name, domain, building_id)
+            for e in g.connectedges:
+                save_edge_data(e, block_id, system_name, domain, building_id)
+            block_id += 1
+except Exception as e:
+    plpy.warning(e)
+    return False
+else:
+    return True
+$BODY$
+LANGUAGE plpython3u VOLATILE
+COST 100
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+CREATE OR REPLACE FUNCTION "public"."sys_direction"("project_id" varchar, "building_id" varchar, "block_id" varchar, "system_name" varchar, "domain" varchar, "is_source" bool=true)
+  RETURNS "pg_catalog"."bool" AS $BODY$
+from relations.src.system_relation import calc_flowdirection, systemdatautils,systemutils
+
+def get_connected_block_data(project_id,building_id,block_id,system_name,domain):
+  sql ="select * from revit_calc.connected_block " \
+		  "where project_id=$1" \
+		  "and block_id=$2" \
+		  "and mep_system_type=$3" \
+		  "and domain=$4" \
+		  "and ($5 is null or building_id=$5)" \
+		  "order by cast(depth as int) asc"
+  join_plan=plpy.prepare(sql,["text","text","text","text","text"])
+  data=join_plan.execute([project_id,block_id,system_name,domain,building_id])
+  objs=systemutils.sqldata2objlist(data)
+  return objs
+
+def get_connected_block_source_data(project_id,building_id,block_id,system_name,domain,is_source):
+  sql = "select * from revit_calc.connected_block_source " \
+		  "where project_id=$1" \
+		  "and block_id=$2" \
+		  "and mep_system_type=$3" \
+		  "and domain=$4" \
+		  "and ($5 is null or building_id=$5)" \
+		  "and is_source=$6"
+  join_plan=plpy.prepare(sql,["text","text","text","text","text","boolean"])
+  data=join_plan.execute([project_id,block_id,system_name,domain,building_id,is_source])
+  objs=systemutils.sqldata2objlist(data)
+  return objs
+
+def update_connected_block_data(block_data):
+  direction=block_data.direction
+  depth=block_data.depth
+  id=block_data.id
+  sql="update revit_calc.connected_block set direction=$1,depth=$2 where id=$3"
+  plpy.info(sql+str(direction)+str(depth)+str(id))
+  join_plan=plpy.prepare(sql,['int','int','int'])
+  join_plan.execute([direction,depth,id])
+
+try:
+  # 将下面对数据库的操作作为一个事务, 出异常则自动rollback
+  with plpy.subtransaction():
+    block_datas=get_connected_block_data(project_id,building_id, block_id,system_name,domain)
+    source_datas=get_connected_block_source_data(project_id,building_id, block_id,system_name,domain,is_source)
+    block_datas= calc_flowdirection.calc(block_datas, source_datas)
+    for block_data in block_datas:
+      update_connected_block_data(block_data)
+except Exception as e:
+    plpy.warning(e)
+    return False
+else:
+    return True
+$BODY$
+  LANGUAGE plpython3u VOLATILE
+  COST 100
+
+
+
+
+
+
+
+
+
+
+
+
+
+CREATE OR REPLACE FUNCTION "pointconfig"."rel_description_param_key"("word" _varchar)
+  RETURNS "pg_catalog"."text" AS $BODY$
+
+import json
+from kwextraction.extraction import extractor
+
+
+def get_key_words(text_list):
+    return extractor(text_list)
+
+try:
+    arr = list()
+    for item in get_key_words(word):
+        item.pop('str')
+        arr.append(item)
+    return json.dumps(arr, sort_keys=True, indent=0, ensure_ascii=False)
+except Exception as e:
+    plpy.warning(e)
+    return "error"
+$BODY$
+  LANGUAGE plpython3u VOLATILE
+  COST 100
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+ 22 - 1
src/system_relation/systemdatautils.py

@@ -16,7 +16,27 @@ def get_dicdata(sql,dic):
 	data=test.get_data(sql)
 	dic_data=[dict(zip(dic,item)) for item in data]
 	return list(dic_data)
-def get_project_models(project_id,building_id):
+
+def get_real_model_id(models):
+	sql_str = ""
+	for model in models:
+		sql_str += '\'' + model.model_id + '\','
+	if sql_str.endswith(','):
+		sql_str = sql_str[0:-1]
+	sql = "select mid.id, file.id fid, file.status from (select id, current_model_id from revit.model_floor where id in ({0})) mid left join revit.model_file file on mid.current_model_id = file.id".format(
+		sql_str)
+	MODEL_KEYS = [
+		'id',
+		'fid',
+		'status'
+	]
+	element_data = []
+	element_data.extend(get_dicdata(sql, MODEL_KEYS))
+	element_data = list(map(systemutils.dic2obj, element_data))
+	return element_data
+
+
+def get_project_models(project_id, building_id):
 	'''
 	Get all the models in the project.
 	project-->folder-->model.
@@ -29,6 +49,7 @@ def get_project_models(project_id,building_id):
 		"from floor " \
 		"where project_id='%s' " \
 		"and ('%s' is null or building_id='%s') " \
+		"and (model_id is not null) " \
 		"order by sequence_id desc" \
 		% (project_id, building_id, building_id)
 		# "order by sequence_id desc"\

+ 1 - 2
src/system_relation/systemutils.py

@@ -61,8 +61,7 @@ def dic2obj(d):
 		if isinstance(j0, dict):
 			setattr(top, i, dic2obj(j0))
 		elif isinstance(j0, seqs):
-			setattr(top, i,
-					type(j0)(dic2obj(sj) if isinstance(sj, dict) else sj for sj in j0))
+			setattr(top, i, type(j0)(dic2obj(sj) if isinstance(sj, dict) else sj for sj in j0))
 		else:
 			setattr(top, i, j0)
 	return top

+ 2 - 0
src/system_relation/test_flowdirection.py

@@ -1,6 +1,8 @@
 import sys
 sys.path.append("../../..")
 from relations.src.system_relation import calc_flowdirection, systemdatautils,systemgraph_display
+
+
 if __name__=="__main__":
 	project_id="Pj1101010015"
 	block_id="0"

+ 39 - 3
src/system_relation/test_systemblock.py

@@ -3,13 +3,49 @@ sys.path.append("../../..")
 from relations.src.system_relation import systemutils, graph_model, systemdatautils,systemgraph_display
 
 from cffi import FFI
+
+
+def get_real_model_id(models):
+	real_model_ids = systemdatautils.get_real_model_id(models)
+	floor_model_dict = dict()
+	for row in real_model_ids:
+		floor_id = row.id
+		model_id = row.fid
+		status = row.status
+		if status == 4:
+			floor_model_dict[floor_id] = model_id
+	for model in models:
+		if model.model_id not in floor_model_dict:
+			del models[model.model_id]
+		else:
+			model.model_id = floor_model_dict[model.model_id]
+'''
+Bd110229000152e862dcd34711e8a471478dc5d7eb0d
+Bd110229000190c881e0594011eaa6df0db8c24bda0e
+Bd1102290001a1796d11594011eaa6df9d51f0a54633
+Bd1102290001b0ea5e32594011eaa6df7f063170ebf2
+Bd1102290001bd41da53594011eaa6df5f7f0c23be5d
+Bd1102290001c67ea95a594011ea8fa07973fb6df60a
+Bd1102290001ce0e643b594011ea8fa09b56c4ca731b
+Bd1102290001d6cd4fe4594011eaa6dfa5ccf8979b10
+Bd1102290001e2f0eac5594011eaa6dfe3d285700533
+Bd1102290001f30b1436594011eaa6df5b8d12bc707e
+Bd110229000143dbf15d69a111eaa6dfeb7566e0258e
+Bd1102290001f8755707594011eaa6df6bb8f4f81dd4
+Bd110229000100f824dc594111ea8fa09d5be066de42
+Bd11022900013df921f75b7511eaa6dfc5825970b94b
+Bd110229000134cd447c69a111eaa6dff75d40e4221d
+Bd11022900014c922a1469a111ea8fa0f5eef8d9657e
+'''
+
 if '__main__'==__name__:
-	project_id="Pj1101010015"
-	system_name="冷冻水供水管"
+	project_id="Pj1101080002"
+	system_name="空调热水供水管"
 	domain="DomainPiping"
-	building_id = 'Bd11010100153c05821ed8fd11e9b8f2d79d0a5b4bf6'
+	building_id = 'Bd1101080002a0a394e30d1b4ebda204515c64f3082e'
 	models= systemdatautils.get_project_models(project_id,building_id)
 	floorgraphs=[]
+	get_real_model_id(models)
 	for model in models:
 		floor_connectors = systemdatautils.get_connectors_data(model.model_id, system_name, domain)
 		floor_elements = systemdatautils.get_element_data(model.model_id)

+ 104 - 91
test.py

@@ -4,18 +4,31 @@ import json
 
 import psycopg2
 
-from adjacent import calc_adjacent_relation
+#from business_space_adjacent import calc_adjacent_relation
 
-def save_data(sql):
-    record = []
-    try:
-        connection = psycopg2.connect(
+
+def get_online_conn():
+    return psycopg2.connect(
+            database='datacenter',
+            user='postgres',
+            password='123qwe!@#',
+            host='47.94.89.44',
+            port='5432'
+        )
+
+def get_test_conn():
+    return psycopg2.connect(
             database='datacenter',
             user='postgres',
             password='123456',
-            host='192.168.20.234',
+            host='172.16.44.234',
             port='5432'
         )
+
+def save_data(sql):
+    record = []
+    try:
+        connection = get_test_conn()
         cursor = connection.cursor()
         print(sql)
         cursor.execute(sql)
@@ -32,13 +45,7 @@ def save_data(sql):
 def get_data(sql):
     record = []
     try:
-        connection = psycopg2.connect(
-            database='datacenter',
-            user='postgres',
-            password='123456',
-            host='192.168.20.234',
-            port='5432'
-        )
+        connection = get_test_conn()
         cursor = connection.cursor()
         print(sql)
         cursor.execute(sql)
@@ -65,81 +72,87 @@ def loads_curve(x):
 
 
 if __name__ == '__main__':
-    segment_sql = "SELECT * FROM revit.boundary_segment where model_id = '8544a3c3cd2611e99abc839db1015353'"
-    wall_sql = "SELECT * FROM revit.wall where model_id = '8544a3c3cd2611e99abc839db1015353'"
-    v_wall_sql = "SELECT * FROM revit.virtual_wall where model_id = '8544a3c3cd2611e99abc839db1015353'"
-    columns_sql = "SELECT * FROM revit.column where model_id = '8544a3c3cd2611e99abc839db1015353'"
-    segment_data = get_data(segment_sql)
-    wall_data = get_data(wall_sql)
-    v_wall_data = get_data(v_wall_sql)
-    columns_data = get_data(columns_sql)
-    SEGMENT_KEYS = [
-        'id',
-        'model_id',
-        'belong',
-        'belong_type',
-        'curve',
-        'space_id',
-        'group_index',
-        'sequence',
-        'reference',
-        'revit_id',
-        'type',
-    ]
-    WALL_KEYS = [
-        'id',
-        'model_id',
-        'level_id',
-        'width',
-        'location',
-        'outline',
-        'last_update',
-        'create_time',
-        'name',
-        'source_id',
-        'revit_id',
-        'type',
-    ]
-    V_WALL_KEYS = [
-        'id',
-        'model_id',
-        'location',
-        'outline',
-        'last_update',
-        'create_time',
-        'name',
-        'source_id',
-        'revit_id',
-        'type',
-    ]
-    COLUMNS_KEYS = [
-        'id',
-        'model_id',
-        'location',
-        'outline',
-        'bounding',
-        'last_update',
-        'create_time',
-        'name',
-        'source_id',
-        'revit_id',
-        'type',
-    ]
-    segment_data = [dict(zip(SEGMENT_KEYS, item)) for item in segment_data]
-    segment_data = list(map(loads_curve, segment_data))
-    wall_data = [dict(zip(WALL_KEYS, item)) for item in wall_data]
-    wall_data = list(map(loads, wall_data))
-    v_wall_data = [dict(zip(V_WALL_KEYS, item)) for item in v_wall_data]
-    v_wall_data = list(map(loads, v_wall_data))
-    columns_data = [dict(zip(COLUMNS_KEYS, item)) for item in columns_data]
-    columns_data = list(map(loads, columns_data))
-
-
-    test_result = calc_adjacent_relation(
-        segments=segment_data,
-        walls=wall_data,
-        v_walls=v_wall_data,
-        columns=columns_data
-    )
-    for item in test_result:
-        print(item)
+    foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
+    my_map = dict()
+    i = 1
+    print(i if (i %2 == 0) else i/2 for i in foo)
+
+
+    # segment_sql = "SELECT * FROM revit.boundary_segment where model_id = '8544a3c3cd2611e99abc839db1015353'"
+    # wall_sql = "SELECT * FROM revit.wall where model_id = '8544a3c3cd2611e99abc839db1015353'"
+    # v_wall_sql = "SELECT * FROM revit.virtual_wall where model_id = '8544a3c3cd2611e99abc839db1015353'"
+    # columns_sql = "SELECT * FROM revit.column where model_id = '8544a3c3cd2611e99abc839db1015353'"
+    # segment_data = get_data(segment_sql)
+    # wall_data = get_data(wall_sql)
+    # v_wall_data = get_data(v_wall_sql)
+    # columns_data = get_data(columns_sql)
+    # SEGMENT_KEYS = [
+    #     'id',
+    #     'model_id',
+    #     'belong',
+    #     'belong_type',
+    #     'curve',
+    #     'space_id',
+    #     'group_index',
+    #     'sequence',
+    #     'reference',
+    #     'revit_id',
+    #     'type',
+    # ]
+    # WALL_KEYS = [
+    #     'id',
+    #     'model_id',
+    #     'level_id',
+    #     'width',
+    #     'location',
+    #     'outline',
+    #     'last_update',
+    #     'create_time',
+    #     'name',
+    #     'source_id',
+    #     'revit_id',
+    #     'type',
+    # ]
+    # V_WALL_KEYS = [
+    #     'id',
+    #     'model_id',
+    #     'location',
+    #     'outline',
+    #     'last_update',
+    #     'create_time',
+    #     'name',
+    #     'source_id',
+    #     'revit_id',
+    #     'type',
+    # ]
+    # COLUMNS_KEYS = [
+    #     'id',
+    #     'model_id',
+    #     'location',
+    #     'outline',
+    #     'bounding',
+    #     'last_update',
+    #     'create_time',
+    #     'name',
+    #     'source_id',
+    #     'revit_id',
+    #     'type',
+    # ]
+    # segment_data = [dict(zip(SEGMENT_KEYS, item)) for item in segment_data]
+    # segment_data = list(map(loads_curve, segment_data))
+    # wall_data = [dict(zip(WALL_KEYS, item)) for item in wall_data]
+    # wall_data = list(map(loads, wall_data))
+    # v_wall_data = [dict(zip(V_WALL_KEYS, item)) for item in v_wall_data]
+    # v_wall_data = list(map(loads, v_wall_data))
+    # columns_data = [dict(zip(COLUMNS_KEYS, item)) for item in columns_data]
+    # columns_data = list(map(loads, columns_data))
+    #
+    #
+    # test_result = calc_adjacent_relation(
+    #     segments=segment_data,
+    #     walls=wall_data,
+    #     v_walls=v_wall_data,
+    #     columns=columns_data
+    # )
+    # for item in test_result:
+    #     print(item)