2 Replies Latest reply on Apr 7, 2017 8:38 PM by Ross John

    VB.NET: how to get selection from two selction controls?

    Ross John

      Hi everyone,

      I would like to ask how to get "values" from two selection controls in the UserPMPage. I have to say it is a very beginner's question.


      What I have been doing so far...

      1. I made two selection controls (selection1 and selection2) in the UserPMPage.vb file. The two selection controls were so defined, that only surfaces could be selected. It works fine till this step. Here are some of the codes....

      Dim selection1 As PropertyManagerPageSelectionbox

      Dim selection2 As PropertyManagerPageSelectionbox

      Dim selection1ID As Integer = 51

      Dim selection2ID As Integer = 52

      selection1 = group1.AddControl(selection1ID, controlType, "selection1", leftAlign, options, "Select cylinder A.") 

      selection2 = group1.AddControl(selection2ID, controlType, "selection2", leftAlign, options, "Select cylinder B.") 



      2. Then I wrote a Function in SwAddin.vb. The two selected surfaces are inputs the outputs are the diameters of the two surfaces. Here is the struction of the Function...

      Public Function GetDiameterOfSelectedSurfaces(ByVal selection1 As Object, ByVal selection2 As Object) As Double

              Dim DiameterA As Double

              Dim DiameterB As Double


              Dim swModel As ModelDoc2

              swModel = iSwApp.ActiveDoc


              Dim faces1 As IFace2 = DirectCast(selection1, IFace2)

              ' Do the job here to get DiameterA



              Dim faces2 As IFace2 = DirectCast(selection2, IFace2)

              ' Do the job here to get DiameterB



              Dim newRow(1) As String

              newRow = {DiameterA, DiameterB}

              Return newRow(1)

      End Function



      In the PMPHandler.vb file, I added the following codes...

         Public Function OnSubmitSelection(ByVal Id As Integer, ByVal selection1 As Object, ByVal selection2 As Object, ByVal SelType As Integer, ByRef ItemText As String) As Boolean Implements SolidWorks.Interop.swpublished.IPropertyManagerPage2Handler9.OnSubmitSelection

              axis = userAddin.GetDiameterOfSelectedSurfaces(selection1, selection2)

              Return True

          End Function



      My question: I am very sure I have been going to far on the wrong way. There are so many error messages from Visual Studio. Thus I would like to have your advices to give me a correct direction. What have I done wrong?


      As a beginner I could imagine to use the IDs of the two surfaces. But I dont know how.


      Thank you so much!

        • Re: VB.NET: how to get selection from two selction controls?
          Matthew Cempa


          The signature of OnSubmitSelection in SWX 2014 is

          Function OnSubmitSelection( _

             ByVal Id As System.Integer, _

             ByVal Selection As System.Object, _

             ByVal SelType As System.Integer, _

             ByRef ItemText As System.String _

          ) As System.Boolean


          You are passing in two selections (5 arguments instead of 4) so I can imagine that it doesn't like that unless it has changed in the version you are working but that is doubtful.  This method is used to validate selections as they are made.  It looks like you are trying to use this to do the work generating output.  I would suggest you use the OnClose method or add a button and use the onButtonPress method or if you want immediate feedback you could use the OnSelectionboxListChanged method which I do often to update feedback text boxes for the user.  Again if you use different marks for the selection boxes which is kind of a necessity other wise selections can appear in multiple boxes, review the code I submitted to you yesterday to pick each selected face.  I know my method looks a bit less concise it offers substantial robustness in handling situations like this.

            • Re: VB.NET: how to get selection from two selction controls?
              Ross John

              Thank you very much Matthew! You are right. In my codes the numbers of arguments are not same in two functions.


              As you adviced, I added a button control into my userPMPage. It seems now I can make multi selections. I read the code you give me yesterday again. I have to say, till yesterday I could not understand your purpose very exactly. But with this problem of mine, I think I can understand your coding idea. Thank you again!