AnsweredAssumed Answered

Loop through part selection in an assembly

Question asked by Miguel Goncalves on Mar 21, 2019
Latest reply on Mar 26, 2019 by Miguel Goncalves

Hello, I'm trying to set the mass units/decimal places of the selected parts inside an assembly but when I run it throws an error "Run time error '438' Object doesn't support this property or method". GetSelectedObjectsComponent4 doesn't output a Component? Therefore I defined SetUnits(swComp As Component2)


Can somebody help me fix this, and if possible explain me why it wasn't working?


Thanks in advance!


' Mass_Section_Properties (Mass) ---------------------------------------------21/08/2016
' ****************************************************************************************
' Macro will change the Mass units for Mass/Section Properties from all others to Grams;
' You can use TASK and run this macro on all parts or ass at once, they go on 1 decimals;
' You can change the units and decimals by your self according to the parameters table;
' Written by: Deepak Gupta ( )
' Modified by: Jeff Lee ( Specially thanks to Deepak Gupta )
' ****************************************************************************************
Option Explicit
'On Error Resume Next

Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()
Dim i As Integer
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Dim selMgr As SelectionMgr
Dim swSelComp As SldWorks.Component2
Dim swSelModel As SldWorks.ModelDoc2

If Part.GetType = swconst.swDocASSEMBLY Then
    Set selMgr = Part.SelectionManager

    If selMgr.GetSelectedObjectCount2(-1) = 0 Then
        MsgBox "No selected component"
        Exit Sub
    End If

    If selMgr.GetSelectedObjectType3(1, -1) <> swSelCOMPONENTS Then
        MsgBox "No selected component"
        Exit Sub
    End If
    For i = 1 To selMgr.GetSelectedObjectCount2(-1)
        Set swSelComp = selMgr.GetSelectedObject6(i, -1) 'obtem a part segundo o index da seleção (index = numero inteiro, se selecionar 3parts o index vai de 1 a 3)
        'Set swModelPart = myComp.ModelDoc2
        If swSelComp Is Nothing Then
            Set swSelComp = swSelComp.GetComponent
            Debug.Print "whatelse"
            Set swSelComp = selMgr.GetSelectedObjectsComponent4(i, -1)
        End If
        SetUnits (swSelComp)
        'MsgBox myComp.GetPathName
    SetUnits (Part)
End If
 End Sub

Function SetUnits(swComp As Component2)
    ' Set the Unit system from (MKS, CGS, MMGS, IPS) to Custom
    boolstatus = swComp.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitSystem, 0, swUnitSystem_Custom)
    ' Get and Check the Mass_Section_Properties - Mass - Unit, if it is not Grams
    ' Mass units Member               Description(Value)
    ' swUnitsMassPropMass_Grams       2
    ' swUnitsMassPropMass_Kilograms   3
    ' swUnitsMassPropMass_Milligrams  1
    ' swUnitsMassPropMass_Pounds      4
    Debug.Print "try"
    If (swComp.Extension.GetUserPreferenceInteger(swUnitsMassPropMass, 0) <> 3) Then
        ' Change to Grams
         boolstatus = swComp.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitsMassPropMass, 0, 3) '
        'Else                           // Please ignore this code, it is the original code from Deepak Gupta
        ' Else keep or set to Grams     // Please ignore this code, it is the original code from Deepak Gupta
        ' You can choose the decimal place format:
        ' boolstatus = Part.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitsMassPropMass, 0, 2)
        ' Iza zareza zaoruzuje na stotu decimalu ( Behind the point set the hundredth decimal place,
        ' Integer value could be set to (0, 1, 2, 3, 4, 5, 6, 7, 8 )
           boolstatus = swComp.Extension.SetUserPreferenceInteger(swUserPreferenceIntegerValue_e.swUnitsMassPropDecimalPlaces, swUserPreferenceOption_e.swDetailingNoOptionSpecified, 1)
    End If
End Function