Jelajahi Sumber

mxg:calc connected_block direction

mengxiangge 5 tahun lalu
induk
melakukan
e9cf960e60

+ 94 - 0
systemrelation/calc_flowdirection.py

@@ -0,0 +1,94 @@
+'''calc flow direction.
+step1. division block.
+step2. appoint source.
+setp3. calc flow direction.
+'''
+from systemrelation import systemdatautils
+import networkx as nx
+from systemrelation import revit_const
+
+def calc(project_id,block_id):
+	'''
+	calc block flow direction.
+	:param project_id:
+	:param block_id:
+	:return:
+	'''
+	block_datas=systemdatautils.get_connected_block_data(project_id,block_id)
+	source_datas=systemdatautils.get_connected_block_source_data(project_id,block_id)
+	# get roots
+	roots=list(i.source_id for i in source_datas)
+	g = nx.Graph()
+	for item in block_datas:
+		item.direction=None
+		g.add_edge(item.id1,item.id2);
+	G = g.to_directed()
+	# get leaves
+	leaves=list(v for v,d in G.out_degree() if d==1)
+	G_new=nx.DiGraph()
+	try:
+		for i in range(len(roots)):
+			root=roots[i]
+			for leaf in leaves:
+				paths = nx.all_simple_paths(G, root, leaf)
+				for path in paths:
+					# remove contain source path
+					if is_path_contain_root(path, roots, root):
+						continue
+					# set path direction
+					for j in range(len(path) - 1):
+						G_new.add_edge(path[j], path[j + 1],weight=str(j))
+			# add source direct to source path.
+			for j in range(i + 1, len(roots)):
+				next_root=roots[j]
+				paths = list(nx.all_simple_paths(G, root, next_root))
+				for path in paths:
+					if len(path) == 2:
+						G_new.add_edge(root, next_root,weight="0")
+						G_new.add_edge(next_root, root,weight="0")
+	except Exception as ex:
+		print(ex)
+	for edge in G_new.edges:
+		edge_data=G_new.get_edge_data(edge[0],edge[1])
+		weight=edge_data["weight"]
+		set_edge_direction(block_datas,edge,weight)
+	return block_datas
+
+def is_path_contain_root(path, roots, cur_root):
+	'''
+	judge whether path contain root node except cur_root.
+	:param path:
+	:param roots:
+	:param cur_root:
+	:return:
+	'''
+	result = False
+	for root in roots:
+		if root == cur_root:
+			continue
+		result = (root in path)
+		if result:
+			break
+	return result
+def set_edge_direction(block_datas,edge,weight):
+	'''
+	set connected_block direction.
+	connector1 to connector2.sign:1
+	connector2 to connector1.sign:-1
+	both way.sign:0
+	un calc.sign:None
+	:param block_datas:
+	:param edge:
+	:return:
+	'''
+	for block_data in block_datas:
+		# connector1 to connector2.sign:1
+		if block_data.id1 == edge[0] and block_data.id2 == edge[1]:
+			j=1
+			block_data.direction=(block_data.direction+j if block_data.direction else j)
+			block_data.block_id=str(weight)
+		# connector2 to connector1.sing:-1
+		if block_data.id1 == edge[1] and block_data.id2 == edge[0]:
+			j = -1
+			block_data.direction=(block_data.direction+j if block_data.direction else j)
+			block_data.block_id = str(weight)

+ 0 - 1
systemrelation/revit_const.py

@@ -8,4 +8,3 @@ OTHER='Other'
 CONNECTOR='Connector'
 DUCT='Duct'
 PIPE='Pipe'
-MEPCURVE=[DUCT,PIPE]

+ 66 - 3
systemrelation/systemdatautils.py

@@ -56,7 +56,6 @@ def get_project_models(project_id):
 	element_data.extend(get_dicdata(sql, COMPONENT_KEYS))
 	element_data = list(map(systemutils.dic2obj, element_data))
 	return element_data
-
 def get_element_data(model_id):
 	"""
 	Cache all Element from Db
@@ -167,6 +166,7 @@ def get_element_data(model_id):
 	element_data=list(map(systemutils.dic2obj, element_data))
 	return element_data
 
+
 def get_connectors_data(model_id, system_name, domain):
 	"""Cache all connector from Db。"""
 	connector_data=[]
@@ -224,7 +224,7 @@ def save_edge_data(edge,block_id):
 	c2=vertex2.system_data
 	model1=systemutils.get_specify_type_parent(vertex1,graph_model.FloorGraph).model
 	model2 = systemutils.get_specify_type_parent(vertex2, graph_model.FloorGraph).model
-	sql="insert into revit.connected_block" \
+	sql="insert into revit_calc.connected_block" \
 		"(id1,type1,model_id1,id2,type2,model_id2,block_id,project_id) values " \
 		"('%s','%s','%s','%s','%s','%s','%s','%s')"\
 		%(c1.id,c1.type,model1.file_id,c2.id,c2.type,model2.file_id,block_id,model1.project_id)
@@ -235,8 +235,71 @@ def del_project_data(project_id):
 	:param project_id:
 	:return:
 	"""
-	sql = "delete from revit.connected_block where project_id='%s'"% (project_id)
+	sql = "delete from revit_calc.connected_block where project_id='%s'"% (project_id)
 	data = test.save_data(sql)
