C# SolidWorks 二次开发 API---如何通过面属性命名选中对象

今天有人问到如何选中零件中已经命名的面.那我们就先把这个记录一下.

比如,零件中我们命名了一个面的名称

有两种情况:

1,零件中选中该面:

             ISldWorks swApp = Utility.ConnectToSolidWorks();
            ModelDoc2 swModel = swApp.ActiveDoc;
            SelectionMgr swSelMgr = swModel.SelectionManager;          
             PartDoc part1 = (PartDoc)swModel;
            //在零件中选择
            Face2 face1 = part1.GetEntityByName("SFace1", (int)swSelectType_e.swSelFACES);
            Entity entity1 = (Entity)face1;
            entity1.Select(false);

2.装配中:

 #region 装配中选择

            //这里我们默认该零件已经是选中装配,否则我们需要遍历一次零件,仅做示例

            Component2 component = swSelMgr.GetSelectedObjectsComponent4(1, -1);

            swModel.ClearSelection();

            ModelDoc2 modelDoc = component.GetModelDoc2();

            //转换为PartDoc
            PartDoc part = (PartDoc)modelDoc;

            Face2 face = part.GetEntityByName("SFace1", (int)swSelectType_e.swSelFACES);

            Entity entity = (Entity)face;

            //在装配中再转换成装配中的实体
            Entity entityInComp = (Entity)component.GetCorrespondingEntity(entity);

            entityInComp.Select(false);

            #endregion
发布了51 篇原创文章 · 获赞 17 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/zengqh0314/article/details/101706960