C# SolidWorks secondary development API---traverse all editable dimensions of parts

Recently, a college student asked a question about how to get all editable size information in a part. Including all features and sketch dimensions.
The previous blog only wrote about how to traverse the features and the dimensions in the drawings.
Through checking the api, it is found that this is the same as the size marked in the drawing. Use it directly:
first look at the result:
parts:
Insert picture description here

Insert picture description here

        /// <summary>
        /// 遍历特征
        /// </summary>
        /// <param name="thisFeat"></param>
        /// <param name="isTopLevel"></param>
        public static void TraverseFeatures(Feature thisFeat, bool isTopLevel, bool isShowDimension = false)
        {
    
    
            Feature curFeat = default(Feature);
            curFeat = thisFeat;

            while ((curFeat != null))
            {
    
    
                //输出特征名称
                Debug.Print(curFeat.Name);
                if (isShowDimension == true) ShowDimensionForFeature(curFeat);

                Feature subfeat = default(Feature);
                subfeat = (Feature)curFeat.GetFirstSubFeature();

                while ((subfeat != null))
                {
    
    
                    //if (isShowDimension == true) ShowDimensionForFeature(subfeat);
                    TraverseFeatures(subfeat, false);
                    Feature nextSubFeat = default(Feature);
                    nextSubFeat = (Feature)subfeat.GetNextSubFeature();
                    subfeat = nextSubFeat;
                    nextSubFeat = null;
                }

                subfeat = null;

                Feature nextFeat = default(Feature);

                if (isTopLevel)
                {
    
    
                    nextFeat = (Feature)curFeat.GetNextFeature();
                }
                else
                {
    
    
                    nextFeat = null;
                }

                curFeat = nextFeat;
                nextFeat = null;
            }
        }

        /// <summary>
        /// 遍历零件中的所有特征
        /// </summary>
        /// <param name="feature"></param>
        public static void ShowDimensionForFeature(Feature feature)
        {
    
    
            var thisDisplayDim = (DisplayDimension)feature.GetFirstDisplayDimension();

            while (thisDisplayDim != null)
            {
    
    
                var dimen = (Dimension)thisDisplayDim.GetDimension();

                Debug.Print($"---特征 {feature.Name} 尺寸-->" + dimen.GetNameForSelection() + "-->" + dimen.Value);

                thisDisplayDim = (DisplayDimension)feature.GetNextDisplayDimension(thisDisplayDim);
            }
        }

The code has been uploaded, please pick it up.
Insert picture description here

Guess you like

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