2 Replies Latest reply on Aug 15, 2018 9:25 AM by Christopher Elyassi

    extrude sketch by region/contour

    Christopher Elyassi

      Hey all,

       

      I have a sketch with multiple contours and I am trying to write some code to extrude a specific contour of that sketch.

      Idea is that the method takes in the sketch name and the coordinates a point in the desired region, but it is not working. The "test" boolean in the snippet below always returns false:

       

      public static void ExtrudeSketchWithContour(string SketchName, double value, double X, double Y, double Z)
      {
          ModelDoc2 swModel = (ModelDoc2)swApp.ActiveDoc;
          SelectionMgr swSelectionMgr = swModel.SelectionManager;
      
      
          swModel.Extension.SelectByID2(SketchName, "SKETCH", 0, 0, 0, false, 0, null, 0);
      
      
          swSelectionMgr.EnableContourSelection = true;
      
      
          bool test = swModel.Extension.SelectByID2(SketchName, "SKETCHREGION", X, Y, Z, true, 0, null, 0);
      
      
          swModel.FeatureManager.FeatureExtrusion3
              (true, false, true, 6, 6, value, 0, false, false, false, false, 0, 0, false, false, false, false, false, true, true, 0, 0, false);
      
      
          swSelectionMgr.EnableContourSelection = false;
      
      
          swModel.ClearSelection2(true);
      }
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      

       

      Does anyone know what I am doing wrong?

      Also, what is the difference between a sketch contour and a sketch region?

       

      Thanks in advance!

      Chris

        • Re: extrude sketch by region/contour
          Christopher Elyassi

          Ok, I understand this a lot better now. Contour is the outline and region is the area inside (duh..) so I am actually trying to extrude a region.

          Also the coordinates given seem to depend on the current view, so I also have to change the shown view for it to work.

          I updated my code and despite being a bit hacky it now works quite well:

           

          public static void ExtrudeSketch_Region(string SketchName, double value, double X, double Y, double Z)
          {
              ModelDoc2 swModel = (ModelDoc2)swApp.ActiveDoc;
              SelectionMgr swSelectionMgr = swModel.SelectionManager;
          
          
              if (X == 0) { swModel.ShowNamedView2("*Right", -1); } else
              if (Y == 0) { swModel.ShowNamedView2("*Top", -1); } else
              if (Z == 0) { swModel.ShowNamedView2("*Front", -1); }
          
          
              swSelectionMgr.EnableContourSelection = true;
              swModel.Extension.SelectByID2(SketchName, "SKETCHREGION", X, Y, Z, true, 4, null, 0);
              swModel.FeatureManager.FeatureExtrusion3
                  (true, false, true, 6, 6, value / 1000, 0, false, false, false, false, 0, 0, false, false, false, false, false, true, true, 0, 0, false);
              swSelectionMgr.EnableContourSelection = false;
          
          
              swModel.ClearSelection2(true);
          }
          
          
          
          
          
          
          
          
          
          
          

           

          Issue now is that there are already sketch points in the only (X, Y, Z) coordinates that I know are in the region I need and when running the code the selection fails because it selects the sketch point instead of the region. To be sure I'm selecting the region instead of a point I need to offset the coordinates by at least 1.5mm (any less and it will select the point). But this is too hacky even for me because it runs the risk of putting the selection outside of the region.

           

          Any way to have the SelectByID2 method ignore sketch points and just select the region behind?

          • Re: extrude sketch by region/contour
            Christopher Elyassi

            So I realized that the regions I need extruded are always the same when indexed in swSketch.GetSketchRegions();

            I've rewritten the method to take an index as an argument instead of a location and now it works much better.

            Posting code snippet in case this can help someone else some day:

             

            public static void ExtrudeSketch_Region(string SketchName, double value, int region)
            {
                ModelDoc2 swModel = (ModelDoc2)swApp.ActiveDoc;
                SelectionMgr swSelectionMgr = swModel.SelectionManager;
                Feature swFeature; Sketch swSketch; SketchRegion swRegion;
            
            
                swModel.Extension.SelectByID2(SketchName, "SKETCH", 0, 0, 0, false, 0, null, 0);
                swFeature = swSelectionMgr.GetSelectedObject6(1, -1);
            
            
                swSketch = (Sketch)swFeature.GetSpecificFeature2();
                object[] swSketchRegions = swSketch.GetSketchRegions();
            
            
                swRegion = (SketchRegion)swSketchRegions[region];
                swRegion.Select2(false, default(SelectData));
            
            
                swModel.FeatureManager.FeatureExtrusion3
                    (true, false, true, 6, 6, value / 1000, 0, false, false, false, false, 0, 0, false, false, false, false, false, true, true, 0, 0, false);
            
            
            
            
                swModel.ClearSelection2(true);
            }