{"id":655,"date":"2019-09-25T20:07:00","date_gmt":"2019-09-25T12:07:00","guid":{"rendered":"http:\/\/localhost:8080\/?p=655"},"modified":"2019-09-25T20:07:00","modified_gmt":"2019-09-25T12:07:00","slug":"c-solidworks-%e4%ba%8c%e6%ac%a1%e5%bc%80%e5%8f%91-api-%e6%8f%92%e5%85%a5%e5%ba%93%e7%89%b9%e5%be%81","status":"publish","type":"post","link":"https:\/\/blog.ke-zhi.com\/?p=655","title":{"rendered":"C# SolidWorks \u4e8c\u6b21\u5f00\u53d1 API&#8212;\u63d2\u5165\u5e93\u7279\u5f81"},"content":{"rendered":"<h1> <a href=\"https:\/\/www.cnblogs.com\/solidworksapi\/p\/12897477.html\" target=\"_blank\" rel=\"nofollow noopener\"> <span>C# SolidWorks \u4e8c\u6b21\u5f00\u53d1 API&#8212;\u63d2\u5165\u5e93\u7279\u5f81<\/span> <\/a> <\/h1>\n<p>\u8981\u60f3\u63d0\u9ad8\u8bbe\u8ba1\u6548\u7387\uff0c\u505a\u5e93\u7279\u5f81\u662f\u5f88\u5e38\u89c1\u7684\u3002\u4f8b\u5982\u6211\u4eec\u4e0b\u9762\u8fd9\u4e2a\u89c6\u9891\uff0c\u5229\u7528\u5e93\u7279\u5f81\u5728\u6253\u4e86\u5f88\u591a\u5c0f\u5b54\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img-blog.csdnimg.cn\/20190925200535173.gif\" class=\"aligncenter\">  \u5229\u7528\u5e93\u7279\u5f81\u5feb\u901f\u63d2\u5165\u7279\u5f81   <\/p>\n<p>\u8fd9\u4e00\u6b21\u6211\u4eec\u6765\u770b\u5982\u4f55\u7528\u4ee3\u7801\u63d2\u5165\u5e93\u7279\u5f81\u3002<\/p>\n<p>\u6211\u4eec\u4ee5API\u5b9e\u4f8b\u4e2d\u7684\u4e00\u4e2a\u4f8b\u5b50\u6765\u8bf4\u660e\u4e00\u4e0b\u8fc7\u7a0b\uff1a<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/img-blog.csdnimg.cn\/20190925195339791.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3plbmdxaDAzMTQ=,size_16,color_FFFFFF,t_70\"><\/p>\n<pre><code>        public DispatchWrapper[] ObjectArrayToDispatchWrapperArray(object[] Objects)\n        {\n            int ArraySize = 0;\n            ArraySize = Objects.GetUpperBound(0);\n            DispatchWrapper[] d = new DispatchWrapper[ArraySize + 1];\n            int ArrayIndex = 0;\n            for (ArrayIndex = 0; ArrayIndex &lt;= ArraySize; ArrayIndex++)\n            {\n                d[ArrayIndex] = new DispatchWrapper(Objects[ArrayIndex]);\n            }\n            return d;\n        }\n\n        public DispatchWrapper[] LibRefs;\n\n        private void btnInsertLibF_Click(object sender, EventArgs e)\n        {\n            ISldWorks swApp = Utility.ConnectToSolidWorks();\n\n            \/\/\u53ef\u4ee5\u53c2\u8003API\u5e2e\u52a9\u4e2d\u7684:Create Library Feature Data Object and Library Feature With References Example (C#)\n\n            \/\/Step1:\u65b0\u5efa\u4e00\u4e2a\u96f6\u4ef6.\n            Feature swFeature = default(Feature);\n            ModelDoc2 swModel = default(ModelDoc2);\n            ModelDocExtension swModelDocExt = default(ModelDocExtension);\n            SketchManager swSketchManager = default(SketchManager);\n            SelectionMgr swSelectionManager = default(SelectionMgr);\n            FeatureManager swFeatureManager = default(FeatureManager);\n            LibraryFeatureData swLibFeat = default(LibraryFeatureData);\n            bool status = false;\n            object[] sketchLines = null;\n            object Refs = null;\n            object RefTypes = null;\n            int RefCount = 0;\n            int k = 0;\n            int i = 0;\n            DispatchWrapper[] LibRefs = null;\n\n            string libPath = \"C:\\\\ProgramData\\\\SOLIDWORKS\\\\SOLIDWORKS 2018\\\\design library\\\\features\\\\metric\\\\slots\\\\straight slot.sldlfp\";\n\n            \/\/ Create part\n            swModel = (ModelDoc2)swApp.NewDocument(\"C:\\\\ProgramData\\\\SolidWorks\\\\SOLIDWORKS 2018\\\\templates\\\\Part.prtdot\", 0, 0, 0);\n            swModelDocExt = (ModelDocExtension)swModel.Extension;\n            status = swModelDocExt.SelectByID2(\"Top Plane\", \"PLANE\", 0, 0, 0, false, 0, null, 0);\n            swModel.ClearSelection2(true);\n            status = swModelDocExt.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swSketchAddConstToRectEntity, (int)swUserPreferenceOption_e.swDetailingNoOptionSpecified, false);\n            status = swModelDocExt.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swSketchAddConstLineDiagonalType, (int)swUserPreferenceOption_e.swDetailingNoOptionSpecified, true);\n            swSketchManager = (SketchManager)swModel.SketchManager;\n            sketchLines = (object[])swSketchManager.CreateCornerRectangle(0, 0, 0, 1, 0.5, 0);\n            swModel.ShowNamedView2(\"*Trimetric\", 8);\n            swModel.ClearSelection2(true);\n            status = swModelDocExt.SelectByID2(\"Line2\", \"SKETCHSEGMENT\", 0, 0, 0, false, 0, null, 0);\n            status = swModelDocExt.SelectByID2(\"Line1\", \"SKETCHSEGMENT\", 0, 0, 0, true, 0, null, 0);\n            status = swModelDocExt.SelectByID2(\"Line4\", \"SKETCHSEGMENT\", 0, 0, 0, true, 0, null, 0);\n            status = swModelDocExt.SelectByID2(\"Line3\", \"SKETCHSEGMENT\", 0, 0, 0, true, 0, null, 0);\n            swFeatureManager = (FeatureManager)swModel.FeatureManager;\n            swFeature = (Feature)swFeatureManager.FeatureExtrusion2(true, false, false, 0, 0, 0.01, 0.01, false, false, false,\n            false, 0.0174532925199433, 0.0174532925199433, false, false, false, false, true, true, true,\n            0, 0, false);\n            swSelectionManager = (SelectionMgr)swModel.SelectionManager;\n            swSelectionManager.EnableContourSelection = false;\n\n            swModel = (ModelDoc2)swApp.ActiveDoc;\n\n            string actName = swModel.GetPathName();\n\n            #region \u7b2c\u4e00\u79cd\u65b9\u6cd5\n\n            \/\/Step2:\u521d\u59cb\u5316\u5e93\u7279\u5f81\n            swLibFeat = (LibraryFeatureData)swFeatureManager.CreateDefinition((int)swFeatureNameID_e.swFmLibraryFeature);\n            status = swLibFeat.Initialize(libPath);\n\n            \/\/step3:\u83b7\u53d6\u5e93\u7279\u5f81\u9700\u8981\u7684\u53c2\u8003\u5bf9\u8c61\n            RefCount = swLibFeat.GetReferencesCount();\n            Refs = (object[])swLibFeat.GetReferences2((int)swLibFeatureData_e.swLibFeatureData_FeatureRespect, out RefTypes);\n\n            if ((RefTypes != null))\n            {\n                Debug.Print(\"Types of references required (edge = 1): \");\n                int[] RefType = (int[])RefTypes;\n                for (k = RefType.GetLowerBound(0); k &lt;= RefType.GetUpperBound(0); k++)\n                {\n                    Debug.Print(\"    \" + RefType[k].ToString());\n                }\n            }\n            \/\/setp4:\u8bbe\u5b9a\u5e93\u7279\u5f81\u9ed8\u8ba4\u7684\u914d\u7f6e\u540d\u79f0\n            swLibFeat.ConfigurationName = \"Default\";\n            \/\/setp5:\u9009\u62e9\u4e00\u4e2a\u9762,\u5e76\u63d2\u5165\u5e93\u7279\u5f81\n            status = swModelDocExt.SelectByID2(\"\", \"FACE\", 0.522458766456054, 0.288038964184011, 0.00999999999987722, false, 0, null, 0);\n            swFeature = (Feature)swFeatureManager.CreateFeature(swLibFeat);\n            \/\/step6:\n            swLibFeat = null;\n            swLibFeat = (LibraryFeatureData)swFeature.GetDefinition();\n            status = swLibFeat.AccessSelections(swModel, null);\n\n            \/\/step7:\u9009\u62e9\u771f\u5b9e\u7684\u53c2\u8003\n            status = swModelDocExt.SelectByID2(\"\", \"EDGE\", 0.960865149149924, 0.497807163546383, 0.0131011390528215, true, 0, null, 0);\n            status = swModelDocExt.SelectByID2(\"\", \"EDGE\", 0.99866860703213, 0.481385806014544, 0.0113313929676906, true, 0, null, 0);\n            int selCount = 0;\n            selCount = swSelectionManager.GetSelectedObjectCount2(-1);\n\n            object[] selectedObjects = new object[selCount];\n\n            for (i = 0; i &lt; selCount; i++)\n            {\n                object selectedObject = null;\n                selectedObject = (object)swSelectionManager.GetSelectedObject6(i + 1, -1);\n                selectedObjects[i] = selectedObject;\n            }\n\n            \/\/ \u8f6c\u6362\u5bf9\u8c61\n            LibRefs = (DispatchWrapper[])ObjectArrayToDispatchWrapperArray((selectedObjects));\n\n            \/\/ \u8bbe\u5b9a\u5f15\u7528\u5173\u7cfb\u5230\u521a\u751f\u6210\u7684\u5e93\u7279\u5f81\n            swLibFeat.SetReferences(LibRefs);\n\n            \/\/ \u66f4\u65b0\u5e93\u529f\u80fd\n            status = swFeature.ModifyDefinition(swLibFeat, swModel, null);\n\n            \/\/ \u53d6\u6d88\u6291\u5236\u5e93\u529f\u80fd\n            status = swModelDocExt.SelectByID2(\"straight slot&lt;1&gt;\", \"BODYFEATURE\", 0, 0, 0, false, 0, null, 0);\n            swModel.EditUnsuppress2();\n\n            swModel.ClearSelection2(true);\n\n            #endregion \u7b2c\u4e00\u79cd\u65b9\u6cd5\n\n            #region \u7b2c\u4e8c\u79cd\u65b9\u6cd5(\u5df2\u8fc7\u65f6)\n\n            \/\/\/\/\u5148\u9009\u4e2d\u7ebf,\u518d\u63d2\u5165\u5e93\u7279\u5f81.\n\n            \/\/\/\/\u8981\u5148\u6253\u5f00\u5e93\u7279\u5f81,\u7136\u540e\u5207\u6362\u5230\u5f53\u524d\u96f6\u4ef6,\u9009\u4e2d\u53c2\u8003\u7279\u5f81,\u6700\u540e\u63d2\u5165\u7279\u5f81\u5e93\n\n            \/\/int errors = 0;\n            \/\/int warnings = 0;\n\n            \/\/swApp.OpenDoc6(libPath, 1, 0, \"\", errors, warnings);\n\n            \/\/swModel = swApp.ActivateDoc2(actName, true, errors);\n\n            \/\/status = swModelDocExt.SelectByID2(\"\", \"FACE\", 0.522458766456054, 0.288038964184011, 9.99999999987722E-03, false, 0, null, 0);\n            \/\/status = swModelDocExt.SelectByID2(\"\", \"EDGE\", 0.960865149149924, 0.497807163546383, 0.0131011390528215, true, 1, null, 0);\n            \/\/status = swModelDocExt.SelectByID2(\"\", \"EDGE\", 0.99866860703213, 0.481385806014544, 0.0113313929676906, true, 2, null, 0);\n\n            \/\/swModel.InsertLibraryFeature(libPath);\n\n            #endregion \u7b2c\u4e8c\u79cd\u65b9\u6cd5(\u5df2\u8fc7\u65f6)\n        }\n  <\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>          posted @<br \/>\n<span>2019-09-25 20:07<\/span>&nbsp;<br \/>\n<a href=\"https:\/\/www.cnblogs.com\/solidworksapi\/\" target=\"_blank\" rel=\"nofollow noopener\">painezeng<\/a>&nbsp; \u9605\u8bfb(<br \/>\n<span>210<\/span>)&nbsp; \u8bc4\u8bba(<br \/>\n<span>0<\/span>)&nbsp;<br \/>\n<a href=\"https:\/\/i.cnblogs.com\/EditPosts.aspx?postid=12897477\" 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;\u63d2\u5165\u5e93\u7279\u5f81 \u8981\u60f3\u63d0\u9ad8\u8bbe\u8ba1\u6548\u7387\uff0c\u505a\u5e93\u7279\u5f81\u662f\u5f88\u5e38\u89c1\u7684\u3002\u4f8b [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":656,"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-655","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-solidworks-","category-solidworks","tag-api","tag-solidworks","tag-8"],"views":176,"_links":{"self":[{"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=\/wp\/v2\/posts\/655","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=655"}],"version-history":[{"count":0,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=\/wp\/v2\/posts\/655\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=\/wp\/v2\/media\/656"}],"wp:attachment":[{"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=655"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fseries&post=655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}