图纸空间的打印测试

研究测试了一天,搞定了。深入认真研究清楚一个问题,功力一下子就升了一级。


>>> # -*- coding: utf-8 -*-

>>> from  pyautocad  import Autocad,APoint
>>> import pyautocad.types
>>> import math
>>> import os
>>> import time
>>> import win32com.client
>>> import pythoncom

>>> def vtFloat(list):
    """列表转化为浮点数"""
    return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, list)

>>> def vtInt(list):
    """列表转化为整数"""
    return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_I2, list)

>>> def vtVariant(list):
    """列表转化为变体"""
    return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_VARIANT, list)

>>> def ConvertArrays2Variant(inputdata, vartype="Variant"):
    import pythoncom
    if vartype == "ArrayofObjects":  # 对象数组
        outputdata = win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_DISPATCH, inputdata)
    if vartype == "Double":  # 双精度
        outputdata = win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, inputdata)
    if vartype == "ShortInteger":  # 短整型
        outputdata = win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_I2, inputdata)
    if vartype == "LongInteger":  # 长整型
        outputdata = win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_I4, inputdata)
    if vartype == "Variant":  # 变体
        outputdata = win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_VARIANT, inputdata)
    return outputdata

>>> acad = win32com.client.Dispatch("AutoCAD.Application")
>>> acad.ActiveDocument.Utility.Prompt("Hello AutoCAD\n")
>>> mp = acad.ActiveDocument.ModelSpace
>>> print(acad.ActiveDocument.Name)
道路.dwg

>>> def vtpnt(x, y, z=0):
    """坐标点转化为浮点数"""
    return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (x, y, z))

					    
>>> [pnt1, pnt2, pnt3, pnt4, pnt5, pnt6] = [vtpnt(-40, -40), vtpnt(500, 500), vtpnt(300, 200),
                                        vtpnt(600, 200), vtpnt(700, 200), vtpnt(100, 0)]
					    
>>> def vtobj(obj):
    """转化为对象数组"""
    return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_DISPATCH, obj)

					    
>>> def vtFloat(list):
    """列表转化为浮点数"""
    return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, list)

					    
>>> def vtInt(list):
    """列表转化为整数"""
    return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_I2, list)

					    
>>> def vtVariant(list):
    """列表转化为变体"""
    return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_VARIANT, list)

					    
>>> Padk = acad.ActiveDocument.PaperSpace
					    
>>> CircleObj = Padk.AddCircle(pnt3, 1000)
					    
>>> acad.ActiveDocument.SendCommand("_TILEMODE" + chr(13) + "0" + chr(13))
					    
>>> ACADPref = acad.ActiveDocument.Application.preferences.Files
					    
>>> originalValue = ACADPref.PrinterConfigPath \
    = r"C:\Users\Administrator\AppData\Roaming\Autodesk\AutoCAD 2011\R18.1\chs\Plotters"

>>> currentPlot = acad.ActiveDocument.Plot
					    
>>> currentLayout = acad.ActiveDocument.ActiveLayout
					    
>>> currentLayout.ConfigName = "DWG To PDF.pc3"
					    
>>> p1 = vtpnt(19357.8591776, 7161.9227564, z=0)
					    
>>> p2 = vtpnt(20198.8591776, 6567.9227564, z=0)
					    
>>> currentLayout.CanonicalMediaName = 'ISO_full_bleed_A1_(594.00_x_841.00_MM)'  # A*图纸
					    
>>> currentLayout.PlotRotation = 0
					    
>>> currentLayout.PlotRotation = 1

>>> acad.ActiveDocument.Plot.DisplayPlotPreview
					    
<bound method DisplayPlotPreview of <COMObject <unknown>>>
>>> acad.ActiveDocument.Plot.SetLayoutsToPlot
					    
<bound method SetLayoutsToPlot of <COMObject <unknown>>>

>>> xs = 19357.8591776
					    
>>> ys = 7161.9227564
					    
>>> xe = 20198.8591776
					    
>>> ye = 6567.9227564
					    
>>> LowerLeft = [xs,ys]
					    
>>> UpperRight = [xe,ye]
					    
>>> LowerLeft = ConvertArrays2Variant(LowerLeft, "Double")
					    
