明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 3585|回复: 3

[原创]打印函数

[复制链接]
发表于 2009-5-30 15:47:00 | 显示全部楼层 |阅读模式
  1. Public Function P_DWG(ByVal ZX As String, ByVal YS As String, Optional ByVal P_Option _
  2.     As Integer = 1, Optional ByVal Paper_Units As String = "毫米", Optional ByVal CustomScale As Long = 0, Optional _
  3.     ByVal Plot_Device As String = "", Optional ByVal Style_Sheet As String = "", Optional ByVal CanonicalMedia As _
  4.     String = "", Optional ByVal File_Path As String = "", Optional ByVal Number_Copies As Integer = 1, Optional ByVal _
  5.     Plot_Origin As String = ",", Optional ByVal Degree As String = "自 动") As Long
  6. 'P_DWG 返回值:
  7. '   1       操作成功
  8. '   0       操作被用户中断
  9. '   -1      接口使用错误
  10. '   -2      函数内部错误
  11. On Error GoTo Err_handle
  12.     Dim FilePath As String
  13.     FilePath = File_Path
  14.     If Plot_Device <> "" Then
  15.         If ThisDrawing.ActiveLayout.ConfigName <> Plot_Device Then ThisDrawing.ActiveLayout.ConfigName = Plot_Device
  16.     End If
  17.     If Style_Sheet <> "" Then
  18.         If ThisDrawing.ActiveLayout.StyleSheet <> Style_Sheet Then ThisDrawing.ActiveLayout.StyleSheet = Style_Sheet
  19.     End If
  20.     If CanonicalMedia <> "" Then
  21.         If ThisDrawing.ActiveLayout.CanonicalMediaName <> CanonicalMedia Then ThisDrawing.ActiveLayout.CanonicalMediaName = CanonicalMedia
  22.     End If
  23.     If Not Exists(Left(FilePath, InStrRev(FilePath, ""))) And P_Option = 2 Then
  24.         P_DWG = -2
  25.         Exit Function
  26.     End If
  27.     If Val(ThisDrawing.Application.Version) >= 16.2 Then
  28.         ThisDrawing.SetVariable "BACKGROUNDPLOT", 0
  29.     End If
  30.     '获取后缀名
  31.     Dim Extension As String
  32.     Extension = ".plt"
  33.     If InStr(1, Plot_Device, "png", 1) Then Extension = ".png"
  34.     If InStr(1, Plot_Device, "tif", 1) Then Extension = ".tif"
  35.    
  36.     '设置打印区域
  37.     Dim P1(0 To 1) As Double
  38.     Dim P2(0 To 1) As Double
  39.     P1(0) = Val(ZX)
  40.     P1(1) = Val(Right(ZX, Len(ZX) - InStr(ZX, ",")))
  41.     P2(0) = Val(YS)
  42.     P2(1) = Val(Right(YS, Len(YS) - InStr(YS, ",")))
  43. '    Dim W As Double
  44. '    Dim H As Double
  45. '    W = P2(0) - P1(0)
  46. '    H = P2(1) - P1(1)
  47. '    P1(0) = P1(0) - 0.001 * W
  48. '    P1(1) = P1(1) - 0.001 * H
  49. '    P2(0) = P2(0) + 0.001 * W
  50. '    P2(1) = P2(1) + 0.001 * H
  51.     ThisDrawing.ActiveLayout.SetWindowToPlot P1, P2
  52.     ThisDrawing.ActiveLayout.PlotType = acWindow
  53.      '设置旋转角度
  54.     Select Case Degree
  55.         Case "自 动"
  56.             Dim zW As Double
  57.             Dim zH As Double
  58.             Dim tW As Double
  59.             Dim tH As Double
  60.             Dim str1 As String
  61.             Dim str2 As String
  62.             zW = Abs(Val(ZX) - Val(YS))
  63.             str1 = Right(ZX, Len(ZX) - InStr(ZX, ","))
  64.             str2 = Right(YS, Len(YS) - InStr(YS, ","))
  65.             zH = Abs(Val(str1) - Val(str2))
  66.             ThisDrawing.ActiveLayout.GetPaperSize tW, tH
  67.             If ((tW > tH) And (zW > zH)) Or ((tW < tH) And (zW < zH)) Then
  68.                 ThisDrawing.ActiveLayout.PlotRotation = ac0degrees
  69.             Else
  70.                 ThisDrawing.ActiveLayout.PlotRotation = ac90degrees
  71.             End If
  72.         Case "  0度"
  73.             ThisDrawing.ActiveLayout.PlotRotation = ac0degrees
  74.         Case " 90度"
  75.             ThisDrawing.ActiveLayout.PlotRotation = ac90degrees
  76.         Case "180度"
  77.             ThisDrawing.ActiveLayout.PlotRotation = ac180degrees
  78.         Case "270度"
  79.             ThisDrawing.ActiveLayout.PlotRotation = ac270degrees
  80.         Case Else
  81.             P_DWG = -1
  82.             ThisDrawing.SetVariable "BACKGROUNDPLOT", 2
  83.             Exit Function
  84.     End Select
  85.     '设置打印比例,Paper_Units控制尺寸单位
  86.     Select Case Paper_Units
  87.         Case "英寸"
  88.             ThisDrawing.ActiveLayout.PaperUnits = acInches
  89.         Case "毫米"
  90.             ThisDrawing.ActiveLayout.PaperUnits = acMillimeters
  91.         Case "像素"
  92.             ThisDrawing.ActiveLayout.PaperUnits = acPixels
  93.         Case Else
  94.             P_DWG = -1
  95.             ThisDrawing.SetVariable "BACKGROUNDPLOT", 2
  96.             Exit Function
  97.     End Select
  98.     If CustomScale < 0 Then
  99.         P_DWG = -1
  100.         ThisDrawing.SetVariable "BACKGROUNDPLOT", 2
  101.         Exit Function
  102.     End If
  103.     If CustomScale = 0 Then
  104.         ThisDrawing.ActiveLayout.StandardScale = acScaleToFit
  105.     Else
  106.         ThisDrawing.ActiveLayout.SetCustomScale 1, CustomScale
  107.     End If
  108.     '设置偏移量
  109.     Dim NewValue(0 To 1) As Double
  110.     If Plot_Origin = "," Or Plot_Origin = "0,0" Then
  111.         ThisDrawing.ActiveLayout.CenterPlot = True
  112.     Else
  113.         NewValue(0) = Val(Plot_Origin)
  114.         NewValue(1) = Val(Right(Plot_Origin, Len(Plot_Origin) - InStr(Plot_Origin, ",")))
  115.         ThisDrawing.ActiveLayout.PlotOrigin = NewValue
  116.     End If    '设置打印份数
  117.     ThisDrawing.Plot.NumberOfCopies = Number_Copies
  118.     '区分打印的类型
  119.     Select Case P_Option
  120.         Case 0  '空操作
  121.             P_DWG = 1
  122.             Exit Function
  123.         Case 1      '打印到设备
  124.             If ThisDrawing.Plot.PlotToDevice Then P_DWG = 1 Else P_DWG = 0
  125.         Case 2      '打印到文件
  126.             If Exists(FilePath & Extension) Then
  127.                 If MsgBox(FilePath & Extension & " 已经存在。是否覆盖原文件?", vbYesNo, "覆盖文件") = vbYes Then
  128.                     If ThisDrawing.Plot.PlotToFile(FilePath & Extension) Then P_DWG = 1 Else P_DWG = 0
  129.                 End If
  130.             Else
  131.                 If ThisDrawing.Plot.PlotToFile(FilePath & Extension) Then P_DWG = 1 Else P_DWG = 0
  132.             End If
  133.         Case 3      '打印预览
  134.             ThisDrawing.Plot.DisplayPlotPreview acFullPreview
  135.             P_DWG = 1
  136.         Case Else
  137.             P_DWG = -1
  138.     End Select
  139.     If Val(ThisDrawing.Application.Version) >= 16.2 Then
  140.         ThisDrawing.SetVariable "BACKGROUNDPLOT", 2
  141.     End If
  142.     Exit Function
  143. Err_handle:
  144.     Select Case Err.Number
  145.         Case -2145386493        '打印单位设置错误
  146.             P_DWG = -1
  147.         Case Else
  148.             P_DWG = -2
  149.     End Select
  150. End Function
 楼主| 发表于 2009-5-30 15:54:00 | 显示全部楼层

有人看了我的Flash向我要源码 就放到这里吧 顺便赚点积分

不是专业人员 很多地方写得不专业 请见谅

函数参数说明:

zx,ys打印区域左下点坐标(用string传递主要是参数表太长了,呵呵,很不专业 有空的话可以改成数组)

P_Option 打印方式
   0 不操作(仅设置打印区域)
   1 打印到设备
   2打印到文件

其他参数应该很容易看懂的。另外Degree= "自 动" 就是可以根据打印区域的高宽比自动选纸张大小。

发表于 2010-5-27 20:35:00 | 显示全部楼层

用什么写的啊?

发表于 2013-6-3 21:07:11 | 显示全部楼层
这个记下了研究一下。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 13:51 , Processed in 0.174256 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表