javasolidworkssolidworksapi

SolidWorks automatic unit set assignment


Friends, I have a code that will MMGS all the components of an assembly in SolidWorks in VBA, but I am getting an error on the yellow marked line. Can anyone tell me why? Thank you. enter image description here

Dim swApp As Object
Dim swModel As Object
Dim swAssy As Object
Dim swComp As Object
Dim swPart As Object
Dim swConf As Object
Dim swConfMgr As Object

Const swMM = 2
Const swGram = 2
Const swSecond = 0
Const swDegrees = 0

Sub Main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    If swModel Is Nothing Then
        MsgBox "Lütfen bir montaj dosyası açın.", vbExclamation
        Exit Sub
    End If
    
    If swModel.GetType <> 2 Then ' 2 represents an Assembly document
        MsgBox "Lütfen bir montaj dosyası açın.", vbExclamation
        Exit Sub
    End If
    
    Set swAssy = swModel
    Set swConfMgr = swAssy.ConfigurationManager
    Set swConf = swConfMgr.ActiveConfiguration
    
    Dim vComponents As Variant
    vComponents = swConf.GetComponents(False)
    
    For i = 0 To UBound(vComponents)
        Set swComp = vComponents(i)
        Set swPart = swComp.GetModelDoc2
        If Not swPart Is Nothing Then
            ' Linear Units
            swPart.SetUserPreferenceDoubleValue 1, swMM
            ' Mass Units
            swPart.SetUserPreferenceDoubleValue 2, swGram
            ' Time Units
            swPart.SetUserPreferenceDoubleValue 3, swSecond
            ' Angle Units
            swPart.SetUserPreferenceDoubleValue 4, swDegrees
            swPart.EditRebuild3
        End If
    Next i
    
    MsgBox "Tüm bileşenlerin birimleri MMGS olarak ayarlandı."
End Sub

I have a code that will MMGS all the components of an assembly in SolidWorks, but I am getting an error on the yellow marked line


Solution

  • As Stone said, GetComponents is a function of AssemblyDoc.

    Also you need to use SetUserPreferenceInteger instead of SetUserPreferenceDoubleValue for either global metric units or custom units.

    Option Explicit
    Sub Main()
        Dim swApp As SldWorks.SldWorks
        Dim swModel As SldWorks.ModelDoc2
        Dim swAssy As SldWorks.AssemblyDoc
        Dim swComp As SldWorks.Component2
        Dim swPart As SldWorks.ModelDoc2
        Dim vComp As Variant
        Dim vComps As Variant
        
        Set swApp = Application.SldWorks
        Set swModel = swApp.ActiveDoc
        
        If swModel Is Nothing Then MsgBox "Lütfen bir montaj dosyasi açin.": Exit Sub
        If swModel.GetType <> swDocumentTypes_e.swDocASSEMBLY Then MsgBox "Lütfen bir montaj dosyasi açin.": Exit Sub
        
        Set swAssy = swModel
        vComps = swAssy.GetComponents(False)
    
        For Each vComp In vComps
            Set swComp = vComp
            Set swPart = swComp.GetModelDoc2
            If Not swPart Is Nothing Then
                'Global metric units
                swPart.Extension.SetUserPreferenceInteger swUserPreferenceIntegerValue_e.swUnitSystem, swUserPreferenceOption_e.swDetailingNoOptionSpecified, swUnitSystem_e.swUnitSystem_MMGS
                
                ''Custom metric units
                'swPart.Extension.SetUserPreferenceInteger swUserPreferenceIntegerValue_e.swUnitSystem, swUserPreferenceOption_e.swDetailingNoOptionSpecified, swUnitSystem_e.swUnitSystem_Custom
                '' Linear Units
                'swPart.Extension.SetUserPreferenceInteger swUserPreferenceIntegerValue_e.swUnitsLinear, swUserPreferenceOption_e.swDetailingNoOptionSpecified, swLengthUnit_e.swMM
                'swPart.Extension.SetUserPreferenceInteger swUserPreferenceIntegerValue_e.swUnitsLinearDecimalPlaces, swUserPreferenceOption_e.swDetailingNoOptionSpecified, 0
                '' Mass Units
                'swPart.Extension.SetUserPreferenceInteger swUserPreferenceIntegerValue_e.swUnitsMassPropMass, swUserPreferenceOption_e.swDetailingNoOptionSpecified, swUnitsMassPropMass_e.swUnitsMassPropMass_Grams
                'swPart.Extension.SetUserPreferenceInteger swUserPreferenceIntegerValue_e.swUnitsMassPropDecimalPlaces, swUserPreferenceOption_e.swDetailingNoOptionSpecified, 0
                ''volume
                'swPart.Extension.SetUserPreferenceInteger swUserPreferenceIntegerValue_e.swUnitsMassPropLength, swUserPreferenceOption_e.swDetailingNoOptionSpecified, swLengthUnit_e.swMM
                'swPart.Extension.SetUserPreferenceInteger swUserPreferenceIntegerValue_e.swUnitsMassPropVolume, swUserPreferenceOption_e.swDetailingNoOptionSpecified, swUnitsMassPropVolume_e.swUnitsMassPropVolume_Millimeters3
                '' Angle Units
                'swPart.Extension.SetUserPreferenceInteger swUserPreferenceIntegerValue_e.swUnitsAngular, swUserPreferenceOption_e.swDetailingNoOptionSpecified, swAngleUnit_e.swDegrees
                '' Time Units
                'swPart.Extension.SetUserPreferenceInteger swUserPreferenceIntegerValue_e.swUnitsTimeUnits, swUserPreferenceOption_e.swDetailingNoOptionSpecified, swUnitsTimeUnit_e.swUnitsTimeUnit_Second
            End If
        Next
        
        MsgBox "Tüm bilesenlerin birimleri MMGS olarak ayarlandi."
    End Sub