NX Lab

Khối cơ bản bằng Block, Cylinder và Sphere builder: tạo nhanh để thử ý tưởng

#Python#Primitive1 lượt xem

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

    Câu hỏi ·

    Khi không cần sketch, các feature cơ bản (Block, Cylinder, Sphere, Cone) tạo khối chỉ bằng vài tham số. Chúng rất hợp để dựng mô hình thử hoặc dữ liệu cho test.

    Khối cơ bản bằng Block, Cylinder và Sphere… Khối cơ bản bằng Block, Cylinder và Sphere…

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

    1. Block. CreateBlockFeatureBuilder, SetOriginAndLengths.
    2. Cylinder. CreateCylinderBuilder với Diameter, Height, Direction.
    3. Sphere. CreateSphereBuilder với CenterPoint và Diameter.
    4. Đặt kiểu Boolean. BooleanOption.Type = Create hoặc Unite.
    5. Commit từng cái. Mỗi builder một lần Commit rồi Destroy.

    Code tham khảo (Python)

    import NXOpen
    
    BOOL = NXOpen.GeometricUtilities.BooleanOperation.BooleanType
    
    def khoi_hop(part, goc, dai, rong, cao):
        b = part.Features.CreateBlockFeatureBuilder(NXOpen.Features.Feature.Null)
        try:
            b.Type = NXOpen.Features.BlockFeatureBuilder.Types.OriginAndEdgeLengths
            b.SetOriginAndLengths(NXOpen.Point3d(*goc), str(dai), str(rong), str(cao))
            return b.CommitFeature()
        finally:
            b.Destroy()
    
    def tru(part, goc, duong_kinh, cao, huong=(0, 0, 1)):
        b = part.Features.CreateCylinderBuilder(NXOpen.Features.Feature.Null)
        try:
            b.Type = NXOpen.Features.CylinderBuilder.Types.AxisDiameterAndHeight
            b.Origin = NXOpen.Point3d(*goc)
            b.Direction = NXOpen.Vector3d(*huong)
            b.Diameter.RightHandSide = str(duong_kinh)
            b.Height.RightHandSide = str(cao)
            b.BooleanOption.Type = BOOL.Create
            return b.CommitFeature()
        finally:
            b.Destroy()
    
    def cau(part, tam, duong_kinh):
        b = part.Features.CreateSphereBuilder(NXOpen.Features.Feature.Null)
        try:
            b.Type = NXOpen.Features.SphereBuilder.Types.CenterPointAndDiameter
            b.CenterPoint = part.Points.CreatePoint(NXOpen.Point3d(*tam))
            b.Diameter.RightHandSide = str(duong_kinh)
            b.BooleanOption.Type = BOOL.Create
            return b.CommitFeature()
        finally:
            b.Destroy()
    
    def main():
        session = NXOpen.Session.GetSession()
        lw = session.ListingWindow
        lw.Open()
        part = session.Parts.Work
        if part is None:
            return
        session.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Primitive")
        for f in (khoi_hop(part, (0, 0, 0), 60, 40, 10),
                  tru(part, (100, 0, 0), 30, 50),
                  cau(part, (180, 0, 25), 40)):
            lw.WriteLine("Đã tạo: " + f.GetFeatureName())
    
    if __name__ == "__main__":
        main()
    

    Giải thích code

    • Ba hàm cùng khuôn: tạo builder, đặt thuộc tính, CommitFeature, Destroy trong finally.
    • Chiều dài, đường kính, chiều cao truyền dưới dạng chuỗi biểu thức nên có thể thay bằng tên expression.
    • Alias BOOL rút gọn tên enum dài để dòng code dễ đọc.

    Lỗi thường gặp

    • Tên giá trị enum Types khác nhau theo từng loại builder; xem gợi ý code hoàn thành hoặc journal ghi sẵn.
    • Ba khối tạo ở ba vị trí rời nhau nên là ba body riêng; dùng Unite nếu muốn gộp.

    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.