>>> UpperRight = ConvertArrays2Variant(UpperRight, "Double")

>>> acad = win32com.client.Dispatch("AutoCAD.Application")
					    
>>> acad.ActiveDocument.Utility.Prompt("Hello AutoCAD\n")
					    
>>> print(acad.ActiveDocument.Name)
					    

>>> currentLayout.SetWindowToPlot(LowerLeft, UpperRight)
					    
>>> acad.ActiveDocument.Plot.DisplayPlotPreview
					    
<bound method DisplayPlotPreview of <COMObject <unknown>>>

>>> 

>>> xs = 19357.8591776
					    
>>> ys = 7161.9227564
					    
>>> xe = 20198.8591776
					    
>>> ye = 6567.9227564
					    
>>> LowerLeft = [xs,ys]
					    
>>> UpperRight = [xe,ye]
					    
>>> LowerLeft = ConvertArrays2Variant(LowerLeft, "Double")
					    
>>> UpperRight = ConvertArrays2Variant(UpperRight, "Double")
					    
>>> currentPlot = acad.ActiveDocument.Plot
					    
>>> currentLayout = acad.ActiveDocument.ActiveLayout
					    
>>> currentLayout.SetWindowToPlot(LowerLeft, UpperRight)
					    
>>> acad.ActiveDocument.SendCommand("_TILEMODE" + chr(13) + "0" + chr(13))
					    
>>> acad.ActiveDocument.SendCommand("_plot"+chr(13)+chr(13)+chr(13)+chr(13))
					    

>>> currentLayout.PlotType = 4
					    
>>> acad.ActiveDocument.Plot.PlotToFile(r"D:\pythonCAD\Shuchu" + '\ ' +'图纸空间1.pdf')
					    
True
>>> print('重新测试,以上内容作废')
					    
重新测试,以上内容作废
>>> #1首先连接控制cad
					    
>>> acad = win32com.client.Dispatch("AutoCAD.Application")
					    
>>> acad.ActiveDocument.Utility.Prompt("Hello AutoCAD\n")
					    
>>> #如果一开始是布局空间,上面的连接命令就会出错
					    
>>> Padk = acad.ActiveDocument.PaperSpace
					    
>>> CircleObj = Padk.AddCircle(pnt3, 1800)
					    
>>> acad.ActiveDocument.SendCommand("_TILEMODE" + chr(13) + "0" + chr(13))
					    
>>> #进入图纸空间,看到刚才在图纸空间画的圆
					    
>>> #将打印设置先不要设为窗口
					    
>>> #现在来设定窗口
					    
>>> xs = 19357.8591776
					    
>>> ys = 7161.9227564
					    
>>> xe = 20198.8591776
					    
>>> ye = 6567.9227564
					    
>>> LowerLeft = [xs,ys]
					    
>>> UpperRight = [xe,ye]
					    
>>> LowerLeft = ConvertArrays2Variant(LowerLeft, "Double")
					    
>>> UpperRight = ConvertArrays2Variant(UpperRight, "Double")
					    
>>> currentPlot = acad.ActiveDocument.Plot
					    
>>> currentLayout = acad.ActiveDocument.ActiveLayout
					    
>>> currentLayout.SetWindowToPlot(LowerLeft, UpperRight)
					    
>>> #看看上面的命令是不是改变了打印设置
					    
>>> #还没有
					    
>>> currentLayout.PlotType = 4
					    
Traceback (most recent call last):
  File "<pyshell#120>", line 1, in <module>
    currentLayout.PlotType = 4
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\win32com\client\dynamic.py", line 565, in __setattr__
    self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
pywintypes.com_error: (-2147418111, '被呼叫方拒绝接收呼叫。', None, None)
>>> #把查看打印设置的窗口关闭再来执行命令
					    
>>> currentLayout.PlotType = 4
					    
>>> #确实要关闭,再去查看
					    
>>> #已经变成窗口
					    
>>> #设置一下打印图纸的图号
					    
>>> currentLayout.CanonicalMediaName = 'ISO_full_bleed_A1_(594.00_x_841.00_MM)'  # A*图纸
					    
Traceback (most recent call last):
  File "<pyshell#126>", line 1, in <module>
    currentLayout.CanonicalMediaName = 'ISO_full_bleed_A1_(594.00_x_841.00_MM)'  # A*图纸
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\win32com\client\dynamic.py", line 565, in __setattr__
    self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
