⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sdxf.py

📁 使用python写的一个关于AutoCAD dxf文件读写的程序
💻 PY
📖 第 1 页 / 共 2 页
字号:
        if self.justifyhor:result+='\n72\n%s'%self.justifyhor        if self.alignment:result+='\n%s'%_point(self.alignment,1)        if self.justifyver:result+='\n73\n%s'%self.justifyver        return resultclass Mtext(Text):    """Surrogate for mtext, generates some Text instances."""    def __init__(self,text='',point=(0,0,0),width=250,spacingFactor=1.5,down=0,spacingWidth=None,**options):        Text.__init__(self,text=text,point=point,**options)        if down:spacingFactor*=-1        self.spacingFactor=spacingFactor        self.spacingWidth=spacingWidth        self.width=width        self.down=down    def __str__(self):        texts=self.text.replace('\r\n','\n').split('\n')        if not self.down:texts.reverse()        result=''        x=y=0        if self.spacingWidth:spacingWidth=self.spacingWidth        else:spacingWidth=self.height*self.spacingFactor        for text in texts:            while text:                result+='\n%s'%Text(text[:self.width],                    point=(self.point[0]+x*spacingWidth,                           self.point[1]+y*spacingWidth,                           self.point[2]),                    alignment=self.alignment,flag=self.flag,height=self.height,                    justifyhor=self.justifyhor,justifyver=self.justifyver,                    rotation=self.rotation,obliqueAngle=self.obliqueAngle,                    style=self.style,xscale=self.xscale,parent=self                )                text=text[self.width:]                if self.rotation:x+=1                else:y+=1        return result[1:]        ##class _Mtext(_Entity):##    """Mtext not functioning for minimal dxf."""##    def __init__(self,text='',point=(0,0,0),attachment=1,##                 charWidth=None,charHeight=1,direction=1,height=100,rotation=0,##                 spacingStyle=None,spacingFactor=None,style=None,width=100,##                 xdirection=None,**common):##        _Entity.__init__(self,**common)##        self.text=text##        self.point=point##        self.attachment=attachment##        self.charWidth=charWidth##        self.charHeight=charHeight##        self.direction=direction##        self.height=height##        self.rotation=rotation##        self.spacingStyle=spacingStyle##        self.spacingFactor=spacingFactor##        self.style=style##        self.width=width##        self.xdirection=xdirection##    def __str__(self):##        input=self.text##        text=''##        while len(input)>250:##            text+='\n3\n%s'%input[:250]##            input=input[250:]##        text+='\n1\n%s'%input##        result= '0\nMTEXT\n%s\n%s\n40\n%s\n41\n%s\n71\n%s\n72\n%s%s\n43\n%s\n50\n%s'%\##                (self._common(),_point(self.point),self.charHeight,self.width,##                 self.attachment,self.direction,text,##                 self.height,##                 self.rotation)##        if self.style:result+='\n7\n%s'%self.style##        if self.xdirection:result+='\n%s'%_point(self.xdirection,1)##        if self.charWidth:result+='\n42\n%s'%self.charWidth##        if self.spacingStyle:result+='\n73\n%s'%self.spacingStyle##        if self.spacingFactor:result+='\n44\n%s'%self.spacingFactor##        return result    #---tablesclass Block(_Collection):    """Use list methods to add entities, eg append."""    def __init__(self,name,layer='0',flag=0,base=(0,0,0),entities=[]):        self.entities=copy.copy(entities)        _Collection.__init__(self,entities)        self.layer=layer        self.name=name        self.flag=0        self.base=base    def __str__(self):        e='\n'.join([str(x)for x in self.entities])        return '0\nBLOCK\n8\n%s\n2\n%s\n70\n%s\n%s\n3\n%s\n%s\n0\nENDBLK'%\               (self.layer,self.name.upper(),self.flag,_point(self.base),self.name.upper(),e)            class Layer(_Call):    """Layer"""    def __init__(self,name='pydxf',color=7,lineType='continuous',flag=64):        self.name=name        self.color=color        self.lineType=lineType        self.flag=flag    def __str__(self):        return '0\nLAYER\n2\n%s\n70\n%s\n62\n%s\n6\n%s'%\               (self.name.upper(),self.flag,self.color,self.lineType)    class LineType(_Call):    """Custom linetype"""    def __init__(self,name='continuous',description='Solid line',elements=[],flag=64):        # TODO: Implement lineType elements        self.name=name        self.description=description        self.elements=copy.copy(elements)        self.flag=flag    def __str__(self):        return '0\nLTYPE\n2\n%s\n70\n%s\n3\n%s\n72\n65\n73\n%s\n40\n0.0'%\            (self.name.upper(),self.flag,self.description,len(self.elements))class Style(_Call):    """Text style"""    def __init__(self,name='standard',flag=0,height=0,widthFactor=40,obliqueAngle=50,                 mirror=0,lastHeight=1,font='arial.ttf',bigFont=''):        self.name=name        self.flag=flag        self.height=height        self.widthFactor=widthFactor        self.obliqueAngle=obliqueAngle        self.mirror=mirror        self.lastHeight=lastHeight        self.font=font        self.bigFont=bigFont    def __str__(self):        return '0\nSTYLE\n2\n%s\n70\n%s\n40\n%s\n41\n%s\n50\n%s\n71\n%s\n42\n%s\n3\n%s\n4\n%s'%\               (self.name.upper(),self.flag,self.flag,self.widthFactor,                self.obliqueAngle,self.mirror,self.lastHeight,                self.font.upper(),self.bigFont.upper())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=width        self.height=height        self.center=center        self.direction=direction        self.target=target        self.lens=lens        self.frontClipping=frontClipping        self.backClipping=backClipping        self.twist=twist        self.mode=mode    def __str__(self):        return '0\nVIEW\n2\n%s\n70\n%s\n40\n%s\n%s\n41\n%s\n%s\n%s\n42\n%s\n43\n%s\n44\n%s\n50\n%s\n71\n%s'%\               (self.name,self.flag,self.height,_point(self.center),self.width,                _point(self.direction,1),_point(self.target,2),self.lens,                self.frontClipping,self.backClipping,self.twist,self.mode)def ViewByWindow(name,leftBottom=(0,0),rightTop=(1,1),**options):    width=abs(rightTop[0]-leftBottom[0])    height=abs(rightTop[1]-leftBottom[1])    center=((rightTop[0]+leftBottom[0])*0.5,(rightTop[1]+leftBottom[1])*0.5)    return View(name=name,width=width,height=height,center=center,**options)#---drawingclass Drawing(_Collection):    """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),extmax=(0.0,0.0),                 layers=[Layer()],linetypes=[LineType()],styles=[Style()],blocks=[],                 views=[],entities=None,fileName='test.dxf'):        # TODO: replace list with None,arial        if not entities:entities=[]        _Collection.__init__(self,entities)        self.insbase=insbase        self.extmin=extmin        self.extmax=extmax        self.layers=copy.copy(layers)        self.linetypes=copy.copy(linetypes)        self.styles=copy.copy(styles)        self.views=copy.copy(views)        self.blocks=copy.copy(blocks)        self.fileName=fileName        #private        self.acadver='9\n$ACADVER\n1\nAC1006'    def _name(self,x):        """Helper function for self._point"""        return '9\n$%s'%x.upper()    def _point(self,name,x):        """Point setting from drawing like extmin,extmax,..."""        return '%s\n%s'%(self._name(name),_point(x))    def _section(self,name,x):        """Sections like tables,blocks,entities,..."""        if x:xstr='\n'+'\n'.join(x)        else:xstr=''        return '0\nSECTION\n2\n%s%s\n0\nENDSEC'%(name.upper(),xstr)    def _table(self,name,x):        """Tables like ltype,layer,style,..."""        if x:xstr='\n'+'\n'.join(x)        else:xstr=''        return '0\nTABLE\n2\n%s\n70\n%s%s\n0\nENDTAB'%(name.upper(),len(x),xstr)    def __str__(self):        """Returns drawing as dxf string."""        header=[self.acadver]+[self._point(attr,getattr(self,attr)) for attr in _HEADER_POINTS]        header=self._section('header',header)                tables=[self._table('ltype',[str(x) for x in self.linetypes]),                self._table('layer',[str(x) for x in self.layers]),                self._table('style',[str(x) for x in self.styles]),                self._table('view',[str(x) for x in self.views]),        ]        tables=self._section('tables',tables)        blocks=self._section('blocks',[str(x) for x in self.blocks])        entities=self._section('entities',[str(x) for x in self.entities])                all='\n'.join([header,tables,blocks,entities,'0\nEOF\n'])        return all    def saveas(self,fileName):        self.fileName=fileName        self.save()    def save(self):        test=open(self.fileName,'w')        test.write(str(self))        test.close()#---extrasclass Rectangle(_Entity):    """Rectangle, creates lines."""    def __init__(self,point=(0,0,0),width=1,height=1,solid=None,line=1,**common):        _Entity.__init__(self,**common)        self.point=point        self.width=width        self.height=height        self.solid=solid        self.line=line    def __str__(self):        result=''        points=[self.point,(self.point[0]+self.width,self.point[1],self.point[2]),            (self.point[0]+self.width,self.point[1]+self.height,self.point[2]),            (self.point[0],self.point[1]+self.height,self.point[2]),self.point]        if self.solid:            result+='\n%s'%Solid(points=points[:-1],parent=self.solid)        if self.line:            for i in range(4):result+='\n%s'%\                Line(points=[points[i],points[i+1]],parent=self)        return result[1:]class LineList(_Entity):    """Like polyline, but built of individual lines."""    def __init__(self,points=[],closed=0,**common):        _Entity.__init__(self,**common)        self.closed=closed        self.points=copy.copy(points)    def __str__(self):        if self.closed:points=self.points+[self.points[0]]        else: points=self.points        result=''        for i in range(len(points)-1):result+='\n%s'%\            Line(points=[points[i],points[i+1]],parent=self)        return result[1:]PolyLine=LineList#---testdef main():    #Blocks    b=Block('test')    b.append(Solid(points=[(0,0,0),(1,0,0),(1,1,0),(0,1,0)],color=1))    b.append(Arc(center=(1,0,0),color=2))        #Drawing    d=Drawing()    #tables    d.blocks.append(b)                      #table blocks    d.styles.append(Style())                #table styles    d.views.append(View('Normal'))          #table view    d.views.append(ViewByWindow('Window',leftBottom=(1,0),rightTop=(2,1)))  #idem    #entities    d.append(Circle(center=(1,1,0),color=3))    d.append(Face(points=[(0,0,0),(1,0,0),(1,1,0),(0,1,0)],color=4))    d.append(Insert('test',point=(3,3,3),cols=5,colspacing=2))    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(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(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.saveas('c:\\test.dxf')if __name__=='__main__':main()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -