MoDeNa  1.0
Software framework facilitating sequential multi-scale modelling
periodicBox.py
Go to the documentation of this file.
1 
13 import vtk
14 
28 def main(filenameIn,filenameOut,xmin,ymin,zmin,dx,dy,dz,render):
29  # print vtk.VTK_MAJOR_VERSION # Check the version
30  # Read the file and create polydata
31  reader = vtk.vtkSTLReader()
32  reader.SetFileName(filenameIn)
33  # Define planes for clipping
34  Origins=[
35  [xmin,ymin,zmin],
36  [xmin,ymin,zmin],
37  [xmin,ymin,zmin],
38  [xmin+dx,ymin+dy,zmin+dz],
39  [xmin+dx,ymin+dy,zmin+dz],
40  [xmin+dx,ymin+dy,zmin+dz],
41  ]
42  Normals=[
43  [[-1,0,0],[0,-1,0],[0,0,-1],[-1,0,0],[0,-1,0],[0,0,-1]],
44  [[+1,0,0],[0,-1,0],[0,0,-1],[-1,0,0],[0,-1,0],[0,0,-1]],
45  [[+1,0,0],[0,-1,0],[0,0,-1],[+1,0,0],[0,-1,0],[0,0,-1]],
46  [[-1,0,0],[0,+1,0],[0,0,-1],[-1,0,0],[0,-1,0],[0,0,-1]],
47  [[+1,0,0],[0,+1,0],[0,0,-1],[-1,0,0],[0,-1,0],[0,0,-1]],
48  [[+1,0,0],[0,+1,0],[0,0,-1],[+1,0,0],[0,-1,0],[0,0,-1]],
49  [[-1,0,0],[0,+1,0],[0,0,-1],[-1,0,0],[0,+1,0],[0,0,-1]],
50  [[+1,0,0],[0,+1,0],[0,0,-1],[-1,0,0],[0,+1,0],[0,0,-1]],
51  [[+1,0,0],[0,+1,0],[0,0,-1],[+1,0,0],[0,+1,0],[0,0,-1]],
52 
53  [[-1,0,0],[0,-1,0],[0,0,+1],[-1,0,0],[0,-1,0],[0,0,-1]],
54  [[+1,0,0],[0,-1,0],[0,0,+1],[-1,0,0],[0,-1,0],[0,0,-1]],
55  [[+1,0,0],[0,-1,0],[0,0,+1],[+1,0,0],[0,-1,0],[0,0,-1]],
56  [[-1,0,0],[0,+1,0],[0,0,+1],[-1,0,0],[0,-1,0],[0,0,-1]],
57  [[+1,0,0],[0,+1,0],[0,0,+1],[-1,0,0],[0,-1,0],[0,0,-1]],
58  [[+1,0,0],[0,+1,0],[0,0,+1],[+1,0,0],[0,-1,0],[0,0,-1]],
59  [[-1,0,0],[0,+1,0],[0,0,+1],[-1,0,0],[0,+1,0],[0,0,-1]],
60  [[+1,0,0],[0,+1,0],[0,0,+1],[-1,0,0],[0,+1,0],[0,0,-1]],
61  [[+1,0,0],[0,+1,0],[0,0,+1],[+1,0,0],[0,+1,0],[0,0,-1]],
62 
63  [[-1,0,0],[0,-1,0],[0,0,+1],[-1,0,0],[0,-1,0],[0,0,+1]],
64  [[+1,0,0],[0,-1,0],[0,0,+1],[-1,0,0],[0,-1,0],[0,0,+1]],
65  [[+1,0,0],[0,-1,0],[0,0,+1],[+1,0,0],[0,-1,0],[0,0,+1]],
66  [[-1,0,0],[0,+1,0],[0,0,+1],[-1,0,0],[0,-1,0],[0,0,+1]],
67  [[+1,0,0],[0,+1,0],[0,0,+1],[-1,0,0],[0,-1,0],[0,0,+1]],
68  [[+1,0,0],[0,+1,0],[0,0,+1],[+1,0,0],[0,-1,0],[0,0,+1]],
69  [[-1,0,0],[0,+1,0],[0,0,+1],[-1,0,0],[0,+1,0],[0,0,+1]],
70  [[+1,0,0],[0,+1,0],[0,0,+1],[-1,0,0],[0,+1,0],[0,0,+1]],
71  [[+1,0,0],[0,+1,0],[0,0,+1],[+1,0,0],[0,+1,0],[0,0,+1]],
72  ]
73  # Define directions for moving clipped regions
74  Direction=[
75  [dx,dy,dz],
76  [0,dy,dz],
77  [-dx,dy,dz],
78  [dx,0,dz],
79  [0,0,dz],
80  [-dx,0,dz],
81  [dx,-dy,dz],
82  [0,-dy,dz],
83  [-dx,-dy,dz],
84  [dx,dy,0],
85  [0,dy,0],
86  [-dx,dy,0],
87  [dx,0,0],
88  [0,0,0],
89  [-dx,0,0],
90  [dx,-dy,0],
91  [0,-dy,0],
92  [-dx,-dy,0],
93  [dx,dy,-dz],
94  [0,dy,-dz],
95  [-dx,dy,-dz],
96  [dx,0,-dz],
97  [0,0,-dz],
98  [-dx,0,-dz],
99  [dx,-dy,-dz],
100  [0,-dy,-dz],
101  [-dx,-dy,-dz],
102  ]
103  regions=[]
104  n=27
105  for j in xrange(n):
106  polydata=reader
107  # Clip it with all 6 planes
108  for i in xrange(6):
109  plane=vtk.vtkPlane()
110  plane.SetOrigin(Origins[i])
111  plane.SetNormal(Normals[j][i])
112  clipper = vtk.vtkClipPolyData()
113  clipper.SetInputConnection(polydata.GetOutputPort())
114  clipper.SetClipFunction(plane)
115  polydata=clipper
116  polydata.Update()
117  # Move it if not empty
118  if polydata.GetOutput().GetLength()>0:
119  transform = vtk.vtkTransform()
120  transform.Translate(Direction[j])
121  transformFilter = vtk.vtkTransformPolyDataFilter()
122  transformFilter.SetTransform(transform)
123  transformFilter.SetInputConnection(polydata.GetOutputPort())
124  transformFilter.Update()
125  regions.append(vtk.vtkPolyData())
126  regions[j].ShallowCopy(transformFilter.GetOutput())
127  else:
128  regions.append(vtk.vtkPolyData())
129  regions[j].ShallowCopy(polydata.GetOutput())
130  # Append the all regions
131  appendFilter = vtk.vtkAppendPolyData()
132  if vtk.VTK_MAJOR_VERSION <= 5:
133  for j in xrange(n):
134  appendFilter.AddInputConnection(regions[j].GetProducerPort())
135  else:
136  for j in xrange(n):
137  appendFilter.AddInputData(regions[j])
138  appendFilter.Update()
139  # Remove any duplicate points
140  cleanFilter = vtk.vtkCleanPolyData()
141  cleanFilter.SetInputConnection(appendFilter.GetOutputPort())
142  cleanFilter.Update()
143  # One more rotation - not needed
144  # transform = vtk.vtkTransform()
145  # transform.Translate(-6,-6,-6)
146  # transformFilter = vtk.vtkTransformPolyDataFilter()
147  # transformFilter.SetTransform(transform)
148  # transformFilter.SetInputConnection(cleanFilter.GetOutputPort())
149  # transformFilter.Update()
150  # transform = vtk.vtkTransform()
151  # transform.RotateWXYZ(90,1,0,0)
152  # transform.RotateWXYZ(-90,0,1,0)
153  # transformFilter2 = vtk.vtkTransformPolyDataFilter()
154  # transformFilter2.SetTransform(transform)
155  # transformFilter2.SetInputConnection(transformFilter.GetOutputPort())
156  # transformFilter2.Update()
157  # transform = vtk.vtkTransform()
158  # transform.Translate(6,6,6)
159  # transformFilter = vtk.vtkTransformPolyDataFilter()
160  # transformFilter.SetTransform(transform)
161  # transformFilter.SetInputConnection(transformFilter2.GetOutputPort())
162  # transformFilter.Update()
163  # Final data to be saved and displayed
164  finalData=cleanFilter
165  # Write the stl file to disk
166  stlWriter = vtk.vtkSTLWriter()
167  stlWriter.SetFileName(filenameOut)
168  stlWriter.SetInputConnection(finalData.GetOutputPort())
169  stlWriter.Write()
170  if render:
171  # Create mappper and actor for rendering
172  mapper = vtk.vtkPolyDataMapper()
173  if vtk.VTK_MAJOR_VERSION <= 5:
174  mapper.SetInput(finalData.GetOutput())
175  else:
176  mapper.SetInputConnection(finalData.GetOutputPort())
177  actor = vtk.vtkActor()
178  actor.SetMapper(mapper)
179  # Create a rendering window and renderer
180  ren = vtk.vtkRenderer()
181  renWin = vtk.vtkRenderWindow()
182  renWin.AddRenderer(ren)
183  # Create a renderwindowinteractor
184  iren = vtk.vtkRenderWindowInteractor()
185  iren.SetRenderWindow(renWin)
186  # Assign actor to the renderer
187  ren.AddActor(actor)
188  # Enable user interface interactor
189  iren.Initialize()
190  renWin.Render()
191  iren.Start()
def main(filenameIn, filenameOut, xmin, ymin, zmin, dx, dy, dz, render)
Main function for manipulation with RVE.
Definition: periodicBox.py:28