How can I get all the dimensions of the selected component in the assembly file?

I wrote a macro to select a component by name from an assembly and then change its dimensions as required to automate the process. 

Since I used dimension names, this script cannot be currently used for other similar assemblies.

My question is, how can I get all the dimensions of the selected component in the assembly file.

Including a part of my code here:

----------------------------------------------------------------------------------------------------------------------------------------------------

Dim vComponents As Variant
Dim vComp As Variant

Dim Part As Object

Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc

vComponents = Part.GetComponents(True)

For Each vComp In vComponents
Set swComp = vComp
If InStr(swComp.Name2, "_ST_") > 0 Then swComp.Select4 True, Nothing, False
Next


Set swSelMgr = Part.SelectionManager
Set swSelComp = swSelMgr.GetSelectedObjectsComponent4(1, -1)
Set swReferenceModel = swSelComp.GetModelDoc2

originalStello = swReferenceModel.Parameter("D7@Schizzo1").Value  // this is where I am hard coding the name

----------------------------------------------------------------------------------------------------------------------------------------------------

In the last line, you can see that I am getting the dimension value by hard coding its name. I would rather like to get all dimensions of the SELECTED component and then write logic to get a particular dimension from that list.

I started working with Solidworks API 2 days ago. Please help!

SolidworksApi/macros
Answers 2
AT
AT 2021-04-08 Best Answer
PB
PB 2021-04-08

Hi,

You can use the below code sample:

Option Explicit<br><br>' Create a variable for Solidworks application<br>Dim swApp As SldWorks.SldWorks<br><br>' Create a variable for Solidworks document<br>Dim swDoc As SldWorks.ModelDoc2<br><br>' Create a variable for Solidworks Assembly document<br>Dim swAsm As SldWorks.AssemblyDoc<br><br>' Create a variable for Solidworks Assembly Component<br>Dim swComponent As SldWorks.Component2<br><br>' Create a variable for Solidworks Feature<br>Dim swFeature As SldWorks.Feature<br><br>' Create a variable for Solidworks Display Dimension<br>Dim swDisDimension As SldWorks.DisplayDimension<br><br>' Create variable for Solidworks Model Dimension<br>Dim swModelDimension As SldWorks.Dimension<br><br>' Boolean Variable<br>Dim BoolStatus As Boolean<br><br>' Main function of our VBA program<br>Sub GetDimensionOfComponentsInAnAssembly()<br><br>    ' Set Solidworks variable to Solidworks application<br>    Set swApp = Application.SldWorks<br><br>    ' Set Solidworks document to active document<br>    Set swDoc = swApp.ActiveDoc<br><br>    ' Local variable for User reponce if we get some error<br>    Dim userResponce As Integer<br><br>    ' Check if any Document is open or Not<br>    If swDoc Is Nothing Then<br><br>        ' If there are no document is open then Inform user and stop macro<br>        userResponce = swApp.SendMsgToUser2("Please open an Assembly!", swMessageBoxIcon_e.swMbWarning, swMessageBoxBtn_e.swMbOk)<br><br>        ' Stop macro<br>        End<br>    End If<br><br>    ' Check if Assembly Document is open or Not<br>    If Not swDoc.GetType = swDocASSEMBLY Then<br><br>        ' If there are no Assembly document is open then Inform user and stop macro<br>        userResponce = swApp.SendMsgToUser2("Please open an Assembly!", swMessageBoxIcon_e.swMbWarning, swMessageBoxBtn_e.swMbOk)<br><br>        ' Stop macro<br>        End<br>    End If<br><br>    ' Set Solidworks Assembly document to Solidworks document<br>    Set swAsm = swDoc<br><br>    ' Create a Variant type variable to hold a List of Components in this assembly<br>    Dim ComponentsList As Variant<br><br>    ' Set this variable value by "GetComponents()" method from<br>    ComponentsList = swAsm.GetComponents(True)<br><br>    ' Create variable for a single component<br>    Dim singleComponent As Variant<br><br>    ' Loop through each component in our Component list<br>    For Each singleComponent In ComponentsList<br><br>        ' Set Solidworks Assembly Component to the single component from our component list<br>        Set swComponent = singleComponent<br><br>        ' Set Solidworks Feature to the First feature of this component<br>        Set swFeature = swComponent.FirstFeature<br><br>        ' Applying a While loop until swFeature is nothing i.e. till our last feature<br>        While Not swFeature Is Nothing<br><br>            ' Print Feature name in Immediate Window<br>            Debug.Print swFeature.Name<br><br>            ' Set Solidworks Display Dimension by GetFirstDisplayDimension method from Solidworks Feature<br>            Set swDisDimension = swFeature.GetFirstDisplayDimension<br><br>            ' While loop for All Display Dimensions of this feature<br>            While Not swDisDimension Is Nothing<br><br>                ' Set Solidworks Model dimension by GetDimension2 method from Solidworks Display Dimension<br>                Set swModelDimension = swDisDimension.GetDimension2(0)<br><br>                ' Print Solidworks Model dimension FullName in Immediate Window<br>                Debug.Print swModelDimension.FullName<br><br>                ' Print Solidworks Model dimension value in Immediate Window<br>                Debug.Print swModelDimension.Value<br><br>                ' Set Solidworks Display dimension to next display dimension<br>                Set swDisDimension = swFeature.GetNextDisplayDimension(swDisDimension)<br><br>            ' Looping over display dimension<br>            Wend<br><br>            ' Set Solidworks Feature to the First feature of this component<br>            Set swFeature = swFeature.GetNextFeature<br><br>        ' While loop end here<br>        Wend<br><br>    ' Next component in our Component list<br>    Next<br><br>End Sub<br><span class="line-numbers-rows"><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span><span>‍</span></span>

I have commented each line so that you can understand why I am using those functions and objects.

This macro will give output on the Immediate Window of Visual Basic Editor.

In case you don't know how to open the Immediate window in VBE then go to this blog.

Since you have just started you will found this blog interested to you.

Thanks

Prashant Baher