pywintypes.com_error: (-2147418111, '被呼叫方拒绝接收呼叫。', None, None)
>>> #同样要关闭查看的打印窗口,再来
					    
>>> currentLayout.CanonicalMediaName = 'ISO_full_bleed_A1_(594.00_x_841.00_MM)'  # A*图纸
					    
>>> #非常OK
					    
>>>  currentLayout.ConfigName = "DWG To PDF.pc3"
					    
SyntaxError: unexpected indent
>>> currentLayout.ConfigName = "DWG To PDF.pc3"
					    
>>> currentLayout.CenterPlot = True  # 居中打印
					    
>>> #现在默认的是纵向,转过来
					    
>>>  currentLayout.PlotRotation = 0
					    
SyntaxError: unexpected indent
>>> currentLayout.PlotRotation = 1  # 横向打印0
					    
>>> #在运行这个命令一次看是否又转回纵向
					    
>>> currentLayout.PlotRotation = 1
					    
>>> #还是横向
					    
>>> currentLayout.PlotRotation = 1
					    
>>> currentLayout.PlotRotation = 0
					    
>>> currentLayout.PlotRotation = 0
					    
>>> currentLayout.PlotRotation = 1
					    
>>> #测试证明1是横向
					    
>>> #根据模型空间的打印经验,最后一个plottofile应该可以了
					    
>>> acad.ActiveDocument.SetVariable("BACKGROUNDPLOT", 0)
					    
>>> #前台打印
					    
>>> acad.ActiveDocument.Plot.QuietErrorMode = True
					    
>>> #安静
					    
>>> acad.ActiveDocument.Plot.PlotToFile(r"D:\pythonCAD\Shuchu" + '\ ' +'图纸空间1.pdf')
					    
True
>>> #是的打印了什么却没有
					    
>>> acad.ActiveDocument.Plot.PlotToFile(r"D:\pythonCAD\Shuchu" + '\ ' +'图纸空间1.pdf')
					    
True
>>> #这次把打印线打出来了,这根线是画在图纸空间的
					    
>>> acad.ActiveDocument.Plot.PlotToFile(r"D:\pythonCAD\Shuchu" + '\ ' +'图纸空间1.pdf')
					    
True
>>> #画了圆但只看到一点,比例的问题
					    
>>> acad.ActiveDocument.Plot.PlotToFile(r"D:\pythonCAD\Shuchu" + '\ ' +'图纸空间1.pdf')
					    
True
>>> #现在我知道可能是比例的原因,实际上打印的区域在我们所选范围的很小的区域
					    
>>> #至少我确认打印范围确实跑到图纸空间来了,只是没有显示视口中的模型对象
					    
>>> acad.ActiveDocument.Plot.PlotToFile(r"D:\pythonCAD\Shuchu" + '\ ' +'图纸空间1.pdf')
					    
True
>>> #现在我知道文字都能打印,那就只剩一个问题,怎么让视口中的模型显现呢
					    
>>> #在目前的状态下,让我们用人工来打印,结果和程序一样,这证明是设置的原因了
					    
>>> #观察发现能打印的区域模型是空的,所以进入模型空间增加一些内容
					    
>>> acad.ActiveDocument.Plot.PlotToFile(r"D:\pythonCAD\Shuchu" + '\ ' +'图纸空间1.pdf')
					    
True
>>> acad.ActiveDocument.Plot.PlotToFile(r"D:\pythonCAD\Shuchu" + '\ ' +'图纸空间1.pdf')
					    
True
>>> acad.ActiveDocument.Plot.PlotToFile(r"D:\pythonCAD\Shuchu" + '\ ' +'图纸空间1.pdf')
					    
True
>>> #通过将模型加密,显现了
					    
>>> #那么现在证明就是比例没有认真搞而已
					    
>>> #认真做一个测试样板模型,考虑以毫米为单位绘制一条4公里的道路
					    
>>> #道路宽24米
					    
>>> xs = 19357.8591776
					    
>>> ys = 8114.0905217
					    
>>> xe = 20412.3938689
					    
>>> ye = 7805.1859568
					    
