dafoam_stltransform.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 """
3 This script transforms the coordinates in a stl file. We support three modes: scale, translate, or rotate
4 
5 Usage:
6 
7  python dafoam_plot3dscale.py scale plot3d_file_input.xyz plot3d_file_output.xyz 2 2 2
8  This will scale the x, y, and z coordinates by a factor of 2
9 
10  python dafoam_plot3dscale.py translate plot3d_file_input.xyz plot3d_file_output.xyz 1 2 3
11  This will translate the x, y, and z coordinates by 1, 2, and 3
12 
13  python dafoam_plot3dscale.py rotate plot3d_file_input.xyz plot3d_file_output.xyz x 10
14  This will rotate the x, y, and z coordinates with respect to the x axis by 10 degree
15 """
16 
17 import sys
18 from stl import mesh
19 import numpy as np
20 
21 mode = sys.argv[1]
22 
23 inputFileName = sys.argv[2]
24 outputFileName = sys.argv[3]
25 
26 if mode == "scale":
27  scaleX = float(sys.argv[4])
28  scaleY = float(sys.argv[5])
29  scaleZ = float(sys.argv[6])
30 
31  print(
32  "Scaling %s to %s with scaleX: %g scaleY: %g scaleZ: %g ...."
33  % (inputFileName, outputFileName, scaleX, scaleY, scaleZ)
34  )
35 
36  myMesh = mesh.Mesh.from_file(inputFileName)
37 
38  myMesh.x *= scaleX
39  myMesh.y *= scaleY
40  myMesh.z *= scaleZ
41 
42  myMesh.save(outputFileName)
43 
44  print(
45  "Scaling %s to %s with scaleX: %g scaleY: %g scaleZ: %g Done!"
46  % (inputFileName, outputFileName, scaleX, scaleY, scaleZ)
47  )
48 elif mode == "translate":
49  dX = float(sys.argv[4])
50  dY = float(sys.argv[5])
51  dZ = float(sys.argv[6])
52 
53  print("Translating %s to %s with dX: %g dY: %g dZ: %g ...." % (inputFileName, outputFileName, dX, dY, dZ))
54 
55  myMesh = mesh.Mesh.from_file(inputFileName)
56 
57  myMesh.x += dX
58  myMesh.y += dY
59  myMesh.z += dZ
60 
61  myMesh.save(outputFileName)
62 
63  print("Translating %s to %s with dX: %g dT: %g dZ: %g Done!" % (inputFileName, outputFileName, dX, dY, dZ))
64 elif mode == "rotate":
65  axis = str(sys.argv[4])
66  deg = float(sys.argv[5])
67 
68  print("Rotating %s to %s wrt to the %s axis by %g degree...." % (inputFileName, outputFileName, axis, deg))
69 
70  # ************** NOTE ******************
71  # we have to add a minus sign here because numpy-stl is known
72  # to have a problem for the direction of the rotation. It somehow rotates
73  # clockwise for a positive angle, which is not consistent with the conventional
74  # right-hand-side rule of rotation.
75  # Check their documentation https://numpy-stl.readthedocs.io/en/latest/stl.html
76  theta = -deg * np.pi / 180.0
77 
78  myMesh = mesh.Mesh.from_file(inputFileName)
79 
80  if axis == "x":
81  myMesh.rotate([1.0, 0.0, 0.0], theta)
82  elif axis == "y":
83  myMesh.rotate([0.0, 1.0, 0.0], theta)
84  elif axis == "z":
85  myMesh.rotate([0.0, 0.0, 1.0], theta)
86  else:
87  print("Axis %s not supported! Options are: x, y, or z" % s)
88 
89  myMesh.save(outputFileName)
90 
91  print("Rotating %s to %s wrt to the %s axis by %g degree Done!" % (inputFileName, outputFileName, axis, deg))
92 else:
93  print("Mode %s not supported! Options are: scale, translate, or rotate" % s)