{"id":576,"date":"2020-04-17T10:00:00","date_gmt":"2020-04-17T02:00:00","guid":{"rendered":"http:\/\/localhost:8080\/?p=576"},"modified":"2020-04-17T10:00:00","modified_gmt":"2020-04-17T02:00:00","slug":"c-solidworks-%e4%ba%8c%e6%ac%a1%e5%bc%80%e5%8f%91-api-solidworks%e7%9a%84%e5%a4%9a%e5%bc%80%e6%93%8d%e4%bd%9c%e4%b8%8e%e8%bf%9e%e6%8e%a5%e6%8c%87%e5%ae%9a%e7%89%88%e6%9c%acsolidworks","status":"publish","type":"post","link":"https:\/\/blog.ke-zhi.com\/?p=576","title":{"rendered":"C# SolidWorks \u4e8c\u6b21\u5f00\u53d1 API &#8212;Solidworks\u7684\u591a\u5f00\u64cd\u4f5c\u4e0e\u8fde\u63a5\u6307\u5b9a\u7248\u672cSolidworks"},"content":{"rendered":"<h1> <a href=\"https:\/\/www.cnblogs.com\/solidworksapi\/p\/12897434.html\" target=\"_blank\" rel=\"nofollow noopener\"> <span>C# SolidWorks \u4e8c\u6b21\u5f00\u53d1 API &#8212;Solidworks\u7684\u591a\u5f00\u64cd\u4f5c\u4e0e\u8fde\u63a5\u6307\u5b9a\u7248\u672cSolidworks<\/span> <\/a> <\/h1>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; \u4eca\u5929\u6709\u7f51\u53cb\u95ee\u6211\u5173\u4e8e\u5982\u4f55\u7528Exe\u64cd\u4f5c\u591a\u4e2aSolidworks\u7684\u95ee\u9898\uff0c\u5176\u5b9e\u4e4b\u524d\u6211\u770b\u5230\u8fc7\u89e3\u51b3\u65b9\u6848\uff0c\u662f\u901a\u8fc7\u8fdb\u7a0b\u548cid\u83b7\u53d6\uff0c\u800c\u4e0d\u662f\u6211\u4eec\u5e73\u65f6\u5e38\u7528\u7684&nbsp;(SldWorks)Marshal.GetActiveObject(&#8220;SldWorks.Application.27&#8221;);<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; \u6240\u4ee5\u665a\u4e0a\u56de\u6765\u67e5\u4e86\u4e9b\u8d44\u6599\uff0c\u53d1\u73b0\u4e86\u6709\u4e86\u89e3\u51b3\u65b9\u6848\uff0c\u6211\u7cbe\u7b80\u4e86\u4e00\u4e0b\uff0c\u505a\u4e86\u4e2adll.\u5927\u5bb6\u53ef\u4ee5\u8bd5\u8bd5\u3002&nbsp; &nbsp;&nbsp;<\/p>\n<pre><code> ''' &lt;summary&gt;\n    ''' \u521b\u5efa\u4e00\u4e2a\u65b0\u7684Solidworks\u5e76\u8fd4\u56de\u5b9e\u4f8b\n    ''' &lt;\/summary&gt;\n    ''' &lt;param name=\"version\"&gt;\u6307\u5b9a\u7248\u672c\u53f7\uff0c-1\u8868\u793a\u9ed8\u8ba4.&lt;\/param&gt;\n    ''' &lt;param name=\"suppressDialogs\"&gt;True \u5219\u7981\u7528solidworks\u5f39\u51fa\u6d88\u606f.&lt;\/param&gt;\n    ''' &lt;param name=\"requireMainWindow\"&gt;True \u8868\u793a\u8fd0\u884c\u5b8c\u663e\u793a\u5230\u4e3b\u7a97\u53e3&lt;\/param&gt;\n    ''' &lt;param name=\"startProcessTimeout\"&gt;\u8fd4\u56deNull \u5982\u679cSolidWorks\u5728\u6307\u5b9a\u65f6\u95f4\u5185\u6ca1\u6709\u6253\u5f00\u3002&lt;\/param&gt;\n    ''' &lt;param name=\"createWindowTimeout\"&gt;\u8fd4\u56deNull \u5982\u679cSolidWorks\u4e3b\u7a97\u53e3\u5728\u6307\u5b9a\u65f6\u95f4\u5185\u6ca1\u6709\u663e\u793a.&lt;\/param&gt;\n    ''' &lt;returns&gt;&lt;\/returns&gt;\n    Public Shared Function RunSolidWorks(version As Integer,\n                                         visible As Boolean,\n                                         Optional suppressDialogs As Boolean = False,\n                                         Optional requireMainWindow As Boolean = True,\n                                         Optional startProcessTimeout As Integer = 30,\n                                         Optional createWindowTimeout As Integer = 15) As SldWorks\n\n        Dim executablePath As String = CTFileSystem.GetSolidWorksExecutablePath(version)\n\n        If File.Exists(executablePath) = False Then Return Nothing\n\n        Dim info As ProcessStartInfo = New ProcessStartInfo(executablePath)\n\n        If suppressDialogs Then info.Arguments = \"\/r\"\n\n        Dim process As Process = Process.Start(info)\n        Dim app As SldWorks = Nothing\n        Dim t As DateTime = DateTime.Now\n\n        While app Is Nothing\n            Threading.Thread.Sleep(1000)\n            If Math.Abs(DateTime.Now.Subtract(t).Seconds) &gt; startProcessTimeout Then Return Nothing\n\n            'If it were possible to get a GUID from a process ID then we could use GetActiveObject instead of this\n            app = GetComObjectFromProcessId(process.Id)\n        End While\n\n        t = DateTime.Now\n        While IsRunning(isMainWindowCreated:=True) = False\n            Threading.Thread.Sleep(1000)\n            If Math.Abs(DateTime.Now.Subtract(t).Seconds) &gt; createWindowTimeout Then Return Nothing\n        End While\n\n        If visible = False Then\n            Dim frame As Frame = app.Frame()\n            If frame Is Nothing Then Return app\n            Dim handle As IntPtr = frame.GetHWndx64()\n            If ShowWindow(handle, 0) Then Return app\n        End If\n\n        Return app\n    End Function<\/code><\/pre>\n<p>\u6211\u521a\u521a\u8fdb\u884c\u4e86\u6d4b\u8bd5\uff0c\u7528\u8d77\u6765\u6bd4\u8f83\u7b80\u5355\u3002\u6211\u7528\u4ee3\u7801\u65b0\u5efa\u4e86\u4e24\u4e2asolidworks 2018\u7a97\u53e3\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img-blog.csdnimg.cn\/20200417100120372.png\" class=\"aligncenter\"><\/p>\n<pre><code>\n        private SldWorks sldWorks2018_1 = null;\n        private SldWorks sldWorks2018_2 = null;\n\n        private void button1_Click(object sender, EventArgs e)\n        {\n            sldWorks2018_1 = PStandAlone.RunSolidWorks(26, true, false, false, 30, 15);\n        }\n\n        private void button2_Click(object sender, EventArgs e)\n        {\n            sldWorks2018_2 = PStandAlone.RunSolidWorks(26, true, false, false, 30, 15);\n        }\n\n        private void button3_Click(object sender, EventArgs e)\n        {\n            sldWorks2018_1.SendMsgToUser(\"From 2018 -1\");\n            sldWorks2018_2.SendMsgToUser(\"From 2018 -2\");\n        }\n\n      <\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/img-blog.csdnimg.cn\/20200417101009530.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3plbmdxaDAzMTQ=,size_16,color_FFFFFF,t_70\" class=\"aligncenter\"><\/p>\n<p>\u6e90\u7801<a href=\"https:\/\/gitee.com\/painezeng\/PSWStandalone\" target=\"_blank\" rel=\"nofollow noopener\">https:\/\/gitee.com\/painezeng\/PSWStandalone<\/a>(vb.net)<\/p>\n<p>          posted @<br \/>\n<span>2020-04-17 10:00<\/span>&nbsp;<br \/>\n<a href=\"https:\/\/www.cnblogs.com\/solidworksapi\/\" target=\"_blank\" rel=\"nofollow noopener\">painezeng<\/a>&nbsp; \u9605\u8bfb(<br \/>\n<span>250<\/span>)&nbsp; \u8bc4\u8bba(<br \/>\n<span>0<\/span>)&nbsp;<br \/>\n<a href=\"https:\/\/i.cnblogs.com\/EditPosts.aspx?postid=12897434\" rel=\"nofollow noopener\" target=\"_blank\">\u7f16\u8f91<\/a>&nbsp;<br \/>\n<a href=\"\" target=\"_blank\" rel=\"nofollow noopener\">\u6536\u85cf<\/a>&nbsp;<br \/>\n<a href=\"\" target=\"_blank\" rel=\"nofollow noopener\">\u4e3e\u62a5<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>C# SolidWorks \u4e8c\u6b21\u5f00\u53d1 API &#8212;Solidworks\u7684\u591a\u5f00\u64cd\u4f5c\u4e0e\u8fde\u63a5\u6307\u5b9a\u7248\u672cSol [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":577,"comment_status":"closed","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":[4,5],"tags":[7,6,8],"series":[],"class_list":["post-576","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-solidworks-","category-solidworks","tag-api","tag-solidworks","tag-8"],"views":172,"_links":{"self":[{"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=\/wp\/v2\/posts\/576","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\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=576"}],"version-history":[{"count":0,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=\/wp\/v2\/posts\/576\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=\/wp\/v2\/media\/577"}],"wp:attachment":[{"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=576"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fseries&post=576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}