我们开发的程序使用过程中经常要使用一些做好的模板,尤其是参数化的时候,这样就难免有一些文件需要从指定的地方复制过来,或者说还要把文件名替换掉,还可以加一些前缀或者后缀。这个例子也只是一个最简单的打包方案,当然大家还可以参考API帮助文件中的Pack And Go函数来打包文件。

        /// <summary>/// 不打开文件,复制文件/// </summary>/// <param name="sourcefile">源文件路径</param>/// <param name="target">目标文件</param>/// <param name="ReplaceFrom">需要替换的文件</param>/// <param name="ReplaceTo">替换成什么</param>/// <param name="likeFile">是否模糊匹配</param>/// <param name="ReplaceFrom2">替换字符2</param>/// <param name="ReplaceTo2">第二次替换成什么</param>/// <param name="likeFile2">模糊匹配?</param>public void CopySolidworksFile(string sourcefile, string target, string ReplaceFrom = "", string ReplaceTo = "", bool likeFile = true, string ReplaceFrom2 = "", string ReplaceTo2 = "", bool likeFile2 = false){ISldWorks swApp = Utility.ConnectToSolidWorks();target = System.IO.Path.GetDirectoryName(target) + @"\";int sourcecount = 0;string[] sourcefiles = null;string[] targetfiles = null;object[] depends;var sourcefileName = System.IO.Path.GetFileName(sourcefile);var oldSourcefiles = sourcefiles;sourcefiles = new string[sourcecount + 1];if (oldSourcefiles != null)Array.Copy(oldSourcefiles, sourcefiles, Math.Min(sourcecount + 1, oldSourcefiles.Length));var oldTargetfiles = targetfiles;targetfiles = new string[sourcecount + 1];if (oldTargetfiles != null)Array.Copy(oldTargetfiles, targetfiles, Math.Min(sourcecount + 1, oldTargetfiles.Length));sourcefiles[sourcecount] = sourcefile;progressBarCopy.Value = 10;if (ReplaceFrom != ""){if (likeFile == true)targetfiles[sourcecount] = target + sourcefileName.Replace(ReplaceFrom, ReplaceTo).Replace(ReplaceFrom2, ReplaceTo2);else if (sourcefileName.ToUpper() == ReplaceFrom.ToUpper())targetfiles[sourcecount] = target + sourcefileName.Replace(ReplaceFrom, ReplaceTo).Replace(ReplaceFrom2, ReplaceTo2);elsetargetfiles[sourcecount] = target + sourcefileName.Replace(ReplaceFrom2, ReplaceTo2);}elsetargetfiles[sourcecount] = target + sourcefileName;if (FileSystem.Dir(targetfiles[sourcecount]) != ""){if (Interaction.MsgBox(targetfiles[sourcecount] + "已经存在,是否替换?", Constants.vbYesNo, "文件打包") == Constants.vbNo){progressBarCopy.Value = 0;return;}}try{FileSystem.FileCopy(sourcefiles[sourcecount], targetfiles[sourcecount]);}catch (Exception ex){}progressBarCopy.Value = 35;sourcecount = sourcecount + 1;if (FileSystem.Dir(Strings.Replace(sourcefile, ".SLDASM", ".SLDDRW")) != ""){oldSourcefiles = sourcefiles;sourcefiles = new string[sourcecount + 1];if (oldSourcefiles != null)Array.Copy(oldSourcefiles, sourcefiles, Math.Min(sourcecount + 1, oldSourcefiles.Length));oldTargetfiles = targetfiles;targetfiles = new string[sourcecount + 1];if (oldTargetfiles != null)Array.Copy(oldTargetfiles, targetfiles, Math.Min(sourcecount + 1, oldTargetfiles.Length));sourcefiles[sourcecount] = Strings.Replace(sourcefile, ".SLDASM", ".SLDDRW");sourcefileName = System.IO.Path.GetFileName(sourcefiles[sourcecount]);if (ReplaceFrom != ""){if (likeFile == true)targetfiles[sourcecount] = target + sourcefileName.Replace(ReplaceFrom, ReplaceTo).Replace(ReplaceFrom2, ReplaceTo2);else if (sourcefileName.ToUpper() == ReplaceFrom.ToUpper())targetfiles[sourcecount] = target + sourcefileName.Replace(ReplaceFrom, ReplaceTo).Replace(ReplaceFrom2, ReplaceTo2);elsetargetfiles[sourcecount] = target + sourcefileName.Replace(ReplaceFrom2, ReplaceTo2);}elsetargetfiles[sourcecount] = target + sourcefileName;if (FileSystem.Dir(targetfiles[sourcecount]) != ""){if (Interaction.MsgBox(targetfiles[sourcecount] + "已经存在,是否替换?", Constants.vbYesNo, "文件打包") == Constants.vbNo)return;}try{FileSystem.FileCopy(sourcefiles[sourcecount], targetfiles[sourcecount]);}catch (Exception ex){}sourcecount = sourcecount + 1;}progressBarCopy.Value = 50;depends = (string[])swApp.GetDocumentDependencies2(sourcefile, true, true, false);if (depends == null)return;bool bRet;var idx = 1;while (idx <= Information.UBound(depends)){oldSourcefiles = sourcefiles;sourcefiles = new string[sourcecount + 1];if (oldSourcefiles != null)Array.Copy(oldSourcefiles, sourcefiles, Math.Min(sourcecount + 1, oldSourcefiles.Length));oldTargetfiles = targetfiles;targetfiles = new string[sourcecount + 1];if (oldTargetfiles != null)Array.Copy(oldTargetfiles, targetfiles, Math.Min(sourcecount + 1, oldTargetfiles.Length));sourcefiles[sourcecount] = depends[idx].ToString();sourcefileName = System.IO.Path.GetFileName(depends[idx].ToString());if (ReplaceFrom != ""){if (likeFile == true)targetfiles[sourcecount] = target + sourcefileName.Replace(ReplaceFrom, ReplaceTo).Replace(ReplaceFrom2, ReplaceTo2);else if (sourcefileName.ToUpper() == ReplaceFrom.ToUpper())targetfiles[sourcecount] = target + sourcefileName.Replace(ReplaceFrom, ReplaceTo).Replace(ReplaceFrom2, ReplaceTo2);elsetargetfiles[sourcecount] = target + sourcefileName.Replace(ReplaceFrom2, ReplaceTo2);}elsetargetfiles[sourcecount] = target + sourcefileName;try{FileSystem.FileCopy(sourcefiles[sourcecount], targetfiles[sourcecount]);}catch (Exception ex){}idx = idx + 2;sourcecount = sourcecount + 1;}// swApp.SendMsgToUser("Done")progressBarCopy.Value = 80;for (int n = 0; n <= sourcecount - 1; n++){var NewName = targetfiles[n];var RefQ = swApp.GetDocumentDependenciesCount(NewName, 1, 1) / (double)2;// Debug.Print(NewName & "--->参考文件有  " & RefQ)if (RefQ > 0){var q = 0;for (q = 0; q <= sourcecount - 1; q++){if (n == q){}elsebRet = swApp.ReplaceReferencedDocument(targetfiles[n], System.IO.Path.GetFileName(sourcefiles[q]), targetfiles[q]);}}}progressBarCopy.Value = 95;}

