{"id":694,"date":"2023-07-15T15:03:27","date_gmt":"2023-07-15T07:03:27","guid":{"rendered":"http:\/\/localhost:8080\/?p=694"},"modified":"2023-07-15T15:03:27","modified_gmt":"2023-07-15T07:03:27","slug":"solidworks%e4%ba%8c%e6%ac%a1%e5%bc%80%e5%8f%91-pack-and-go%e5%ae%9e%e4%be%8b","status":"publish","type":"post","link":"https:\/\/blog.ke-zhi.com\/?p=694","title":{"rendered":"SolidWorks\u4e8c\u6b21\u5f00\u53d1-Pack And Go\u5b9e\u4f8b"},"content":{"rendered":"<p>SolidWorks\u4e8c\u6b21\u5f00\u53d1\u6307\u7684\u662f\u4f7f\u7528SolidWorks\u8f6f\u4ef6\u63d0\u4f9b\u7684\u5f00\u53d1\u5de5\u5177\u548cAPI\uff08\u5e94\u7528\u7a0b\u5e8f\u63a5\u53e3\uff09\uff0c\u5bf9SolidWorks\u8f6f\u4ef6\u8fdb\u884c\u5b9a\u5236\u5316\u5f00\u53d1\uff0c\u4ee5\u9002\u5e94\u7528\u6237\u7279\u5b9a\u9700\u6c42\u7684\u8fc7\u7a0b\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u7528\u6237\u53ef\u4ee5\u81ea\u5b9a\u4e49SolidWorks\u7684\u529f\u80fd\u3001\u754c\u9762\u548c\u5de5\u5177\uff0c\u4f7f\u5176\u66f4\u52a0\u7b26\u5408\u5176\u5de5\u4f5c\u6d41\u7a0b\u548c\u9700\u6c42\uff0c\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u548c\u7cbe\u5ea6\u3002SolidWorks\u4e8c\u6b21\u5f00\u53d1\u9700\u8981\u5177\u5907\u4e00\u5b9a\u7684\u7f16\u7a0b\u548c\u8f6f\u4ef6\u5f00\u53d1\u6280\u80fd\uff0c\u6bd4\u5982\u719f\u6089C++\u3001C#\u3001VB\u3001Python\u7b49\u7f16\u7a0b\u8bed\u8a00\uff0c\u5e76\u719f\u6089SolidWorks API\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<p>\u521a\u597d\u6700\u8fd1\u6ca1\u4ec0\u4e48\u4e3b\u9898\u5199\u6587\u7ae0\uff0c\u6709\u4e2a\u7f51\u53cb\u81ea\u5b66\u5f00\u53d1\uff0c\u95ee\u5230\u6253\u5305\u7684\u6539\u540d\u95ee\u9898\u3002\u6211\u5c31\u6765\u5199\u4e2a\u4f8b\u5b50\u5427\uff0c\u56e0\u4e3a\u5b98\u65b9\u7684\u4f8b\u5b50\u53ea\u5199\u4e86\u52a0\u524d\u7f00\u548c\u751f\u7f00\uff0c\u6ca1\u6709\u5199\u5982\u4f55\u4fee\u6539\u6bcf\u4e2a\u96f6\u4ef6\u7684\u540d\u79f0\uff0c\u4ee5\u53ca\u5982\u4f55\u4fdd\u6301\u6807\u51c6\u4ef6\u7684\u539f\u59cb\u5f15\u7528\u5173\u7cfb\u3002<\/p>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u4e0b\u9762\u8fd9\u4e2a\u96f6\u4ef6\u6709\u4e09\u4e2a\u96f6\u4ef6\u4e00\u4e2a\u5de5\u7a0b\u56fe<br \/>\n<img decoding=\"async\" src=\"https:\/\/img-blog.csdnimg.cn\/96c1b211be6a43a180db873000dc72e8.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img-blog.csdnimg.cn\/909f1281097743269a0338379db056ab.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n\u5982\u679c\u7528\u82f1\u6587\u7cfb\u7edf\u6216\u8005\u82f1\u6587\u7248solidworks\u7684\u4eba\u80af\u5b9a\u80fd\u770b\u51fa\u6765\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u662fPack And Go<br \/>\n\u6240\u4ee5\u5148\u5728api\u5e2e\u52a9\u4e2d\u627e\u4e00\u627e\u3002<br \/>\n<img decoding=\"async\" src=\"https:\/\/img-blog.csdnimg.cn\/4140ce619d8c4c77a716239a98902061.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n\u518d\u5177\u4f53\u7684\u4f8b\u5b50\u6211\u5c31\u4e0d\u8bb2\u4e86\uff0c\u76f4\u63a5\u4e0a\u5f15\u8d77\u4ee3\u7801:<\/p>\n<pre><code class=\"language-csharp\">\n        private void btnPackAndGo_Click(object sender, EventArgs e)\n        {\n\n            SldWorks swApp = Utility.ConnectToSolidWorks();\n\n            string sourceAsmFile = @&amp;quot;D:\\09_Study\\CSharpAndSolidWorks\\CSharpAndSolidWorks\\TemplateModel\\TempAssembly.sldasm&amp;quot;;\n            string targetAsmFolder = @&amp;quot;D:\\PackPath\\&amp;quot;;\n            Dictionary&amp;lt;string, string&amp;gt; replaceList= new Dictionary&amp;lt;string, string&amp;gt;();\n\n            replaceList.Add(&amp;quot;TempAssembly&amp;quot;, &amp;quot;\u6211\u7684\u65b0\u88c5\u914d&amp;quot;);\n            replaceList.Add(&amp;quot;clamp1&amp;quot;, &amp;quot;\u65b0\u96f6\u4ef61&amp;quot;);\n            replaceList.Add(&amp;quot;clamp3&amp;quot;, &amp;quot;\u65b0\u96f6\u4ef66&amp;quot;);\n            replaceList.Add(&amp;quot;MateTest&amp;quot;, &amp;quot;\u914d\u5408\u6d4b\u8bd5&amp;quot;);\n\n            PackAndGo(swApp, sourceAsmFile, targetAsmFolder, true,replaceList,&amp;quot;SW-&amp;quot;,&amp;quot;&amp;quot;);\n\n        }\n\n        \/\/\/ &amp;lt;summary&amp;gt;\n        \/\/\/ \u5229\u7528solidworks\u6253\u5305\n        \/\/\/ &amp;lt;\/summary&amp;gt;\n        \/\/\/ &amp;lt;param name=&amp;quot;swApp&amp;quot;&amp;gt;&amp;lt;\/param&amp;gt;\n        \/\/\/ &amp;lt;param name=&amp;quot;sourceAsmFile&amp;quot;&amp;gt;\u6e90\u88c5\u914d\u4f53\u8def\u5f84&amp;lt;\/param&amp;gt;\n        \/\/\/ &amp;lt;param name=&amp;quot;targetAsmFile&amp;quot;&amp;gt;\u76ee\u6807\u8def\u5f84&amp;lt;\/param&amp;gt;\n        \/\/\/ &amp;lt;param name=&amp;quot;includeDwg&amp;quot;&amp;gt;\u662f\u5426\u5305\u542b\u56fe\u7eb8&amp;lt;\/param&amp;gt;\n        \/\/\/ &amp;lt;param name=&amp;quot;replaceList&amp;quot;&amp;gt;\u66ff\u6362\u5185\u5bb9&amp;lt;\/param&amp;gt;\n        \/\/\/ &amp;lt;param name=&amp;quot;AddPrefix&amp;quot;&amp;gt;\u524d\u7f00&amp;lt;\/param&amp;gt;\n        \/\/\/ &amp;lt;param name=&amp;quot;AddSuffix&amp;quot;&amp;gt;\u540e\u7f00&amp;lt;\/param&amp;gt;\n        private void PackAndGo(SldWorks swApp, string sourceAsmFile,string targetAsmFolder,bool includeDwg, Dictionary&amp;lt;string,string&amp;gt; replaceList,string AddPrefix=&amp;quot;&amp;quot;,string AddSuffix=&amp;quot;&amp;quot;)\n        {\n            ModelDoc2 swModelDoc = default(ModelDoc2);\n            ModelDocExtension swModelDocExt = default(ModelDocExtension);\n            PackAndGo swPackAndGo = default(PackAndGo);         \n            bool status = false;\n            int warnings = 0;\n            int errors = 0;\n            int i = 0;\n            int namesCount = 0;\n            string myPath = null;\n            int[] statuses = null;\n\n            \/\/ Open assembly\n\n            swModelDoc = (ModelDoc2)swApp.OpenDoc6(sourceAsmFile, (int)swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, &amp;quot;&amp;quot;, ref errors, ref warnings);\n            swModelDocExt = (ModelDocExtension)swModelDoc.Extension;\n\n            \/\/ Get Pack and Go object\n            Debug.Print(&amp;quot;Pack and Go&amp;quot;);\n            swPackAndGo = (PackAndGo)swModelDocExt.GetPackAndGo();\n\n            \/\/ Get number of documents in assembly\n            namesCount = swPackAndGo.GetDocumentNamesCount();\n            Debug.Print(&amp;quot;  Number of model documents: &amp;quot; + namesCount);\n\n            \/\/ Include any drawings, SOLIDWORKS Simulation results, and SOLIDWORKS Toolbox components\n            swPackAndGo.IncludeDrawings = includeDwg;\n            Debug.Print(&amp;quot; Include drawings: &amp;quot; + swPackAndGo.IncludeDrawings);\n            swPackAndGo.IncludeSimulationResults = true;\n            Debug.Print(&amp;quot; Include SOLIDWORKS Simulation results: &amp;quot; + swPackAndGo.IncludeSimulationResults);\n            swPackAndGo.IncludeToolboxComponents = true;\n            Debug.Print(&amp;quot; Include SOLIDWORKS Toolbox components: &amp;quot; + swPackAndGo.IncludeToolboxComponents);\n\n            \/\/ Get current paths and filenames of the assembly&amp;#039;s documents\n            object fileNames;\n            object[] pgFileNames = new object[namesCount - 1];\n            status = swPackAndGo.GetDocumentNames(out fileNames);\n            pgFileNames = (object[])fileNames;\n\n            Debug.Print(&amp;quot;&amp;quot;);\n            Debug.Print(&amp;quot;  Current path and filenames: &amp;quot;);\n            if ((pgFileNames != null))\n            {\n                for (i = 0; i &amp;lt;= pgFileNames.GetUpperBound(0); i++)\n                {\n                    Debug.Print(&amp;quot;    The path and filename is: &amp;quot; + pgFileNames[i]);\n                }\n            }\n\n            \/\/ Get current save-to paths and filenames of the assembly&amp;#039;s documents\n            object pgFileStatus;\n            status = swPackAndGo.GetDocumentSaveToNames(out fileNames, out pgFileStatus);\n            pgFileNames = (object[])fileNames;\n            Debug.Print(&amp;quot;&amp;quot;);\n            Debug.Print(&amp;quot;  Current default save-to filenames: &amp;quot;);\n            if ((pgFileNames != null))\n            {\n                for (i = 0; i &amp;lt;= pgFileNames.GetUpperBound(0); i++)\n                {\n                    Debug.Print(&amp;quot;   The path and filename is: &amp;quot; + pgFileNames[i]);\n                }\n            }\n\n            \/\/ Set folder where to save the files\n\n            status = swPackAndGo.SetSaveToName(true, targetAsmFolder);\n\n            \/\/ Flatten the Pack and Go folder structure; save all files to the root directory\n            swPackAndGo.FlattenToSingleFolder = true;\n\n            \/\/ Add a prefix and suffix to the filenames\n            swPackAndGo.AddPrefix = AddPrefix;\n            swPackAndGo.AddSuffix = AddSuffix;\n\n            \/\/ Verify document paths and filenames after adding prefix and suffix\n            object getFileNames;\n            object getDocumentStatus;\n            string[] pgGetFileNames = new string[namesCount - 1];\n\n            status = swPackAndGo.GetDocumentSaveToNames(out getFileNames, out getDocumentStatus);\n            pgGetFileNames = (string[])getFileNames;\n            Debug.Print(&amp;quot;&amp;quot;);\n            Debug.Print(&amp;quot;  My Pack and Go path and filenames after adding prefix and suffix: &amp;quot;);\n            for (i = 0; i &amp;lt;= pgGetFileNames.Count()-1; i++)\n            {\n\n                Debug.Print(&amp;quot;  \u66ff\u6362\u524d  My path and filename is: &amp;quot; + pgGetFileNames[i]);\n\n                foreach (var replaceItem  in replaceList)\n                {\n                    pgGetFileNames[i]= pgGetFileNames[i].Replace(replaceItem.Key, replaceItem.Value);\n                }\n\n                Debug.Print(&amp;quot;  \u66ff\u6362\u540e  My path and filename is: &amp;quot; + pgGetFileNames[i]);\n\n             }\n\n            \/\/\u91cd\u65b0\u4fdd\u5b58\u540d\u79f0\n            swPackAndGo.SetDocumentSaveToNames(pgGetFileNames);\n\n            \/\/ \u6267\u884c\u6253\u5305\u3002\n            statuses = (int[])swModelDocExt.SavePackAndGo(swPackAndGo);\n\n            swApp.CloseDoc(swApp.IActiveDoc2.GetPathName());\n\n        }<\/code><\/pre>\n<p>\u6267\u884c\u5b8c\u7ed3\u679c \u5c31\u8fd9\u6837\u4e86\uff0c\u6253\u5f00\u662f\u6b63\u5e38\u7684\u3002<br \/>\n<img decoding=\"async\" src=\"https:\/\/img-blog.csdnimg.cn\/7572260a00b24bce8816724b224b93c4.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n\u597d\u4e86\uff0c\u4eca\u5929\u5c31\u5199\u8fd9\u4e48\u591a\u4e86\u3002\u8fd9\u662f\u662f\u7ecf\u5e38\u7528\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u9700\u8981\u5927\u5bb6\u81ea\u5df1\u6d88\u5316\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SolidWorks\u4e8c\u6b21\u5f00\u53d1\u6307\u7684\u662f\u4f7f\u7528SolidWorks\u8f6f\u4ef6\u63d0\u4f9b\u7684\u5f00\u53d1\u5de5\u5177\u548cAPI\uff08\u5e94\u7528\u7a0b\u5e8f\u63a5\u53e3\uff09\uff0c\u5bf9Sol [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[5],"tags":[],"series":[],"class_list":["post-694","post","type-post","status-publish","format-standard","hentry","category-solidworks"],"views":878,"_links":{"self":[{"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=\/wp\/v2\/posts\/694","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=694"}],"version-history":[{"count":1,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=\/wp\/v2\/posts\/694\/revisions"}],"predecessor-version":[{"id":695,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=\/wp\/v2\/posts\/694\/revisions\/695"}],"wp:attachment":[{"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=694"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fseries&post=694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}