
Nạp đầy đủ các component chưa nạp bằng OpenComponents và kiểm tra trạng thái
#Python#Assembly#Part1 lượt xem
Quản trị viênQuản trị· 0 điểm
Câu hỏi ·
Assembly mở ở chế độ nhẹ thường để nhiều component chưa nạp. Trước khi đo đạc hay xuất dữ liệu cần nạp đủ. OpenComponents cho phép nạp toàn bộ hay từng nhánh.
Nạp đầy đủ các component chưa nạp bằng…Các bước thực hiện
- Tìm component chưa nạp. Prototype là None.
- Chọn phạm vi nạp. OpenOption.ComponentOnly, ImmediateChildren hoặc WholeAssembly.
- Gọi OpenComponents. Trả về tuple (status, mảng trạng thái từng component).
- Kiểm tra lại. Duyệt lại cây để chắc không còn mục chưa nạp.
- Báo cáo thiếu. Những component vẫn không nạp được.
Code tham khảo (Python)
import NXOpen def tat_ca(root): kq, ngan_xep = [], list(root.GetChildren()) while ngan_xep: c = ngan_xep.pop() kq.append(c) ngan_xep.extend(c.GetChildren()) return kq def chua_nap(root): return [c for c in tat_ca(root) if c.Prototype is None] def nap_day_du(part): root = part.ComponentAssembly.RootComponent thieu = chua_nap(root) if not thieu: return 0, [] status, trang_thai = part.ComponentAssembly.OpenComponents( NXOpen.Assemblies.ComponentAssembly.OpenOption.WholeAssembly, [root]) status.Dispose() con_thieu = [c.DisplayName for c in chua_nap(root)] return len(thieu) - len(con_thieu), con_thieu def main(): session = NXOpen.Session.GetSession() lw = session.ListingWindow lw.Open() part = session.Parts.Display if part is None or part.ComponentAssembly.RootComponent is None: lw.WriteLine("Không phải assembly") return da_nap, con_thieu = nap_day_du(part) lw.WriteLine("Đã nạp thêm %d component" % da_nap) if con_thieu: lw.WriteLine("Vẫn thiếu (không tìm thấy file?):") for t in con_thieu: lw.WriteLine(" - " + t) if __name__ == "__main__": main()Giải thích code
OpenOption.WholeAssemblynạp toàn bộ cây dưới component được chỉ định.OpenComponentscó hai giá trị ra (status và mảng trạng thái) nên Python trả về tuple.- Đếm trước và sau để báo số component thực sự nạp được.
Lỗi thường gặp
- Component thiếu file vẫn thiếu sau khi nạp; nguyên nhân thường là đường dẫn tìm kiếm (search path) chưa đúng.
- Nạp toàn bộ assembly lớn tốn bộ nhớ; nạp theo nhánh nếu chỉ cần một phầ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.