
Sinh nhiều biến thể từ một part mẫu theo bảng CSV: mở mẫu, đổi expression, lưu thành file mới
#Python#Tham số#Hàng loạt1 lượt xem
Quản trị viênQuản trị· 0 điểm
Câu hỏi ·
Họ chi tiết (cùng hình dạng, khác kích thước) là ứng dụng sinh lợi nhất của thiết kế tham số. Mỗi dòng bảng CSV mô tả một biến thể; script mở part mẫu, gán expression, cập nhật và lưu thành file riêng.
Sinh nhiều biến thể từ một part mẫu theo…Các bước thực hiện
- Chuẩn bị part mẫu. Có sẵn các expression điều khiển (bài trước).
- Chuẩn bị bảng CSV. Cột ten_file và các cột trùng tên expression.
- Với mỗi dòng. Mở mẫu, sửa expression, cập nhật.
- Lưu thành file mới. SaveAs trước khi sửa để không đụng file mẫu.
- Đóng và báo cáo. Thành công/lỗi từng biến thể.
Code tham khảo (Python)
import csv import os import NXOpen def sinh_bien_the(session, duong_dan_mau, bang_csv, thu_muc_ra, lw): ok = loi = 0 with open(bang_csv, "r", newline="", encoding="utf-8-sig") as f: cac_dong = list(csv.DictReader(f)) os.makedirs(thu_muc_ra, exist_ok=True) for d in cac_dong: ten_file = d["ten_file"].strip() dich = "%s/%s.prt" % (thu_muc_ra.rstrip("/"), ten_file) part = None try: part, st = session.Parts.OpenDisplay(duong_dan_mau) st.Dispose() s = part.SaveAs(dich) # đổi tên ngay để không sửa vào file mẫu s.Dispose() mark = session.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Bien the") for ten_exp, gia_tri in d.items(): if ten_exp == "ten_file" or not (gia_tri or "").strip(): continue part.Expressions.Edit(part.Expressions.FindObject(ten_exp), gia_tri.strip()) ma = session.UpdateManager.DoUpdate(mark) session.DeleteUndoMark(mark, "Bien the") if ma != 0: raise RuntimeError("DoUpdate mã lỗi %d" % ma) s2 = part.Save(NXOpen.BasePart.SaveComponents.FalseValue, NXOpen.BasePart.CloseAfterSave.FalseValue) s2.Dispose() lw.WriteLine("OK " + dich) ok += 1 except Exception as ex: lw.WriteLine("LỖI %s : %s" % (ten_file, ex)) loi += 1 finally: if part is not None: part.Close(NXOpen.BasePart.CloseWholeTree.FalseValue, NXOpen.BasePart.CloseModified.CloseModified, None) return ok, loi def main(): session = NXOpen.Session.GetSession() lw = session.ListingWindow lw.Open() ok, loi = sinh_bien_the(session, "D:/NXLab/mau/khoi_tham_so.prt", "D:/NXLab_nhap/bien_the.csv", "D:/NXLab_xuat/bien_the", lw) lw.WriteLine("Hoàn tất: %d thành công, %d lỗi" % (ok, loi)) if __name__ == "__main__": main()Giải thích code
SaveAsngay sau khi mở bảo đảm mọi sửa đổi nằm trên file mới, file mẫu luôn nguyên vẹn.- Tên cột CSV (trừ
ten_file) phải trùng tên expression trong part mẫu. - Một dòng lỗi không dừng cả lô nhờ
try/except/finallycho từng biến thể.
Lỗi thường gặp
- Part mẫu phải mở được ở chế độ ghi (không bị khóa); nếu chỉ đọc,
SaveAsvẫn tạo được file mới. - Giá trị ngoài khả năng của mô hình (ví dụ lỗ lớn hơn thân) làm
DoUpdatebáo lỗi; thêm kiểm tra hợp lệ trước khi sửa.
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.