拓展方法及在SW二次开发中应用

一、什么是扩展方法

拓展方法:能够向现有类型“添加”方法,而无需创建新的派生类型、重新编译或以其他方式修改原始类型。

二、扩展方法结构

1.c#

  • 扩展方法所在的类必须声明为static。
  • 扩展方法必须声明为public和static。
  • 扩展方法的第一个参数必须包含关键字this,并且在后面指定扩展的类的名称。
  • 2.vb,net 

  • 必须写入在模块中;
  • 必须表明拓展特性(引入拓展属性命名空间)。
  •  

     

     

     

  •  public static class SWAPI
        {
    
             public static string 文件名(this ModelDoc2 文件) 
            {
              
                return Path.GetFileNameWithoutExtension(文件.GetPathName());
            }
           
            public static string 目录(this ModelDoc2 文件)
            {
                return Path.GetDirectoryName(文件.GetPathName())+"\\";
            }
           
            public static string 工程图路径(this string 零件装配体全路径)
            {
                return Path.GetDirectoryName(零件装配体全路径) + "\\" + Path.GetFileNameWithoutExtension(零件装配体全路径) + ".SLDDRW";
    
            }
            public static string 工程图路径( this ModelDoc2 文件)
            {
                if (文件.GetType() != 3)
                {
                  return  Path.GetDirectoryName(文件.GetPathName()) + "\\" + Path.GetFileNameWithoutExtension(文件.GetPathName()) + ".SLDDRW";
                }
    
                return "";
            }
            public static bool 是否存在工程图( this ModelDoc2 文件)
            {
                if (文件.GetType() !=3)
                {
                  string str    = Path.GetDirectoryName(文件.GetPathName()) + "\\" + Path.GetFileNameWithoutExtension(文件.GetPathName()) + ".SLDDRW";
                    return File.Exists(str);
                }
    
                return false;
            }
            public static string 材质(this PartDoc 零件)
            {
                return 零件.GetMaterialPropertyName2("", out _);
            }
            public static double 重量(this ModelDoc2 文件)
            {
                if (文件.GetType()!=3)
                {
                    MassProperty massProperty = 文件.Extension.CreateMassProperty();
                    massProperty.UseSystemUnits = true;
                    return Math.Round(massProperty.Mass, 3);
    
                }
                return 0;
            }
            public static void 图号分离(this ModelDoc2 文件,string 图号,string 名称)
            {
                string name = 文件.文件名();
                if (name.Contains(" "))
                {
                    string TH = name.Split(new char[1] { ' ' })[0];
                    string MC = name.Split(new char[1] { ' ' })[1];
                    文件.DeleteCustomInfo2("", 图号);
                    文件.DeleteCustomInfo2("", 名称);
                    文件.AddCustomInfo3("", 图号, 30, TH);
                    文件.AddCustomInfo3("", 名称, 30, MC);
                }
            }
            public static void 删除自定义属性(this ModelDoc2 文件)
            {
                string[] names = (string[])文件.GetCustomInfoNames();
                if (names !=null)
                {
                    foreach (string name in names)
                    {
                        文件.DeleteCustomInfo2("", name);
                    }
                }
            }
            public static void 删除配置属性(this ModelDoc2 文件)
            {   
                string[] CurCFGnames = (string[])文件.GetConfigurationNames();
                foreach (string cname in CurCFGnames)
                {
                    CustomPropertyManager custom = 文件.Extension.CustomPropertyManager[cname];
                    string[] names = (string[])custom.GetNames();
                    foreach (string name in names)
                    {
                        文件.DeleteCustomInfo2(cname, name);
                    }
    
                }
            }
           
        }

     

  • 上述代码只是个人使用,不保证无误。
  • 三、扩展方法调用

  • 调用:拓展方法可以直接通过对象(变量)点(.)去调用。
  •  

     

  • c#
  • 1   ModelDoc2 modelDoc2 = swApp.ActiveDoc;
    2             string 文件名 = modelDoc2.文件名();
    View Code

     vb.net

  • 1         Dim DOC As ModelDoc2 = Swapp.ActiveDoc
    2         Dim 文件名 As String = DOC.文件名()
    View Code

     

 

posted @ 2022-11-09 18:48  流云带走悲伤  阅读(298)  评论(0)    收藏  举报