C# SolidWorks 二次开发 API—获取配合信息

C# SolidWorks 二次开发 API—获取配合信息

好像快有一个月没有更新,最近有个小需求,设计时不时就会把安装螺丝的孔弄错。
想利用工具来检查一下孔径是否正确。这就需要获取到装配体中那些配合特征。
并读取配合的两个直径,来判断设计打孔特征是否做错了。
比如下图,这里需要锁螺丝,或者螺丝过孔。 (模型中是不安装螺丝零件的)

所以我们要读取配合特征,然后通过配合的一些信息来做判断。
通过查找api可以找到这个实例:

        private void btnGetMateInfor_Click(object sender, EventArgs e)
        {
            //请先打开TemplateModel文件夹下的装配TempAssembly.sldasm D:\09_Study\CSharpAndSolidWorks\CSharpAndSolidWorks\TemplateModel

            var swApp = PStandAlone.GetSolidWorks();

            var swModel = (ModelDoc2)swApp.ActiveDoc;

            var swFeat = (Feature)swModel.FirstFeature();

            Feature swMateFeat = null;
            Feature swSubFeat = default(Feature);
            Mate2 swMate = default;
            Component2 swComp = default(Component2);
            MateEntity2[] swMateEnt = new MateEntity2[3];
            //string fileName = null;
            //int errors = 0;
            //int warnings = 0;
            int i = 0;
            double[] entityParameters = new double[8];

            //从特征树中查找配合文件夹 Iterate over features in FeatureManager design tree

            while ((swFeat != null))
            {
                if ("MateGroup" == swFeat.GetTypeName())
                {
                    swMateFeat = (Feature)swFeat;
                    break;
                }
                swFeat = (Feature)swFeat.GetNextFeature();
            }
            Debug.Print(" " + swMateFeat.Name);
            Debug.Print("");

            //获取第一个子配合特征 Get first mate, which is a subfeature
            swSubFeat = (Feature)swMateFeat.GetFirstSubFeature();
            while ((swSubFeat != null))
            {
                swMate = (Mate2)swSubFeat.GetSpecificFeature2();
                if ((swMate != null))
                {
                    for (i = 0; i <= 1; i++)
                    {
                        swMateEnt[i] = swMate.MateEntity(i);
                        Debug.Print(" " + swSubFeat.Name);
                        Debug.Print(" Type = " + swMate.Type);
                        Debug.Print(" Alignment = " + swMate.Alignment);
                        Debug.Print(" Can be flipped = " + swMate.CanBeFlipped);
                        Debug.Print("");
                        swComp = (Component2)swMateEnt[i].ReferenceComponent;
                        Debug.Print(" Component = " + swComp.Name2);
                        Debug.Print(" Mate enity type = " + swMateEnt[i].ReferenceType);
                        entityParameters = (double[])swMateEnt[i].EntityParams;
                        Debug.Print(" (x,y,z) = (" + (double)entityParameters[0] + ", " + (double)entityParameters[1] + ", " + (double)entityParameters[2] + ")");
                        Debug.Print(" (i,j,k) = (" + (double)entityParameters[3] + ", " + (double)entityParameters[4] + ", " + (double)entityParameters[5] + ")");
                        Debug.Print(" Radius 1 = " + (double)entityParameters[6]);
                        Debug.Print(" Radius 2 = " + (double)entityParameters[7]);
                        Debug.Print("");
                    }
                    Debug.Print(" ");
                }
                // 从配合组中遍历 下一个配合 Get the next mate in MateGroup
                swSubFeat = (Feature)swSubFeat.GetNextSubFeature();
            }
        }

输出显示如下图:
可以看出来这个配合的两个孔一个是1.8mm 一个是1.4mm.
后续就可以通过这个来判断是否正确了。

posted @
2023-02-18 08:49 
painezeng  阅读(
0)  评论(
0
编辑 
收藏 
举报  
来源