cad约束怎么调出来,cad怎么自带约束了

首页 > 实用技巧 > 作者:YD1662023-04-16 18:09:24

本教程介绍在CadQuery中如何使用装配约束功能来构建逼真的模型,我们将组装一个由 20x20 V 型槽型材制成的门组件。

cad约束怎么调出来,cad怎么自带约束了(1)

1、定义参数

我们希望从定义模型参数开始,以便以后可以轻松更改尺寸:

import cadquery as cq # Parameters H = 400 W = 200 D = 350 PROFILE = cq.importers.importDXF("vslot-2020_1.DXF").wires() SLOT_D = 5 PANEL_T = 3 HANDLE_D = 20 HANDLE_L = 50 HANDLE_W = 4

值得注意的是,v 槽轮廓是从 DXF 文件导入的。 这样就很容易更换为其他铝型材,例如博世或其他供应商提供的 DXF 文件。

2、定义可重用组件

接下来我们要定义根据指定参数生成装配组件的函数。

def make_vslot(l): return PROFILE.toPending().extrude(l) def make_connector(): rv = ( cq.Workplane() .box(20, 20, 20) .faces("<X") .workplane() .cboreHole(6, 15, 18) .faces("<Z") .workplane(centerOption="CenterOfMass") .cboreHole(6, 15, 18) ) # tag mating faces rv.faces(">X").tag("X").end() rv.faces(">Z").tag("Z").end() return rv def make_panel(w, h, t, cutout): rv = ( cq.Workplane("XZ") .rect(w, h) .extrude(t) .faces(">Y") .vertices() .rect(2*cutout,2*cutout) .cutThruAll() .faces("<Y") .workplane() .pushPoints([(-w / 3, HANDLE_L / 2), (-w / 3, -HANDLE_L / 2)]) .hole(3) ) # tag mating edges rv.faces(">Y").edges("%CIRCLE").edges(">Z").tag("hole1") rv.faces(">Y").edges("%CIRCLE").edges("<Z").tag("hole2") return rv def make_handle(w, h, r): pts = ((0, 0), (w, 0), (w, h), (0, h)) path = cq.Workplane().polyline(pts) rv = ( cq.Workplane("YZ") .rect(r, r) .sweep(path, transition="round") .tag("solid") .faces("<X") .workplane() .faces("<X", tag="solid") .hole(r / 1.5) ) # tag mating faces rv.faces("<X").faces(">Y").tag("mate1") rv.faces("<X").faces("<Y").tag("mate2") return rv3、初始装配

接下来我们要实例化所有组件并将它们添加到组件中。

# define the elements door = ( cq.Assembly() .add(make_vslot(H), name="left") .add(make_vslot(H), name="right") .add(make_vslot(W), name="top") .add(make_vslot(W), name="bottom") .add(make_connector(), name="con_tl", color=cq.Color("black")) .add(make_connector(), name="con_tr", color=cq.Color("black")) .add(make_connector(), name="con_bl", color=cq.Color("black")) .add(make_connector(), name="con_br", color=cq.Color("black")) .add( make_panel(W SLOT_D, H SLOT_D, PANEL_T, SLOT_D), name="panel", color=cq.Color(0, 0, 1, 0.2), ) .add( make_handle(HANDLE_D, HANDLE_L, HANDLE_W), name="handle", color=cq.Color("yellow"), ) )4、约束定义

然后我们要定义所有的约束:

# define the constraints ( door # left profile .constrain("left@faces@<Z", "con_bl?Z", "Plane") .constrain("left@faces@<X", "con_bl?X", "Axis") .constrain("left@faces@>Z", "con_tl?Z", "Plane") .constrain("left@faces@<X", "con_tl?X", "Axis") # top .constrain("top@faces@<Z", "con_tl?X", "Plane") .constrain("top@faces@<Y", "con_tl@faces@>Y", "Axis") # bottom .constrain("bottom@faces@<Y", "con_bl@faces@>Y", "Axis") .constrain("bottom@faces@>Z", "con_bl?X", "Plane") # right connectors .constrain("top@faces@>Z", "con_tr@faces@>X", "Plane") .constrain("bottom@faces@<Z", "con_br@faces@>X", "Plane") .constrain("left@faces@>Z", "con_tr?Z", "Axis") .constrain("left@faces@<Z", "con_br?Z", "Axis") # right profile .constrain("right@faces@>Z", "con_tr@faces@>Z", "Plane") .constrain("right@faces@<X", "left@faces@<X", "Axis") # panel .constrain("left@faces@>X[-4]", "panel@faces@<X", "Plane") .constrain("left@faces@>Z", "panel@faces@>Z", "Axis") # handle .constrain("panel?hole1", "handle?mate1", "Plane") .constrain("panel?hole2", "handle?mate2", "Point") )

如果你需要做一些基于字符串的选择器不可能做的不寻常的事情,例如使用 cadquery.selectors.BoxSelector 或用户定义的选择器类,可以直接将 cadquery.Shape 对象传递给 cadquery.Assembly.constrain( ) 方法。 例如,上面的

.constrain('part1@faces@>Z','part3@faces@<Z','Axis')

等效于:

