{"id":594,"date":"2020-01-06T13:30:00","date_gmt":"2020-01-06T05:30:00","guid":{"rendered":"http:\/\/localhost:8080\/?p=594"},"modified":"2020-01-06T13:30:00","modified_gmt":"2020-01-06T05:30:00","slug":"c-solidworks-%e4%ba%8c%e6%ac%a1%e5%bc%80%e5%8f%91-api-%e6%8f%92%e5%85%a5%e5%9b%be%e5%9d%97%e5%92%8c%e5%b1%9e%e6%80%a7%e5%9d%97","status":"publish","type":"post","link":"https:\/\/blog.ke-zhi.com\/?p=594","title":{"rendered":"C# SolidWorks \u4e8c\u6b21\u5f00\u53d1 API &#8212; \u63d2\u5165\u56fe\u5757\u548c\u5c5e\u6027\u5757"},"content":{"rendered":"<h1> <a href=\"https:\/\/www.cnblogs.com\/solidworksapi\/p\/12897442.html\" target=\"_blank\" rel=\"nofollow noopener\"> <span>C# SolidWorks \u4e8c\u6b21\u5f00\u53d1 API &#8212; \u63d2\u5165\u56fe\u5757\u548c\u5c5e\u6027\u5757<\/span> <\/a> <\/h1>\n<p>\u5728\u8fd8\u6ca1\u6709\u53d6\u6d88\u6240\u67092D\u56fe\u7eb8\u65f6\uff0c\u6211\u4eec\u96be\u514d\u9700\u8981\u5728\u56fe\u7eb8\u4e2d\u63d2\u5165\u4e00\u4e9b\u6807\u51c6\u56fe\u5757\uff0c\u6216\u8005\u50cfAutoCAD\u90a3\u4e2a\u7684\u5c5e\u6027\u5757\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/img-blog.csdnimg.cn\/20200106132619649.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3plbmdxaDAzMTQ=,size_16,color_FFFFFF,t_70\"><\/p>\n<p>\u5982\u4e0b\u56fe\uff1a&nbsp;<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/img-blog.csdnimg.cn\/20200106132728149.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3plbmdxaDAzMTQ=,size_16,color_FFFFFF,t_70\">&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u76f4\u63a5\u4e0a\u4ee3\u7801\uff1a<\/p>\n<pre><code>private void btn_Insert_Block_Click(object sender, EventArgs e)\n        {\n            \/\/\u8fde\u63a5\u5230Solidworks\n            ISldWorks swApp = Utility.ConnectToSolidWorks();\n\n            ModelDoc2 swModel = (ModelDoc2)swApp.ActiveDoc;\n\n            DrawingDoc dc = (DrawingDoc)swModel;\n\n            \/\/ SelectionMgr selectionMgr = (SelectionMgr)swModel.SelectionManager;\n\n            double[] nPt = new double[3];\n\n            nPt[0] = 0;\n            nPt[1] = 0;\n            nPt[2] = 0;\n\n            MathUtility swMathUtil = swApp.GetMathUtility();\n\n            MathPoint swMathPoint = swMathUtil.CreatePoint(nPt);\n\n            double blockScale = 1;\n\n            string blockPath = @\"D:\\09_Study\\CSharpAndSolidWorks\\CSharpAndSolidWorks\\TemplateModel\\TestBlock.SLDBLK\";\n\n            \/\/\u63d2\u5165\u56fe\u5757\n            var swBlockInst = InsertBlockReturnInst(swApp, swModel, swMathPoint, blockPath, blockScale);\n\n            \/\/  swModel.SketchManager.MakeSketchBlockFromFile(mathPoint, blockPath, false, blockScale, 0);\n\n            \/\/\u4fee\u6539\u5757\u7684\u5c5e\u6027(\u5982\u679c\u53ea\u662f\u666e\u901a\u7684\u56fe\u5757\uff0c\u5219\u65e0\u9700\u8981\u8fd9\u4e00\u6b65\u3002\u76f4\u63a5\u4f7f\u7528\u4e0a\u9762\u7684\u4e00\u884c\u63d2\u5165\u56fe\u5757\u5373\u53ef)\n            swBlockInst.SetAttributeValue(\"Title1\", \"Paine\");\n        }\n\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u63d2\u5165\u5e76\u8fd4\u56de\u6700\u540e\u4e00\u4e2a\u56fe\u5757\u5b9e\u4f8b\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;param name=\"sldWorks\"&gt;&lt;\/param&gt;\n        \/\/\/ &lt;param name=\"modelDoc2\"&gt;&lt;\/param&gt;\n        \/\/\/ &lt;param name=\"mathPoint\"&gt;&lt;\/param&gt;\n        \/\/\/ &lt;param name=\"blockPath\"&gt;&lt;\/param&gt;\n        \/\/\/ &lt;param name=\"blockScale\"&gt;&lt;\/param&gt;\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\n        private SketchBlockInstance InsertBlockReturnInst(ISldWorks sldWorks, ModelDoc2 modelDoc2, MathPoint mathPoint, String blockPath, double blockScale)\n        {\n            SketchBlockInstance swBlockInst;\n            List&lt;String&gt; NowBlockName = new List&lt;String&gt;();\n            var swModel = modelDoc2;\n            Boolean boolstatus = swModel.Extension.SelectByID2(System.IO.Path.GetFileNameWithoutExtension(blockPath), \"SUBSKETCHDEF\", 0, 0, 0, false, 0, null, 0);\n\n            if (boolstatus == true)\n            {\n                Feature swFeat = swModel.SelectionManager.GetSelectedObject6(1, 0);\n                var swSketchBlockDef = swFeat.GetSpecificFeature2();\n\n                var nbrBlockInst = swSketchBlockDef.GetInstanceCount;\n\n                if (nbrBlockInst &gt; 0)\n                {\n                    var vBlockInst = swSketchBlockDef.GetInstances();\n\n                    for (int i = 0; i &lt; nbrBlockInst; i++)\n                    {\n                        swBlockInst = vBlockInst[i];\n\n                        NowBlockName.Add(swBlockInst.Name.ToString());\n                    }\n\n                    swModel.SketchManager.MakeSketchBlockFromFile(mathPoint, blockPath, false, blockScale, 0);\n\n                    swModel.ClearSelection2(true);\n\n                    boolstatus = swModel.Extension.SelectByID2(System.IO.Path.GetFileNameWithoutExtension(blockPath), \"SUBSKETCHDEF\", 0, 0, 0, false, 0, null, 0);\n\n                    swFeat = swModel.SelectionManager.GetSelectedObject6(1, 0);\n                    swSketchBlockDef = swFeat.GetSpecificFeature2();\n\n                    nbrBlockInst = swSketchBlockDef.GetInstanceCount;\n\n                    if (nbrBlockInst &gt; 0)\n                    {\n                        vBlockInst = swSketchBlockDef.GetInstances();\n\n                        for (int j = 0; j &lt; nbrBlockInst; j++)\n                        {\n                            swBlockInst = vBlockInst[j];\n                            if (!NowBlockName.Contains(swBlockInst.Name.ToString()))\n                            {\n                                swModel.Extension.SelectByID2(swBlockInst.Name, \"SUBSKETCHINST\", 0, 0, 0, false, 0, null, 0);\n                                swBlockInst = GetSketchBlockInstanceFromSelection();\n                                return swBlockInst;\n                            }\n                        }\n                    }\n                    return null;\n                }\n                else\n                {\n                    return null;\n                }\n            }\n            else\n            {\n                var swSketchBlockDef = swModel.SketchManager.MakeSketchBlockFromFile(mathPoint, blockPath, false, blockScale, 0);\n                swBlockInst = swSketchBlockDef.GetInstances()[0];\n\n                return swBlockInst;\n            }\n        }\n\n        private SketchBlockInstance GetSketchBlockInstanceFromSelection()\n        {\n            ISldWorks swApp = Utility.ConnectToSolidWorks();\n\n            ModelDoc2 swModel;\n            ModelDocExtension swModelDocExt;\n            SketchBlockInstance SketchBlockInstance;\n\n            DateTime time = DateTime.Now;\n\n            try\n            {\n                swModel = swApp.ActiveDoc;\n                swModelDocExt = swModel.Extension;\n\n                SelectionMgr swSelectionMgr;\n                swSelectionMgr = swModel.SelectionManager;\n\n                string SelectByString = \"\";\n                string ObjectType = \"\";\n                int type;\n                double x;\n                double y;\n                double z;\n\n                if (swSelectionMgr.GetSelectedObjectCount2(-1) &gt; 1)\n                {\n                    \/\/ Return only a SketchblockInstance when only one is selected...\n                    \/\/ modify if you want return more than one (or only the first) selected Sketchblockinstance\n                    return null;\n                }\n\n                swSelectionMgr.GetSelectionSpecification(1, out SelectByString, out ObjectType, out type, out x, out y, out z);\n                Debug.WriteLine(SelectByString + \" \" + ObjectType + \" \" + type);\n\n                if (type == (int)swSelectType_e.swSelSUBSKETCHINST)\n                {\n                    SketchBlockInstance = swSelectionMgr.GetSelectedObject6(1, -1);\n                    Debug.WriteLine(\"Found:\" + SketchBlockInstance.Name);\n                    return SketchBlockInstance;\n                }\n                else if (type == (int)swSelectType_e.swSelSKETCHSEGS | type == (int)swSelectType_e.swSelSKETCHPOINTS)\n                {\n                    \/\/ Show if a sketchblockinstance has the same name\n                    SketchManager SwSketchMgr;\n                    SwSketchMgr = swModel.SketchManager;\n\n                    object[] blockDefinitions = (object[])SwSketchMgr.GetSketchBlockDefinitions();\n                    foreach (SketchBlockDefinition blockDef in blockDefinitions)\n                    {\n                        foreach (SketchBlockInstance blockInstance in blockDef.GetInstances())\n                        {\n                            if (SelectByString.EndsWith(blockInstance.Name))\n                            {\n                                Debug.WriteLine(\"Found:\" + blockInstance.Name);\n                                return blockInstance;\n                            }\n                        }\n                    }\n                }\n                else\n                {\n                    return null;\n                }\n            }\n            catch (Exception ex)\n            {\n            }\n            finally\n            {\n                Debug.WriteLine(DateTime.Now.Subtract(time).Milliseconds);\n            }\n\n            return null;\n        }\n  <\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>          posted @<br \/>\n<span>2020-01-06 13:30<\/span>&nbsp;<br \/>\n<a href=\"https:\/\/www.cnblogs.com\/solidworksapi\/\" target=\"_blank\" rel=\"nofollow noopener\">painezeng<\/a>&nbsp; \u9605\u8bfb(<br \/>\n<span>225<\/span>)&nbsp; \u8bc4\u8bba(<br \/>\n<span>0<\/span>)&nbsp;<br \/>\n<a href=\"https:\/\/i.cnblogs.com\/EditPosts.aspx?postid=12897442\" 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\u56fe\u5757\u548c\u5c5e\u6027\u5757 \u5728\u8fd8\u6ca1\u6709\u53d6\u6d88\u6240\u67092D\u56fe\u7eb8\u65f6\uff0c\u6211 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":595,"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-594","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-solidworks-","category-solidworks","tag-api","tag-solidworks","tag-8"],"views":167,"_links":{"self":[{"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=\/wp\/v2\/posts\/594","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=594"}],"version-history":[{"count":0,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=\/wp\/v2\/posts\/594\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=\/wp\/v2\/media\/595"}],"wp:attachment":[{"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=594"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/blog.ke-zhi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fseries&post=594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}