2 # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 # + This file is part of enGrid. +
6 # + Copyright 2008-2014 enGits GmbH +
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. +
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. +
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/>. +
21 # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29 def __init__(self
, file_name
):
30 self
.file_name
= file_name
32 self
.setResolution(10)
33 self
.setTolerance(0.005)
36 def setResolution(self
, N
):
39 def setTolerance(self
, t
):
43 cmd
= "mged -c " + self
.file_name
+ " ls -l"
44 lines
= commands
.getoutput(cmd
).split("\n");
48 self
.object_type
[words
[0]] = words
[1]
51 cmd
= "rm -rf " + self
.file_name
52 commands
.getoutput(cmd
)
54 def mged(self
, *args
):
55 cmd
= "mged -c " + self
.file_name
+ " '"
65 pcmd
= cmd
[:80] + ' ...'
68 output
= commands
.getoutput(cmd
)
73 return self
.object_type
.keys()
75 def getSubObjects(self
, object_name
):
77 lines
= self
.mged("l", object_name
).split("\n")
81 if words
[0] == "u" or words
[0] == "-" or words
[0] == "+":
82 sub_objects
.append(words
[1])
85 def getSolids(self
, object_name
):
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
)
91 solids
.append(object_name
)
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;
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
):
109 solids
= self
.getSolids(object_name
)
110 dir_name
= object_name
.split(".")[0] + ".gegc"
111 cmd
= "rm -rf " + dir_name
;
113 commands
.getoutput(cmd
)
114 cmd
= "mkdir " + dir_name
116 commands
.getoutput(cmd
)
117 file_name
= dir_name
+ "/volume.stl"
118 self
.writeStl(object_name
, file_name
)
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
):
136 H
= math
.sqrt(nx
*nx
+ ny
*ny
+ nz
*nz
)
148 while math
.fabs(scal
) > 1e-6 and count
< 1000:
154 L
= math
.sqrt(vx
*vx
+ vy
*vy
)
155 scal
= (vx
*xt
+ vy
*yt
)/(L
*r
)
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
):
182 num_nodes
= int(line
.split()[0])
183 num_faces
= int(line
.split()[1])
185 for i
in range(num_nodes
):
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()
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
):
210 H
= math
.sqrt(nx
*nx
+ ny
*ny
+ nz
*nz
)
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
)