C# SolidWorks 二次开发 API—调用Solidworks命令

比如说用户选中一个尺寸时,solidworks的左侧属性框就会自动出来(有个选项可以控制),但此时如果我们想关闭属性框。应该怎么操作? 通过正常的测试可以发现按esc就可以。
当然如果直接发送系统的键盘命令的话,还需要先切换焦点到solidworks中。虽然 也可以实现,相对麻烦。
这时候我们可以通过另一种方式来执行命令,这个方式和我们手动点击菜单中的命令按钮是一样的效果。
这个我就不细说了,下面看一下简单的例子:

首先需要引用SolidWorks.Interop.swcommands
在这里插入图片描述
代码比较简单

			 SldWorks swApp = PStandAlone.GetSolidWorks();

            //执行命令监控
            swApp.CommandOpenPreNotify += SwApp_CommandOpenPreNotify;

            //请参考SolidWorks.Interop.swcommands

            //swCommands_e 命令操作

            //swMouse_e  鼠标操作

            //打开选项对话框
            //swApp.RunCommand((int)swCommands_e.swCommands_Options, "");

            //开始3d草图
            swApp.RunCommand((int)swCommands_e.swCommands_3DSketch, "");

            //单击右键
            //swApp.RunCommand((int)swMouse_e.swMouse_Click, "");

在零件或者装配体的状态下执行后,就会看到solidworks已经在3d草图状态下了。
在这里插入图片描述

下面这个事件就可以对执行的命令进行记录。还可以限制用户进行一些特殊的命令,如下面的代码就禁用了过滤面命令。
这个可以扩展很多功能,比如做一些标准化管理,限制用户乱改配置。

 		/// <summary>
        /// 在执行命令前通知。
        /// </summary>
        /// <param name="Command"></param>
        /// <param name="UserCommand"></param>
        /// <returns></returns>
        private int SwApp_CommandOpenPreNotify(int Command, int UserCommand)
        {
            Debug.Print($@"command is :{Enum.GetName(typeof(swCommands_e), Command)}");

            Debug.Print($@"user command Id is :{UserCommand}");

            if (Command == (int)swCommands_e.swCommands_FilterFaces)
            {
                MessageBox.Show("Fillet Faces Command is disable!");
                return 1;
            }

            return 0;
        }

在用户点击过滤面的时候就会出现:
在这里插入图片描述

C# SolidWorks 二次开发 API—遍历零件所有可编辑尺寸

最近有高校学生问到一个问题,如何得到零件中所有可以编辑的尺寸信息。包括所有的特征以及草图尺寸。之前的博客中只是写了如何遍历特征以及图纸中的尺寸。通过查api发现,其实这个和图纸中标注的尺寸一样,直接使用:先看结果:零件:

        /// <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);
          }
      }

代码已经上传,请自取。