VBA Solidworks 从工程图中保存模型

VBA Solidworks save model from drawing

我有一个 VBA 允许我发布图纸的宏 - 作为其中的一部分,它允许更改模型的属性、发布、发布日期等。

这个想法只是打开绘图、更新问题、日期等(另存为 pdf 和 dwg)- 它有效,更改了属性,并保存了正确的视图。

但是 属性 更改不会保存到模型中,除非我打开模型并强制保存,因此当我重新打开 drawing/model 时,它们会恢复到原来的状态。

有谁知道我怎样才能强制保存模型,即使它没有打开。 请参阅最后几行以了解我的微弱尝试:(

Sub WriteModelProperties(swApp As SldWorks.SldWorks, swModel As SldWorks.ModelDoc2)
Dim element As Integer
Dim boolstatus As Boolean
Dim ctrl As MSForms.Control
Dim fieldName As String
Dim fieldType As Integer
Dim fieldValue As String

For element = 0 To 25
    fieldName = propertiesValue(0, element)
    Select Case propertiesValue(1, element)
        Case "Text": fieldType = 30
        Case "Date": fieldType = 64
    End Select
    Set ctrl = UserForm1.Controls(propertiesValue(2, element)) 'to make a compact code
        
        Select Case propertiesValue(3, element)
            Case "Caption": fieldValue = ctrl.Caption
            Case "Value": fieldValue = ctrl.Value
        End Select
    Debug.Print fieldValue
    boolstatus = swCustProp.Add3(fieldName, fieldType, fieldValue, swCustomPropertyDeleteAndAdd)
Next element


swModel.Rebuild (swRebuildAll)
swModel.EditRebuild3 ' Update model properties     
swModel.ViewZoomtofit2
boolstatus = swModel.Save3(swSaveAsOptions_Silent, lErrors, lWarnings)
End Sub
 

对不起...我脸上有点鸡蛋...它没有用必须拆分零件和组件:(

这似乎有效...如果它侮辱了 vba,我深表歉意。 您只需要打开工程图,而不是零件或装配体:)抱歉无法抗拒。

Option Explicit
    Public swApp        As SldWorks.SldWorks
    Public swModDoc     As SldWorks.ModelDoc2
    Dim swView          As SldWorks.View
    Dim swPart          As PartDoc
    Dim swAss           As AssemblyDoc
    Dim boolstatus      As Boolean
    Dim lErrors         As Long 'Varaible to collect Errors
    Dim lWarnings       As Long 'Varaible to collect Errors
        
Sub main()

    Set swApp = Application.SldWorks
    Set swModDoc = swApp.ActiveDoc
    Set swView = swModDoc.GetFirstView
    Set swView = swView.GetNextView
    
    If swView.ReferencedDocument.GetType = 1 Then
        Set swPart = swView.ReferencedDocument
        boolstatus = swPart.Save3(swSaveAsOptions_Silent, lErrors, lWarnings)
    ElseIf swView.ReferencedDocument.GetType = 2 Then
        Set swAss = swView.ReferencedDocument
        boolstatus = swAss.Save3(swSaveAsOptions_Silent, lErrors, lWarnings)
    End If
End Sub