C# SolidWorks 二次开发 API — 实例:打包文件

C# SolidWorks 二次开发 API — 实例:打包文件

我们开发的程序使用过程中经常要使用一些做好的模板,尤其是参数化的时候,这样就难免有一些文件需要从指定的地方复制过来,或者说还要把文件名替换掉,还可以加一些前缀或者后缀。这个例子也只是一个最简单的打包方案,当然大家还可以参考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);
                else
                    targetfiles[sourcecount] = target + sourcefileName.Replace(ReplaceFrom2, ReplaceTo2);
            }
            else
                targetfiles[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);
                    else
                        targetfiles[sourcecount] = target + sourcefileName.Replace(ReplaceFrom2, ReplaceTo2);
                }
                else
                    targetfiles[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);
                    else
                        targetfiles[sourcecount] = target + sourcefileName.Replace(ReplaceFrom2, ReplaceTo2);
                }
                else
                    targetfiles[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)
                        {
                        }
                        else
                            bRet = swApp.ReplaceReferencedDocument(targetfiles[n], System.IO.Path.GetFileName(sourcefiles[q]), targetfiles[q]);
                    }
                }
            }

            progressBarCopy.Value = 95;
        }
  

 

posted @
2019-11-22 09:15 
painezeng  阅读(
214)  评论(
0
编辑 
收藏 
举报