C# SolidWorks 二次开发 API --- 实例:打包文件相关推荐

  1. C# SolidWorks 二次开发 API --- 2018版 中文翻译 之官方示例

    这是2018版 自带帮助文件的API例子中文导航,如果有错误的,请指出来 时间原因,我没有一一测试,只是大概过滤了一下. 共计750个例 子: 中文标题 Web Link 将绘图表缩放到窗口中的最大尺 ...

  2. C# SolidWorks 二次开发 API --- 2018版 中文翻译 ModelDocExtension 方法

    AddAngularRunningDim Method (IModelDocExtension) 为选定实体添加指定的角度运行标注. AddComment Method (IModelDocExten ...

  3. api 二次 开发 禅道_VBA SolidWorks 二次开发 API ---从宏开始

    Macro是可以用来录制一些操作的工具,然后你可以看到具体内部执行的一些代码.通过这些代码,我们可以更快更直接的找到所需要查询的API函数.我们以最简单的做一个拉伸特征开始,看如何找到对应的API 1 ...

  4. VBA SolidWorks 二次开发 API ---从宏开始

    Macro是可以用来录制一些操作的工具,然后你可以看到具体内部执行的一些代码.通过这些代码,我们可以更快更直接的找到所需要查询的API函数.我们以最简单的做一个拉伸特征开始,看如何找到对应的API 1 ...

  5. C# SolidWorks 二次开发 API --- 提升exe执行效率接近DLL

    最近一段时间没更新博客,原因比较多.作为湖北人,今年的班上的不容易.当然还是要感谢各地的支援. 今天来和大家分享一下一个关于exe提升性能的选项,因为之前主要写的exe比较多,后来改成dll之后效率提 ...

  6. C# SolidWorks 二次开发 API --- 2018版 中文翻译-完整版共享

    这是2018的api帮助文档看了一下翻译版,我把之前翻译的文件免费共享下,希望能对大家有所帮助. 如果大家想查找快速了解某个功能,可以直接在Excel表中查找全部. 之前有两篇共享了官方示例与Mode ...

  7. C# SolidWorks 二次开发 API --- 2018版 中文翻译 ModelDoc2 属性和IModelDocExtension属性

    属性名称 描述 ActiveView Property (IModelDoc2) 获取只读模式下的当前活动模型视图.注意:此属性是一个get-only属性.集合未实现. ConfigurationMa ...

  8. C# SolidWorks 二次开发 API ---读取零件相关属性

    如何读取零件相关属性 这一篇看下如何读取属性: 直接上代码: 代码如下: private void BtnGetPartData_Click(object sender, EventArgs e){/ ...

  9. C# SolidWorks 二次开发 API ---遍历特定圆柱面进行处理

    功能要求 如下图,从客户处过来的图档螺丝孔后缀尺寸第三位都是0.001 ,这是客户用来定义螺纹的标识. 但是对于加工处理上,不需要这个后缀,会让生产人员误解为精度要求比较高. 所以考虑是否有批处理的功 ...

最新文章

  1. ubuntu升级python_ubuntu升级python版本
  2. 关于MySQL事务和存储引擎10个FAQ(Frequently asked questions
  3. selenium python (七)层级定位(二次定位)
  4. 2.3.1 进程同步 进程互斥
  5. js php 正则差别,JavaScript正则表达式的浏览器的差异
  6. wordpress index.php 跳转,wordpress点击内容页跳转到其他url的解决方法
  7. js获取精确宽度/高度(带小数位像素)
  8. 完成端口的回射服务器,给别人的代码添加了补丁(竞争问题,发送数据)
  9. C#坏习惯:通过不好的例子学习如何制作好的代码——第4部分
  10. p73_万维网和HTTP协议
  11. 循环渐进NsDoor(三)
  12. Sql Server 分区
  13. 带通滤波器作用和用途_带通滤波器是什么,带通滤波器的作用
  14. 《北京市工作居住证》办理攻略
  15. 电脑计算机网络都打不开怎么办,电脑网页打不开怎么回事的常规处理方法
  16. Charles——charles代理菜单proxy总结—— 开始/暂停模拟慢网速—— stop/start throttling 和 throttling settings...
  17. ICN: Integrated crosstalk noise
  18. 数据库安全性 --- 控制
  19. 高端对话:过程改进领域热点话题集锦
  20. 【PYTHON数据分析实战】电影票房数据分析(一)数据采集

热门文章

  1. mysql中级_mysql中级操作 - osc_mi06gsf5的个人空间 - OSCHINA - 中文开源技术交流社区...
  2. 联通新措施上线了,一文带你探索联通大数据的前世今生
  3. 【村长的算法教程】算法与数据结构基础重点
  4. Debian 6.0安装igb驱动
  5. 15.(cesium篇)cesium暗色系地图样式地图(滤镜实现,反色滤镜)
  6. 四驾马车:从“制造之城”到“智能制造之都”的长沙缘何飞速前进
  7. 简单线性回归截距假设检验_第10章 简单线性回归分析思考与练习参考答案.doc...
  8. Android视图对象绑定方式的抉择对比分析
  9. 园区动态 | 产业前沿——“数字经济与元宇宙赋能大兴新国门创新发展研讨会”在大兴经开区召开
  10. ¶Steps 步骤条更改线条,字体,头部样式