12 Replies Latest reply on Sep 13, 2018 8:52 AM by Shubham Misal

    can anybody convert this to c#

    Shubham Misal

      Option Explicit

       

      Dim swApp As SldWorks.SldWorks

      Dim swModel As ModelDoc2

      Dim sketch As sketch

      Dim feature As feature

       

      Sub main()

       

      Set swApp = Application.SldWorks

       

      Set swModel = swApp.ActiveDoc

      swModel.SketchManager.InsertSketch True

      Set sketch = swModel.SketchManager.ActiveSketch

       

      Set feature = sketch

      MsgBox feature.Name

       

      End Sub

       

       

       

       

       

      ' i want to get the name of active sketch/ feature;

        • Re: can anybody convert this to c#
          Alex Burnett

          I don't know what program you're using to write this but the syntax would look similar to this:

           

          Disclaimer: I haven't compiled this and there may be some errors.

          SldWorks.SldWorks swApp;
          ModelDoc2 swModel;
          Sketch sketch;
          Feature feature;
          
          static void Main(string[] args) // This maydiffer depending on what program you're using to code this.
          {
               swApp = new SldWorks(); // This will definitely differ depending on what you want to do.
          
               swModel = swApp.ActiveDoc;
               swModel.SketchManager.InsertSketch(true);
               sketch = swModel.SketchManager.ActiveSketch;
               
               feature = (Feature)sketch;
               MessageBox.Show(feature.name, "Title");
          }
          
            • Re: can anybody convert this to c#
              Shubham Misal

              actually when i run the program for the first time it runs perfectly, but for second time it throws an exception.

              for first time it creates sketch1 and the second time it creates sketch2. to run the program for second time  i change the dimension from "D1@Sketch1" to "D1@Sketch2".

               

              // here is the code

               

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

                          bool boolstatus = swDoc.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, false, 0, null, 0);

                          swDoc.ClearSelection2(true);

                          SketchSegment skSegment = ((SketchSegment)(swDoc.SketchManager.CreateCircle(0, 0, 0, 0.058997, -0.000425, 0)));

                          swDoc.ClearSelection2(true);

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

                          DisplayDimension myDisplayDim = ((DisplayDimension)(swDoc.AddDimension2(0.08480805731973709, 0.0015600144323028586,                                    0)));

                          swDoc.ClearSelection2(true);

                          boolstatus = swDoc.Extension.SelectByID2("D1@Sketch2@Part3.SLDPRT", "DIMENSION", 0.08480805731973709,                                    0.0015600144323028586, 0, false, 0, null, 0);

                          Dimension myDimension = ((Dimension)(swDoc.Parameter("D1@Sketch1")));

                          myDimension.SystemValue = 0.12;

                          swDoc.ClearSelection2(true);

                          swDoc.SketchManager.InsertSketch(true);

               

              can you help me in this....?

                • Re: can anybody convert this to c#
                  Alex Burnett

                  Shubham Misal wrote:

                   

                  actually when i run the program for the first time it runs perfectly, but for second time it throws an exception.

                  for first time it creates sketch1 and the second time it creates sketch2. to run the program for second time i change the dimension from "D1@Sketch1" to "D1@Sketch2".

                   

                  // here is the code

                   

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

                  bool boolstatus = swDoc.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, false, 0, null, 0);

                  swDoc.ClearSelection2(true);

                  SketchSegment skSegment = ((SketchSegment)(swDoc.SketchManager.CreateCircle(0, 0, 0, 0.058997, -0.000425, 0)));

                  swDoc.ClearSelection2(true);

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

                  DisplayDimension myDisplayDim = ((DisplayDimension)(swDoc.AddDimension2(0.08480805731973709, 0.0015600144323028586, 0)));

                  swDoc.ClearSelection2(true);

                  boolstatus = swDoc.Extension.SelectByID2("D1@Sketch2@Part3.SLDPRT", "DIMENSION", 0.08480805731973709, 0.0015600144323028586, 0, false, 0, null, 0);

                  Dimension myDimension = ((Dimension)(swDoc.Parameter("D1@Sketch1")));

                  Dimension myDimension = (Dimension)myDisplayDim.GetDimension();

                  myDimension.SystemValue = 0.12;

                  swDoc.ClearSelection2(true);

                  swDoc.SketchManager.InsertSketch(true);

                   

                  can you help me in this....?

                  If you're using SelectByID2, then there is almost always another way to programatically select the item without having to know its name, coordinates, etc.

                   

                  I've updated the code you posted to use the same practice as I have in one of my programs. I've included a snip of code below.

                  // Draw Stock Size Circle
                  swSketchSeg = swSketchMan.CreateCircle(swSketchPt.X, swSketchPt.Y, swSketchPt.Z, swSketchArc.GetRadius(), swSketchArc.GetRadius(), 0); // Create second circle to represent the actual stock size
                  swSketchSeg.Select4(false, null); // Select this circle
                                  
                  swDispDimActualDiam = (DisplayDimension)swModel.AddDimension2(0, MaxLinearDistance, 0); // Add a driving dimension to this circle
                  swDimActualDiam = (Dimension)swDispDimActualDiam.GetDimension(); // Get dimension variable of this display dimension
                  swDimActualDiam.SystemValue = 2 * MaxLinearDistance; // Set this value to the min diameter
                  
                    • Re: can anybody convert this to c#
                      Shubham Misal

                      thank you very much..

                      • Re: can anybody convert this to c#
                        Shubham Misal

                        can i use the same for rectangle. for an array which selection method is convenient..?

                        u told select4 method for circle, i used the same for rectangle.

                         

                        Array rectangle = swDoc.SketchManager.CreateCornerRectangle(0, 0, 0, 5, 5, 0);

                        rectangle[0].select4(false,null);   // this is for base

                        DisplayDimension myDisplayDim = (DisplayDimension) swDoc.AddDimension2(0, 0, 0);   // AddDimension2 adds location of the dimension only.

                        Dimension myDimension = (Dimension) myDisplayDim.GetDimension();

                        myDimension.systemValue=0.2;

                         

                        rectangle[1].select4(false,null);   // this is for hight

                        DisplayDimension myDisplayDim = (DisplayDimension) swDoc.AddDimension2(0, 0, 0);

                        Dimension myDimension = (Dimension) myDisplayDim.GetDimension();

                        myDimension.systemValue=0.4;

                        • Re: can anybody convert this to c#
                          Shubham Misal

                           

                          swDoc= (ModelDoc2)swApp.ActiveDoc;

                          SelMgr = (SelectionMgr)swDoc.SelectionManager;

                          SelData = (SelectData)SelMgr.CreateSelectData();

                          Array Rect= swDoc.SketchManager.createCornerRectangle(0,0,0,5,5,0);

                          boolstatus = Part.Extension.SelectByID2("Line3@Sketch2", "EXTSKETCHSEGMENT", -0.01022262320328, 0.01646364019604, 0, false, 0, null, 0);

                          swApp.RunCommand ((int)swCommands_e.swCommands_Select_Chain, "");

                           

                           

                           

                          instead of sketch2 can i write the following code:

                          boolstatus = Part.Extension.SelectByID2("Line3@Rect", "EXTSKETCHSEGMENT", -0.01022262320328, 0.01646364019604, 0, false, 0, null, 0);

                    • Re: can anybody convert this to c#
                      Christian Chu

                      SharpDevelop is a good tool for this task