我使用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
答案 0 :(得分:0)
你的变量设置全部出现故障。
PDFpath
派生的currpath
来自Filename
,但是Filename
之后才设置currpath
。
试试这个:
Filename = Right(swModel.GetPathName, Len(swModel.GetPathName) - InStrRev(swModel.GetPathName, "\"))
currpath = Left(Filename, InStrRev(Filename, "\"))
PDFpath = currpath & "PDF"
自从你说你刚开始编程以来还有其他一些建议:
选择命名和大写惯例并坚持下去。您可以从MSDN中描述的Microsoft Visual Basic Naming Conventions开始:)
尽量使您的变量名称具有描述性和适当性。例如。你有Filename
但它实际上不是文件名,它是文件的完整路径。更好的名称是FullPath
或FilePath
。 FileName
表示没有目录的文件名。
您正在对同一过程进行多次调用,这只会为您的程序带来不必要的开销。进行一次调用,将值存储在变量中,并根据需要多次引用该变量。
声明您的变量更接近它们的使用位置;它更容易看到它们的使用位置,并且可以防止必须向上和向下滚动以查看它们的声明。
使用scope对您有利 - 如果您想重置变量,在循环块内声明,它将超出范围并在每次循环迭代时重新创建。无需每次都清除它。