9 Replies Latest reply on May 16, 2018 1:15 PM by Jacob Corder

    SelectByID2 without type and without location

    Ethan Kinney

      Is there a way to use SelectByID2 with ONLY the name of the feature / annotation?

       

      In the macro I'm writing I don't know if an annotation is a Dimension, GTOL, or Note. I also do not know where the annotation is. Usually i use this code:

       

      boolStatus = false
      If Not boolStatus Then boolStatus = swModel.Extension.SelectByID2(NewLink, "DIMENSION", 0, 0, 0, False, 0, Nothing, 0) : If boolStatus Then MyAnnotationType = "DIMENSION"
      If Not boolStatus Then boolStatus = swModel.Extension.SelectByID2(NewLink, "GTOL", 0, 0, 0, False, 0, Nothing, 0) : If boolStatus Then MyAnnotationType = "GTOL"
      If Not boolStatus Then boolStatus = swModel.Extension.SelectByID2(NewLink, "NOTE", 0, 0, 0, False, 0, Nothing, 0) : If boolStatus Then MyAnnotationType = "NOTE"
                                 
      
      

       

      However i would like to condense this down to a single "SelectByID2" line.

       

      Is there anyway to do this, or are you required to have either the type or the location of the annotation in order to select it? And yes i have tried it by passing an empty string to the type field.

       

      Thanks

        • Re: SelectByID2 without type and without location
          Josh Brady

          Let's back up a line... Why are you using SelectByID?  It's usually my very last resort as a method of selecting something. 

            • Re: SelectByID2 without type and without location
              Ethan Kinney

              Ok, so....

               

              In our home-grown automation software we generate our own inspection reports from the drawings. When we setup the templates for generating the inspection reports, we add diamond notes next to the dimensions that we are interested in capturing. When creating these notes I have a little macro that gets the id of the annotation to attach it to and it places that annotation in the "tag" property of that note.

               

              Later when the other macro runs, it gets all of the inspection notes and process each one. In order to get the current dimension information, or notes, or gtol info, it has to select the appropriate annotation that it is "linked" to. That's why it uses the SelectByID2 method to do so.

               

              I'm curious what you mean by using it as a very last resort of selecting something. What methods do you use?

              • Re: SelectByID2 without type and without location
                Amen Allah Jlili

                SelectByID2 has never failed me. Have you had any problems using?

                  • Re: SelectByID2 without type and without location
                    Josh Brady

                    I didn't mean that it doesn't work (although in this particular case it seems that it doesn't...), I just mean that in my experience getting the info to put into SelectByID is usually more difficult than selecting by some other method.  The fact that the macro recorder always records SelectByID seems to point folks that direction too often.  I've seen some people post macros where they already have a pointer to a face or something, then they go through gyrations to get the data to pass to SelectByID.

                    • Re: SelectByID2 without type and without location
                      Jacob Corder

                      i hate selectbyid2.  i use my own selector.

                       

                      and call SelectObjects(modDoc,SomeRandomCrapToSelectOrMultipleRandomThings)

                       

                      ''' <summary>
                          ''' Selects objects in the Model Document specified, objects can be a single item or an array of items
                          ''' </summary>
                          ''' <param name="ModDoc">model document to select in</param>
                          ''' <param name="Objects">Single selectable object or an array of selectable objects</param>
                          ''' <param name="ClearSelectionFirst">Clear existing selections first or not</param>
                          ''' <returns>Count of newly selected objects</returns>
                          Function SelectObjects(ByVal ModDoc As ModelDoc2, ByVal Objects As Object, ByVal ClearSelectionFirst As Boolean) As Integer
                              Dim SelCt As Integer = 0
                              If IsNothing(ModDoc) = False Then
                                  If ClearSelectionFirst Then
                                      ModDoc.ClearSelection2(True)
                                  End If
                                  Try
                                      Dim DWrapper() As DispatchWrapper = GetDispatchWrapperArray(Objects)
                                      If IsNothing(DWrapper) = False Then
                                          Dim SelMgr As SelectionMgr = ModDoc.SelectionManager
                                          Dim SelData As SelectData = SelMgr.CreateSelectData
                                          SelCt = SelMgr.AddSelectionListObjects(DWrapper, SelData)
                                          SelMgr = Nothing
                                          SelData = Nothing
                                      End If
                                  Catch ex As Exception
                      
                      
                                  End Try
                              End If
                              Return SelCt
                          End Function
                      

                       

                       ''' <summary>
                          ''' Length of an array if it is an array,else -1 for non array
                          ''' </summary>
                          ''' <param name="ArrayObject"></param>
                          ''' <returns>array length or -1 if not an array</returns>
                          Public Shared Function ArrayLength(ByVal ArrayObject As Object) As Integer
                              Dim RetINt As Integer = -1
                              If IsObjectAnArray(ArrayObject) Then
                                  Try
                                      RetINt = CType(ArrayObject, Array).Length
                                  Catch ex As Exception
                                      RetINt = -1
                                  End Try
                              End If
                              Return RetINt
                          End Function
                      
                        ''' <summary>
                          ''' Determines if the object is actually an array or a single object
                          ''' </summary>
                          ''' <param name="Obj">single item or array</param>
                          ''' <returns>True if is an array, False if not</returns>
                          Public Shared Function IsObjectAnArray(ByVal Obj As Object) As Boolean
                              If Obj Is Nothing Then Return False
                              If IsArray(Obj) Then
                                  Return True
                              ElseIf TypeOf Obj Is Object() Then
                                  Return True
                              End If
                          End Function
                      
                       ''' <summary>
                          ''' Returns a dispatch Wrapper array for selection or whatever else you might need it for
                          ''' </summary>
                          ''' <param name="Objects">single solidwork selectable entity, or an array of selectable entities</param>
                          ''' <returns>DispatchWrapper as an array</returns>
                          Public Function GetDispatchWrapperArray(ByVal Objects As Object) As DispatchWrapper()
                              GetDispatchWrapperArray = Nothing
                              Dim IntCt As Integer = -1
                              If IsNothing(Objects) = False Then
                                  IntCt = ArrayLength(Objects)
                                  If IntCt > 0 Then
                                      If IntCt > -1 Then
                                          Dim ActCt As Integer = 0
                                          If Objects.GetType.IsGenericType Then
                                              For i = 0 To Objects.count - 1
                                                  If Not Objects.item(i) Is Nothing Then
                                                      ActCt += 1
                                                  End If
                                              Next
                                          Else
                                              For i = 0 To UBound(Objects)
                                                  If IsNothing(Objects(i)) = False Then
                                                      ActCt += 1
                                                  End If
                                              Next
                                          End If
                      
                      
                                          If ActCt > 0 Then
                                              Dim DWrapper(ActCt - 1) As DispatchWrapper
                                              For i = 0 To UBound(Objects)
                                                  For p = 0 To UBound(DWrapper)
                                                      If IsNothing(DWrapper(p)) Then
                                                          DWrapper(p) = New DispatchWrapper(Objects(i))
                                                          Exit For
                                                      ElseIf p = UBound(DWrapper) Then
                                                          ReDim Preserve DWrapper(UBound(DWrapper) + 1)
                                                          DWrapper(UBound(DWrapper)) = New DispatchWrapper(Objects(i))
                                                          Exit For
                                                      End If
                                                  Next
                                              Next
                                              For p = 0 To UBound(DWrapper)
                                                  If IsNothing(DWrapper(p)) Then
                                                      If p = 0 Then
                                                          DWrapper = Nothing
                                                          Return Nothing
                                                      End If
                                                      ReDim Preserve DWrapper(p - 1)
                                                      Exit For
                                                  End If
                                              Next
                                              Return DWrapper
                                          End If
                                      End If
                                  Else
                                      Try
                                          If TypeOf Objects Is Face2 Then
                                              Dim dwrapper(0) As DispatchWrapper
                                              dwrapper(0) = New DispatchWrapper(Objects)
                                              Return dwrapper
                                          ElseIf TypeOf Objects Is Body2 Then
                                              Dim dwrapper(0) As DispatchWrapper
                                              dwrapper(0) = New DispatchWrapper(Objects)
                                              Return dwrapper
                                          ElseIf TypeOf Objects Is Feature Then
                                              Dim dwrapper(0) As DispatchWrapper
                                              dwrapper(0) = New DispatchWrapper(Objects)
                                              Return dwrapper
                                          ElseIf TypeOf Objects Is Edge Then
                                              Dim dwrapper(0) As DispatchWrapper
                                              dwrapper(0) = New DispatchWrapper(Objects)
                                              Return dwrapper
                                          ElseIf TypeOf Objects Is SilhouetteEdge Then
                                              Dim dwrapper(0) As DispatchWrapper
                                              dwrapper(0) = New DispatchWrapper(Objects)
                                              Return dwrapper
                                          ElseIf TypeOf Objects Is View Then
                                              Dim dwrapper(0) As DispatchWrapper
                                              dwrapper(0) = New DispatchWrapper(Objects)
                                              Return dwrapper
                                          Else
                                              Try
                                                  Dim Dwrapper(0) As DispatchWrapper
                                                  Dwrapper(0) = New DispatchWrapper(Objects)
                                                  Return Dwrapper
                                              Catch ICE As InvalidCastException
                                              Catch ex As Exception
                      
                      
                                              End Try
                      
                      
                                          End If
                      
                      
                                      Catch ICE As InvalidCastException
                                          Dim dwrapper(0) As DispatchWrapper
                                          dwrapper(0) = New DispatchWrapper(Objects)
                                          Return dwrapper
                                      Catch ex As Exception
                      
                      
                                      End Try
                      
                      
                                  End If
                              Else
                                  Return Nothing
                              End If
                          End Function