C# SolidWorks Secondary Development API --- Selection of Sketch Contour Ring

This article discusses how to operate when there are multiple closed-loop contours in the Solidworks sketch.
Let's introduce the simplest stretch:
By default:

  • When there is only one closed-loop contour or multiple disjoint closed-loop contours, Solidworks will automatically select all the contours for stretching by default.
    As shown below:Insert picture description here

  • When multiple contours intersect, Solidworks will enter the contour selection state after clicking the stretch command, prompting you to select the contour you need.
    As shown below:
    Insert picture description here

Let us first consider a simple situation, that is, how to select all the contours and let the program perform extruding features.
The current part sketch is as follows:
Insert picture description here
The following code simply traverses the contours, selects all contours, and makes an extruded feature.

 		/// <summary>
        /// 遍历 草图中的闭环轮廓
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butGetSketchContour_Click(object sender, EventArgs e)
        {
    
    
            SldWorks swApp = PStandAlone.GetSolidWorks();

            ModelDoc2 swModel = (ModelDoc2)swApp.ActiveDoc;

            SelectionMgr swSelMgr = (SelectionMgr)swModel.SelectionManager;

            //选择草图
            swModel.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, false, 4, null, 0);

            //把选择转换为特征
            var swFeat = (Feature)swSelMgr.GetSelectedObject6(1, -1);

            swModel.ClearSelection();

            //把特征转换为草图
            var swSketch = (Sketch)swFeat.GetSpecificFeature2();

            //获取轮廓数量
            var sketchContoursCount = swSketch.GetSketchContourCount();
            var sketchContours = (object[])swSketch.GetSketchContours();

            //选择所有轮廓
            for (int i = 0; i < sketchContoursCount; i++)
            {
    
    
                var skContous = (SketchContour)sketchContours[i];

                skContous.Select(true, 0);

            }


            var swFeatureManager = (FeatureManager)swModel.FeatureManager;
            //做一个简单的拉伸
            var swFeature = (Feature)swFeatureManager.FeatureExtrusion2(true, false, false, 0, 0, 0.01, 0.01, false, false, false,
            false, 0, 0, false, false, false, false, true, true, true, 0, 0, false);



        }

So the question is, if I only want to select a certain contour, how should I play it?

  1. Two methods, traverse once according to the above scheme. Then use skContous.GetEdges to find the corresponding edge, or other information to determine which contour is desired.
  2. If you know the coordinates of an internal point, you can also select it directly. For details, please refer to the example in the api.
    Enable Contour Selection Example (C#)

The key points are here:
Insert picture description here

The source code has been uploaded:

Insert picture description here
https://gitee.com/painezeng/CSharpAndSolidWorks
or
https://github.com/painezeng/CSharpAndSolidWorks

Guess you like

Origin blog.csdn.net/zengqh0314/article/details/109071850