5 Replies Latest reply on Apr 25, 2018 7:41 PM by John Alexander

    Robust Selections

    John Alexander

      I try to avoid using selection-dependent functions in my macro workflow - it isn't always possible.

       

      Sometimes I hit a dead end and the only feasible method of progressing is to use SelectbyID2 on some coordinates.

      2012 SOLIDWORKS API Help - SelectByID2 Method (IModelDocExtension)

       

      One problem I have is that the viewport can impact the behaviour of the selection event.

       

      Figure 1 - macro collects data regarding hole instances on a part in a drawing.

       

      Figure 2 - same macro fails to relate dimensions to hole instances because the view is zoomed out.

       

      This is a pretty extreme example, but I don't like having uncaptured/irrelevant UI state determining how my macro performs.

       

      Does anyone have any suggestions for making these selection-dependent functions more robust? They seem to be ubiquitous in this API so it would be nice to develop a consistent method. Is it common practice to zoom the camera port to the selection point before calling SelectbyID? Is there something like AddToDB but for selection points?

        • Re: Robust Selections
          Josh Brady

          Usually there are other ways of selecting things.  Most objects that need selecting (IEntity, IFace, etc.) have a .Select function.  I am sure I can count on one hand the number of times I've used SelectByID with coords.

          • Re: Robust Selections
            Michael Spens

            Hi John,

            To Josh's point, could you share which selection is forcing you into SelectByID and maybe we can offer an alternative selection technique.  For example, you may need to iterate through the edges in the view or the annotations and find all center marks.

             

            Mike

              • Re: Robust Selections
                Josh Brady

                Funny you should mention center marks, Mike... Please see

                 

                Get Centermark from DisplayDimension

                 

                I believe that may be one source of the wonky selection methods...

                • Re: Robust Selections
                  John Alexander

                  Josh is correct, the post he linked to describes my implementation. In it, I'm trying to get references to CenterMarks from DisplayDimensions that are attached to them.

                    • Re: Robust Selections
                      Michael Spens

                      OK, that makes sense after reading the other post.  You're correct John, the external sketch segment returned by GetAttachedEntities3 is one of the sketch segments of the centermark annotation's DisplayData.  Unfortunately, you can't navigate from the sketch segment to the owning annotation directly.  Your SelectByID2 method is simplest, but not robust if you have many centermarks on a zoomed out sheet - as you've seen.

                       

                      As an alternative, after getting the sketch segment connected to the dimension, you could traverse all centermarks in the view (traverse all annotations, filtering only centermarks).  From each ICenterMark interface, get its IDisplayData.  That contains all of the lines in the annotation.  Loop through the lines, comparing the start and end x,y,z locations to the sketch segment found originally.  When you get a match, you have the desired centermark.  I haven't tested this completely, but it should work as long as the centermark doesn't have a different transform than the view.

                       

                      Not a simple process, but more robust than SelectByID2.

                       

                      Mike