0

我已经使用 VBA 为 Solidworks 编写了一个宏。宏将所有打开的工程图文件保存为 PDF(从工程图文件中的参考模型收集信息以构建文件名)到它在打开的工程图文件目录中创建的文件夹中,然后关闭工程图并继续下一个一。

我的问题是它将任何打开的绘图文件中的所有 PDF 保存到它从第一张绘图创建的同一个文件夹中。因此,如果我有多个项目,我正在处理所有 PDF 文件进入创建的第一个文件夹,直到我重新启动计算机才会出现这种情况。

我是否需要清除一些值或引用,以便它可以使用相同的宏执行多个项目?

请注意这里有一些加倍和不必要的东西,但这只是因为在我处理它的同时,我想在我忘记它们之前为未来保留一些选择!

Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swCustProp As CustomPropertyManager
Dim valOut1 As String
Dim valOut2 As String
Dim resolvedValOut1 As String
Dim resolvedValOut2 As String

Dim ConfigName As String
Dim PartNo As String
Dim FullName As String
Dim nFileName As String
Dim swView As SldWorks.View
Dim PDFpath As String
Dim Filename As String
Dim currpath As String

Sub main()

    Dim swDocs As Variant
    Dim i As Integer
    Set swApp = Application.SldWorks

    swDocs = swApp.GetDocuments
    For i = 0 To UBound(swDocs)
        Set swModel = swDocs(i)
        If swModel.GetType = swDocDRAWING Then

        currpath = Left(Filename, InStrRev(Filename, "\"))
        Filename = Right(swModel.GetPathName, Len(swModel.GetPathName) - InStrRev(swModel.GetPathName, "\"))
        PDFpath = currpath & "PDF"

            Set swDraw = swModel
            Set swView = swDraw.GetFirstView
            Set swView = swView.GetNextView
            Set swModel = swView.ReferencedDocument

            currpath = Left(Filename, InStrRev(Filename, "\"))
            Filename = Right(swModel.GetPathName, Len(swModel.GetPathName) - InStrRev(swModel.GetPathName, "\"))
            PDFpath = currpath & "PDF"

            If (swModel.GetType = swDocPART) Then
                Set swModel = swView.ReferencedDocument
                Set swView = swDraw.GetFirstView
                Set swView = swView.GetNextView
                ConfigName = swView.ReferencedConfiguration
                FullName = swModel.GetTitle
                PartNo = Left(FullName, Len(FullName) - 7)

                Set swCustProp = swModel.Extension.CustomPropertyManager(ConfigName)
                swCustProp.Get2 "Description", valOut1, resolvedValOut1
                swCustProp.Get2 "Revision", valOut2, resolvedValOut2

                If Dir(PDFpath, vbDirectory) = "" Then MkDir PDFpath

                    nFileName = PDFpath & "\" & PartNo & "-" & ConfigName & "-" & resolvedValOut2 & " " & resolvedValOut1

                    swDraw.SaveAs3 nFileName & ".PDF", 0, 0

                    'MsgBox nFileName & ".PDF" + " Saved as a PDF"

                    swApp.QuitDoc swDraw.GetPathName

                ElseIf (swModel.GetType = swDocASSEMBLY) Then

                    Set swView = swDraw.GetFirstView
                    Set swView = swView.GetNextView
                    Set swModel = swView.ReferencedDocument
                    ConfigName = swView.ReferencedConfiguration
                    FullName = swModel.GetTitle
                    PartNo = Left(FullName, Len(FullName) - 7)

                    Set swCustProp = swModel.Extension.CustomPropertyManager("")
                        swCustProp.Get2 "Description", valOut1, resolvedValOut1
                        swCustProp.Get2 "Revision", valOut2, resolvedValOut2

                    If Dir(PDFpath, vbDirectory) = "" Then MkDir PDFpath

                    nFileName = PDFpath & "\" & PartNo & "-" & resolvedValOut2 & " " & resolvedValOut1

                    swDraw.SaveAs3 nFileName & ".PDF", 0, 0

                    swApp.QuitDoc swDraw.GetPathName
                End If
        End If
    Next i

    MsgBox ("All open drawings saved as PDF!" & vbNewLine & vbNewLine & "That was too fast and too furious.")

End Sub
4

1 回答 1

0

你的变量设置是所有它出现的顺序。

看起来PDFpathis derived from currpathis derived from Filename,但是你不设置Filenametil after currpath

尝试这个:

    Filename = Right(swModel.GetPathName, Len(swModel.GetPathName) - InStrRev(swModel.GetPathName, "\"))
    currpath = Left(Filename, InStrRev(Filename, "\"))

    PDFpath = currpath & "PDF"

自从您说您刚刚开始编程以来,还有其他一些建议:

选择一个命名和大小写约定并坚持下去。您可以从 MSDN 中描述的Microsoft Visual Basic 命名约定开始:)

尝试使您的变量名称尽可能具有描述性和适当性。例如,您有Filename,但实际上不是文件名,而是文件的完整路径。更好的名字是FullPathor FilePathFileName将指示没有目录的文件名。

您正在对同一过程进行多次调用,这只会为您的程序带来不必要的开销。调用一次,将值存储在变量中,并根据需要多次引用该变量。

在更接近使用它们的位置声明您的变量;更容易查看它们的使用位置,并且不必上下滚动查看它们的声明是什么。

使用范围对您有利 - 如果您想重置变量,在循环块内声明,它将超出范围并在每次循环迭代时重新创建。无需每次都清除。

于 2015-08-28T02:15:30.630 回答