
Kiểm tra assembly thiếu file: liệt kê component không mở được và đường dẫn dự kiến
#Python#Assembly#Kiểm tra1 lượt xem
Quản trị viênQuản trị· 0 điểm
Câu hỏi ·
Assembly chuyển sang máy khác hay hỏng vì thiếu file. Một báo cáo liệt kê component không nạp được giúp nhanh chóng tìm file cần bổ sung.
Kiểm tra assembly thiếu fileCác bước thực hiện
- Duyệt cây. Mọi component.
- Phát hiện thiếu. Prototype là None hoặc part không đầy đủ.
- Lấy tên mong đợi. DisplayName và tên file từ part occurrence.
- Gom theo thư mục. Nếu biết thư mục tìm kiếm.
- Ghi báo cáo. Text và CSV.
Code tham khảo (Python)
import os import NXOpen def duyet(root): ngan_xep = list(root.GetChildren()) while ngan_xep: c = ngan_xep.pop() yield c ngan_xep.extend(c.GetChildren()) def kiem_tra(root, thu_muc_tim=None): thieu, thieu_day_du = [], [] for c in duyet(root): p = c.Prototype if p is None: thieu.append(c.DisplayName) elif not p.IsFullyLoaded: thieu_day_du.append(p.Leaf) return thieu, thieu_day_du def goi_y_tim(ten, thu_muc_tim): ket_qua = [] for goc, _, files in os.walk(thu_muc_tim): for f in files: if f.lower() == ten.lower() + ".prt": ket_qua.append(os.path.join(goc, f).replace("\\", "/")) return ket_qua def main(): session = NXOpen.Session.GetSession() lw = session.ListingWindow lw.Open() part = session.Parts.Display root = part.ComponentAssembly.RootComponent if part else None if root is None: lw.WriteLine("Không phải assembly") return thieu, mot_phan = kiem_tra(root) lw.WriteLine("Thiếu file: %d, nạp một phần: %d" % (len(thieu), len(mot_phan))) for t in thieu: lw.WriteLine(" THIẾU: " + t) for g in goi_y_tim(t, "D:/Du_an")[:3]: lw.WriteLine(" gợi ý: " + g) for t in mot_phan: lw.WriteLine(" Nạp một phần: " + t) if __name__ == "__main__": main()Giải thích code
- Phân biệt hai tình huống: thiếu hẳn (
PrototypelàNone) và nạp một phần (IsFullyLoadedsai). - Hàm gợi ý quét một thư mục dự án tìm file trùng tên để chỉ cho người dùng chỗ có thể chứa file.
.replace("\\", "/")đổi dấu gạch ngược Windows sang gạch xuôi để in và dùng thống nhất.
Lỗi thường gặp
- Quét ổ đĩa lớn mất nhiều thời gian; giới hạn thư mục tìm.
- Tên component trong cây có thể khác tên file khi đã đổi tên instance; dùng thêm đường dẫn nếu có.
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.