AnsweredAssumed Answered

Getting Dimension from Part in an Assembly Not Working (long)

Question asked by Steve Fye on Feb 21, 2017
Latest reply on Feb 21, 2017 by Steve Fye

This macro is designed to work from Excel.  I've been struggling with this for 3 days now and can't figure out how to get it to give me consistent results.  Here's what's going on:

 

The goal is to do this:

1.  Have the assembly active (Assem1.SLDASM)

2.  Press the "Grab Dimension" button from Excel

3.  Select the component (SSM-Straight-Section NO DESIGN TABLE.SLDPRT) in the assembly.

4.  Grab the ModelDoc from the selected part and read the value of 2 named dimensions in the part.  One dimension is named "length@frame@SSM-Straight-Section TEST.SLDPRT", the other is "chain-width@Sketch3@SSM-Straight-Section TEST.SLDPRT"

5.  Enter those values into the spreadsheet.

 

 

Now, for the most part, this Excel/VBA program WORKS...  It allows selection, grabs the component, even gets dimensions, but here's the problem... if I change the configuration of the part from within the assembly and run the macro again, it does not grab the most current dimensions after the rebuild (See the results posted below).  What's really weird is that if I open the component, then go back to the assembly, then run the macro again, it works fine (with correct dimension values).

 

You'll notice that the macro uses 5 different methods for grabbing the dimension value, and there's not one single method that gives me the right "combination" of values that I can use.

 

I'd really really appreciate any help I can get with this.

 

Here is what the output looks like

Here is what the output SHOULD look like

 

Download files here

 

 

 

Here's the code:

 

Public swApp               As Object

Public swModel             As Object

Public swReferenceModel    As Object

Public swSelComp           As Object

Public swSelMgr            As Object

Public swDisplayDimension  As Object

Public swDimension  As Object

Public DimensionName, SelectedComponentParam_ConfigurationName As String

Public vConfigNameArray As Variant

Public vConfigName As Variant

Public sSpecConfigNameArr(0) As String

Public vSpecConfigNameArr As Variant

   

 

 

Sub GrabbingDimensionIssue()

    'Attach to SolidWorks from Excel,

    'call routine to grab a component

    'and get the dimensions from it

    Set swApp = GetObject(, "Sldworks.Application")

    Set swModel = swApp.ActiveDoc

    SelectComponentInAssembly

    If Not swSelComp Is Nothing Then

        ExtractDataFromSelectedComponent

    End If

End Sub

 

 

 

 

Sub SelectComponentInAssembly()

    'Allows user to select a component from the assembly.

    'Sets up the selection manager and the ModelDoc for

    'the selected component

    Dim All As Boolean

    swModel.ClearSelection2 (All)

    Set swReferenceModel = Nothing

    Set swSelComp = Nothing

    Set swSelMgr = Nothing

    Do While swSelComp Is Nothing

        Set swSelMgr = swModel.SelectionManager

        Set swSelComp = swSelMgr.GetSelectedObjectsComponent2(1)

        DoEvents

    Loop

End Sub

 

 

 

 

Sub ExtractDataFromSelectedComponent()

    'Name of component in the model tree

    SelectedComponentParam_SSMComponentName = swSelComp.Name

   

    'Get active configuration name of referenced component

    'Also set up array for configuraitons of selected component

    Set swReferenceModel = swSelComp.GetModelDoc

    SelectedComponentParam_ConfigurationName = swSelComp.ReferencedConfiguration

    vConfigNameArray = swReferenceModel.GetConfigurationNames

 

 

    'Title info for selected component

    Range("B1") = Now()

    Range("B2") = SelectedComponentParam_ConfigurationName

 

 

    'Grab values for LENGTH dimension using different methods

    DimensionName = "length@frame@SSM-Straight-Section NO DESIGN TABLE.SLDPRT"

    boolstatus = swReferenceModel.Extension.SelectByID2(DimensionName, "DIMENSION", 0, 0, 0, False, 0, Nothing, swSelectOptionDefault)

    Set swSelMgr = swReferenceModel.SelectionManager

    Set swDisplayDimension = swSelMgr.GetSelectedObject6(1, 0)

    Set swDimension = swDisplayDimension.GetDimension

   

    DimensionValue = 0

    Set swDisplayDimension = swReferenceModel.Parameter(DimensionName)

    DimensionValue1 = swReferenceModel.Parameter(DimensionName).Value

    DimensionValue2 = swDisplayDimension.GetValue2(SelectedComponentParam_ConfigurationName)

    DimensionValue3 = swDisplayDimension.GetSystemValue2(SelectedComponentParam_ConfigurationName)

    DimensionValue4 = swDisplayDimension.SystemValue

    DimensionValue5Array = swDimension.GetSystemValue3(1, vConfigNameArray)

    DimensionValue5 = DimensionValue5Array(0)

   

    Range("B7") = DimensionValue1

    Range("B8") = DimensionValue2

    Range("B9") = DimensionValue3

    Range("B10") = DimensionValue4

    Range("B11") = DimensionValue5

   

   

   

   

   

   

   

    'Grab values for CHAIN WIDTH dimension using different methods

    DimensionName = "chain-width@Sketch3@SSM-Straight-Section NO DESIGN TABLE.SLDPRT"

    boolstatus = swReferenceModel.Extension.SelectByID2(DimensionName, "DIMENSION", 0, 0, 0, False, 0, Nothing, swSelectOptionDefault)

    Set swSelMgr = swReferenceModel.SelectionManager

    Set swDisplayDimension = swSelMgr.GetSelectedObject6(1, 0)

    Set swDimension = swDisplayDimension.GetDimension

   

    DimensionValue = 0

    Set swDisplayDimension = swReferenceModel.Parameter(DimensionName)

    DimensionValue1 = swReferenceModel.Parameter(DimensionName).Value

    DimensionValue2 = swDisplayDimension.GetValue2(SelectedComponentParam_ConfigurationName)

    DimensionValue3 = swDisplayDimension.GetSystemValue2(SelectedComponentParam_ConfigurationName)

    DimensionValue4 = swDisplayDimension.SystemValue

    DimensionValue5Array = swDimension.GetSystemValue3(1, vConfigNameArray)

    DimensionValue5 = DimensionValue5Array(0)

   

    Range("C7") = DimensionValue1

    Range("C8") = DimensionValue2

    Range("C9") = DimensionValue3

    Range("C10") = DimensionValue4

    Range("C11") = DimensionValue5

   

End Sub

Outcomes