NX Lab

Xuất PDF hàng loạt từ nhiều file bản vẽ: mở part, xuất sheet, đóng part, ghi báo cáo

#Python#PDF#Hàng loạt1 lượt xem

  1. Quản trị viênQuản trị· 0 điểm

    Câu hỏi ·

    Phát hành bộ bản vẽ nghĩa là xuất hàng chục PDF cùng lúc. Khung hàng loạt giống bài STEP nhưng mỗi file phải mở vì PDF cần dữ liệu bản vẽ.

    Xuất PDF hàng loạt từ nhiều file bản vẽ Xuất PDF hàng loạt từ nhiều file bản vẽ

    Các bước thực hiện

    1. Quét thư mục. Các .prt có sheet.
    2. Mở part. Parts.OpenDisplay.
    3. Xuất PDF. PrintPDFBuilder cho mọi sheet.
    4. Đóng part. Close để giải phóng bộ nhớ.
    5. Ghi báo cáo. Số sheet xuất, thời gian.

    Code tham khảo (Python)

    import csv
    import os
    import NXOpen
    
    def xuat_pdf(part, duong_dan):
        sheets = list(part.DrawingSheets)
        if not sheets:
            return 0
        b = part.PlotManager.CreatePrintPdfbuilder()
        try:
            b.SourceBuilder.SetSheets(sheets)
            b.Filename = duong_dan
            b.Colors = NXOpen.PrintPDFBuilder.Color.BlackOnWhite
            b.Action = NXOpen.PrintPDFBuilder.ActionOption.Overwrite
            b.Commit()
        finally:
            b.Destroy()
        return len(sheets)
    
    def main(thu_muc_nguon="D:/Du_an/ban_ve", thu_muc_ra="D:/NXLab_xuat/pdf"):
        session = NXOpen.Session.GetSession()
        lw = session.ListingWindow
        lw.Open()
        os.makedirs(thu_muc_ra, exist_ok=True)
        with open(os.path.join(thu_muc_ra, "bao_cao_pdf.csv"), "w", newline="", encoding="utf-8-sig") as f:
            w = csv.writer(f)
            w.writerow(["file", "so_sheet", "trang_thai", "loi"])
            for ten in sorted(os.listdir(thu_muc_nguon)):
                if not ten.lower().endswith(".prt"):
                    continue
                prt = thu_muc_nguon.rstrip("/") + "/" + ten
                part = None
                try:
                    part, st = session.Parts.OpenDisplay(prt)
                    st.Dispose()
                    n = xuat_pdf(part, "%s/%s.pdf" % (thu_muc_ra, os.path.splitext(ten)[0]))
                    w.writerow([ten, n, "OK" if n else "KHÔNG CÓ SHEET", ""])
                except Exception as ex:
                    w.writerow([ten, 0, "LOI", str(ex)])
                finally:
                    if part is not None:
                        part.Close(NXOpen.BasePart.CloseWholeTree.TrueValue,
                                   NXOpen.BasePart.CloseModified.CloseModified, None)
                lw.WriteLine("Đã xử lý: " + ten)
    
    if __name__ == "__main__":
        main()
    

    Giải thích code

    • Mỗi PDF được tạo trong khi part đang mở và đóng ngay sau để không tích lũy bộ nhớ.
    • File không có sheet được ghi nhận riêng thay vì coi là lỗi.
    • part.Close(...) đặt trong finally đảm bảo luôn đóng kể cả khi xuất lỗi.

    Lỗi thường gặp

    • Đặt tên PDF theo tên part; nếu hai thư mục có cùng tên file sẽ ghi đè lẫn nhau.
    • View lỗi thời sẽ sinh PDF sai; thêm bước cập nhật view trước khi xuất nếu cần.

    Code mang tính tham khảo, viết cho NX 12 trở lên (Python journal). Tên menu và một số lớp có thể khác theo phiên bản NX; hãy chạy thử trên bản sao của chi tiết trước khi dùng cho dữ liệu thật.

    Điểm: 0

Trả lời

Đăng nhập để tham gia thảo luận.