在课程设计过程中,数据处理往往让学生头痛。本该但是由电脑完成的工作强加于人,也难怪学生叫苦连天。尤其是在求节点流量时,虽然鸿业和天正等软件具备此功能,但有兴趣的同学可以从底层开发出属于自己的程序。这里给出我的一些方法,即根据cad作图输出.dxf文件,利用python的matplotlib作出节点流量图。当然,这里要用到一个另一个第三方库dxfgrabber。具体方法如下:
1、根据所给的城市道路图按照相关规范(干管或连接管之间的间距有规范)设计节点和管段。这里应该注意,节点一定要用cad的点命令画出,再用管线连接。
2、根据最高日最高时用水量的结果,执行程序,即可获得结果图
.dxf文件的图形如下:
代码如下:
1 ‘‘‘根据管段流量算节点流量‘‘‘ 2 import dxfgrabber 3 import matplotlib.pyplot as plt 4 import numpy as np 5 6 dxf = dxfgrabber.readfile("C:\\1.dxf") 7 j = -1 8 pipe = [] 9 coodinate = [] 10 q_total = 1012-92#最高时用水量,(L/s),92为总集中流量 11 for e in dxf.entities: 12 if e.dxftype == ‘LINE‘: 13 j += 1 14 start_node = [int(i) for i in list(e.start)] 15 end_node = [int(i) for i in list(e.end)] 16 length = int(((start_node[0] - end_node[0])**2 + 17 (start_node[1] - end_node[1])**2)**(0.5)) 18 pipe.append([start_node, end_node, length]) 19 #print([start_node, end_node, length]) 20 elif e.dxftype == ‘POINT‘: 21 coodinate.append([int(i) for i in e.point]) 22 else: 23 None 24 #print(coodinate) 25 26 l_total = sum([i[2] for i in pipe]) 27 q_specific = [q_total*i[2]/l_total for i in pipe]#管段流量列表 28 print(q_specific) 29 30 x=[] 31 y=[] 32 z=[] 33 node = []#节点坐标以两元素列表存在node中 34 j=-1 35 for i in coodinate: 36 j+=1 37 x.append(i[0]) 38 y.append(i[1]) 39 z.append(i[2]) 40 node.append([int(x[j]), int(y[j]), int(z[j])]) 41 #print(node[j]) 42 43 dic = {} 44 for i in range(len(node)): 45 dic[i]=node[i] 46 print(dic) 47 for i in node: 48 for element in pipe: 49 None 50 51 dic_q = {k:0 for k in range(len(node))} 52 for k in dic.keys(): 53 for p in range(len(pipe)): 54 if dic[k]==pipe[p][0]: 55 dic_q[k] += q_specific[p]/2 56 elif dic[k]==pipe[p][1]: 57 dic_q[k] += q_specific[p]/2 58 else: 59 None 60 print(dic_q) 61 62 63 64 65 fig = plt.figure(1) 66 ax = fig.gca() 67 68 x = np.array(x) 69 y = np.array(y) 70 qtext = [i for i in dic_q.values()] 71 ax.scatter(x,y,c=‘r‘,marker=‘o‘)#print(x,y) 72 for i in range(len(pipe)): 73 ax.plot([pipe[i][0][0], pipe[i][1][0]], [pipe[i][0][1], pipe[i][1][1]], c=‘b‘) 74 75 for i in range(len(x)): 76 ax.annotate(int(qtext[i]), xy=(x[i],y[i])) 77 78 fig = plt.figure() 79 ax = fig.add_subplot(111) 80 for i in range(len(pipe)): 81 ax.plot([pipe[i][0][0], pipe[i][1][0]], [pipe[i][0][1], pipe[i][1][1]], c=‘b‘) 82 ax.annotate(pipe[i][2], xy=((pipe[i][0][0]+pipe[i][1][0])/2, (pipe[i][0][1]+pipe[i][1][1])/2)) 83 plt.show
原文:https://www.cnblogs.com/w-netboy/p/12185833.html