import sys
class writer:
def __init__(self,name):
try:
self.filename=name+".dxf"
self.dxffile=open(self.filename,'w')
except:
print "pydxf:init: Error opening dxf."
sys.exit(-1)
self.dxffile.write("0\n")
self.dxffile.write("SECTION\n")
self.dxffile.write("2\n")
self.dxffile.write("ENTITIES\n")
def point(self,layer,p):
x,y,z=p
self.dxffile.write("0\n")
self.dxffile.write("POINT\n")
self.dxffile.write("8\n")
self.dxffile.write('%s\n'%layer)
self.dxffile.write("10\n")
self.dxffile.write('%f\n'%x)
self.dxffile.write("20\n")
self.dxffile.write('%f\n'%y)
self.dxffile.write("30\n")
self.dxffile.write('%f\n'%z)
def line(self,layer,p1,p2,thick):
x1,y1,z1=p1
x2,y2,z2=p2
self.dxffile.write("0\n")
self.dxffile.write("LINE\n")
self.dxffile.write("8\n")
self.dxffile.write('%s\n'%layer)
self.dxffile.write("10\n")
self.dxffile.write('%f\n'%x1)
self.dxffile.write("20\n")
self.dxffile.write('%f\n'%y1)
self.dxffile.write("30\n")
self.dxffile.write('%f\n'%z1)
self.dxffile.write("11\n")
self.dxffile.write('%f\n'%x2)
self.dxffile.write("21\n")
self.dxffile.write('%f\n'%y2)
self.dxffile.write("31\n")
self.dxffile.write('%f\n'%z2)
self.dxffile.write("39\n")
self.dxffile.write('%f\n'%thick)
def circle(self,layer,p,radius):
x,y,z=p
self.dxffile.write("0\n")
self.dxffile.write("CIRCLE\n")
self.dxffile.write("8\n")
self.dxffile.write('%s\n'%layer)
self.dxffile.write("10\n")
self.dxffile.write('%f\n'%x)
self.dxffile.write("20\n")
self.dxffile.write('%f\n'%y)
self.dxffile.write("30\n")
self.dxffile.write('%f\n'%z)
self.dxffile.write("40\n")
self.dxffile.write('%f\n'%radius)
def text(self,layer,p,size,s):
x,y,z=p
self.dxffile.write("0\n")
self.dxffile.write("TEXT\n")
self.dxffile.write("8\n")
self.dxffile.write('%s\n'%layer)
self.dxffile.write("10\n")
self.dxffile.write('%f\n'%x)
self.dxffile.write("20\n")
self.dxffile.write('%f\n'%y)
self.dxffile.write("30\n")
self.dxffile.write('%f\n'%z)
self.dxffile.write("40\n")
self.dxffile.write('%f\n'%size)
self.dxffile.write("50\n")
self.dxffile.write('%f\n'%0.)
self.dxffile.write("1\n")
self.dxffile.write('%s\n'%s)
def face(self,layer,p1,p2,p3,p4):
x1,y1,z1=p1
x2,y2,z2=p2
x3,y3,z3=p3
x4,y4,z4=p4
self.dxffile.write("0\n")
self.dxffile.write("3DFACE\n")
self.dxffile.write("8\n")
self.dxffile.write('%s\n'%layer)
self.dxffile.write("10\n")
self.dxffile.write('%f\n'%x1)
self.dxffile.write("20\n")
self.dxffile.write('%f\n'%y1)
self.dxffile.write("30\n")
self.dxffile.write('%f\n'%z1)
self.dxffile.write("11\n")
self.dxffile.write('%f\n'%x2)
self.dxffile.write("21\n")
self.dxffile.write('%f\n'%y2)
self.dxffile.write("31\n")
self.dxffile.write('%f\n'%z2)
self.dxffile.write("12\n")
self.dxffile.write('%f\n'%x3)
self.dxffile.write("22\n")
self.dxffile.write('%f\n'%y3)
self.dxffile.write("32\n")
self.dxffile.write('%f\n'%z3)
self.dxffile.write("13\n")
self.dxffile.write('%f\n'%x4)
self.dxffile.write("23\n")
self.dxffile.write('%f\n'%y4)
self.dxffile.write("33\n")
self.dxffile.write('%f\n'%z4)
def close(self):
self.dxffile.write("0\n")
self.dxffile.write("ENDSEC\n")
self.dxffile.write("0\n")
self.dxffile.write("EOF\n")
self.dxffile.close()
# uncomment to test
# x=writer("test")
# x.line("lines", (0.,0.,0.), (1.,1.,0.), 0.)
# x.face("faces", (1.,1.,0.), (2.,1.,0.), (2.,2.,0.), (1.,2.,0.))
# x.point("nodes", (3.,3.,3.))
# x.circle("circs", (3.,3.,3.), 1.)
# x.text("texts", (3.,3.,3.),0.1,"c")
# print x.filename
# x.close()