+def get_connected_block_data(project_id,block_id):
+	'''
+	Get all the connected_block .
+	:param project_id:
+	:param block_id:
+	:return:
+	'''
+	element_data = []
+	sql = "select * from revit_calc.connected_block " \
+		  "where project_id='%s'" \
+		  "and block_id='%s'" \
+		  % (project_id,block_id)
+	sql = "select * from revit_calc.connected_block " \
+		  "where project_id='%s'" \
+		  "order by cast(block_id as int) asc"\
+		  % (project_id)
+	KEYS = [
+		'id',
+		'id1',
+		'type1',
+		'model_id1',
+		'id2',
+		'type2',
+		'model_id2',
+		'direction',
+		'block_id',
+		'project_id',
+	]
+	element_data.extend(get_dicdata(sql, KEYS))
+	element_data = list(map(systemutils.dic2obj, element_data))
+	return element_data
+def get_connected_block_source_data(project_id,block_id):
+	'''
+	Get all the connected_block source .
+	:param project_id:
+	:param block_id:
+	:return:
+	'''
+	element_data = []
+	sql = "select * from revit_calc.connected_block_source " \
+		  "where project_id='%s'" \
+		  "and block_id='%s'" \
+		  % (project_id,block_id)
+	KEYS = [
+		'id',
+		'project_id',
+		'block_id',
+		'source_id',
+	]
+	element_data.extend(get_dicdata(sql, KEYS))
+	element_data = list(map(systemutils.dic2obj, element_data))
+	return element_data
+def update_connected_block_data(block_datas):
+	'''
+	update connected_block direction by id.
+	:param block_datas:
+	:return:
+	'''
+	for block_data in block_datas:
+		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)
+		data = test.save_data(sql)
+
+
 if '__main__'==__name__:
 	# model_id="cf4e11a0e25811e999b6dbc4c75fa946"
 	# system_name="喷淋管"

+ 52 - 3
systemrelation/systemgraph_display.py

@@ -11,7 +11,7 @@ def show_system(system_graph):
 	for e in system_graph.system_edges:
 		#g.add_edge(e.start_vertex.system_data.source_id,e.end_vertex.system_data.source_id)
 		g.add_edge(e.start_vertex, e.end_vertex)
-	nx.draw(g,with_labels=True)
+	nx.draw_shell(g,with_labels=True)
 	plt.show()
 
 def show_floor(floor_graph):
@@ -44,5 +44,54 @@ def show_groupedsystem(grouped_system_graph):
 			g.add_edge(e.start_vertex, e.end_vertex)
 	for e in grouped_system_graph.connectedges:
 			g.add_edge(e.start_vertex, e.end_vertex)
-	nx.draw(g,with_labels=True)
-	plt.show()
+	nx.draw_spring(g,with_labels=True,font_size=8,with_label=True)
+	plt.show()
+
+from systemrelation import systemdatautils
+def show_connected_block(project_id,block_id):
+	'''
+	show connected block.
+	:param project_id:
+	:param block_id:
+	:return:
+	'''
+	block_datas=systemdatautils.get_connected_block_data(project_id,block_id)
+	g=nx.DiGraph()
+	model_dic = {}
+	floor_data_dic = {}
+	for block_data in block_datas:
+		v1=get_connected_info(model_dic,floor_data_dic,block_data.project_id,block_data.model_id1,block_data.type1,block_data.id1)
+		v2 = get_connected_info(model_dic,floor_data_dic,block_data.project_id,block_data.model_id2, block_data.type2, block_data.id2)
+		direction=block_data.direction
+		if direction==1 or direction==0:
+			g.add_edge(v1,v2)
+		if direction==-1 or direction==0:
+			g.add_edge(v2,v1)
+	nx.draw_planar(g, with_labels=True, font_size=8, with_label=True)
+	plt.show()
+
+def get_connected_info(model_dic,floor_data_dic,project_id,model_id,type,id):
+	'''
+	get graphvertex display info
+	:param model_dic:
+	:param floor_data_dic:
+	:param project_id:
+	:param model_id:
+	:param type:
+	:param id:
+	:return:
+	'''
+	if model_id not in model_dic:
+		models = systemdatautils.get_project_models(project_id)
+		ids=[i.file_id for i in models]
+		model_dic=dict(zip(ids,models))
+	model=model_dic[model_id]
+	if model_id not in floor_data_dic:
+		datas=systemdatautils.get_element_data(model_id)
+		floor_data_dic[model_id]=datas
+	floor_datas = floor_data_dic[model_id]
+	datas=list(i for i in floor_datas if i.type==type and i.id==id)
+	str0=id
+	if datas:
+		str0=str(model.floor_name)+':'+str(datas[0].source_id)
+	return str0

+ 10 - 0
systemrelation/test_flowdirection.py

@@ -0,0 +1,10 @@
+from systemrelation import calc_flowdirection
+from systemrelation import systemdatautils
+from systemrelation import systemgraph_display
+
+if __name__=="__main__":
+	project_id="Pj1101010015"
+	block_id="0"
+	block_datas=calc_flowdirection.calc(project_id,block_id)
+	systemdatautils.update_connected_block_data(block_datas)
+	systemgraph_display.show_connected_block(project_id,block_id)