SolidWorks二次开发-Pack And Go实例

SolidWorks二次开发指的是使用SolidWorks软件提供的开发工具和API(应用程序接口),对SolidWorks软件进行定制化开发,以适应用户特定需求的过程。通过这种方式,用户可以自定义SolidWorks的功能、界面和工具,使其更加符合其工作流程和需求,提高工作效率和精度。SolidWorks二次开发需要具备一定的编程和软件开发技能,比如熟悉C++、C#、VB、Python等编程语言,并熟悉SolidWorks API的使用方法。

刚好最近没什么主题写文章,有个网友自学开发,问到打包的改名问题。我就来写个例子吧,因为官方的例子只写了加前缀和生缀,没有写如何修改每个零件的名称,以及如何保持标准件的原始引用关系。

举个例子,下面这个零件有三个零件一个工程图
在这里插入图片描述

在这里插入图片描述
如果用英文系统或者英文版solidworks的人肯定能看出来,这个操作是Pack And Go
所以先在api帮助中找一找。
在这里插入图片描述
再具体的例子我就不讲了,直接上引起代码:


        private void btnPackAndGo_Click(object sender, EventArgs e)
        {

            SldWorks swApp = Utility.ConnectToSolidWorks();

            string sourceAsmFile = @"D:\09_Study\CSharpAndSolidWorks\CSharpAndSolidWorks\TemplateModel\TempAssembly.sldasm";
            string targetAsmFolder = @"D:\PackPath\";
            Dictionary<string, string> replaceList= new Dictionary<string, string>();

            replaceList.Add("TempAssembly", "我的新装配");
            replaceList.Add("clamp1", "新零件1");
            replaceList.Add("clamp3", "新零件6");
            replaceList.Add("MateTest", "配合测试");

            PackAndGo(swApp, sourceAsmFile, targetAsmFolder, true,replaceList,"SW-","");

        }

        /// <summary>
        /// 利用solidworks打包
        /// </summary>
        /// <param name="swApp"></param>
        /// <param name="sourceAsmFile">源装配体路径</param>
        /// <param name="targetAsmFile">目标路径</param>
        /// <param name="includeDwg">是否包含图纸</param>
        /// <param name="replaceList">替换内容</param>
        /// <param name="AddPrefix">前缀</param>
        /// <param name="AddSuffix">后缀</param>
        private void PackAndGo(SldWorks swApp, string sourceAsmFile,string targetAsmFolder,bool includeDwg, Dictionary<string,string> replaceList,string AddPrefix="",string AddSuffix="")
        {
            ModelDoc2 swModelDoc = default(ModelDoc2);
            ModelDocExtension swModelDocExt = default(ModelDocExtension);
            PackAndGo swPackAndGo = default(PackAndGo);         
            bool status = false;
            int warnings = 0;
            int errors = 0;
            int i = 0;
            int namesCount = 0;
            string myPath = null;
            int[] statuses = null;

            // Open assembly

            swModelDoc = (ModelDoc2)swApp.OpenDoc6(sourceAsmFile, (int)swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
            swModelDocExt = (ModelDocExtension)swModelDoc.Extension;

            // Get Pack and Go object
            Debug.Print("Pack and Go");
            swPackAndGo = (PackAndGo)swModelDocExt.GetPackAndGo();

            // Get number of documents in assembly
            namesCount = swPackAndGo.GetDocumentNamesCount();
            Debug.Print("  Number of model documents: " + namesCount);

            // Include any drawings, SOLIDWORKS Simulation results, and SOLIDWORKS Toolbox components
            swPackAndGo.IncludeDrawings = includeDwg;
            Debug.Print(" Include drawings: " + swPackAndGo.IncludeDrawings);
            swPackAndGo.IncludeSimulationResults = true;
            Debug.Print(" Include SOLIDWORKS Simulation results: " + swPackAndGo.IncludeSimulationResults);
            swPackAndGo.IncludeToolboxComponents = true;
            Debug.Print(" Include SOLIDWORKS Toolbox components: " + swPackAndGo.IncludeToolboxComponents);

            // Get current paths and filenames of the assembly's documents
            object fileNames;
            object[] pgFileNames = new object[namesCount - 1];
            status = swPackAndGo.GetDocumentNames(out fileNames);
            pgFileNames = (object[])fileNames;

            Debug.Print("");
            Debug.Print("  Current path and filenames: ");
            if ((pgFileNames != null))
            {
                for (i = 0; i <= pgFileNames.GetUpperBound(0); i++)
                {
                    Debug.Print("    The path and filename is: " + pgFileNames[i]);
                }
            }

            // Get current save-to paths and filenames of the assembly's documents
            object pgFileStatus;
            status = swPackAndGo.GetDocumentSaveToNames(out fileNames, out pgFileStatus);
            pgFileNames = (object[])fileNames;
            Debug.Print("");
            Debug.Print("  Current default save-to filenames: ");
            if ((pgFileNames != null))
            {
                for (i = 0; i <= pgFileNames.GetUpperBound(0); i++)
                {
                    Debug.Print("   The path and filename is: " + pgFileNames[i]);
                }
            }

            // Set folder where to save the files

            status = swPackAndGo.SetSaveToName(true, targetAsmFolder);

            // Flatten the Pack and Go folder structure; save all files to the root directory
            swPackAndGo.FlattenToSingleFolder = true;

            // Add a prefix and suffix to the filenames
            swPackAndGo.AddPrefix = AddPrefix;
            swPackAndGo.AddSuffix = AddSuffix;

            // Verify document paths and filenames after adding prefix and suffix
            object getFileNames;
            object getDocumentStatus;
            string[] pgGetFileNames = new string[namesCount - 1];

            status = swPackAndGo.GetDocumentSaveToNames(out getFileNames, out getDocumentStatus);
            pgGetFileNames = (string[])getFileNames;
            Debug.Print("");
            Debug.Print("  My Pack and Go path and filenames after adding prefix and suffix: ");
            for (i = 0; i <= pgGetFileNames.Count()-1; i++)
            {

                Debug.Print("  替换前  My path and filename is: " + pgGetFileNames[i]);

                foreach (var replaceItem  in replaceList)
                {
                    pgGetFileNames[i]= pgGetFileNames[i].Replace(replaceItem.Key, replaceItem.Value);
                }

                Debug.Print("  替换后  My path and filename is: " + pgGetFileNames[i]);

             }

            //重新保存名称
            swPackAndGo.SetDocumentSaveToNames(pgGetFileNames);

            // 执行打包。
            statuses = (int[])swModelDocExt.SavePackAndGo(swPackAndGo);

            swApp.CloseDoc(swApp.IActiveDoc2.GetPathName());

        }

执行完结果 就这样了,打开是正常的。
在这里插入图片描述
好了,今天就写这么多了。这是是经常用的一个方法,需要大家自己消化。

发表回复