DXF-export update: dxfLibrary.py v1.33 - 2009.06.16

- fix _point(): converts all coords to floats
 - fix LineType class: implement elements
 - added VPORT class, incl. defaults
 - fix Insert class
This commit is contained in:
Remigiusz Fiedler 2009-06-16 10:43:43 +00:00
parent 0a66e24bd7
commit 1025b750ee

@ -1,6 +1,6 @@
#dxfLibrary.py : provides functions for generating DXF files #dxfLibrary.py : provides functions for generating DXF files
# -------------------------------------------------------------------------- # --------------------------------------------------------------------------
__version__ = "v1.32 - 2009.06.06" __version__ = "v1.33 - 2009.06.16"
__author__ = "Stani Michiels(Stani), Remigiusz Fiedler(migius)" __author__ = "Stani Michiels(Stani), Remigiusz Fiedler(migius)"
__license__ = "GPL" __license__ = "GPL"
__url__ = "http://wiki.blender.org/index.php/Scripts/Manual/Export/autodesk_dxf" __url__ = "http://wiki.blender.org/index.php/Scripts/Manual/Export/autodesk_dxf"
@ -20,8 +20,14 @@ IDEAs:
TODO: TODO:
- add support for DXFr14 (needs extended file header) - add support for DXFr14 (needs extended file header)
- add support for SPLINEs (possible first in DXFr14 version) - add support for SPLINEs (possible first in DXFr14 version)
- add user preset for floating point precision (3-16?)
History History
v1.33 - 2009.06.16 by migius
- modif _point(): converts all coords to floats
- modif LineType class: implement elements
- added VPORT class, incl. defaults
- fix Insert class
v1.32 - 2009.06.06 by migius v1.32 - 2009.06.06 by migius
- modif Style class: changed defaults to widthFactor=1.0, obliqueAngle=0.0 - modif Style class: changed defaults to widthFactor=1.0, obliqueAngle=0.0
- modif Text class: alignment parameter reactivated - modif Text class: alignment parameter reactivated
@ -88,7 +94,7 @@ _HEADER_POINTS=['insbase','extmin','extmax']
def _point(x,index=0): def _point(x,index=0):
"""Convert tuple to a dxf point""" """Convert tuple to a dxf point"""
#print 'deb: _point=', x #------------- #print 'deb: _point=', x #-------------
return '\n'.join([' %s\n%s'%((i+1)*10+index,x[i]) for i in range(len(x))]) return '\n'.join([' %s\n%s'%((i+1)*10+index,float(x[i])) for i in range(len(x))])
def _points(plist): def _points(plist):
"""Convert a list of tuples to dxf points""" """Convert a list of tuples to dxf points"""
@ -287,7 +293,7 @@ class Insert(_Entity):
self.rotation=rotation self.rotation=rotation
def __str__(self): def __str__(self):
result=' 0\nINSERT\n 2\n%s\n%s\n%s\n'%\ result=' 0\nINSERT\n 2\n%s\n%s%s\n'%\
(self.name,self._common(),_point(self.point)) (self.name,self._common(),_point(self.point))
if self.xscale!=None:result+=' 41\n%s\n'%self.xscale if self.xscale!=None:result+=' 41\n%s\n'%self.xscale
if self.yscale!=None:result+=' 42\n%s\n'%self.yscale if self.yscale!=None:result+=' 42\n%s\n'%self.yscale
@ -535,15 +541,22 @@ class Layer(_Call):
#----------------------------------------------- #-----------------------------------------------
class LineType(_Call): class LineType(_Call):
"""Custom linetype""" """Custom linetype"""
def __init__(self,name='continuous',description='Solid line',elements=[],flag=64): def __init__(self,name='CONTINUOUS',description='Solid line',elements=[0.0],flag=0):
# TODO: Implement lineType elements
self.name=name self.name=name
self.description=description self.description=description
self.elements=copy.copy(elements) self.elements=copy.copy(elements)
self.flag=flag self.flag=flag
def __str__(self): def __str__(self):
return ' 0\nLTYPE\n 2\n%s\n 70\n%s\n 3\n%s\n 72\n65\n 73\n%s\n 40\n0.0\n'%\ result = ' 0\nLTYPE\n 2\n%s\n 70\n%s\n 3\n%s\n 72\n65\n'%\
(self.name.upper(),self.flag,self.description,len(self.elements)) (self.name.upper(),self.flag,self.description)
if self.elements:
elements = ' 73\n%s\n' %(len(self.elements)-1)
elements += ' 40\n%s\n' %(self.elements[0])
for e in self.elements[1:]:
elements += ' 49\n%s\n' %e
result += elements
return result
#----------------------------------------------- #-----------------------------------------------
class Style(_Call): class Style(_Call):
@ -566,27 +579,137 @@ class Style(_Call):
self.font.upper(),self.bigFont.upper()) self.font.upper(),self.bigFont.upper())
#----------------------------------------------- #-----------------------------------------------
class View(_Call): class VPort(_Call):
def __init__(self,name,flag=0,width=1,height=1,center=(0.5,0.5), def __init__(self,name,flag=0,
direction=(0,0,1),target=(0,0,0),lens=50, leftBottom=(0.0,0.0),
frontClipping=0,backClipping=0,twist=0,mode=0): rightTop=(1.0,1.0),
center=(0.5,0.5),
snap_base=(0.0,0.0),
snap_spacing=(0.1,0.1),
grid_spacing=(0.1,0.1),
direction=(0.0,0.0,1.0),
target=(0.0,0.0,0.0),
height=1.0,
ratio=1.0,
lens=50,
frontClipping=0,
backClipping=0,
snap_rotation=0,
twist=0,
mode=0,
circle_zoom=100,
fast_zoom=1,
ucsicon=1,
snap_on=0,
grid_on=0,
snap_style=0,
snap_isopair=0
):
self.name=name self.name=name
self.flag=flag self.flag=flag
self.width=width self.leftBottom=leftBottom
self.height=height self.rightTop=rightTop
self.center=center
self.snap_base=snap_base
self.snap_spacing=snap_spacing
self.grid_spacing=grid_spacing
self.direction=direction
self.target=target
self.height=float(height)
self.ratio=float(ratio)
self.lens=float(lens)
self.frontClipping=float(frontClipping)
self.backClipping=float(backClipping)
self.snap_rotation=float(snap_rotation)
self.twist=float(twist)
self.mode=mode
self.circle_zoom=circle_zoom
self.fast_zoom=fast_zoom
self.ucsicon=ucsicon
self.snap_on=snap_on
self.grid_on=grid_on
self.snap_style=snap_style
self.snap_isopair=snap_isopair
def __str__(self):
output = [' 0', 'VPORT',
' 2', self.name,
' 70', self.flag,
_point(self.leftBottom),
_point(self.rightTop,1),
_point(self.center,2), # View center point (in DCS)
_point(self.snap_base,3),
_point(self.snap_spacing,4),
_point(self.grid_spacing,5),
_point(self.direction,6), #view direction from target (in WCS)
_point(self.target,7),
' 40', self.height,
' 41', self.ratio,
' 42', self.lens,
' 43', self.frontClipping,
' 44', self.backClipping,
' 50', self.snap_rotation,
' 51', self.twist,
' 71', self.mode,
' 72', self.circle_zoom,
' 73', self.fast_zoom,
' 74', self.ucsicon,
' 75', self.snap_on,
' 76', self.grid_on,
' 77', self.snap_style,
' 78', self.snap_isopair
]
output_str = ''
for s in output:
output_str += '%s\n' %s
return output_str
#-----------------------------------------------
class View(_Call):
def __init__(self,name,flag=0,
width=1,
height=1,
center=(0.5,0.5),
direction=(0,0,1),
target=(0,0,0),
lens=50,
frontClipping=0,
backClipping=0,
twist=0,mode=0
):
self.name=name
self.flag=flag
self.width=float(width)
self.height=float(height)
self.center=center self.center=center
self.direction=direction self.direction=direction
self.target=target self.target=target
self.lens=lens self.lens=float(lens)
self.frontClipping=frontClipping self.frontClipping=float(frontClipping)
self.backClipping=backClipping self.backClipping=float(backClipping)
self.twist=twist self.twist=float(twist)
self.mode=mode self.mode=mode
def __str__(self): def __str__(self):
return ' 0\nVIEW\n 2\n%s\n 70\n%s\n 40\n%s\n%s\n 41\n%s\n%s\n%s\n 42\n%s\n 43\n%s\n 44\n%s\n 50\n%s\n 71\n%s\n'%\ output = [' 0', 'VIEW',
(self.name,self.flag,self.height,_point(self.center),self.width, ' 2', self.name,
_point(self.direction,1),_point(self.target,2),self.lens, ' 70', self.flag,
self.frontClipping,self.backClipping,self.twist,self.mode) ' 40', self.height,
_point(self.center),
' 41', self.width,
_point(self.direction,1),
_point(self.target,2),
' 42', self.lens,
' 43', self.frontClipping,
' 44', self.backClipping,
' 50', self.twist,
' 71', self.mode
]
output_str = ''
for s in output:
output_str += '%s\n' %s
return output_str
#----------------------------------------------- #-----------------------------------------------
def ViewByWindow(name,leftBottom=(0,0),rightTop=(1,1),**options): def ViewByWindow(name,leftBottom=(0,0),rightTop=(1,1),**options):
@ -601,7 +724,7 @@ class Drawing(_Collection):
"""Dxf drawing. Use append or any other list methods to add objects.""" """Dxf drawing. Use append or any other list methods to add objects."""
def __init__(self,insbase=(0.0,0.0,0.0),extmin=(0.0,0.0,0.0),extmax=(0.0,0.0,0.0), def __init__(self,insbase=(0.0,0.0,0.0),extmin=(0.0,0.0,0.0),extmax=(0.0,0.0,0.0),
layers=[Layer()],linetypes=[LineType()],styles=[Style()],blocks=[], layers=[Layer()],linetypes=[LineType()],styles=[Style()],blocks=[],
views=[],entities=None,fileName='test.dxf'): views=[],vports=[],entities=None,fileName='test.dxf'):
# TODO: replace list with None,arial # TODO: replace list with None,arial
if not entities: if not entities:
entities=[] entities=[]
@ -613,6 +736,7 @@ class Drawing(_Collection):
self.linetypes=copy.copy(linetypes) self.linetypes=copy.copy(linetypes)
self.styles=copy.copy(styles) self.styles=copy.copy(styles)
self.views=copy.copy(views) self.views=copy.copy(views)
self.vports=copy.copy(vports)
self.blocks=copy.copy(blocks) self.blocks=copy.copy(blocks)
self.fileName=fileName self.fileName=fileName
#private #private
@ -656,7 +780,8 @@ class Drawing(_Collection):
header=[self.acadver]+[self._point(attr,getattr(self,attr))+'\n' for attr in _HEADER_POINTS] header=[self.acadver]+[self._point(attr,getattr(self,attr))+'\n' for attr in _HEADER_POINTS]
header=self._section('header',header) header=self._section('header',header)
tables=[self._table('ltype',[str(x) for x in self.linetypes]), tables=[self._table('vport',[str(x) for x in self.vports]),
self._table('ltype',[str(x) for x in self.linetypes]),
self._table('layer',[str(x) for x in self.layers]), self._table('layer',[str(x) for x in self.layers]),
self._table('style',[str(x) for x in self.styles]), self._table('style',[str(x) for x in self.styles]),
self._table('view',[str(x) for x in self.views]), self._table('view',[str(x) for x in self.views]),
@ -740,17 +865,16 @@ def test():
d.append(Line(points=[(0,0,0),(1,1,1)])) d.append(Line(points=[(0,0,0),(1,1,1)]))
d.append(Mtext('Click on Ads\nmultiple lines with mtext',point=(1,1,1),color=5,rotation=90)) d.append(Mtext('Click on Ads\nmultiple lines with mtext',point=(1,1,1),color=5,rotation=90))
d.append(Text('Please donate!',point=(3,0,1))) d.append(Text('Please donate!',point=(3,0,1)))
d.append(Rectangle(point=(2,2,2),width=4,height=3,color=6,solid=Solid(color=2))) #d.append(Rectangle(point=(2,2,2),width=4,height=3,color=6,solid=Solid(color=2)))
d.append(Solid(points=[(4,4,0),(5,4,0),(7,8,0),(9,9,0)],color=3)) d.append(Solid(points=[(4,4,0),(5,4,0),(7,8,0),(9,9,0)],color=3))
d.append(PolyLine(points=[(1,1,1),(2,1,1),(2,2,1),(1,2,1)],closed=1,color=1)) #d.append(PolyLine(points=[(1,1,1),(2,1,1),(2,2,1),(1,2,1)],flag=1,color=1))
#d.saveas('c:\\test.dxf') #d.saveas('c:\\test.dxf')
d.saveas('test.dxf') d.saveas('test.dxf')
#----------------------------------------------------- #-----------------------------------------------------
if __name__=='__main__': if __name__=='__main__':
if not copy: if not copy:
Draw.PupMenu('Error%t|This script requires a full python install') Draw.PupMenu('Error%t|This script requires a full python install')
else: main() else: test()