salome: salomeDialogPanelExample.py

File salomeDialogPanelExample.py, 5.6 KB (added by brankm, 6 years ago)

Koda za izdelavo dialoga, ki izdela zasuke panela.

Line 
1# import libraries
2import salome
3import GEOM
4from salome.geom import geomBuilder
5import os
6from functools import partial
7# GUI library
8from PyQt5.QtWidgets import *
9from PyQt5.QtCore import *
10from PyQt5.QtGui import *
11from PyQt5 import QtWidgets
12import math
13geompy = geomBuilder.New(salome.myStudy)
14
15def createPanel():
16    global panelIsMade, panel_FW
17    panelIsMade = True
18    # If file is not opened, determine path with os library
19    os.chdir(os.path.expanduser('d:/vaje_KT/'))
20    with open('data.txt','r') as f:
21        fileread = f.readlines()
22        content = [x.strip() for x in fileread]
23        # Define data list
24        data = []
25        for coord in content:
26            data.append([x.strip() for x in coord.split(" ")])
27    f.close()
28   
29    coord_x = []
30    coord_y = []
31    # convert coordinates to mm and to float type
32    for i in range(len(data)):
33        data[i][0] = float(data[i][0])*(1000)
34        data[i][1] = float(data[i][1])*(1000)
35        data[i][2] = float(data[i][1])*(1000)
36        coord_x.append(data[i][0])
37        coord_y.append(data[i][1])
38   
39   
40    # mirror x coordinatex over yz - plane
41    coord_mirror_x = coord_x[::-1]
42    for n in range(len(coord_mirror_x)):
43        coord_mirror_x[n] = coord_mirror_x[n]*(-1)
44    coord_mirror_y = coord_y[::-1]
45   
46    # Iterate over coord_x and create geompy vertices and store those
47    # vertices into python list.
48    points_x = []
49    for i in range(len(coord_x)):
50        point_x = geompy.MakeVertex(coord_x[i],coord_y[i],0)
51        points_x.append(point_x)
52
53    points_y = []
54    for i in range(len(coord_x)):
55        point_y = geompy.MakeVertex(coord_mirror_x[i],coord_mirror_y[i],0)
56        points_y.append(point_y)
57   
58    # Create B-Spline curve on the set of points with
59    # geompy.MakeInterpol() and add curve to Salome study.
60    b_spline = geompy.MakeInterpol(points_x)
61    b_splineID = geompy.addToStudy(b_spline,"b_spline_ft")
62   
63    b_spline_mirror = geompy.MakeInterpol(points_y)
64    b_splineID_mirror = geompy.addToStudy(b_spline_mirror,"b_spline_ft_mirrored")
65   
66    # Create line between both curves.
67    ml_point1 = geompy.MakeVertex(-coord_x[0],coord_y[0],0)
68    ml_point2 = geompy.MakeVertex(coord_x[0],coord_y[0],0)
69    middle_line = geompy.MakeLineTwoPnt(ml_point1,ml_point2)
70    middle_lineID = geompy.addToStudy(middle_line,"middle_line")
71   
72    # Create wire out of all three components
73    wire = geompy.MakeWire([b_spline_mirror,middle_line,b_spline])
74    wireID = geompy.addToStudy(wire,"wire")
75   
76    # Load data for extrusion
77    dataExtrusion = []
78    with open('dataPolyline.txt','r') as f:
79        fileread = f.readlines()
80        content = [x.strip() for x in fileread]
81        # Define data list
82        for coord in content:
83            dataExtrusion.append([x.strip() for x in coord.split(" ")])
84    f.close()
85   
86    # Read data for extrusion
87    coord_xe = []
88    coord_ye = []
89    coord_ze = []
90    for n in range(len(dataExtrusion)):
91        coord_xe.append(float(dataExtrusion[n][0]))
92        coord_ye.append(float(dataExtrusion[n][1]))
93        coord_ze.append(float(dataExtrusion[n][2]))
94   
95    # Convert data to geompy point and store it in a list.
96    points_e = []
97    for coord_e in range(len(coord_xe)):
98        point_e = geompy.MakeVertex(coord_xe[coord_e],coord_ye[coord_e],coord_ze[coord_e])
99        points_e.append(point_e)
100   
101    # Create polyline for extrusion and add it to study.
102    polylineVert = geompy.MakePolyline(points_e)
103    polylineVertID = geompy.addToStudy(polylineVert,"polyline_ft_vert")
104    panel_FW = geompy.MakePipe(wire, polylineVert)
105    panel_FW = geompy.MakeTranslation(panel_FW, 0,-4200,0)
106    geompy.addToStudy(panel_FW,'Panel')
107    salome.sg.updateObjBrowser(True)
108
109def createNewPanel(lin1, lin2, lin3):
110    # If panel is not made, there is nothing to rotate
111    # If lin2.text() is not float, angle cannot be determined
112    # If lin3.text() is not int, number of rotated panels cannot be determined
113    if panelIsMade and float(lin2.text()) and int(lin3.text()):
114        # Create rotation vector around z-axis
115        p1 = geompy.MakeVertex(0, 0, 0)
116        p2 = geompy.MakeVertex(0, 0, 1)
117        v = geompy.MakeVector(p1, p2)
118        # This for loop iterates over number of roteted panels and
119        # adds angle to every new panel. Result is panel_FW_rotated
120        # Name of panel is given by user + angle of rotation
121        for nr in range(1,int(lin3.text())+1):
122            panel_FW_rotated = geompy.MakeRotation(panel_FW, v, math.pi/180*nr*float(str(lin2.text())))
123            panel_name = str(lin1.text())+"_rotated_"+str(float(str(lin2.text()))*nr)+"_deg"
124            geompy.addToStudy(panel_FW_rotated,panel_name)
125        salome.sg.updateObjBrowser(True)
126
127
128widgetHDF = QtWidgets.QWidget()
129widgetHDF.setFixedSize(500, 250)
130widgetHDF.setWindowTitle('Rotate panel')
131#widgetHDF.setWindowFlags(widgetHDF.windowFlags() | QtCore.Qt.Window)
132lblPanel = QLabel('Create panel:')
133btnPanel = QPushButton('Create Panel')
134lbl1 = QLabel('Set name for rotation panels:')
135lin1 = QLineEdit('panel4')
136lbl2 = QLabel('Set rotation angle:')
137lin2 = QLineEdit('0')
138lbl3 = QLabel('Set number of rotations:')
139lin3 = QLineEdit('1')
140btn = QPushButton('Apply')
141panelIsMade = False
142
143btnPanel.setToolTip('Create panel')
144btn.setToolTip('Create rotated panels')
145btnPanel.clicked.connect(createPanel)
146btn.clicked.connect(partial(createNewPanel,lin1,lin2,lin3))
147layout = QtWidgets.QVBoxLayout()
148layout.addWidget(lblPanel)
149layout.addWidget(btnPanel)
150layout.addWidget(lbl1)
151layout.addWidget(lin1)
152layout.addWidget(lbl2)
153layout.addWidget(lin2)
154layout.addWidget(lbl3)
155layout.addWidget(lin3)
156layout.addWidget(btn)
157widgetHDF.setLayout(layout)
158widgetHDF.move(500, 500)
159widgetHDF.show()
160