>>> LowerLeft = [xs,ys]
					    
>>> UpperRight = [xe,ye]
					    
>>> LowerLeft = ConvertArrays2Variant(LowerLeft, "Double")
					    
>>> UpperRight = ConvertArrays2Variant(UpperRight, "Double")
					    
>>> currentPlot = acad.ActiveDocument.Plot
					    
>>> currentLayout.SetWindowToPlot(LowerLeft, UpperRight)
					    
>>> acad.ActiveDocument.Plot.PlotToFile(r"D:\pythonCAD\Shuchu" + '\ ' +'图纸空间1.pdf')
					    
True
>>> #还有个鬼,就是没有设置布满图纸
					    
>>> #查看layout在帮助文档的属性信息
					    
>>> acad = win32com.client.Dispatch("AutoCAD.Application")
					    
>>> acad.ActiveDocument.Utility.Prompt("Hello AutoCAD\n")
					    
>>> print(acad.ActiveDocument.Name)
					    
道路.dwg
>>> acad.ActiveDocument.SendCommand("_TILEMODE" + chr(13) + "0" + chr(13))
					    
>>> #一时找不到布满图纸的选项,应该使用英文版的cad就能容易找到相应的词
					    
>>> #采取默认法,将文件打印手工设置成布满图纸,打印一份,应用到布局。关闭,再打开文件
					    
>>> #包括打印样式等都可以不控制,还有更强的适应性。重新打开,发现文件的布满图纸选项已经打钩
					    
>>> #重新再控制进入图纸空间和窗口打印,其他不用重复输入,这就是shell终端的好处
					    
>>> acad = win32com.client.Dispatch("AutoCAD.Application")
					    
>>> acad.ActiveDocument.Utility.Prompt("Hello AutoCAD\n")
					    
>>> print(acad.ActiveDocument.Name)
					    
道路.dwg
>>> #注意图纸必须是模型空间状态,否则上述连接报错
					    
>>> #进入图纸空间状态
					    
>>> acad.ActiveDocument.SendCommand("_TILEMODE" + chr(13) + "0" + chr(13))
					    
>>> xs = 19975.5145557
					    
>>> ys = 8114.0905217
					    
>>> xe = 20412.3938689
					    
>>> ye = 7805.1859568
					    
>>> LowerLeft = [xs,ys]
					    
>>> UpperRight = [xe,ye]
					    
>>> LowerLeft = ConvertArrays2Variant(LowerLeft, "Double")
					    
>>> UpperRight = ConvertArrays2Variant(UpperRight, "Double")
					    
>>> currentLayout.SetWindowToPlot(LowerLeft, UpperRight)
					    
Traceback (most recent call last):
  File "<pyshell#204>", line 1, in <module>
    currentLayout.SetWindowToPlot(LowerLeft, UpperRight)
  File "<COMObject <unknown>>", line 3, in SetWindowToPlot
pywintypes.com_error: (-2147023174, 'RPC 服务器不可用。', None, None)
>>> currentPlot = acad.ActiveDocument.Plot
					    
>>> currentLayout.SetWindowToPlot(LowerLeft, UpperRight)
					    
Traceback (most recent call last):
  File "<pyshell#206>", line 1, in <module>
    currentLayout.SetWindowToPlot(LowerLeft, UpperRight)
  File "<COMObject <unknown>>", line 3, in SetWindowToPlot
pywintypes.com_error: (-2147023174, 'RPC 服务器不可用。', None, None)
>>> currentLayout = acad.ActiveDocument.ActiveLayout
					    
>>> currentPlot = acad.ActiveDocument.Plot
					    
>>> currentLayout.SetWindowToPlot(LowerLeft, UpperRight)
					    
>>> #因为开关文件所以需要重新运行活动桌面的命令
					    
>>> acad.ActiveDocument.Plot.PlotToFile(r"D:\pythonCAD\Shuchu" + '\ ' +'图纸空间1.pdf')
					    
True
>>> #一击致命,成功了
					    
>>> #分享给CSDN的网友吧,我从别人那里弄了很多东西学习,分享一些心得是应该的
					    
>>> #惭愧,学了这么久python,今天才知道原来shell是这么用的,要晓得仅仅用F5在编辑器里跑进行复杂测试实在是太不方便了