.constrain('part1',part1.faces('>z').val(),'part3',part3.faces('<Z').val(),'Axis')

此方法需要一个 cadquery.Shape 对象,因此请记住使用 cadquery.Workplane.val() 方法传递单个 cadquery.Shape 而不是整个 cadquery.Workplane 对象。

5、最终结果

下面是完整的代码,包括最后的求解步骤。

cad约束怎么调出来,cad怎么自带约束了(2)

import cadquery as cq # Parameters H = 400 W = 200 D = 350 PROFILE = cq.importers.importDXF("vslot-2020_1.dxf").wires() SLOT_D = 6 PANEL_T = 3 HANDLE_D = 20 HANDLE_L = 50 HANDLE_W = 4 def make_vslot(l): return PROFILE.toPending().extrude(l) def make_connector(): rv = ( cq.Workplane() .box(20, 20, 20) .faces("<X") .workplane() .cboreHole(6, 15, 18) .faces("<Z") .workplane(centerOption="CenterOfMass") .cboreHole(6, 15, 18) ) # tag mating faces rv.faces(">X").tag("X").end() rv.faces(">Z").tag("Z").end() return rv def make_panel(w, h, t, cutout): rv = ( cq.Workplane("XZ") .rect(w, h) .extrude(t) .faces(">Y") .vertices() .rect(2*cutout,2*cutout) .cutThruAll() .faces("<Y") .workplane() .pushPoints([(-w / 3, HANDLE_L / 2), (-w / 3, -HANDLE_L / 2)]) .hole(3) ) # tag mating edges rv.faces(">Y").edges("%CIRCLE").edges(">Z").tag("hole1") rv.faces(">Y").edges("%CIRCLE").edges("<Z").tag("hole2") return rv def make_handle(w, h, r): pts = ((0, 0), (w, 0), (w, h), (0, h)) path = cq.Workplane().polyline(pts) rv = ( cq.Workplane("YZ") .rect(r, r) .sweep(path, transition="round") .tag("solid") .faces("<X") .workplane() .faces("<X", tag="solid") .hole(r / 1.5) ) # tag mating faces rv.faces("<X").faces(">Y").tag("mate1") rv.faces("<X").faces("<Y").tag("mate2") return rv # define the elements door = ( cq.Assembly() .add(make_vslot(H), name="left") .add(make_vslot(H), name="right") .add(make_vslot(W), name="top") .add(make_vslot(W), name="bottom") .add(make_connector(), name="con_tl", color=cq.Color("black")) .add(make_connector(), name="con_tr", color=cq.Color("black")) .add(make_connector(), name="con_bl", color=cq.Color("black")) .add(make_connector(), name="con_br", color=cq.Color("black")) .add( make_panel(W 2*SLOT_D, H 2*SLOT_D, PANEL_T, SLOT_D), name="panel", color=cq.Color(0, 0, 1, 0.2), ) .add( make_handle(HANDLE_D, HANDLE_L, HANDLE_W), name="handle", color=cq.Color("yellow"), ) ) # define the constraints ( door # left profile .constrain("left@faces@<Z", "con_bl?Z", "Plane") .constrain("left@faces@<X", "con_bl?X", "Axis") .constrain("left@faces@>Z", "con_tl?Z", "Plane") .constrain("left@faces@<X", "con_tl?X", "Axis") # top .constrain("top@faces@<Z", "con_tl?X", "Plane") .constrain("top@faces@<Y", "con_tl@faces@>Y", "Axis") # bottom .constrain("bottom@faces@<Y", "con_bl@faces@>Y", "Axis") .constrain("bottom@faces@>Z", "con_bl?X", "Plane") # right connectors .constrain("top@faces@>Z", "con_tr@faces@>X", "Plane") .constrain("bottom@faces@<Z", "con_br@faces@>X", "Plane") .constrain("left@faces@>Z", "con_tr?Z", "Axis") .constrain("left@faces@<Z", "con_br?Z", "Axis") # right profile .constrain("right@faces@>Z", "con_tr@faces@>Z", "Plane") .constrain("right@faces@<X", "left@faces@<X", "Axis") # panel .constrain("left@faces@>X[-4]", "panel@faces@<X", "Plane") .constrain("left@faces@>Z", "panel@faces@>Z", "Axis") # handle .constrain("panel?hole1", "handle?mate1", "Plane") .constrain("panel?hole2", "handle?mate2", "Point") ) # solve door.solve() show_object(door,name='door')6、数据导出

生成的程序集可以导出为 STEP 文件或内部 OCCT XML 格式。

STEP 可以加载到所有 CAD 工具中,例如在 FreeCAD 中,XML 可用于其他使用 OCCT 的应用程序。

door.save('door.step') door.save('door.xml')

当保存为STEP格式时,颜色被保留但不透明。

cad约束怎么调出来,cad怎么自带约束了(3)

7、对象位置

可以将对象添加到具有提供的初始位置的装配中,例如:

cad约束怎么调出来,cad怎么自带约束了(4)

首页 12345下一页

栏目热文

文档排行

本站推荐

Copyright © 2018 - 2021 www.yd166.com., All Rights Reserved.