4 Replies Latest reply on Nov 19, 2018 3:58 AM by Artem Taturevych

    Type Mismatch Exception when calling AccessSelections method

    Jeremy Mitchell

      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
      
        • Re: Type Mismatch Exception when calling AccessSelections method
          Simon Turner

          You're running into a late binding issue:

          https://support.microsoft.com/el-gr/help/814912/you-receive-a-system-runtime-interopservices-comexception-error-messag

          The simplest solution is one that you have already mentioned: "If I change it to type ExtrudeFeatureData2, the macro works as expected."

            • Re: Type Mismatch Exception when calling AccessSelections method
              Jeremy Mitchell

              I agree that in this simple case, declaring the type as ExtrudeFeatureData2 would be the way to go.

               

              I'm trying to understand in more detail what is going wrong because the earlier if and case statements both call ExtrudeFeatureData2 methods (GetDraftWhileExtruding and GetEndCondition) evaluate without a problem, so featureDef apparently contains the correct data object and it is accessible by other methods, so why not AccessSelections?  I thought that perhaps it was because AccessSelections is available in several data types, but so is GetEndCondition.

               

              Also, how would one address this with a more complicated situation where the feature type isn't known at the outset?  It seems to me that declaring the variable as an Object and then dropping the in data for whichever object is being modified would be the appropriate solution.

                • Re: Type Mismatch Exception when calling AccessSelections method
                  Gertjan Van Dijk

                  Declaring variables as "object" is a tricky situation, although not always avoidable.

                   

                  It is always best to explicitly convert the "object" into the exact type you want to use, to prevent both VB and solidworks from misinterpreting what you are trying to do (for example: getting the vertex on a drawing, if you write a method to do that, working on "object", you could mean either the drawingVertex, or the vertex in the 3D model).

                   

                  If you are in a situation where you are not sure which type you are going to be dealing with; write a branching method, Object::GetType, followed by a switch block based on the type returned; This block should then call one of a selection of potential subs tailored to the type of feature you have.

                   

                  Situations like this is why i prefer to write in C#: i am forced to think about my typing, it is not allowed to lazily call "Object::GetEndCondition" for example, it has to be the correct type.

                  • Re: Type Mismatch Exception when calling AccessSelections method
                    Artem Taturevych

                    There are multiple interfaces which might correspond to the object. When you are using late binding environment will try to find the available interface and it might just find unexpected one as I believe happened in your case. I would recommend to use strong types. To make VB.NET strong typed it is required to set the Option Strict On option so all of the late binding will be picked up during the compilation time.