9 Replies Latest reply on Apr 1, 2017 7:42 PM by Nikola Malesevic

    Selecting a sketch segment after creating it

    Nikola Malesevic

      After creating a couple of sketch segments in a 2D sketch, I try to select them later on in order to make relations between them.

       

      I have tested both SketchLine and SketchSpline, but the selection is not working.

       

      Here is the minimum working example:

       

      // Create new or select an existing empty sketch.
      _document.InsertSketch2(true);
      // We are now in sketch edit mode.
      SketchSegment line1 = _document.SketchManager.CreateLine(0, 0, 0, 0, 0.1, 0);
      SketchSegment line2 = _document.SketchManager.CreateLine(0.1, 0, 0, 0.1, 0.1, 0);
      // Both lines created, line2 selected because it was just created.
      // Clear selection and select.
      _document.ClearSelection2(false);
      line1.Select2(false, 0);
      
      

       

      After executing this, line1 is not selected. If I omit clearing the selection, line2 stays selected, just as if I did not execute the last command.

       

      Perhaps I don't have understanding about how the selection works. Am I doing something wrong here?

       

      Using SolidWorks 2015 SP 5.0

       

      EDIT:

       

      The issue occurs only when doing an in-context editing of a part within an assembly, and it affects only sketch segments. Sketch points, for example, are not affected.

       

      Even more minimum working example:

       

      Preconditions:

      • Have an assembly with one part in in-context editing mode.
      • Have an empty sketch in edit mode.
      • _document object retrieved from ISldWorks::ActiveDoc().
      • _sketch object retrieved from IFeature::GetSpecificFeature2() for a feature representing the sketch in the feature tree.

       

      SketchSegment line = _document.SketchManager.CreateLine(0, 0, 0, 0, 0.1, 0);
      // The line is automatically selected after creation.
      line.DeSelect();
      // The line remains selected.
      
      
        • Re: Selecting a sketch segment after creating it
          Amen Allah Jlili

          First of all, why not reverse the lines' creation order? Your code will then be more concise. No need to clear the selection or select the first line. I do see another reason for this to work as you intended in more general case. I tried a similar code in VBA and it worked just fine. Perhaps this is a .NET protability issue? Try using ClearSelection instead of ClearSelection2, or Select4 instead of Select2.

          This is an interesting question, let me know if that works out for you.

          Best,
          Amen
          CADHero.com

            • Re: Selecting a sketch segment after creating it
              Nikola Malesevic

              Thank you for your answer, Amen.

               

              Reversing creation order would not help. This is only a minimum working example, and as you have mentioned, general case would not work. Imagine if I had five lines and had to make relations between them. After creating them, I would have to select the one that was created, say, three steps ago and the selection would not work.

               

              I think I managed to narrow down the issue.

               

              It appears only if I am doing in-context editing of the part within an assembly.

               

              If I manually create a sketch, enter edit mode and then execute the code above, it works. Both when working with an assembly and a part.

               

              However, when doing in-context editing and then doing the steps above, the line does not get selected.

               

              Appart from using standard ISldWorks::ActiveDoc() function to get IModelDoc2 in _document variable, I have also tried using IAssembly::GetEditTarget() and work with that object. In that case, lines do not get created at all.

               

              Thank you for testing VBA code. If it is not too much effort for you, could you try to recreate the issue by running your VBA version of the code while doing in-context editing of the part?