AnsweredAssumed Answered

Type Mismatch Exception when calling AccessSelections method

Question asked by Jeremy Mitchell on Nov 13, 2018
Latest reply on Nov 19, 2018 by Artem Taturevych

Hi,

I am working through Mike Spens' book "Automating Solidworks 2017 Using Macros" and one of  the examples is consistently crashing out with a type mismatch, and I don't understand why.  The complete code is at the bottom.

 

Where it is crashing is on this line:

        'rollback to edit the feature
        retval = featureDef.AccessSelections(part, Nothing)

 

featureDef is defined as type Object.  If I change it to type ExtrudeFeatureData2, the macro works as expected.

The earlier line that calls getdraftwhileextruding does not cause any problems when featureDef is defined as either type, so as near as I can tell the featureDef Object is being correctly populated by GetDefinition.

 

I would like to know:

What am I doing wrong?  (I'm not a programmer, and trying to learn VB.NET at the same time as the Solidworks API, so I'm sure it's something silly that I've missed.)

How can I fix it?

 

I should also note that I'm using 2018 with VSTA 3.0 enabled, although the same problem happens if I disable VSTA 3.0.

 

Thanks in advance,

Jeremy

 

 

Complete macro:

Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System.Runtime.InteropServices
Imports System


Partial Class SolidWorksMacro
    Public Sub main()
        Dim part As ModelDoc2
        Dim MyFeature As Feature
        Dim featureDef As Object
        Dim retval As Object
        Dim message As String

        part = swApp.ActiveDoc

        MyFeature = part.FeatureByName("Cut-Extrude1")
        featureDef = MyFeature.GetDefinition

        'get some settings from the feature
        If featureDef.getdraftwhileextruding(True) = False Then
            message = "The selected feature has no draft" & vbCr
        End If

        Select Case featureDef.getendcondition(True)
            Case swEndConditions_e.swEndCondBlind
                message = message & "Blind"
            Case swEndConditions_e.swEndCondThroughAll
                message = message & "Through all"
            Case swEndConditions_e.swEndCondThroughAllBoth
                message = message & "Through All Both"
            Case swEndConditions_e.swEndCondUpToSurface
                message = message & "Up to surface"
            Case swEndConditions_e.swEndCondMidPlane
                message = message & "Midplane"
            Case swEndConditions_e.swEndCondOffsetFromSurface
                message = message & "Offset from surface"
            Case swEndConditions_e.swEndCondThroughNext
                message = message & "Up to next"
            Case swEndConditions_e.swEndCondUpToBody
                message = message & "Up to body"
        End Select

        MsgBox(message & " end condition. ", MsgBoxStyle.Information)

        'rollback to edit the feature
        retval = featureDef.AccessSelections(part, Nothing)

        'modify some feature value
        featureDef.setendcondition(True, swEndConditions_e.swEndCondThroughAll)
        featureDef.setdraftwhileextruding(True, True)
        featureDef.setdraftangle(True, 2 * Math.PI / 180)

        'complete the edit operation
        retval = MyFeature.ModifyDefinition(featureDef, part, Nothing)

        'in case the modifications failed
        If retval = False Then
            featureDef.releaseselectionaccess()
        End If

    End Sub
    ''' <summary>
    ''' The SldWorks swApp variable is pre-assigned for you.
    ''' </summary>
    Public swApp As SldWorks
End Class

Outcomes