从Matplotlib Mesh写出STL_File

时间:2017-11-22 20:56:56

标签: numpy matplotlib stl wireframe

我的程序与matplotlib一起使用并计算y-z-section中的包络。这可以针对多个部分完成,并将在图表中绘制为3dplot。显示带有线框设计的信封也很容易。 现在我想写一个stl文件。为此,我需要一个网格。我找到了一个名为numpy-stl的模块,它提供了这些功能,但我需要创建网格。 现在我发现matplotlib已经包含了创建网格的功能。 有人有经验吗?到目前为止,这是我的代码。数据来自字典:{' X' :{' Y' :[],' Z' :[]}}。

def lines_draw(self):    

    self.plot_data.clear()
    self.plot_data = publish('3D_DATA_PLOT')
    self.plot_Handling = []
    a = []
    x_keys = []
    x_keys_collect1 = []
    x_keys_collect2 = []
    #print(self.plot_data)

    _big_list_X = []
    _big_list_Y = []
    _big_list_Z = []        
    big_list_X = []
    big_list_Y = []
    big_list_Z = []

    # Draw the section-lines
    for key,val in self.plot_data.items():
        a.clear()

        for i in range(len(self.plot_data[key]['Y'])):
            a.append(key)

        x = np.array(a)
        y = np.array(self.plot_data[key]['Y']) 
        z = np.array(self.plot_data[key]['Z'])
        x = x.astype(np.float)
        y = y.astype(np.float)
        z = z.astype(np.float)

        linesdraw, = self.ax.plot(x,y,z)
        self.plot_Handling.append(linesdraw)

    x_keys = list(self.plot_data)

    dict_length = len(x_keys)
    a=1

    # Draw the wireframes
    for i in range(dict_length-1):
        x_keys_collect1.clear()
        x_keys_collect2.clear()
        for xi in self.plot_data[x_keys[i]]['Y']:
            x_keys_collect1.append(x_keys[i])
            a = a + 1
        for xi in self.plot_data[x_keys[i+1]]['Y']:
            x_keys_collect2.append(x_keys[i+1])  

        x1 = np.array(x_keys_collect1)            
        y1 = np.array(self.plot_data[x_keys[i]]['Y']) 
        z1 = np.array(self.plot_data[x_keys[i]]['Z'])

        x2 = np.array(x_keys_collect2)
        y2 = np.array(self.plot_data[x_keys[i+1]]['Y']) 
        z2 = np.array(self.plot_data[x_keys[i+1]]['Z'])

        x1 = x1.astype(np.float)
        y1 = y1.astype(np.float)
        z1 = z1.astype(np.float)             
        x2 = x2.astype(np.float)
        y2 = y2.astype(np.float)
        z2 = z2.astype(np.float)        



        # i1, h1 = np.meshgrid(np.arange(a-1), np.linspace(x1[0],x2[0],5))
        # print(np.linspace(x1[0],x2[0],5))
        # i1 = i1.astype(np.int)
        # h1 = h1.astype(np.int)
        # X = (y2[i1] - y1[i1]) / (x2 - x1) * (h1 - x1) + y1[i1]
        # Y = (z2[i1] - z1[i1]) / (x2 - x1) * (h1 - x1) + z1[i1]
        # self.ax.plot_surface(h1,X, Y, color='m', alpha=0.3, linewidth=0)

        # y1 = -y1.astype(np.float)
        # y2 = -y2.astype(np.float)
        # i1, h1 = np.meshgrid(np.arange(a-1), np.linspace(x1[0],x2[0],5))            
        # i1 = i1.astype(np.int)
        # h1 = h1.astype(np.int)
        # X = (y2[i1] - y1[i1]) / (x2 - x1) * (h1 - x1) + y1[i1]
        # Y = (z2[i1] - z1[i1]) / (x2 - x1) * (h1 - x1) + z1[i1]
        # self.ax.plot_surface(h1,X, Y, color='m', alpha=0.3, linewidth=0) 


        big_list_X = np.array(x_keys_collect1 + x_keys_collect2)
        big_list_Y = np.array(self.plot_data[x_keys[i]]['Y'] + self.plot_data[x_keys[i+1]]['Y'])
        big_list_Z = np.array(self.plot_data[x_keys[i]]['Z'] + self.plot_data[x_keys[i+1]]['Z'])

        big_list_X = big_list_X.astype(np.float)
        big_list_Y = big_list_Y.astype(np.float)
        big_list_Z = big_list_Z.astype(np.float)



        print(big_list_X)
        # print(big_list_Y)
        # print(big_list_Z)

        # big_list_X, big_list_Z = np.meshgrid(big_list_X,big_list_Z)
        # big_list_X, big_list_Z = big_list_X.flatten(), big_list_Z.flatten()
        # tri = mtri.Triangulation(_big_list_X,_big_list_Z)


        print(big_list_X)
        # print(big_list_Y)
        # print(big_list_Z)

        self.ax.plot_trisurf(big_list_X,big_list_Y,big_list_Z, cmap=cm.jet, linewidth=0)#,triangles=tri.triangles)#,cmap=plt.cm.Spectral)



        a = 1

        # tri = mtri.Triangulation(X,Y)
        # self.ax.plot_trisurf(h1,X,Y,triangles=tri.triangles,cmap=plt.cm.Spectral)

    self.canvas.draw()

0 个答案:

没有答案