python批量将CAD中的每个图分别转化为对应的PDF(cad转pdf)
需要在cad上选择左下角和右小角的坐标,会有提示,注意看即可
通过交互方式将一个CAD中的每个图纸分别转为一个pdf,简单易操作!!
其中有一部分是借鉴各路大神的,其中链接都在下方。另外也参阅了acadauto.chm,以及AutoCADActiveX和VBA参考明经翻译版).chm(打赏可发送参考文件,私信我邮箱即可)
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 26 13:55:01 2020
@author:
"""
import win32com.client
import pythoncom
import numpy as np
def scr():
acad = win32com.client.Dispatch("AutoCAD.Application")
# AutoCAD.Application.19为 ProgID
acaddoc = acad.ActiveDocument
acaddoc.Utility.Prompt("Hello AutoCAD\n")
acadmod = acaddoc.ModelSpace
print('当前处理文件的目录为:%s'%(acaddoc.path))
print('当前处理文件名:%s'%(acaddoc.name))
def APoint(x, y):
"""坐标点转化为浮点数"""
# 需要两个点的坐标
return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (x, y))
layout = acaddoc.layouts.item('Model') # 先来个layout对象
plot = acaddoc.Plot
Ret_dict = dict()
RetVa = layout.GetPlotStyleTableNames() #获取当前可用的打印样式名称
RetVal=filter(lambda x:x[-3:] !='stb' ,list(RetVa))#将后缀为stb的文件过滤,否则选择stb格式的文件会报错
print('可用的打印样式如下:')
for re_index,name in enumerate(list(RetVal)):
Ret_dict[re_index] = name
print(str(re_index) + ' - ' + name)
sty_sh = input('请选择打印样式(输入数字即可,tips:选择格式必须为ctb):')
print('您选的打印样式为:%s'%(Ret_dict[eval(sty_sh)]))
acaddoc.SetVariable('BACKGROUNDPLOT', 0) # 前台打印
layout.StyleSheet = Ret_dict[eval(sty_sh)] # 选择打印样式
layout.PlotWithLineweights = True # 打印线宽
layout.ConfigName = 'DWG To PDF.pc3' # 选择打印机
layout.PlotRotation = 0 # 横向打印
layout.StandardScale = 0# 图纸打印比例
layout.CenterPlot = True # 居中打印
layout.PlotWithPlotStyles = True # 依照样式打印
layout.PlotHidden = False # 隐藏图纸空间对象
cad_number = eval(input('请输入图的张数:'))
zb_array = np.zeros([cad_number,4])#用来储存图框的坐标点
paper_size = []
for zb in range(cad_number):
paper_in = input('请输入打印的第--%s--张图的尺寸(tip:A1→1,A2→2,A3→3,A4→4):'%(zb+1))
paper_size.append(paper_in)
print('请打开CAD点击坐标点,连续选择两个点,第一个点为**左下角**,第二个为**右上角**')
for zb_2 in range(2):
#防止点击错误出现出现闪退
xun = 1
while xun:
try:
zb_scr = acaddoc.Utility.GetPoint()#通过鼠标点击获取坐标
zb_array[zb][zb_2] = zb_scr[0]#将x坐标保存到数组中
zb_array[zb][zb_2+2] = zb_scr[1]#将坐标y保存到数组中
xun = 0
except:
print('重新选择(请点击正确区域)')
if zb_2 == 0:
print('**左**下角的坐标点为:%s'%(list(zb_scr)))
else:
print('**右**上角的坐标点为:%s'%(list(zb_scr)))
for po in range(cad_number):
if paper_size[po] == '1':
layout.CanonicalMediaName = 'ISO_full_bleed_A1_(841.00_x_594.00_MM)'
elif paper_size[po] == '2':
layout.CanonicalMediaName = 'ISO_full_bleed_A2_(594.00_x_420.00_MM)'
elif paper_size[po] == '3':
layout.CanonicalMediaName = 'ISO_full_bleed_A3_(420.00_x_297.00_MM)'
else:
layout.CanonicalMediaName = 'ISO_full_bleed_A4_(297.00_x_210.00_MM)'
po1 = APoint(zb_array[po][0],zb_array[po][2])
po2 = APoint(zb_array[po][1],zb_array[po][3])
layout.SetWindowToPlot(po1, po2)#设置打印窗口的坐标
layout.PlotType = 4#设置安装坐标窗口打印,若无此行,按照窗口显示的打印
plot.PlotToFile(acaddoc.path +'/' + str(po+1) + '-' + acaddoc.name[:-4] + ".pdf")
print('---' + str(po+1) + '-' + acaddoc.name[:-4] + ".pdf---文件已保存到" + acaddoc.path)
print('处理完毕')
x = 1
while x:
scr()
x = eval(input('是否继续计算,若是请输入1,否则请输入0:'))
参考博主链接:
http://blog.sina.com.cn/s/blog_ad9ff24c0102uwah.html
https://blog.csdn.net/Hulunbuir/article/details/83715279
https://blog.csdn.net/weixin_42446662/article
https://blog.csdn.net/xiaokcehui/article/details/48492915
https://blog.csdn.net/qq_44792233/article/details/102946651
最后希望各位大佬能赏口饭吃