📄 sdxf.py
字号:
#(c)www.stani.be (read __doc__ for more information) __version__ = """v1.1 (20/6/2005)"""__author__ = "www.stani.be"__license__ = "GPL"__url__ = "http://www.stani.be/python/sdxf"__doc__ = \"""SDXF - Stani's DXFPython library to generate dxf drawingsCopyright %sVersion %sLicense %sHomepage %sSee the homepage for documentation.This library is created and best editable in SPE.(http://www.stani.be/python/spe)Donate!If you find this library usefull, please do considera donation by paypal for s_t_a_n_i@yahoo.com""" % \(__author__,__version__,__license__,__url__)# TODO: support for Numeric/Numarray for speeding up#_______________________________________________________________________________import copy####1) Private (only for developpers)_HEADER_POINTS=['insbase','extmin','extmax']#---helper functionsdef _point(x,index=0): """Convert tuple to a dxf point""" return '\n'.join(['%s\n%s'%((i+1)*10+index,x[i]) for i in range(len(x))])def _points(p): """Convert a list of tuples to dxf points""" return [_point(p[i],i)for i in range(len(p))]#---base classesclass _Call: """Makes a callable class.""" def copy(self): """Returns a copy.""" return copy.deepcopy(self) def __call__(self,**attrs): """Returns a copy with modified attributes.""" copied=self.copy() for attr in attrs:setattr(copied,attr,attrs[attr]) return copied class _Entity(_Call): """Base class for _common group codes for entities.""" def __init__(self,color=None,extrusion=None,layer='0', lineType=None,lineTypeScale=None,lineWeight=None, thickness=None,parent=None): """None values will be omitted.""" self.color = color self.extrusion = extrusion self.layer = layer self.lineType = lineType self.lineTypeScale = lineTypeScale self.lineWeight = lineWeight self.thickness = thickness self.parent = parent def _common(self): """Return common group codes as a string.""" if self.parent:parent=self.parent else:parent=self result='8\n%s'%parent.layer if parent.color!=None: result+='\n62\n%s'%parent.color if parent.extrusion!=None: result+='\n%s'%_point(parent.extrusion,200) if parent.lineType!=None: result+='\n6\n%s'%parent.lineType if parent.lineWeight!=None: result+='\n370\n%s'%parent.lineWeight if parent.lineTypeScale!=None: result+='\n48\n%s'%parent.lineTypeScale if parent.thickness!=None: result+='\n39\n%s'%parent.thickness return result class _Entities: """Base class to deal with composed objects.""" def __dxf__(self): return [] def __str__(self): return '\n'.join([str(x) for x in self.__dxf__()]) class _Collection(_Call): """Base class to expose entities methods to main object.""" def __init__(self,entities=[]): self.entities=copy.copy(entities) #link entities methods to drawing for attr in dir(self.entities): if attr[0]!='_': attrObject=getattr(self.entities,attr) if callable(attrObject): setattr(self,attr,attrObject)####2) Constants#---color valuesBYBLOCK=0BYLAYER=256#---block-type flags (bit coded values, may be combined): ANONYMOUS =1 # This is an anonymous block generated by hatching, associative dimensioning, other internal operations, or an applicationNON_CONSTANT_ATTRIBUTES =2 # This block has non-constant attribute definitions (this bit is not set if the block has any attribute definitions that are constant, or has no attribute definitions at all)XREF =4 # This block is an external reference (xref)XREF_OVERLAY =8 # This block is an xref overlay EXTERNAL =16 # This block is externally dependentRESOLVED =32 # This is a resolved external reference, or dependent of an external reference (ignored on input)REFERENCED =64 # This definition is a referenced external reference (ignored on input)#---mtext flags#attachment pointTOP_LEFT = 1TOP_CENTER = 2TOP_RIGHT = 3MIDDLE_LEFT = 4MIDDLE_CENTER = 5MIDDLE_RIGHT = 6BOTTOM_LEFT = 7BOTTOM_CENTER = 8BOTTOM_RIGHT = 9#drawing directionLEFT_RIGHT = 1TOP_BOTTOM = 3BY_STYLE = 5 #the flow direction is inherited from the associated text style#line spacing style (optional): AT_LEAST = 1 #taller characters will overrideEXACT = 2 #taller characters will not override#---polyline flagsCLOSED =1 # This is a closed polyline (or a polygon mesh closed in the M direction)CURVE_FIT =2 # Curve-fit vertices have been addedSPLINE_FIT =4 # Spline-fit vertices have been addedPOLYLINE_3D =8 # This is a 3D polylinePOLYGON_MESH =16 # This is a 3D polygon meshCLOSED_N =32 # The polygon mesh is closed in the N directionPOLYFACE_MESH =64 # The polyline is a polyface meshCONTINOUS_LINETYPE_PATTERN =128 # The linetype pattern is generated continuously around the vertices of this polyline#---text flags#horizontalLEFT = 0CENTER = 1RIGHT = 2ALIGNED = 3 #if vertical alignment = 0MIDDLE = 4 #if vertical alignment = 0FIT = 5 #if vertical alignment = 0#verticalBASELINE = 0BOTTOM = 1MIDDLE = 2TOP = 3####3) Classes#---entititiesclass Arc(_Entity): """Arc, angles in degrees.""" def __init__(self,center=(0,0,0),radius=1, startAngle=0.0,endAngle=90,**common): """Angles in degrees.""" _Entity.__init__(self,**common) self.center=center self.radius=radius self.startAngle=startAngle self.endAngle=endAngle def __str__(self): return '0\nARC\n%s\n%s\n40\n%s\n50\n%s\n51\n%s'%\ (self._common(),_point(self.center), self.radius,self.startAngle,self.endAngle)class Circle(_Entity): """Circle""" def __init__(self,center=(0,0,0),radius=1,**common): _Entity.__init__(self,**common) self.center=center self.radius=radius def __str__(self): return '0\nCIRCLE\n%s\n%s\n40\n%s'%\ (self._common(),_point(self.center),self.radius)class Face(_Entity): """3dface""" def __init__(self,points,**common): _Entity.__init__(self,**common) self.points=points def __str__(self): return '\n'.join(['0\n3DFACE',self._common()]+ _points(self.points) )class Insert(_Entity): """Block instance.""" def __init__(self,name,point=(0,0,0), xscale=None,yscale=None,zscale=None, cols=None,colspacing=None,rows=None,rowspacing=None, rotation=None, **common): _Entity.__init__(self,**common) self.name=name self.point=point self.xscale=xscale self.yscale=yscale self.zscale=zscale self.cols=cols self.colspacing=colspacing self.rows=rows self.rowspacing=rowspacing self.rotation=rotation def __str__(self): result='0\nINSERT\n2\n%s\n%s\n%s'%\ (self.name,self._common(),_point(self.point)) if self.xscale!=None:result+='\n41\n%s'%self.xscale if self.yscale!=None:result+='\n42\n%s'%self.yscale if self.zscale!=None:result+='\n43\n%s'%self.zscale if self.rotation:result+='\n50\n%s'%self.rotation if self.cols!=None:result+='\n70\n%s'%self.cols if self.colspacing!=None:result+='\n44\n%s'%self.colspacing if self.rows!=None:result+='\n71\n%s'%self.rows if self.rowspacing!=None:result+='\n45\n%s'%self.rowspacing return result class Line(_Entity): """Line""" def __init__(self,points,**common): _Entity.__init__(self,**common) self.points=points def __str__(self): return '\n'.join(['0\nLINE',self._common()]+ _points(self.points))class PolyLine(_Entity): # TODO: Finish polyline (now implemented as a series of lines) def __init__(self,points,flag=0,width=None,**common): _Entity.__init__(self,**common) self.points=points self.flag=flag self.width=width def __str__(self): result= '0\nPOLYLINE\n%s\n70\n%s'%\ (self._common(),self.flag) for point in self.points: result+='\n0\nVERTEX\n%s'%_point(point) if self.width:result+='\n40\n%s\n41\n%s'%(self.width,self.width) result+='\n0\nSEQEND' return resultclass Point(_Entity): """Colored solid fill.""" def __init__(self,points=None,**common): _Entity.__init__(self,**common) self.points=pointsclass Solid(_Entity): """Colored solid fill.""" def __init__(self,points=None,**common): _Entity.__init__(self,**common) self.points=points def __str__(self): return '\n'.join(['0\nSOLID',self._common()]+ _points(self.points[:2]+[self.points[3],self.points[2]]) )class Text(_Entity): """Single text line.""" def __init__(self,text='',point=(0,0,0),alignment=None, flag=None,height=1,justifyhor=None,justifyver=None, rotation=None,obliqueAngle=None,style=None,xscale=None,**common): _Entity.__init__(self,**common) self.text=text self.point=point self.alignment=alignment self.flag=flag self.height=height self.justifyhor=justifyhor self.justifyver=justifyver self.rotation=rotation self.obliqueAngle=obliqueAngle self.style=style self.xscale=xscale def __str__(self): result= '0\nTEXT\n%s\n%s\n40\n%s\n1\n%s'%\ (self._common(),_point(self.point),self.height,self.text) if self.rotation:result+='\n50\n%s'%self.rotation if self.xscale:result+='\n41\n%s'%self.xscale if self.obliqueAngle:result+='\n51\n%s'%self.obliqueAngle if self.style:result+='\n7\n%s'%self.style if self.flag:result+='\n71\n%s'%self.flag
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -