描述:
利用Python的pycatia包控制CATIA绘制L块,完成约束,并将part装配到product中。
from pycatia import catia
from pycatia.product_structure_interfaces.product_document import ProductDocument
from pycatia.enumeration.enumeration_types import cat_constraint_type
caa = catia() # 实例化catia对象
documents = caa.documents # 定义文本
documents.add('product') # 添加product
document = caa.active_document # 激活product文本
doc_com = ProductDocument(document.com_object) # product接口
product = doc_com.product # 定义product对象
product.part_number = 'test' # 更改产品名称
prod = product.products
documents.add('Part') # 添加part
part1 = caa.active_document
par1_com = ProductDocument(part1.com_object)
par1 = par1_com.product
prod.add_component(par1) # part添加到product
document = caa.active_document # 激活part文本
part_l = document.part
bodies = part_l.bodies # 定义零件体
body = bodies.item(1)
body.name = 'L'
hybrid_shape_factory = part_l.hybrid_shape_factory # 工厂定义
p0 = hybrid_shape_factory.add_new_point_coord(0, 0, 0)
body.insert_hybrid_shape(p0)
origin_elements = part_l.origin_elements # 获取3d轴系
sketch_l = body.sketches # 定义草图
sketch_plane = origin_elements.plane_yz # 选取草图面
sketch_work = sketch_l.add(sketch_plane) # 添加草图面
part_l.in_work_object = sketch_work # 定义工作对象
factory_2d = sketch_work.open_edition() # 打开草图
p1 = factory_2d.create_point(-10, 0) # 草图插入点
p2 = factory_2d.create_point(10, 0)
p3 = factory_2d.create_point(10, -65)
p4 = factory_2d.create_point(-55, -65)
p5 = factory_2d.create_point(-55, -45)
p6 = factory_2d.create_point(-10, -45)
# 点依次相连
line1 = factory_2d.create_line(-10, 0, 10, 0) # 草图线1
line1.start_point, line1.end_point = p1, p2
line2 = factory_2d.create_line(10, 0, 10, -65) # 草图线2
line2.start_point, line2.end_point = p2, p3
line3 = factory_2d.create_line(10, -65, -55, -65) # 草图线3
line3.start_point, line3.end_point = p3, p4
line4 = factory_2d.create_line(-55, -65, -55, -45) # 草图线4
line4.start_point, line4.end_point = p4, p5
line5 = factory_2d.create_line(-55, -45, -10, -45) # 草图线5
line5.start_point, line5.end_point = p5, p6
line6 = factory_2d.create_line(-10, -45, -10, 0) # 草图线6
line6.start_point, line6.end_point = p6, p1
# 约束
constraints = sketch_work.constraints # 定义约束
constraints.add_mono_elt_cst(cat_constraint_type.index("catCstTypeLength"), line1) # 长度约束
constraints.add_mono_elt_cst(cat_constraint_type.index("catCstTypeLength"), line2)
constraints.add_mono_elt_cst(cat_constraint_type.index("catCstTypeLength"), line3)
constraints.add_mono_elt_cst(cat_constraint_type.index("catCstTypeLength"), line4)
axis2d = sketch_work.geometric_elements.item("绝对轴")
axis_v = axis2d.get_item("纵向")
reference_v = part_l.create_reference_from_object(axis_v) # 参考约束为草图纵向轴, 纵向约束
constraints.add_bi_elt_cst(cat_constraint_type.index("catCstTypeVerticality"), line2, reference_v)
constraints.add_bi_elt_cst(cat_constraint_type.index("catCstTypeVerticality"), line4, reference_v)
constraints.add_bi_elt_cst(cat_constraint_type.index("catCstTypeVerticality"), line6, reference_v)
axis_h = axis2d.get_item("横向")
reference_h = part_l.create_reference_from_object(axis_h) # 水平方向约束
constraints.add_bi_elt_cst(cat_constraint_type.index("catCstTypeHorizontality"), line1, reference_h)
constraints.add_bi_elt_cst(cat_constraint_type.index("catCstTypeHorizontality"), line3, reference_h)
constraints.add_bi_elt_cst(cat_constraint_type.index("catCstTypeHorizontality"), line5, reference_h)
constraints.add_bi_elt_cst(cat_constraint_type.index("catCstTypeDistance"), p0, line1) # 与坐标原点的约束
constraints.add_bi_elt_cst(cat_constraint_type.index("catCstTypeDistance"), p0, line2)
sketch_work.close_edition() # 关闭草图
# 凸台操作
part_l.in_work_object = sketch_work
shape_factory = part_l.shape_factory # 定义工厂
pad_l = shape_factory.add_new_pad(sketch_work, 10) # 添加凸台
pad_l.is_symmetric = True # 镜像凸台
part_l.update() # 更新