Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

SolidWorks API Dimension Sensor Value 1

Status
Not open for further replies.

rob1231

Mechanical
Apr 20, 2020
4
Good Evening,

I'm trying to write a SW script in VBA to automatically output the length of a line (defined in a sketch).

To make things easier, I created a dimension sensor for the length of the line (since it is a complex shape).

I keep getting the error: "Object doesn't support this property or method" on the same line of the code even though I'm following the SW help very closely (I get the error when running the sample script too).

Here's the script (with the line giving the error highlighted):
Code:
Option Explicit
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swPart As SldWorks.PartDoc
    Dim swModel As SldWorks.ModelDoc2
    Dim swFeat As SldWorks.Feature
    Dim swSubFeat As SldWorks.Feature
    Dim swSensor As SldWorks.Sensor
    Dim swDimSensor As SldWorks.DimensionSensorData
    Dim sensorValue As Double
    Dim text As String

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swFeat = swModel.FirstFeature

    Do While Not swFeat Is Nothing
        If (swFeat.Name = "Sensors") Then
            Set swSubFeat = swFeat.GetFirstSubFeature
            Do While Not swSubFeat Is Nothing
                Set swSensor = swSubFeat.GetSpecificFeature2
                
                [highlight #FCE94F]swDimSensor = swSubFeat.GetSensorFeatureData[/highlight]
                sensorValue = swDimSensor.sensorValue

                text = (text & (swSensor.Name & ": " & sensorValue))
                Set swSubFeat = swSubFeat.GetNextSubFeature
            Loop
        End If
        Set swFeat = swFeat.GetNextFeature
    Loop
    MsgBox (text)
End Sub

Thanks in advance for your help!
 
Replies continue below

Recommended for you

Why not just add a driven dimension in the sketch and then get it (and its value) using ModelDoc2::parameter?

 
Dim swApp As SldWorks.SldWorks
Dim swDim As SldWorks.Dimension
Sub main()

Set swApp = Application.SldWorks
Set swDim = swApp.ActiveDoc.Parameter("SomeDrivenDimension@AComplexSketch")
MsgBox "Value is: " & swDim.GetUserValueIn(swApp.ActiveDoc)
End Sub

 
Wow! Thanks! Somewhere along the way I forgot about that possibility!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor