
Đổi Work Part sang một component và quay về Display Part để chỉnh sửa tại chỗ
#Python#Assembly#Part1 lượt xem
Quản trị viênQuản trị· 0 điểm
Câu hỏi ·
Muốn tạo feature trong một component, phải đặt component làm Work Part. Sau khi xong cần trả về Display Part để không làm người dùng bối rối.
Đổi Work Part sang một component và quay…Các bước thực hiện
- Tìm component. Theo tên hiển thị.
- Đặt Work Part. session.Parts.SetWorkComponent(component).
- Thực hiện thay đổi. Dùng session.Parts.Work (lúc này là part của component).
- Khôi phục. session.Parts.SetWork(display_part).
- Dùng try/finally. Luôn khôi phục kể cả khi lỗi.
Code tham khảo (Python)
import NXOpen def tim_component(root, ten): ngan_xep = list(root.GetChildren()) while ngan_xep: c = ngan_xep.pop() if c.DisplayName == ten: return c ngan_xep.extend(c.GetChildren()) return None def lam_tai_component(session, comp, ham): """Đặt component làm Work Part, chạy ham(part), rồi trả Work Part về như cũ.""" display = session.Parts.Display status = session.Parts.SetWorkComponent(comp) # hàm Void có một tham số ra -> trả về chính status status.Dispose() try: return ham(session.Parts.Work) finally: session.Parts.SetWork(display) def main(): session = NXOpen.Session.GetSession() lw = session.ListingWindow lw.Open() display = session.Parts.Display root = display.ComponentAssembly.RootComponent if display else None if root is None: return comp = tim_component(root, "BU_LONG_01") if comp is None: lw.WriteLine("Không thấy component") return def in_ten(p): lw.WriteLine("Đang làm việc trên part: " + p.Leaf) lam_tai_component(session, comp, in_ten) lw.WriteLine("Work Part sau khi quay về: " + session.Parts.Work.Leaf) if __name__ == "__main__": main()Giải thích code
SetWorkComponent(comp)đặt component làm Work Part; hàm không có giá trị trả về mà chỉ có tham số raloadStatus, nên Python trả về trực tiếp đối tượng trạng thái (không phải tuple).SetWork(display)trả Work Part về assembly cấp cao nhất.- Hàm
lam_tai_componentbao việc đổi và khôi phục trong một nơi, nên các script khác chỉ truyền hàm xử lý.
Lỗi thường gặp
- Dùng
Parts.Displaylàm điểm quay về; nếu người dùng đang ở component lồng sâu, hãy lưuParts.Workban đầu và khôi phục đúng nó. - Quên khôi phục Work Part là lỗi phổ biến: người dùng tiếp tục thao tác trên component mà không biết.
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.