{"id":22610,"date":"2025-11-06T21:59:50","date_gmt":"2025-11-06T13:59:50","guid":{"rendered":"https:\/\/blog.iyatt.com\/?p=22610"},"modified":"2025-11-06T21:59:51","modified_gmt":"2025-11-06T13:59:51","slug":"siemens-nx%ef%bc%88ug%ef%bc%89python-%e5%bc%80%e5%8f%91-%e5%9b%be%e7%ba%b8%e6%89%93%e5%8d%b0%ef%bc%88%e7%a0%94%e7%a9%b6%e4%b8%ad%ef%bc%89","status":"publish","type":"post","link":"https:\/\/blog.iyatt.com\/?p=22610","title":{"rendered":"Siemens NX\uff08UG\uff09Python \u5f00\u53d1 \u2013 \u56fe\u7eb8\u6253\u5370\uff08\u7814\u7a76\u4e2d\uff09"},"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=22610\/#%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=22610\/#%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<p>\u7b97\u662f\u534a\u6210\u54c1\uff0c\u8fd8\u6ca1\u7814\u7a76\u901a\u900f\uff0c\u4e0d\u77e5\u9053\u4e3a\u5565\u8bbe\u7f6e\u7684\u6253\u5370\u7eb8\u5f20\u5927\u5c0f\u548c\u65b9\u5411\u5728\u5b9e\u9645\u6253\u5370\u4e2d\u65e0\u6548<\/p>\n<pre><code class=\"language-python\">import NXOpen\nfrom NXOpen.Drawings import DraftingDrawingSheet\n\ndef getPrintFormatAndOrientation(sheet: DraftingDrawingSheet) -&gt; tuple[NXOpen.PrintBuilder.PaperSize, NXOpen.PrintBuilder.OrientationOption]:\n    &quot;&quot;&quot;\n    \u6839\u636e\u56fe\u7eb8\u5c3a\u5bf8\u5224\u65ad\u6253\u5370\u6240\u9700\u7684\u7eb8\u5f20\u5927\u5c0f\u548c\u65b9\u5411\n\n    Args:\n        sheet (DraftingDrawingSheet): \u56fe\u7eb8\u5bf9\u8c61\n\n    Returns:\n        str: tuple(PaperSize, Orientation)\n    &quot;&quot;&quot;\n    length = sheet.Length\n    height = sheet.Height\n    units = sheet.Units\n\n    # \u5982\u679c\u662f\u82f1\u5236\u5355\u4f4d\u5219\u5c06\u5c3a\u5bf8\u8f6c\u4e3a\u516c\u5236\n    if units == DraftingDrawingSheet.Unit.Inches:\n        length *= 25.4\n        height *= 25.4\n\n    # \u5224\u65ad\u7eb8\u5f20\u65b9\u5411\n    if height &gt; length:\n        orientation = NXOpen.PrintBuilder.OrientationOption.Portrait\n    else:\n        orientation = NXOpen.PrintBuilder.OrientationOption.Landscape\n\n    # \u5224\u65ad\u7eb8\u5f20\u5927\u5c0f\n    maxDim = max(length, height)\n    minDim = min(length, height)\n    if maxDim &lt;= 297 and minDim &lt;= 210:\n        paperSize = NXOpen.PrintBuilder.PaperSize.A4\n    else:\n        paperSize = NXOpen.PrintBuilder.PaperSize.A3\n\n    return paperSize, orientation\n\ndef printSheet(printBuilder: NXOpen.PrintBuilder, sheet: DraftingDrawingSheet, paperSize: NXOpen.PrintBuilder.PaperSize, orientation: NXOpen.PrintBuilder.OrientationOption, printerName: str, copies: int = 1):\n    &quot;&quot;&quot;\n    \u6253\u5370\u56fe\u7eb8\n\n    Args:\n        printBuilder (NXOpen.PrintBuilder): \u6253\u5370\u6784\u5efa\u5668\n        sheet (DraftingDrawingSheet): \u56fe\u7eb8\u5bf9\u8c61\n        paperSize (NXOpen.PrintBuilder.PaperSize): \u7eb8\u5f20\u5927\u5c0f\n        orientation (NXOpen.PrintBuilder.OrientationOption): \u7eb8\u5f20\u65b9\u5411\n        printerName (str): \u6253\u5370\u673a\u540d\u79f0\n        copies (int, optional): \u6253\u5370\u4efd\u6570. \u9ed8\u8ba4 1 \u4efd.\n    &quot;&quot;&quot;\n    printBuilder.SourceBuilder.SetSheets([sheet])\n    printBuilder.PrinterText = printerName\n    printBuilder.Orientation = orientation\n    printBuilder.Paper = paperSize\n    printBuilder.Copies = copies\n    printBuilder.Commit()\n\ndef printAllSheets(workPart: NXOpen.Part, lw: NXOpen.ListingWindow, printerName: str, copies: int = 1):\n    &quot;&quot;&quot;\n    \u6253\u5370\u6307\u5b9a\u90e8\u4ef6\u4e2d\u7684\u6240\u6709\u56fe\u7eb8\u9875\n\n    Args:\n        workPart (NXOpen.Part): \u90e8\u4ef6\u5bf9\u8c61\n        lw (NXOpen.ListingWindow): \u5217\u8868\u7a97\u53e3\u5bf9\u8c61\n        printerName (str): \u6253\u5370\u673a\u540d\u79f0\n        copies (int, optional): \u6253\u5370\u4efd\u6570. \u9ed8\u8ba4 1 \u4efd.\n\n    Returns:\n        bool: \u6253\u5370\u662f\u5426\u6210\u529f\n    &quot;&quot;&quot;\n    try:\n        printBuilder = workPart.PlotManager.CreatePrintBuilder() # \u521b\u5efa\u6253\u5370\u6784\u5efa\u5668\n        sheets = [sheet for sheet in workPart.DrawingSheets] # \u83b7\u53d6\u6240\u6709\u56fe\u7eb8\n        numberOfSheets = len(sheets)\n        if numberOfSheets == 0:\n            lw.WriteLine(f&#039;{workPart.FullPath} \u4e2d\u6ca1\u6709\u56fe\u7eb8\u9875&#039;)\n            return None\n\n        for i, sheet in enumerate(sheets):\n            lw.WriteLine(f&#039;\u6b63\u5728\u63d0\u4ea4\u6253\u5370\u4efb\u52a1\uff1a {workPart.FullPath} \u4e2d\u7b2c {i+1} \u5f20\u56fe\u7eb8\uff0c\u5171 {numberOfSheets} \u5f20&#039;)\n            paperSize, orientation = getPrintFormatAndOrientation(sheet)\n            printSheet(printBuilder, sheet, paperSize, orientation, printerName, copies)\n        printBuilder.Destroy()\n    except Exception as e:\n        lw.WriteLine(f&#039;\u6253\u5370\u56fe\u7eb8\u96c6\u65f6\u53d1\u751f\u9519\u8bef: {e}&#039;)\n        return False\n    else:\n        lw.WriteLine(f&#039;\u63d0\u4ea4\u6253\u5370\u4efb\u52a1\u5b8c\u6210&#039;)\n        return True\n\ndef main():\n    session = NXOpen.Session.GetSession()\n    workPart = session.Parts.Work\n\n    lw = session.ListingWindow\n    lw.Open()\n\n    printerName = &#039;Microsoft Print to PDF&#039; # \u586b\u6253\u5370\u673a\u7684\u540d\u79f0\uff0c\u8fd9\u91cc\u586b\u7684\u662f\u5fae\u8f6f PDF \u865a\u62df\u6253\u5370\u673a\n    printAllSheets(workPart, lw, printerName)\n\nif __name__ == &#039;__main__&#039;:\n    main()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u73af\u5883 Windows 11 25H2 NX 2506 \u5185\u7f6e Python 3.12.8 \u5b9e\u73b0 \u7b97\u662f\u534a\u6210\u54c1\uff0c\u8fd8\u6ca1 [&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,617],"tags":[1834,1297,1802,1831,1855,1854,1856],"class_list":["post-22610","post","type-post","status-publish","format-standard","hentry","category-all","category-cad-cam","category-siemens-nx","tag-pdf","tag-siemens-nx","tag-1802","tag-1831","tag-1855","tag-1854","tag-1856"],"modified_by":"IYATT-yx","_links":{"self":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/22610","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=22610"}],"version-history":[{"count":3,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/22610\/revisions"}],"predecessor-version":[{"id":22634,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/22610\/revisions\/22634"}],"wp:attachment":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=22610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=22610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=22610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}