{"id":584,"date":"2020-02-28T18:08:00","date_gmt":"2020-02-28T10:08:00","guid":{"rendered":"http:\/\/localhost:8080\/?p=584"},"modified":"2020-02-28T18:08:00","modified_gmt":"2020-02-28T10:08:00","slug":"c-solidworks-%e4%ba%8c%e6%ac%a1%e5%bc%80%e5%8f%91-api-%e4%bf%ae%e6%94%b9%e5%85%a8%e5%b1%80%e5%8f%98%e9%87%8f%e7%9a%84%e5%80%bc","status":"publish","type":"post","link":"https:\/\/blog.ke-zhi.com\/?p=584","title":{"rendered":"C# SolidWorks \u4e8c\u6b21\u5f00\u53d1 API &#8212; \u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u503c"},"content":{"rendered":"<h1> <a href=\"https:\/\/www.cnblogs.com\/solidworksapi\/p\/12897438.html\" target=\"_blank\" rel=\"nofollow noopener\"> <span>C# SolidWorks \u4e8c\u6b21\u5f00\u53d1 API &#8212; \u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u503c<\/span> <\/a> <\/h1>\n<p>\u4eca\u5929\u6765\u7b80\u5355\u8bb2\u4e00\u4e0b\u5982\u4f55\u4fee\u6539\u65b9\u7a0b\u5f0f\u4e2d\u7684\u4e00\u4e9b\u6570\u636e,\u6709\u65f6\u5019\u4e00\u4e9b\u7b80\u5355\u7684\u6a21\u578b\u6211\u4eec\u5c31\u53ef\u4ee5\u5229\u7528\u8fd9\u4e2a\u5168\u5c40\u53d8\u91cf\u6765\u63a7\u5236\u6a21\u578b.<\/p>\n<p>\u5982\u4e0b\u56fe:&nbsp;\u6211\u8bbe\u5b9a\u4e86\u96f6\u4ef6\u7684\u9ad8\u5ea6\u65b9\u7a0b\u5f0f\u4e0e\u5168\u5c40\u53d8\u91cfh\u76f8\u7b49.<\/p>\n<p>\u7b49\u5230\u6211\u4eec\u9700\u8981\u66f4\u65b0\u9ad8\u5ea6\u65f6,\u5c31\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539\u8fd9\u4e2a\u5168\u5c40\u53d8\u91cf,\u505a\u5230\u66f4\u7b80\u5355\u7684\u53c2\u6570\u5316\u65b9\u5f0f,\u8fd9\u4e2a\u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u662f\u4e0a\u7ea7\u88c5\u914d\u4f53\u4e2d\u7684\u4fe1\u606f.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img-blog.csdnimg.cn\/20200228163309208.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3plbmdxaDAzMTQ=,size_16,color_FFFFFF,t_70\" class=\"aligncenter\"><\/p>\n<p>&nbsp;\u4e0b\u9762\u6765\u5177\u4f53\u6f14\u793a\u4e0b\u5982\u4f55\u627eapi:<\/p>\n<p>\u6253\u5f00api\u5e2e\u52a9\u6587\u4ef6,\u641c\u7d22\u5173\u952e\u5b57&nbsp;globalvariable,\u5c31\u4f1a\u53d1\u73b0\u53f3\u4fa7\u7684\u5b9e\u4f8b,\u662f\u4e00\u4e2a\u83b7\u53d6\u4fe1\u606f\u7684.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img-blog.csdnimg.cn\/20200228164217740.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3plbmdxaDAzMTQ=,size_16,color_FFFFFF,t_70\" class=\"aligncenter\"><\/p>\n<pre><code>This example shows how to get the values of equations.\n\n\/\/-----------------------------------------\n\/\/ Preconditions:\n\/\/ 1. Open public_documents\\samples\\tutorial\\api\\partequations.sldprt.\n\/\/ 2. Open the Immediate window.\n\/\/\n\/\/ Postconditions:\n\/\/ 1. Gets each equation's value and index and whether the \n\/\/    equation is a global variable. \n\/\/ 2. Examine the Immediate window.\n\/\/------------------------------------------\nusing SolidWorks.Interop.sldworks;\nusing SolidWorks.Interop.swconst;\nusing System.Runtime.InteropServices;\nusing System;\nusing System.Diagnostics;\n \nnamespace Macro1CSharp.csproj\n{\n    public partial class SolidWorksMacro\n    {\n        public void Main()\n        {\n            ModelDoc2 swModel = default(ModelDoc2);\n            EquationMgr swEqnMgr = default(EquationMgr);\n            int i = 0;\n            int nCount = 0;\n \n            swModel = (ModelDoc2)swApp.ActiveDoc;\n            swEqnMgr = (EquationMgr)swModel.GetEquationMgr();\n            Debug.Print(\"File = \" + swModel.GetPathName());\n            nCount = swEqnMgr.GetCount();\n            for (i = 0; i &lt; nCount; i++)\n            {\n                Debug.Print(\"  Equation(\" + i + \")  = \" + swEqnMgr.get_Equation(i));\n                Debug.Print(\"    Value = \" + swEqnMgr.get_Value(i));\n                Debug.Print(\"    Index = \" + swEqnMgr.Status);\n                Debug.Print(\"    Global variable? \" + swEqnMgr.get_GlobalVariable(i));\n            }\n \n        }\n \n        \/\/\/ &lt;summary&gt;\n        \/\/\/  The SldWorks swApp variable is pre-assigned for you.\n        \/\/\/ &lt;\/summary&gt;\n        public SldWorks swApp;\n    }\n}\n<\/code><\/pre>\n<p>\u6211\u4eec\u53c2\u8003\u8fd9\u6bb5\u4ee3\u7801\u6765\u5b8c\u6210\u65b9\u7a0b\u5f0f\u4fe1\u606f\u7684\u8bfb\u53d6:&nbsp;\u589e\u52a0\u6309\u94ae\u5199\u4ee3\u7801.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img-blog.csdnimg.cn\/20200228164500995.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3plbmdxaDAzMTQ=,size_16,color_FFFFFF,t_70\" class=\"aligncenter\"><\/p>\n<p>&nbsp;\u53c2\u7167\u5e2e\u52a9\u6587\u4ef6\u5199\u5b8c\u7684\u7248\u672c\u5927\u6982\u4e3a:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img-blog.csdnimg.cn\/20200228165657240.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3plbmdxaDAzMTQ=,size_16,color_FFFFFF,t_70\" class=\"aligncenter\"><\/p>\n<p>\u8fd0\u884c\u4e00\u4e0b:<\/p>\n<p>&nbsp;<img decoding=\"async\" src=\"https:\/\/img-blog.csdnimg.cn\/20200228170050798.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3plbmdxaDAzMTQ=,size_16,color_FFFFFF,t_70\" class=\"aligncenter\"><\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u505a\u4fee\u6539:<\/p>\n<pre><code>private void butGlobalVariables_Click(object sender, EventArgs e)\n        {\n            \/\/\u8fde\u63a5solidworks\n            ISldWorks swApp = Utility.ConnectToSolidWorks();\n\n            if (swApp != null)\n            {\n                \/\/\u83b7\u53d6\u5f53\u524d\u6a21\u578b\n                ModelDoc2 swModel = (ModelDoc2)swApp.ActiveDoc;\n                \/\/\u5b9a\u4e49\u65b9\u7a0b\u5f0f\u7ba1\u7406\u5668\n                EquationMgr swEqnMgr = default(EquationMgr);\n\n                int i = 0;\n                int nCount = 0;\n\n                if (swModel != null)\n                {\n                    swEqnMgr = (EquationMgr)swModel.GetEquationMgr();\n                    \/\/ nCount = swEqnMgr.GetCount();\n                    \/\/for (i = 0; i &lt; nCount; i++)\n                    \/\/{\n                    \/\/    Debug.Print(\"  Equation(\" + i + \")  = \" + swEqnMgr.get_Equation(i));\n                    \/\/    Debug.Print(\"    Value = \" + swEqnMgr.get_Value(i));\n                    \/\/    Debug.Print(\"    Index = \" + swEqnMgr.Status);\n                    \/\/    Debug.Print(\"    Global variable? \" + swEqnMgr.get_GlobalVariable(i));\n                    \/\/}\n\n                    \/\/\u4fee\u6539\u9ad8\u5ea6\u4e3a60\n\n                    if (SetEquationValue(swEqnMgr, \"h\", 60))\n                    {\n                        swModel.ForceRebuild3(true);\n                    }\n                    else\n                    {\n                        MessageBox.Show(\"\u6ca1\u6709\u627e\u5230\u8fd9\u4e2a\u503c!\");\n                    }\n                }\n            }\n        }\n\n        #region \u4fee\u6539\u5168\u5c40\u53d8\u91cf\u6240\u7528\u5230\u7684\u65b9\u6cd5\n\n        public bool SetEquationValue(EquationMgr eqMgr, string name, double newValue)\n        {\n            int index = GetEquationIndexByName(eqMgr, name);\n\n            if (index != -1)\n            {\n                eqMgr.Equation[index] = \"\\\"\" + name + \"\\\"=\" + newValue;\n\n                return true;\n            }\n            else\n            {\n                return false;\n            }\n        }\n\n        \/\/\u901a\u8fc7\u540d\u5b57\u627e\u65b9\u7a0b\u5f0f\u7684\u4f4d\u7f6e\n        private int GetEquationIndexByName(EquationMgr eqMgr, string name)\n        {\n            int i;\n            for (i = 0; i &lt;= eqMgr.GetCount() - 1; i++)\n            {\n                var eqName = eqMgr.Equation[i].Split('=')[0].Replace(\"=\", \"\");\n\n                eqName = eqName.Substring(1, eqName.Length - 2); \/\/ removing the \"\" symbols from the name\n\n                if (eqName.ToUpper() == name.ToUpper())\n                {\n                    return i;\n                }\n            }\n\n            return -1;\n        }\n\n        #endregion \u4fee\u6539\u5168\u5c40\u53d8\u91cf\u6240\u7528\u5230\u7684\u65b9\u6cd5<\/code><\/pre>\n<p>\u5230\u6b64,\u96f6\u4ef6\u9ad8\u5ea6\u53d8\u6210\u4e8660,&nbsp;\u8fd9\u4e00\u8282\u5148\u8bb2\u8fd9\u4e48\u591a\u5427.<\/p>\n<p>\u6700\u7ec8\u4ee3\u7801\u8bf7\u5230\u7801\u4e91\u6216\u8005github\u4e0a\u4e0b\u8f7d.<\/p>\n<p>          posted @<br \/>\n<span>2020-02-28 18:08<\/span>&nbsp;<br \/>\n<a href=\"https:\/\/www.cnblogs.com\/solidworksapi\/\" target=\"_blank\" rel=\"nofollow noopener\">painezeng<\/a>&nbsp; \u9605\u8bfb(<br \/>\n<span>284<\/span>)&nbsp; \u8bc4\u8bba(<br \/>\n<span>0<\/span>)&nbsp;<br \/>\n<a href=\"https:\/\/i.cnblogs.com\/EditPosts.aspx?postid=12897438\" 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; \u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u503c \u4eca\u5929\u6765\u7b80\u5355\u8bb2\u4e00\u4e0b\u5982\u4f55\u4fee\u6539\u65b9\u7a0b\u5f0f [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":585,"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-584","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-solidworks-","category-solidworks","tag-api","tag-solidworks","tag-8"],"views":159,"_links":{"self":[{"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=\/wp\/v2\/posts\/584","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=584"}],"version-history":[{"count":0,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=\/wp\/v2\/posts\/584\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=\/wp\/v2\/media\/585"}],"wp:attachment":[{"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=584"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fseries&post=584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}