超级干货-python批量修改替换cad图纸文本

from pyautocad import Autocad,APoint
import math
import os 
import os.path as osp
import re
 
reg=re.compile(r'.*(.dwg)$')#匹配dwg文件
path=r"F:\22年14批预算明细"
lst=[]
for dirpath,dirname,filename in os.walk(path):
    li=[osp.join(dirpath,s) for s in filename if osp.isfile(osp.join(dirpath,s))
                                              if reg.findall(s)]
    for i in li:
        if i:
            lst.append(i)
 
for filename in lst:
    #连接CAD
    acad=Autocad(create_if_not_exists=True,visible=True)#create_if_not_exists—如果CAD不运行,则会创建新的实例.visible -如果为True,新AutoCAD实例将可见(默认)
    #打开/读取CAD文件
    acad.ActiveDocument.Application.Documents.open(filename)
    acad.prompt("Hello! ZWCAD from pyautocad.")
    print(acad.doc.Name)
 
    #遍历CAD文件中的所有文本对象,打印其文本内容及坐标位置
    for text in acad.iter_objects("Text"):#遍历Text对象
        #修改对象属性,将文本中的"x批"字符替换为"14批"
        if 'x批' in text.TextString:
            print('text: %s at: %s' %(text.TextString,text.InsertionPoint))
            # 修改對象屬性
            text1 = str(text.TextString)
            text1 = text1.replace('x批','14批')
            text.TextString = text1
            acad.ActiveDocument.SaveAs(filename, 61)#同名另存
 
    acad.ActiveDocument.Close()#关闭当前
           
 
acad.app.Quit()#退出cad程序
 
 
         
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