{"id":22648,"date":"2025-11-08T20:04:20","date_gmt":"2025-11-08T12:04:20","guid":{"rendered":"https:\/\/blog.iyatt.com\/?p=22648"},"modified":"2025-11-08T20:04:20","modified_gmt":"2025-11-08T12:04:20","slug":"siemens-nx%ef%bc%88ug%ef%bc%89python-%e5%bc%80%e5%8f%91-%e9%81%8d%e5%8e%86%e8%a3%85%e9%85%8d%e4%bd%93%e7%bb%84%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/blog.iyatt.com\/?p=22648","title":{"rendered":"Siemens NX\uff08UG\uff09Python \u5f00\u53d1 \u2013 \u904d\u5386\u88c5\u914d\u4f53\u7ec4\u4ef6"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title ez-toc-toggle\" style=\"cursor:pointer\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.iyatt.com\/?p=22648\/#%E7%8E%AF%E5%A2%83\" >\u73af\u5883<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.iyatt.com\/?p=22648\/#%E5%AE%9E%E7%8E%B0\" >\u5b9e\u73b0<\/a><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E7%8E%AF%E5%A2%83\"><\/span>\u73af\u5883<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<ul>\n<li>Windows 11 25H2<\/li>\n<li>NX 2506<\/li>\n<li>\u5185\u7f6e Python 3.12.8<\/li>\n<\/ul>\n<h1><span class=\"ez-toc-section\" id=\"%E5%AE%9E%E7%8E%B0\"><\/span>\u5b9e\u73b0<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<pre><code class=\"language-python\">import NXOpen\nfrom NXOpen.Assemblies import Component\n\ndef processComponentRecursive(component: Component, lw: NXOpen.ListingWindow, prefix: str = &#039;&#039;):\n    &quot;&quot;&quot;\u9012\u5f52\u5904\u7406\u7ec4\u4ef6\u53ca\u5176\u5b50\u7ec4\u4ef6\n\n    Args:\n        component (Component): \u5f53\u524d\u5904\u7406\u7684\u7ec4\u4ef6\n        lw (NXOpen.ListingWindow): \u5217\u8868\u7a97\u53e3\uff0c\u7528\u4e8e\u8f93\u51fa\u4fe1\u606f\n        prefix (str, optional): \u524d\u7f00\uff0c\u7528\u4e8e\u6253\u5370\u7ec4\u4ef6\u7684\u5c42\u7ea7\u5173\u7cfb. \n    &quot;&quot;&quot;\n    try:\n        part = component.Prototype # \u83b7\u53d6\u7ec4\u4ef6\u7684\u539f\u59cb\u96f6\u4ef6\n        if part is not None:\n            # \u8fd9\u91cc\u653e\u5904\u7406\u6bcf\u4e2a\u7ec4\u4ef6\u7684\u903b\u8f91\n            # \u793a\u4f8b\u5c31\u6253\u5370\u7ec4\u4ef6\u540d\u79f0\n            lw.WriteLine(f&#039;{prefix} {part.Name}&#039;)\n    except Exception as e:\n        lw.WriteLine(f&#039;\u5904\u7406\u7ec4\u4ef6 {component.Name} \u65f6\u53d1\u751f\u9519\u8bef: {e}&#039;)\n\n    for child in component.GetChildren():\n        processComponentRecursive(child, lw, &#039;-&#039; + prefix)\n\ndef main():\n    session = NXOpen.Session.GetSession()\n    lw = session.ListingWindow\n    lw.Open()\n\n    # \u83b7\u53d6\u5f53\u524d\u663e\u793a\u7684\u96f6\u4ef6(\u5728\u88c5\u914d\u4f53\u4e2d\u5c31\u662f\u6700\u9876\u5c42\u7684\u88c5\u914d\u4f53\u96f6\u4ef6)\n    displayPart = session.Parts.Display\n    if displayPart is None:\n        lw.WriteLine(&#039;\u6ca1\u6709\u6253\u5f00\u7684\u96f6\u4ef6&#039;)\n        return\n\n    # \u83b7\u53d6\u6839\u7ec4\u4ef6\n    rootComponent = displayPart.ComponentAssembly.RootComponent\n    if rootComponent is None:\n        lw.WriteLine(f&#039;\u5f53\u524d\u96f6\u4ef6 {rootComponent.Name} \u4e0d\u662f\u88c5\u914d\u4f53&#039;)\n    else:\n        processComponentRecursive(rootComponent, lw)\n\nif __name__ == &#039;__main__&#039;:\n    main()<\/code><\/pre>\n<p>\u8fd9\u662f\u521b\u5efa\u7684\u88c5\u914d\u4f53<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2025\/11\/image-1762603221967.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 254px; --smush-placeholder-aspect-ratio: 254\/176;\" \/><\/p>\n<p>\u6267\u884c\u4ee3\u7801\u4f1a\u6253\u5370\u51fa\u7ec4\u4ef6\uff0c\u6bcf\u4e0b\u4e00\u5c42\u7ec4\u4ef6\u589e\u52a0\u4e00\u4e2a\u524d\u7f00\u201c-\u201d\uff0c\u4fbf\u4e8e\u4f53\u73b0\u5c42\u7ea7\u5173\u7cfb\u3002\u5982\u679c\u8981\u6267\u884c\u5176\u5b83\u64cd\u4f5c\uff0c\u53ef\u4ee5\u628a\u6253\u5370\u540d\u79f0\u6362\u6210\u9700\u8981\u7684\u5b9e\u73b0<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2025\/11\/image-1762603254850.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 352px; --smush-placeholder-aspect-ratio: 352\/187;\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u73af\u5883 Windows 11 25H2 NX 2506 \u5185\u7f6e Python 3.12.8 \u5b9e\u73b0 import N [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"zakra_page_container_layout":"customizer","zakra_page_sidebar_layout":"customizer","zakra_remove_content_margin":false,"zakra_sidebar":"customizer","zakra_transparent_header":"customizer","zakra_logo":0,"zakra_main_header_style":"default","zakra_menu_item_color":"","zakra_menu_item_hover_color":"","zakra_menu_item_active_color":"","zakra_menu_active_style":"","zakra_page_header":true,"_lmt_disableupdate":"no","_lmt_disable":"no","footnotes":""},"categories":[1,618],"tags":[909,1297,1860,1858,1857,1859],"class_list":["post-22648","post","type-post","status-publish","format-standard","hentry","category-all","category-cad-cam","tag-python","tag-siemens-nx","tag-1860","tag-1858","tag-1857","tag-1859"],"modified_by":"IYATT-yx","_links":{"self":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/22648","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=22648"}],"version-history":[{"count":2,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/22648\/revisions"}],"predecessor-version":[{"id":22652,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/22648\/revisions\/22652"}],"wp:attachment":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=22648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=22648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=22648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}