1
2 """Read a STL file and do simple manipulations"""
3
4 from os import path
5 from PyFoam.Error import error
6
8 """Store a complete STL-file and do simple manipulations with it"""
9
10 noName="<no name given>"
11
13 """
14 @param fName: filename of the STL-file. If None then an empty file is created
15 """
16 self._filename=fName
17
18 if fName!=None:
19 self._contents=[l.strip() for l in open(fName).readlines()]
20 else:
21 self._contents=[]
22
23 self.resetInfo()
24
26 """Set cached info to nothing"""
27 self._patchInfo=None
28
30 """The filename (without the full patch)"""
31 if self._filename==None:
32 return "<no filename given>"
33 else:
34 return path.basename(self._filename)
35
39
41 """Get info about the patches. A list of dictionaries with the relevant information"""
42 if self._patchInfo:
43 return self._patchInfo
44
45 self._patchInfo=[]
46
47 newPatch=True
48
49 e=enumerate(self._contents)
50
51 goOn=True
52 while goOn:
53 try:
54 i,l=e.next()
55 if newPatch:
56 self.expectedToken(l,"solid",i)
57 info={}
58 if len(l.split())<2:
59 info["name"]=self.noName
60 else:
61 info["name"]=l.split()[1]
62 info["start"]=i+1
63 info["facets"]=0
64 info["min"]=[1e100]*3
65 info["max"]=[-1e100]*3
66 newPatch=False
67 elif l.strip().find("endsolid")==0:
68 info["end"]=i+1
69 self._patchInfo.append(info)
70 newPatch=True
71 else:
72 self.expectedToken(l,"facet normal",i)
73 i,l=e.next()
74 self.expectedToken(l,"outer loop",i)
75 for v in range(3):
76 i,l=e.next()
77 self.expectedToken(l,"vertex",i)
78 info["min"]=[min(m) for m in zip(info["min"],
79 [float(v) for v in l.strip().split()[1:4]])]
80 info["max"]=[max(m) for m in zip(info["max"],
81 [float(v) for v in l.strip().split()[1:4]])]
82 i,l=e.next()
83 self.expectedToken(l,"endloop",i)
84 i,l=e.next()
85 self.expectedToken(l,"endfacet",i)
86 info["facets"]+=1
87 except StopIteration:
88 goOn=False
89
90
91 if not newPatch:
92 error("File",self.filename(),"seems to be incomplete")
93
94 return self._patchInfo
95
97 """Write to a file"""
98 f=open(fName,"w")
99 f.write("\n".join(self._contents))
100
102 for l in self._contents:
103 yield l
104
106 self.resetInfo()
107
108 fName=path.splitext(other.filename())[0]
109 moreThanOne=len(other.patchInfo())>1
110
111 nr=1
112
113 for l in other:
114 if l.strip().find("solid")==0:
115 parts=l.split()
116 if len(parts)==1:
117 l=parts[0]+" "+fName
118 if moreThanOne:
119 l+="_%04d" % nr
120 else:
121 l=parts[0]+" %s:%s" %(fName," ".join(parts[1:]))
122 nr+=1
123
124 self._contents.append(l)
125
126 return self
127