9 Replies Latest reply on Sep 25, 2017 10:56 AM by Jacob Corder

    Unable to use SelectByID2 in C#

    Oleg Oleg

      Hello, i'm using SW 2016 and VS 2015. So yesterday i wrote a piece of program that creates a 3d model in SW. It worked perfect yesterday. But today i receive error in VS:

       

      An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in SWMod.exe

      Additional information: Ошибка на сервере. (Исключение из HRESULT: 0x80010105 (RPC_E_SERVERFAULT))

       

      Error goes from line:

      boolstatus = swDoc.Extension.SelectByID2("", "FACE", -0.028779301900519944, 0.0099999999999909051, -0.0013146100868138169, false, 0, null, 0);

       

      It works when i put something in first argument of SelectByID2 function (SelectByID2("Front plane", "FACE"...).

      So as i thought from first argument of SelectByID2 function, program cant get an object to select, when i do macro, SW puts an empty string if object is not defined(object with name or orientation such as front plane).

       

      I tried to do the same in SW 2017, and it works fine, because SW 2017 uses SelectByRay method which arguments have coordinates only, but my 2016 SW does not have this method in library(sldworks;swcommands;swconst). And so i cant name object, because i need to select it first. How do i select object without error?

        • Re: Unable to use SelectByID2 in C#
          Amen Allah Jlili

          Are you sure swDoc is not null?

            • Re: Unable to use SelectByID2 in C#
              Oleg Oleg

              SWlab (Debugging) - Microsoft Visual Studio (Administrator).jpg

              I fixed some of lines with SelectByID by putting dimension in first argument. But some actions i cant perform by choosing dimensions, so problem is still actual.

              Status of swDoc in error moment.

              Russian text in field *Message* means Invaild index.

                • Re: Unable to use SelectByID2 in C#
                  Oleg Oleg

                  Program code:       

                   

                             ModelDoc2 swDoc = null;

                              bool boolstatus = false;

                              swDoc = ((ModelDoc2)(swApp.ActiveDoc));

                              boolstatus = swDoc.Extension.SelectByID2("Сверху", "PLANE", 0, 0, 0, false, 0, null, 0);

                              swDoc.SketchManager.InsertSketch(true);

                              swDoc.ClearSelection2(true);

                              SketchSegment skSegment = null;

                              skSegment = ((SketchSegment)(swDoc.SketchManager.CreateCircle(0, 0, 0, x1, 0, 0)));

                              swDoc.ShowNamedView2("*Триметрия", 8);

                              swDoc.ClearSelection2(true);

                              boolstatus = swDoc.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);

                              Feature myFeature = null;

                              myFeature = ((Feature)(swDoc.FeatureManager.FeatureExtrusion2(true, false, false, 0, 0, 0.035000000000000003, 0.01, false, false, false, false, 0.017453292519943334, 0.017453292519943334, false, false, false, false, true, true, true, 0, 0, false)));

                              swDoc.ISelectionManager.EnableContourSelection = false;

                              boolstatus = swDoc.Extension.SelectByID2("Сверху", "PLANE", 0, 0, 0, false, 0, null, 0);

                              swDoc.SketchManager.InsertSketch(true);

                              swDoc.ClearSelection2(true);

                              boolstatus = swDoc.Extension.SetUserPreferenceToggle(((int)(swUserPreferenceToggle_e.swSketchAddConstToRectEntity)), ((int)(swUserPreferenceOption_e.swDetailingNoOptionSpecified)), true);

                              boolstatus = swDoc.Extension.SetUserPreferenceToggle(((int)(swUserPreferenceToggle_e.swSketchAddConstLineDiagonalType)), ((int)(swUserPreferenceOption_e.swDetailingNoOptionSpecified)), true);

                              Array vSkLines = null;

                              vSkLines = ((Array)(swDoc.SketchManager.CreateCenterRectangle(0, 0, 0, -0.038, -0.016, 0)));

                              swDoc.ShowNamedView2("*Сверху", 5);

                              swDoc.ClearSelection2(true);

                              skSegment = ((SketchSegment)(swDoc.SketchManager.CreateLine(-0.038000, 0.016000, 0.000000, 0.000000, 0.021000, 0.000000)));

                              skSegment = ((SketchSegment)(swDoc.SketchManager.CreateLine(0.000000, 0.021000, 0.000000, 0.038000, 0.016000, 0.000000)));

                              skSegment = ((SketchSegment)(swDoc.SketchManager.CreateLine(-0.038000, -0.016000, 0.000000, 0.000000, -0.021000, 0.000000)));

                              skSegment = ((SketchSegment)(swDoc.SketchManager.CreateLine(0.000000, -0.021000, 0.000000, 0.038000, -0.016000, 0.000000)));

                              swDoc.ClearSelection2(true);

                              boolstatus = swDoc.Extension.SelectByID2("Line1", "SKETCHSEGMENT", -0.0065662815082737427, 0, -0.016, false, 2, null, 0);

                              boolstatus = swDoc.SketchManager.SketchTrim(4, 0, 0, 0);

                              boolstatus = swDoc.Extension.SelectByID2("Line3", "SKETCHSEGMENT", -0.0046846865546491534, 0, 0.016, false, 2, null, 0);

                              boolstatus = swDoc.SketchManager.SketchTrim(4, 0, 0, 0);

                              swDoc.ClearSelection2(true);

                              myFeature = ((Feature)(swDoc.FeatureManager.FeatureExtrusion2(true, false, false, 0, 0, 0.01, 0.035000000000000003, false, false, false, false, 0.017453292519943334, 0.017453292519943334, false, false, false, false, true, true, true, 0, 0, false)));

                              swDoc.ISelectionManager.EnableContourSelection = false;

                              boolstatus = swDoc.Extension.SelectByID2("Сверху", "PLANE", 0, 0, 0, false, 0, null, 0);

                              swDoc.SketchManager.InsertSketch(true);

                              swDoc.ClearSelection2(true);

                              boolstatus = swDoc.Extension.SetUserPreferenceToggle(((int)(swUserPreferenceToggle_e.swSketchAddConstToRectEntity)), ((int)(swUserPreferenceOption_e.swDetailingNoOptionSpecified)), true);

                              boolstatus = swDoc.Extension.SetUserPreferenceToggle(((int)(swUserPreferenceToggle_e.swSketchAddConstLineDiagonalType)), ((int)(swUserPreferenceOption_e.swDetailingNoOptionSpecified)), true);

                              vSkLines = ((Array)(swDoc.SketchManager.CreateCenterRectangle(0, 0, 0, -0.038, x4, 0)));

                              swDoc.ClearSelection2(true);

                              boolstatus = swDoc.Extension.SelectByID2("Line5", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);

                              boolstatus = swDoc.Extension.SelectByID2("Line6", "SKETCHSEGMENT", 0, 0, 0, true, 0, null, 0);

                              boolstatus = swDoc.Extension.SelectByID2("Point1", "SKETCHPOINT", 0, 0, 0, true, 0, null, 0);

                              boolstatus = swDoc.Extension.SelectByID2("Line2", "SKETCHSEGMENT", 0, 0, 0, true, 0, null, 0);

                              boolstatus = swDoc.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, true, 0, null, 0);

                              boolstatus = swDoc.Extension.SelectByID2("Line4", "SKETCHSEGMENT", 0, 0, 0, true, 0, null, 0);

                              boolstatus = swDoc.Extension.SelectByID2("Line3", "SKETCHSEGMENT", 0, 0, 0, true, 0, null, 0);

                              myFeature = ((Feature)(swDoc.FeatureManager.FeatureExtrusion2(true, false, false, 0, 0, 0.035000000000000000, 0.01, false, false, false, false, 0.017453292519943334, 0.017453292519943334, false, false, false, false, true, true, true, 0, 0, false)));

                              swDoc.ISelectionManager.EnableContourSelection = false;

                              swDoc.ViewZoomtofit2();

                              swDoc.ViewZoomtofit2();

                              swDoc.ViewZoomtofit2();

                              boolstatus = swDoc.Extension.SelectByID2("Спереди", "PLANE", 0, 0, 0, false, 0, null, 0);

                              swDoc.SketchManager.InsertSketch(true);

                              swDoc.ClearSelection2(true);

                              skSegment = null;

                              skSegment = ((SketchSegment)(swDoc.SketchManager.CreateLine(-0.038000, 0.010000, 0.000000, -0.020396, 0.035000, 0.000000)));

                              swDoc.ClearSelection2(true);

                              boolstatus = swDoc.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);

                              myFeature = ((Feature)(swDoc.FeatureManager.FeatureCut3(false, false, false, 1, 1, 0.025000000000000001, 0.025000000000000001, false, false, false, false, 0.017453292519943334, 0.017453292519943334, false, false, false, false, false, true, true, true, true, false, 0, 0, false)));

                              swDoc.ISelectionManager.EnableContourSelection = false;

                              boolstatus = swDoc.Extension.SelectByID2("Спереди", "PLANE", 0, 0, 0, false, 0, null, 0);

                              swDoc.SketchManager.InsertSketch(true);

                              swDoc.ClearSelection2(true);

                              skSegment = ((SketchSegment)(swDoc.SketchManager.CreateLine(0.038000, 0.010000, 0.000000, 0.020396, 0.035000, 0.000000)));

                              swDoc.ClearSelection2(true);

                              boolstatus = swDoc.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);

                              myFeature = ((Feature)(swDoc.FeatureManager.FeatureCut3(false, true, false, 1, 1, 0.025000000000000001, 0.025000000000000001, false, false, false, false, 0.017453292519943334, 0.017453292519943334, false, false, false, false, false, true, true, true, true, false, 0, 0, false)));

                              swDoc.ISelectionManager.EnableContourSelection = false;

                              swDoc.ShowNamedView2("*Сверху", 5);

                              boolstatus = swDoc.Extension.SelectByID2("", "FACE", -0.0046189003050217114, 0.035000000000025011, -0.0074257704903811896, false, 0, null, 0);

                              swDoc.SketchManager.InsertSketch(true);

                              swDoc.ClearSelection2(true);

                              skSegment = ((SketchSegment)(swDoc.SketchManager.CreateCircle(0, 0, 0, x2, 0, 0)));

                              swDoc.ClearSelection2(true);

                              boolstatus = swDoc.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);

                              myFeature = ((Feature)(swDoc.FeatureManager.FeatureCut3(true, false, false, 0, 0, 0.017999999999999999, 0.025000000000000001, false, false, false, false, 0.017453292519943334, 0.017453292519943334, false, false, false, false, false, true, true, true, true, false, 0, 0, false)));

                              swDoc.ISelectionManager.EnableContourSelection = false;

                              boolstatus = swDoc.Extension.SelectByID2("", "FACE", -0.0043346602862511519, 0.016999999999995907, -0.0034464102275931253, false, 0, null, 0);

                              swDoc.SketchManager.InsertSketch(true);

                              swDoc.ClearSelection2(true);

                              skSegment = ((SketchSegment)(swDoc.SketchManager.CreateCircle(0, 0, 0, x3, 0, 0)));

                              swDoc.ClearSelection2(true);

                              boolstatus = swDoc.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);

                              myFeature = ((Feature)(swDoc.FeatureManager.FeatureCut3(true, false, false, 0, 0, 0.017000000000000001, 0.017999999999999999, false, false, false, false, 0.017453292519943334, 0.017453292519943334, false, false, false, false, false, true, true, true, true, false, 0, 0, false)));

                              swDoc.ISelectionManager.EnableContourSelection = false;