def rotate(self,beta,axis): if self._freeze: print("ERROR: Solid can not be changed!") print(" (function rotate())") hf.flush_output() hf.exit(1) print("Rotating solid \""+self._name+"\" by angle: "+str(beta)+" around "+axis.upper()+"-axis") alpha = math.radians(float(beta)) matrix = [[math.cos(alpha),-math.sin(alpha)],[math.sin(alpha),math.cos(alpha)]] if axis.lower() == "x": for i, [y,z] in enumerate(zip(self._pointsY,self._pointsZ)): self._pointsY[i] = y*matrix[0][0] + z*matrix[0][1] self._pointsZ[i] = y*matrix[1][0] + z*matrix[1][1] for i, [ny,nz] in enumerate(zip(self._normalY,self._normalZ)): self._normalY[i] =ny*matrix[0][0] +nz*matrix[0][1] self._normalZ[i] =ny*matrix[1][0] +nz*matrix[1][1] elif axis.lower() == "y": for i, [z,x] in enumerate(zip(self._pointsZ,self._pointsX)): self._pointsZ[i] = z*matrix[0][0] + x*matrix[0][1] self._pointsX[i] = z*matrix[1][0] + x*matrix[1][1] for i, [nz,nx] in enumerate(zip(self._normalZ,self._normalX)): self._normalZ[i] =nz*matrix[0][0] +nx*matrix[0][1] self._normalX[i] =nz*matrix[1][0] +nx*matrix[1][1] elif axis.lower() == "z": for i, [x,y] in enumerate(zip(self._pointsX,self._pointsY)): self._pointsX[i] = x*matrix[0][0] + y*matrix[0][1] self._pointsY[i] = x*matrix[1][0] + y*matrix[1][1] for i, [nx,ny] in enumerate(zip(self._normalX,self._normalY)): self._normalX[i] =nx*matrix[0][0] +ny*matrix[0][1] self._normalY[i] =nx*matrix[1][0] +ny*matrix[1][1] else: print("ERROR in rotate") sys.exit(1)
def blockMesh(self): if not hf.run(["blockMesh"], self._snappydir) == 0: print("ERROR while writing blockMesh") hf.exit(1) else: self.setMeshCreated()
def addTriangle(self,nx,ny,nz): if self._freeze: print("ERROR: Solid can not be changed!") print(" (function addTriangle())") hf.flush_output() hf.exit(1) self._normalX.append(float(nx)) self._normalY.append(float(ny)) self._normalZ.append(float(nz)) self._nTriangles += 1
def addPoint(self,x,y,z): if self._freeze: print("ERROR: Solid can not be changed!") print(" (function addPoint())") hf.flush_output() hf.exit(1) self._pointsX.append(float(x)) self._pointsY.append(float(y)) self._pointsZ.append(float(z)) self._nVertices += 1
def repairAndWriteToFile(self,filename,directory="stl_final"): import tempfile if self._freeze: print("ERROR: Solid can not be changed!") print(" (function repair())") hf.flush_output() hf.exit(1) path_filename=os.path.abspath(__file__) executablesDir = os.path.split(path_filename)[0] # create and open temporary file mytempfile = tempfile.NamedTemporaryFile(suffix='.stl',dir='.') # get name of file so we have a name for a temporary file filename_start = os.path.split(mytempfile.name)[-1] # close the file (which will delete it) mytempfile.close() print("creating temporary stl: "+str(filename_start)) self.writeToFile(filename_start,".") # create and open temporary file mytempfile = tempfile.NamedTemporaryFile(suffix='.stl',dir='.') # get name of file so we have a name for a temporary file filename_next = os.path.split(mytempfile.name)[-1] # close the file (which will delete it) mytempfile.close() print("creating temporary stl: "+str(filename_next)) command = [] command.append(os.path.join(executablesDir,"admesh")) command.append("-nufdv") command.append("--write-ascii-stl="+filename_next) command.append(filename_start) hf.run(command) print("creating stl: "+str(os.path.join(directory,filename))) stl = open(os.path.join(directory,filename),'w') for line in open(filename_next): elements = line.lower().split() if elements[0] == "solid": stl.write("solid "+self._name+"\n") elif elements[0] == "endsolid": stl.write("endsolid "+self._name+"\n") else: stl.write(line) stl.close() print("removing temporary file: "+filename_start) os.remove(filename_start) print("removing temporary file: "+filename_next) os.remove(filename_next)
def baseProjectPage(self): print("=============") print("= Projects =") print("=============\n") questionair = {} print("Active Projects:") j = -1 for i in range(len(self._projectlist)): if self._projectlist[i] in self._active_projectlist: j += 1 print("("+str(j)+") "+self._projectlist[i].statusShort()) questionair[str(j)] = i print("\nInctive Projects:") for i in range(len(self._projectlist)): if self._projectlist[i] not in self._active_projectlist: j += 1 print("("+str(j)+") "+self._projectlist[i].statusShort()) questionair[str(j)] = i print("\na: prepare all active projects") questionair["a"] = -1 print("c: prepare all active projects without grid check") questionair["c"] = -4 print("b: back") questionair["b"] = -3 print("x: to exit") questionair["x"] = -2 userchoice = self.query_project("Select project to switch active/inactive or a/c/b/x: ",questionair) if userchoice == -3: self.run() elif userchoice == -2: hf.exit(0) elif userchoice == -1: for i in range(len(self._active_projectlist)): self._active_projectlist[i].create() self._active_projectlist[i].createGrid() for i in range(len(self._active_projectlist)): self._active_projectlist[i].checkGrid() for i in range(len(self._active_projectlist)): self._active_projectlist[i].copyMesh() elif userchoice == -4: for i in range(len(self._active_projectlist)): self._active_projectlist[i].create() self._active_projectlist[i].createGrid() for i in range(len(self._active_projectlist)): self._active_projectlist[i].copyMesh() else: if self._projectlist[userchoice] in self._active_projectlist: self._active_projectlist.remove(self._projectlist[userchoice]) else: self._active_projectlist.append(self._projectlist[userchoice]) self.baseProjectPage()
def scale(self,factor): if self._freeze: print("ERROR: Solid can not be changed!") print(" (function scale())") hf.flush_output() hf.exit(1) print("Scaling solid \""+self._name+"\" by factor: "+str(factor)) for i, [x,y,z] in enumerate(zip(self._pointsX,self._pointsY,self._pointsZ)): self._pointsX[i] = float(factor)*x self._pointsY[i] = float(factor)*y self._pointsZ[i] = float(factor)*z
def move(self,dx,dy,dz): if self._freeze: print("ERROR: Solid can not be changed!") print(" (function move())") hf.flush_output() hf.exit(1) print("Moving solid \""+self._name+"\" by (dx/dy/dz): "+str(dx)+","+str(dy)+","+str(dz)) for i, [x,y,z] in enumerate(zip(self._pointsX,self._pointsY,self._pointsZ)): self._pointsX[i] = x + float(dx) self._pointsY[i] = y + float(dy) self._pointsZ[i] = z + float(dz)
def repair(self): import tempfile if self._freeze: print("ERROR: Solid can not be changed!") print(" (function repair())") hf.flush_output() hf.exit(1) path_filename=os.path.abspath(__file__) executablesDir = os.path.split(path_filename)[0] # create and open temporary file mytempfile = tempfile.NamedTemporaryFile(suffix='.stl',dir='.') # get name of file so we have a name for a temporary file filename_start = os.path.split(mytempfile.name)[-1] # close the file (which will delete it) mytempfile.close() print("creating temporary stl: "+str(filename_start)) self.writeToFile(filename_start,".") # create and open temporary file mytempfile = tempfile.NamedTemporaryFile(suffix='.stl',dir='.') # get name of file so we have a name for a temporary file filename_next = os.path.split(mytempfile.name)[-1] # close the file (which will delete it) mytempfile.close() print("creating temporary stl: "+str(filename_next)) command = [] command.append(os.path.join(executablesDir,"admesh")) command.append("-nufdv") command.append("--write-ascii-stl="+filename_next) command.append(filename_start) hf.run(command) self.reset() for line in open(filename_next): elements = line.lower().split() if elements[0] == "solid": self._name = elements[1] if elements[0] == "facet": self.addTriangle(elements[2],elements[3],elements[4]) if elements[0] == "vertex": self.addPoint(elements[1],elements[2],elements[3]) print("removing temporary file: "+filename_start) os.remove(filename_start) print("removing temporary file: "+filename_next) os.remove(filename_next)
def reset(self): if self._freeze: print("ERROR: Solid can not be changed!") print(" (function reset())") hf.flush_output() hf.exit(1) self._nTriangles = 0 self._nVertices = 0 self._pointsX = [] self._pointsY = [] self._pointsZ = [] self._normalX = [] self._normalY = [] self._normalZ = []
def selectProjectToRun(self,withGridCheck=True): if withGridCheck: print("===============================") print("= Select a project to prepare =") print("===============================\n") else: print("==================================================") print("= Select a project to prepare without grid check =") print("==================================================\n") questionair = {} for i in range(len(self._projectlist)): print("("+str(i)+") "+self._projectlist[i].statusShort()) questionair[str(i)] = i print("a: to prepare all projects") questionair["a"] = -1 print("b: back") questionair["b"] = -3 print("x: to exit") questionair["x"] = -2 userchoice = self.query_project("Please select: ",questionair) if userchoice == -3: self.run() elif userchoice == -2: hf.exit(0) elif userchoice == -1: for i in range(len(self._projectlist)): self._projectlist[i].create() self._projectlist[i].createGrid() if withGridCheck: for i in range(len(self._projectlist)): self._projectlist[i].checkGrid() for i in range(len(self._projectlist)): self._projectlist[i].copyMesh() else: self._projectlist[userchoice].create() self._projectlist[userchoice].createGrid() if withGridCheck: self._projectlist[userchoice].checkGrid() self._projectlist[userchoice].copyMesh() self.selectProjectToRun()
def checkObjects(self): if not self._projectdir: print("ERROR: Project->createFolders") print(" project-directory not assigned yet") print(" call \"createProjectName\" first...") hf.flush_output() hf.exit(1) if not self._snappy: print("ERROR: Project->createFolders") print(" snappy object not allocated!") hf.flush_output() hf.exit(1) if not self._domain: print("ERROR: Project->createFolders") print(" snappy object not allocated!") hf.flush_output() hf.exit(1)
def checkFolders(self): self.checkObjects() if not self._projectdir: print("ERROR: Project->checkFolders") print(" project-directory not assigned yet") print(" call \"createProjectName\" first...") hf.flush_output() hf.exit(1) if not self._snappy.foldersExist(): print("ERROR: Project->checkFolders") print(" snappy folders not yet created!") hf.flush_output() hf.exit(1) if not self._domain.foldersExist(): print("ERROR: Project->checkFolders") print(" domain folders not yet created!") hf.flush_output() hf.exit(1)
def exit(self): hf.exit(0)
def checksolid(self, whichfunction): if not self._solid: print("ERROR: Domain -> no solid defined!") print(" in " + whichfunction + "!") hf.flush_output() hf.exit(1)