updated to modern VTK
[engrid-github.git] / src / pymodules / pymged.py
blob7418a135f280ab8e425477a339d25330f7a03ddf
1 #!/usr/bin/python
2 # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 # + +
4 # + This file is part of enGrid. +
5 # + +
6 # + Copyright 2008-2014 enGits GmbH +
7 # + +
8 # + enGrid is free software: you can redistribute it and/or modify +
9 # + it under the terms of the GNU General Public License as published by +
10 # + the Free Software Foundation, either version 3 of the License, or +
11 # + (at your option) any later version. +
12 # + +
13 # + enGrid is distributed in the hope that it will be useful, +
14 # + but WITHOUT ANY WARRANTY; without even the implied warranty of +
15 # + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
16 # + GNU General Public License for more details. +
17 # + +
18 # + You should have received a copy of the GNU General Public License +
19 # + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
20 # + +
21 # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22 import commands
23 import sys
24 import math
25 import time
27 class DataBase:
29 def __init__(self, file_name):
30 self.file_name = file_name
31 self.object_type = {}
32 self.setResolution(10)
33 self.setTolerance(0.005)
34 self.update()
36 def setResolution(self, N):
37 self.resolution = N
39 def setTolerance(self, t):
40 self.tolerance = t
42 def update(self):
43 cmd = "mged -c " + self.file_name + " ls -l"
44 lines = commands.getoutput(cmd).split("\n");
45 for line in lines:
46 words = line.split()
47 if len(words) > 1:
48 self.object_type[words[0]] = words[1]
50 def clear(self):
51 cmd = "rm -rf " + self.file_name
52 commands.getoutput(cmd)
54 def mged(self, *args):
55 cmd = "mged -c " + self.file_name + " '"
56 space = 0
57 for arg in args:
58 if space == 1:
59 cmd += " "
60 space = 1
61 cmd += str(arg)
62 cmd += "'"
63 pcmd = cmd
64 if len(cmd) > 80:
65 pcmd = cmd[:80] + ' ...'
66 print pcmd
67 self.update()
68 output = commands.getoutput(cmd)
69 print output
70 return output
72 def getObjects(self):
73 return self.object_type.keys()
75 def getSubObjects(self, object_name):
76 sub_objects = []
77 lines = self.mged("l", object_name).split("\n")
78 for line in lines:
79 words = line.split()
80 if len(words) >= 2:
81 if words[0] == "u" or words[0] == "-" or words[0] == "+":
82 sub_objects.append(words[1])
83 return sub_objects
85 def getSolids(self, object_name):
86 solids = []
87 if self.object_type[object_name] == "region" or self.object_type[object_name] == "comb":
88 for obj in self.getSubObjects(object_name):
89 solids += self.getSolids(obj)
90 else:
91 solids.append(object_name)
92 return solids
94 def writeStl(self, object_name, file_name="undefined"):
95 if file_name == "undefined":
96 file_name = object_name.split(".")[0] + ".stl"
97 cmd = "g-stl -b -D " + str(self.tolerance) + " -n " + str(math.pi/self.resolution) + " -o " + file_name + " " + self.file_name + " " + object_name;
98 #cmd = "g-stl -b -n " + str(math.pi/self.resolution) + " -o " + file_name + " " + self.file_name + " " + object_name;
99 #cmd = "g-stl -b -r " + str(self.tolerance) + " -o " + file_name + " " + self.file_name + " " + object_name;
100 #cmd = "g-stl -b -r " + str(self.resolution) + " -o " + file_name + " " + self.file_name + " " + object_name;
101 print cmd
102 return commands.getoutput(cmd)
104 def exportStl(self, object_name):
105 self.writeStl(object_name, self.file_name + ".stl")
107 def exportToEngrid(self, object_name):
108 time.sleep(5)
109 solids = self.getSolids(object_name)
110 dir_name = object_name.split(".")[0] + ".gegc"
111 cmd = "rm -rf " + dir_name;
112 print cmd
113 commands.getoutput(cmd)
114 cmd = "mkdir " + dir_name
115 print cmd
116 commands.getoutput(cmd)
117 file_name = dir_name + "/volume.stl"
118 self.writeStl(object_name, file_name)
119 for solid in solids:
120 file_name = dir_name + "/" + solid.split(".")[0] + ".s.stl"
121 self.writeStl(solid, file_name)
123 def createSphere(self, name, x, y, z, radius):
124 self.mged("in", name, "sph", x, y, z, radius)
126 def createCylinder(self, name, x1, y1, z1, x2, y2, z2, radius):
127 self.mged("in", name, "rcc", x1, y1, z1, x2-x1, y2-y1, z2-z1, radius);
129 def createCone(self, name, x1, y1, z1, x2, y2, z2, radius1, radius2):
130 self.mged("in", name, "trc", x1, y1, z1, x2-x1, y2-y1, z2-z1, radius1, radius2);
132 def createConeWithNose(self, name, x1, y1, z1, x2, y2, z2, R, r):
133 nx = x1-x2
134 ny = y1-y2
135 nz = z1-z2
136 H = math.sqrt(nx*nx + ny*ny + nz*nz)
137 nx /= H
138 ny /= H
139 nz /= H
140 xe = R;
141 ye = r - H
142 scal = 1
143 a1 = 0
144 a2 = 0.5*math.pi
145 xt = 0
146 yt = 0
147 count = 0
148 while math.fabs(scal) > 1e-6 and count < 1000:
149 a = 0.5*(a1 + a2)
150 xt = r*math.cos(a)
151 yt = r*math.sin(a)
152 vx = xt - xe
153 vy = yt - ye
154 L = math.sqrt(vx*vx + vy*vy)
155 scal = (vx*xt + vy*yt)/(L*r)
156 if scal > 0:
157 a2 = a
158 else:
159 a1 = a
160 count += 1
161 xs = x1 - r*nx
162 ys = y1 - r*ny
163 zs = z1 - r*nz
164 xc = xs + yt*nx
165 yc = ys + yt*ny
166 zc = zs + yt*nz
168 #self.mged("in", name + "_part.s", "part", x1, y1, z1, xc-x1, yc-y1, zc-z1, R, xt)
169 #self.createCylinder(name + "_rcc", x1, y1, z1, x1 - 1.1*R*nx, y1 - 1.1*R*ny, z1 - 1.1*R*nz, 1.1*R)
171 self.createCone(name + "_cone", x2, y2, z2, xc, yc, zc, R, xt)
172 h = 2*self.tolerance*r
173 self.createSphere(name + "_sphere", xs, ys, zs, math.sqrt(r*r + 0.25*h*h))
174 self.mged("r", name, "u", name + "_cone", "u", name + "_sphere")
176 def createBox(self, name, x1, y1, z1, x2, y2, z2):
177 self.mged("in", name, "rpp", x1, x2, y1, y2, z1, z2)
179 def importBot(self, name, file_name, scale, Dx, Dy, Dz):
180 f = open(file_name)
181 line = f.readline()
182 num_nodes = int(line.split()[0])
183 num_faces = int(line.split()[1])
184 cmd = ""
185 for i in range(num_nodes):
186 line = f.readline()
187 x = scale*float(line.split()[0]) + Dx
188 y = scale*float(line.split()[1]) + Dy
189 z = scale*float(line.split()[2]) + Dz
190 cmd += " " + str(x) + " " + str(y) + " " + str(z)
191 for i in range(num_faces):
192 words = f.readline().split()
193 for word in words:
194 cmd += " " + word
195 self.mged("in", name, "bot", num_nodes, num_faces, 2, 2, cmd)
197 def rotX(self, name, angle, x=0, y=0, z=0):
198 self.mged("e", name, ";", "oed /", name, ";", "keypoint", x, y, z, ";", "rot", angle, 0, 0, ";", "accept;")
200 def createWedge(self, name, x1, y1, z1, x2, y2, z2, x3, y3, z3, L):
201 ux = x2 - x1
202 uy = y2 - y1
203 uz = z2 - z1
204 vx = x3 - x1
205 vy = y3 - y1
206 vz = z3 - z1
207 nx = uy*vz - uz*vy
208 ny = uz*vx - ux*vz
209 nz = ux*vy - uy*vx
210 H = math.sqrt(nx*nx + ny*ny + nz*nz)
211 nx *= L/H
212 ny *= L/H
213 nz *= L/H
214 self.mged("in", name, "arb6", x1, y1, z1, x2, y2, z2, x2+nx, y2+ny, z2+nz, x1+nx, y1+ny, z1+nz, x3, y3, z3, x3+nx, y3+ny, z3+nz)