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(
54  "Translating %s to %s with dX: %g dY: %g dZ: %g ...."
55  % (inputFileName, outputFileName, dX, dY, dZ)
56  )
57 
58  myMesh = mesh.Mesh.from_file(inputFileName)
59 
60  myMesh.x += dX
61  myMesh.y += dY
62  myMesh.z += dZ
63 
64  myMesh.save(outputFileName)
65 
66  print(
67  "Translating %s to %s with dX: %g dT: %g dZ: %g Done!"
68  % (inputFileName, outputFileName, dX, dY, dZ)
69  )
70 elif mode == "rotate":
71  axis = str(sys.argv[4])
72  deg = float(sys.argv[5])
73 
74  print(
75  "Rotating %s to %s wrt to the %s axis by %g degree...."
76  % (inputFileName, outputFileName, axis, deg)
77  )
78 
79  # ************** NOTE ******************
80  # we have to add a minus sign here because numpy-stl is known
81  # to have a problem for the direction of the rotation. It somehow rotates
82  # clockwise for a positive angle, which is not consistent with the conventional
83  # right-hand-side rule of rotation.
84  # Check their documentation https://numpy-stl.readthedocs.io/en/latest/stl.html
85  theta = - deg * np.pi / 180.0
86 
87  myMesh = mesh.Mesh.from_file(inputFileName)
88 
89  if axis == "x":
90  myMesh.rotate([1.0, 0.0, 0.0], theta)
91  elif axis == "y":
92  myMesh.rotate([0.0, 1.0, 0.0], theta)
93  elif axis == "z":
94  myMesh.rotate([0.0, 0.0, 1.0], theta)
95  else:
96  print("Axis %s not supported! Options are: x, y, or z"%s)
97 
98  myMesh.save(outputFileName)
99 
100  print(
101  "Rotating %s to %s wrt to the %s axis by %g degree Done!"
102  % (inputFileName, outputFileName, axis, deg)
103  )
104 else:
105  print("Mode %s not supported! Options are: scale, translate, or rotate"%s)