管网系统设备分块
create or replace function public.sys_block(project_id character varying,system_name character varying,domain character varying) returns boolean
as
$$
from relations.src.system_relation import systemutils, graph_model
def get_models(project_id):
sql ="select " \
"file.id as file_id" \
",file.original_name as file_name" \
",file.status as file_status" \
",floor.id as floor_id" \
",floor.floor_name as floor_name" \
",folder.id as folder_id" \
",folder.name as folder_name" \
",folder.project_id as project_id " \
"from revit.model_file as file " \
"inner join " \
"revit.model_floor as floor " \
"on ( file.id=floor.current_model_id) " \
"inner join " \
"revit.model_folder as folder " \
"on (floor.folder_id=folder.id )" \
"where folder.project_id=$1" \
#"and file.status=4 "
"and folder.id='bbe510dbe26011e999b69b669ea08505'"
join_plan=plpy.prepare(sql,["text"])
data=join_plan.execute([project_id])
models=systemutils.sqldata2objlist(data)
return models
def get_connectors(model, 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.file_id, system_name, domain])
floor_connectors=systemutils.sqldata2objlist(data)
return floor_connectors
def get_elements(model):
floor_elements=[]
sql="select * from revit.equipment where model_id=$1"
join_plan=plpy.prepare(sql,["text"])
data=join_plan.execute([model.file_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.file_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.file_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.file_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.file_id])
floor_elements.extend(systemutils.sqldata2objlist(data))
return floor_elements
def delete_block_data(project_id,system_name,domain):
sql="delete from revit_calc.connected_block " \
"where project_id=$1"\
"and mep_system_type=$2" \
"and domain=$3"
join_plan=plpy.prepare(sql,["text","text","text"])
join_plan.execute([project_id,system_name,domain])
def save_edge_data(edge,block_id,system_name,domain):
vertex1=edge.start_vertex
vertex2=edge.end_vertex
c1=vertex1.system_data
c2=vertex2.system_data
model1= vertex1.get_parent_floor()
model2 = vertex2.get_parent_floor()
sql="insert into revit_calc.connected_block" \
"(id1,type1,model_id1,id2,type2,model_id2,block_id,project_id,mep_system_type,domain) values " \
"($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)"
join_plan=plpy.prepare(sql,["text","text","text","text","text","text","text","text","text","text"])
join_plan.execute([c1.id,c1.type,model1.file_id,c2.id,c2.type,model2.file_id,block_id,model1.project_id,system_name,domain])
try:
# 将下面对数据库的操作作为一个事务, 出异常则自动rollback
with plpy.subtransaction():
models=get_models(project_id)
floorgraphs=[]
for model in models:
floor_connectors=get_connectors(model, system_name, domain)
floor_elements=get_elements(model)
g= graph_model.FloorGraph(model, system_name, domain,floor_connectors,floor_elements)
g.get_floor_graphs()
floorgraphs.append(g)
projectgraph= graph_model.combine_systemgraph(floorgraphs)
delete_block_data(project_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)
for e in g.connectedges:
save_edge_data(e,block_id,system_name,domain)
block_id+=1
except Exception as e:
plpy.warning(e)
return False
else:
return True
$$
LANGUAGE 'plpython3u' VOLATILE;
select public.sys_block('Pj1101010015','冷冻水供水管','DomainPiping')
1. 项目id
2. 需要计算的系统名称
3. 需要计算的管道系统类型,枚举(水管或风管):DomainPiping,DomainHvac
True 成功
False 失败