图纸空间的打印测试
研究測試了一天,搞定了。深入認真研究清楚一個問題,功力一下子就升了一級。
>>> # -*- 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 pythoncomif 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 = 4Traceback (most recent call last):File "<pyshell#120>", line 1, in <module>currentLayout.PlotType = 4File "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 = 0SyntaxError: 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在編輯器里跑進行復雜測試實在是太不方便了總結
- 上一篇: 机械图纸英文标识诠释
- 下一篇: 使用elementUi-table时报: