乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 14|回复: 0

来自Inventor VBA的PDF打印

[复制链接]

3

主题

10

帖子

3

银币

初来乍到

Rank: 1

铜币
22
发表于 2009-8-3 09:45:14 | 显示全部楼层 |阅读模式
嗨,伙计们,我意识到这与发明家有关,所以请随时将这篇文章移动到需要去的地方。(如果确实有别的地方可以让它去,那就是?
无论出于何种原因,我们的客户无法查看由 Inventor 2009 中的本机 pdf 打印机创建的 pdf,因此我不得不想出一种使用 PDFCreator 打印 DrawingDocument (DrawDoc) 内容的方法。
根据安装最新软件包时提供的示例,我拼凑了以下似乎有效的代码,但由于某种原因,我几乎永远无法让它从DrawDoc打印第一页。
我认为问题在于PDFCreator是如何初始化的,但我似乎无法弄清楚,并认为也许你们中的一个人可以帮助我。
这是代码: -
  1. Option Explicit
  2. Public Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
  3. Public tmpstr As String
  4. Public revisions(8, 4) As String ' 10 is the maximum number of revisions the border can have
  5. ' Add a reference to PDFCreator
  6. Public pdfcreator1 As PDFCreator.clsPDFCreator
  7. Public ReadyState As Boolean
  8. Public DefaultPrinter As String
  9. Public ProjectLocation As String
  10. Public ProjectName As String
  11. Public Project As String
  12. Public pErr As PDFCreator.clsPDFCreatorError
  13. Public StartTime As Date
  14. Public Sub PlotPdf()
  15. Dim killit
  16. Dim numsheets As Integer
  17. Dim parameters As String
  18. Set pErr = New PDFCreator.clsPDFCreatorError
  19. Set pdfcreator1 = New clsPDFCreator
  20. pdfcreator1.cPrinterStop = False
  21. pdfcreator1.cVisible = True
  22. numsheets = 0
  23. If ThisApplication.ActiveDocument.DocumentType = kDrawingDocumentObject Then
  24. With pdfcreator1
  25.     .cVisible = True
  26.     parameters = "/NoProcessingAtStartup"
  27.     If .cStart(parameters) = False Then
  28.         If .cStart(parameters, True) = False Then
  29.             .cClearCache
  30.             .cOption("UseAutoSave") = 0
  31.         
  32.             ' killit = Shell("taskkill /f /im PDFCreator.exe", VbAppWinStyle.vbHide)
  33.             ' MsgBox ("There was an error starting the pdf printer, please try (click) again!")
  34.             ' Debug.Print "Can't initialize PDFCreator."
  35.             ' Exit Sub
  36.         End If
  37.         AddStatus "Use an existing running instance!"
  38.         .cVisible = True
  39.     End If
  40. End With
  41.     ' Debug.Print "PDFCreator initialized."
  42. Dim oDrgDoc As DrawingDocument
  43. Set oDrgDoc = ThisApplication.ActiveDocument
  44. UserForm1.ComboBox1.AddItem ("Aliquot")
  45. ' UserForm1.ComboBox1.AddItem ("EDD")
  46. UserForm1.Show
  47. ' Set reference to drawing print manager
  48. Dim oDrgPrintMgr As DrawingPrintManager
  49. Set oDrgPrintMgr = oDrgDoc.PrintManager
  50. ' Set the printer name
  51. oDrgPrintMgr.Printer = "PDFCreator"
  52. Dim shts As sheets
  53. Dim sht As sheet
  54. Dim outName As String
  55. Dim i As Integer
  56. Dim j As Integer
  57. Dim Latestrev As Integer
  58. Dim sheetsize As PaperSizeEnum
  59. sheetsize = kPaperSizeA0
  60. sheetsize = kPaperSizeA1
  61. ' shts = oDrgDoc.sheets
  62. For Each sht In oDrgDoc.sheets
  63.     sht.Activate
  64.     'Set the paper size , scale and orientation
  65.     oDrgPrintMgr.ScaleMode = kPrintFullScale ' kPrintBestFitScale
  66.     ' Change the paper size to a custom size. The units are in centimeters.
  67.     Dim shtsize As Long
  68.     shtsize = sht.Size
  69.     oDrgPrintMgr.PaperSize = kPaperSizeCustom
  70.     If shtsize = 9993 Then ' A0
  71.         oDrgPrintMgr.PaperHeight = 84.1
  72.         oDrgPrintMgr.PaperWidth = 118.9
  73.     ElseIf shtsize = 9994 Then ' A1
  74.         oDrgPrintMgr.PaperHeight = 59.4
  75.         oDrgPrintMgr.PaperWidth = 84.1
  76.     ElseIf shtsize = 9995 Then ' A2
  77.         oDrgPrintMgr.PaperHeight = 42
  78.         oDrgPrintMgr.PaperWidth = 59.4
  79.     ElseIf shtsize = 9996 Then ' A3
  80.         oDrgPrintMgr.PaperHeight = 29.7
  81.         oDrgPrintMgr.PaperWidth = 42
  82.     End If
  83.     oDrgPrintMgr.PrintRange = kPrintCurrentSheet
  84.     oDrgPrintMgr.Orientation = kLandscapeOrientation
  85.     oDrgPrintMgr.AllColorsAsBlack = False
  86.     oDrgPrintMgr.Rotate90Degrees = True
  87.         
  88.     Latestrev = RetrieveRev
  89.    
  90.     outName = RetrievePE("", sht) & " REV " & Latestrev & ".pdf"
  91.     With pdfcreator1
  92.         .cOption("UseAutosave") = 1
  93.         .cOption("UseAutosaveDirectory") = 1
  94.         .cOption("AutosaveDirectory") = "\\bas059\Aliquot\pdfs" ' Project
  95.         .cOption("AutosaveFilename") = outName
  96.         .cOption("AutosaveFormat") = 0                            ' 0 = PDF
  97.         .cClearCache
  98.     End With
  99.     oDrgPrintMgr.SubmitPrint
  100.     StartTime = Now
  101.     Do Until pdfcreator1.cCountOfPrintjobs = 1
  102.     DoEvents
  103.         Sleep 1000
  104.     Loop
  105.     Sleep 1000
  106.     pdfcreator1.cPrinterStop = False
  107.     For i = 1 To 8
  108.         For j = 1 To 4
  109.             revisions(i, j) = ""
  110.         Next j
  111.     Next i
  112.     numsheets = numsheets + 1
  113.     AddStatus pdfcreator1.cOutputFilename & " was created! (" & _
  114.   DateDiff("s", StartTime, Now) & " seconds)"
  115. Next
  116. Else
  117.     MsgBox ("You aren't using an Inventor drawing!")
  118.     Exit Sub
  119. End If
  120. MsgBox ("Done Printing " & numsheets & " sheets!")
  121. pdfcreator1.cClose
  122. killit = Shell("taskkill /f /im PDFCreator.exe", VbAppWinStyle.vbHide)
  123. End Sub
  124. '--- the code hereafter is simply for populating the filename with the correct information/ setting the sheetsize etc.
  125. Public Function Setsheetsize(shtsize As PaperSizeEnum) As PaperSizeEnum
  126. If shtsize = 9993 Then
  127.     Setsheetsize = kPaperSizeA0
  128. ElseIf shtsize = 9994 Then
  129.     Setsheetsize = kPaperSizeA1
  130. ElseIf shtsize = 9995 Then
  131.     Setsheetsize = kPaperSizeA2
  132. ElseIf shtsize = 9996 Then
  133.     Setsheetsize = kPaperSizeA3
  134. End If
  135. End Function
  136. Public Function RetrievePE(searchstring As String, oSheet As sheet) As String
  137. Dim oDrawDoc As DrawingDocument
  138. Set oDrawDoc = ThisApplication.ActiveDocument
  139. ' Dim oSheet As sheet
  140. ' Set oSheet = oDrawDoc.ActiveSheet
  141. ' Get the prompted text value from the title block.
  142. ' This is done by first getting the text box in the title
  143. ' block definition that defines the prompted text.  Then
  144. ' you can use this to get the value specified for this
  145. ' particular title block instance.
  146. Dim oBorderDef As BorderDefinition
  147. Set oBorderDef = oSheet.Border.Definition
  148. Dim oTextBox As TextBox
  149. Dim bFound As Boolean
  150. bFound = False
  151. For Each oTextBox In oBorderDef.Sketch.TextBoxes
  152.     If GetPromptField(oTextBox.FormattedText) = searchstring Then
  153.         bFound = True
  154.         Exit For
  155.     End If
  156. Next
  157. If bFound Then
  158.     ' oSheet.Name = oSheet.Border.GetResultText(oTextBox)
  159.     RetrievePE = oSheet.Border.GetResultText(oTextBox)
  160. Else
  161.     MsgBox "Specified formatted text was not found in the title block."
  162. End If
  163. End Function
  164. Public Function RetrieveRev() As Integer ' will only work whilst the revision is numeric!
  165. Dim oDrawDoc As DrawingDocument
  166. Set oDrawDoc = ThisApplication.ActiveDocument
  167. Dim oSheet As sheet
  168. Set oSheet = oDrawDoc.ActiveSheet
  169. Dim oBorderDef As BorderDefinition
  170. Set oBorderDef = oSheet.Border.Definition
  171. Dim oTextBox As TextBox
  172. Dim bFound As Boolean
  173. bFound = False
  174. Dim Revision As String
  175. Dim cnt As Integer
  176. Dim i As Integer
  177. Dim j As Integer
  178. i = 1
  179. cnt = 0
  180. For Each oTextBox In oBorderDef.Sketch.TextBoxes
  181.     Revision = GetPromptField(oTextBox.FormattedText)
  182.     If Revision Like "*REV*" Then
  183.         If Revision Like "*REV*Change*" Or Revision Like "*REV*CHANGE*" Then
  184.             revisions(i, 1) = oSheet.Border.GetResultText(oTextBox) ' Change
  185.             cnt = cnt + 1
  186.         ElseIf Revision Like "*REV*Date*" Or Revision Like "*REV*DATE*" Then
  187.             revisions(i, 3) = oSheet.Border.GetResultText(oTextBox) ' Date
  188.             cnt = cnt + 1
  189.         ElseIf Revision Like "*REV*" And Len(Revision)  "" Then
  190.         ' Debug.Print revisions(i, 1) & "|" & revisions(i, 2) & "|" & revisions(i, 3)
  191.     End If
  192. Next i
  193. Bubblesort
  194. For i = LBound(revisions) To UBound(revisions)
  195.     If revisions(i, 1)  "" Then
  196.         ' Debug.Print revisions(i, 1) & "|" & revisions(i, 2) & "|" & revisions(i, 3)
  197.     End If
  198. Next i
  199. For i = LBound(revisions) To UBound(revisions)
  200.     If revisions(i, 2)  "" Then
  201.         RetrieveRev = revisions(i, 2)
  202.         If revisions(i + 1, 2) = "" Then ' we reached the highest revision.
  203.             Exit For
  204.         End If
  205.     End If
  206. Next i
  207. End Function
  208. ' Get the text value of the prompted text.  It extracts this from the' formatted text.  If there's a failure then an empty string is =returned.
  209. Private Function GetPromptField(ByVal FormattedText As String) As String
  210. On Error GoTo ErrorFound
  211. ' Verify that this is a prompt field.
  212. If Left$(FormattedText, 7)  "" symbol
  213. ' and to the left of the last ""))
  214. GetPromptField = Left$(GetPromptField, InStr(GetPromptField, " with  symbols.
  215. GetPromptField = Replace(GetPromptField, "", ">")
  216. Exit Function
  217. ErrorFound:    GetPromptField = ""
  218. End Function
  219. Public Sub Bubblesort()
  220. Dim i As Integer
  221. Dim j As Integer
  222. Dim temp As String
  223. Dim iOuter As Long
  224. Dim iInner As Long
  225. Dim iLbound As Long
  226. Dim iUbound As Long
  227. Dim iTemp As String
  228. iLbound = LBound(revisions)
  229. For i = iLbound To UBound(revisions) ' - 1
  230.     If revisions(i, 2)  "" Then
  231.         iUbound = i
  232.     End If
  233. Next i
  234. For iOuter = iLbound To iUbound ' - 1
  235.         'Which comparison
  236.         For iInner = iLbound To iUbound - iOuter - 1
  237.             'Compare this item to the next item
  238.             If revisions(iInner, 2)  "" Then ' Continue
  239.                 ' Debug.Print "About to sort " & revisions(iInner, 4)
  240.                 If CInt(revisions(iInner, 2)) > CInt(revisions(iInner + 1, 2)) Then
  241.                     'Swap
  242.                     iTemp = revisions(iInner, 1)
  243.                     revisions(iInner, 1) = revisions(iInner + 1, 1)
  244.                     revisions(iInner + 1, 1) = iTemp
  245.                     iTemp = revisions(iInner, 2)
  246.                     revisions(iInner, 2) = revisions(iInner + 1, 2)
  247.                     revisions(iInner + 1, 2) = iTemp
  248.                     iTemp = revisions(iInner, 3)
  249.                     revisions(iInner, 3) = revisions(iInner + 1, 3)
  250.                     revisions(iInner + 1, 3) = iTemp
  251.                     iTemp = revisions(iInner, 4)
  252.                     revisions(iInner, 4) = revisions(iInner + 1, 4)
  253.                     revisions(iInner + 1, 4) = iTemp
  254.                 End If
  255.             End If
  256.         Next iInner
  257.     Next iOuter
  258. ' MsgBox ("Done Sorting!")
  259. End Sub
  260. Private Sub PrintPage(PageNumber As Integer)
  261. Dim cPages As Long
  262. cPages = Selection.Information(wdNumberOfPagesInDocument)
  263. If PageNumber > cPages Then
  264.   MsgBox "This document has only " & cPages & " pages!", vbExclamation
  265. End If
  266. DoEvents
  267. ActiveDocument.PrintOut Background:=False, Range:=wdPrintFromTo, From:=CStr(PageNumber), To:=CStr(PageNumber)
  268. DoEvents
  269. End Sub
  270. Private Sub PDFCreator1_eError()
  271. pErr = pdfcreator1.cError
  272. AddStatus ("Status: Error[" & pErr.Number & "]: " & pErr.Description)
  273. End Sub
  274. Private Sub PDFCreator1_eReady()
  275. AddStatus "File'" & pdfcreator1.cOutputFilename & "' was saved."
  276. pdfcreator1.cPrinterStop = True
  277. ' CommandButton1.Enabled = True
  278. End Sub
  279. Private Sub AddStatus(Str1 As String)
  280.     Debug.Print vbCrLf & Now & ": " & Str1
  281. End Sub

另外,理想情况下,我想从IV2009中的工具栏运行它 - 有人可以提供一个如何实现这一点的示例吗?
谢谢。

本帖以下内容被隐藏保护;需要你回复后,才能看到!

游客,如果您要查看本帖隐藏内容请回复
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-7-2 00:52 , Processed in 1.140667 second(s), 55 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表