首页 > 解决方案 > VBA SOLIDWORKS 从文件中添加设计表

问题描述

我有一个“打包”的代码,并将绘图和设计表放到一个新位置,并且所有三个文件的名称完全相同。现在我有一个问题是要从设计表中更新该打包零件,因为我没有将设计表链接到该零件,因为如果我为“打包”而拥有的代码不会重命名文件把它连接起来。

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swPackAndGo As SldWorks.PackAndGo
Dim OpenFilePath As String
Dim OpenFileName As String
Dim SavePath As String
Dim SaveName As String
Dim myFileName As String
Dim myExtension As String
Dim pgFileNames As Variant
Dim pgFileStatus As Variant
Dim status As Boolean
Dim statuses As Variant
Dim i As Long
Dim partDocExt As SldWorks.ModelDocExtension

Sub PackAndGo()

Call Calculate_TFVPM_FormatSketchFileName

OpenFilePath = "E:\FORMAT\FormatSketch.SLDPRT"
SavePath = "E:\FORMAT\Temp\"
SaveName = NewSaveName

Set swApp = GetObject(, "SldWorks.Application")
Set swModel = swApp.OpenDoc(OpenFilePath, swDocPART)

'Set swModel = swApp.ActiveDoc
OpenFilePath = swModel.GetPathName
OpenFileName = Mid(OpenFilePath, InStrRev(OpenFilePath, "\") + 1, InStrRev(OpenFilePath, ".") - InStrRev(OpenFilePath, "\") - 1)

Set swModelDocExt = swModel.Extension

'Update part from design table



'Get Pack and Go object
Set swPackAndGo = swModelDocExt.GetPackAndGo

'Include any drawings
swPackAndGo.IncludeDrawings = True

'Set folder where to save the files
status = swPackAndGo.SetSaveToName(True, SavePath)

'Get files path
status = swPackAndGo.GetDocumentSaveToNames(pgFileNames, pgFileStatus)
For i = 0 To UBound(pgFileNames)
    myFileName = Mid(pgFileNames(i), InStrRev(pgFileNames(i), "\") + 1, InStrRev(pgFileNames(i), ".") - InStrRev(pgFileNames(i), "\") - 1)
    myExtension = Right(pgFileNames(i), Len(pgFileNames(i)) - InStrRev(pgFileNames(i), ".") + 1)

    'Replace name
    If LCase(myFileName) = LCase(OpenFileName) Then
        pgFileNames(i) = SavePath & SaveName & myExtension
    End If
Next

'Set files path
status = swPackAndGo.SetDocumentSaveToNames(pgFileNames)

'Flatten the Pack and Go folder structure; save all files to the root directory
swPackAndGo.FlattenToSingleFolder = True

'Pack and Go
statuses = swModelDocExt.SavePackAndGo(swPackAndGo)
        
'Close initial template
swApp.CloseDoc (swModel.GetPathName)
        
End Sub

不太确定如何用谷歌搜索我正在寻找的东西,但我发现的是: http ://help.solidworks.com/2018/english/api/sldworksapi/get_excel_design_table_worksheet_example_vb.htm

试图弄清楚,也许,这里有人已经知道解决方案?

标签: vbaapisolidworks

解决方案


推荐阅读