import cadquery as cq# Use the Point constraint to position boxes relative to an arc
line = cq.Edge.makeCircle(radius=10, angle1=0, angle2=90)
box = cq.Workplane().box(1, 1, 1)assy = cq.Assembly()
assy.add(line, name="line")# position the red box on the center of the arc
assy.add(box, name="box0", color=cq.Color("red"))
assy.constrain("line", "box0", "Point")# position the green box at a normalized distance of 0.8 along the arc
position0 = line.positionAt(0.8)
assy.add(box, name="box1", color=cq.Color("green"))
assy.constrain("line", cq.Vertex.makeVertex(*position0.toTuple()), "box1", box.val(), "Point",
)# position the orange box 2 units in any direction from the green box
assy.add(box, name="box2", color=cq.Color("orange"))
assy.constrain("line",cq.Vertex.makeVertex(*position0.toTuple()),"box2",box.val(),"Point",param=2,
)# position the blue box offset 2 units in the x direction from the green box
position1 = position0 + cq.Vector(2, 0, 0)
assy.add(box, name="box3", color=cq.Color("blue"))
assy.constrain("line", cq.Vertex.makeVertex(*position1.toTuple()), "box3", box.val(), "Point",
)assy.solve()
show_object(assy)
import cadquery as cqplate = cq.Workplane().box(10, 10, 1).faces(">Z").workplane().hole(2)
cone = cq.Solid.makeCone(0.8, 0, 4)assy = cq.Assembly()
assy.add(plate, name="plate", color=cq.Color("green"))
assy.add(cone, name="cone", color=cq.Color("blue"))
# place the center of the flat face of the cone in the center of the upper face of the plate
assy.constrain("plate@faces@>Z", "cone@faces@<Z", "Point")# set both the flat face of the cone and the upper face of the plate to point in the same direction
assy.constrain("plate@faces@>Z", "cone@faces@<Z", "Axis", param=0)assy.solve()
show_object(assy)
在創建軸約束時,將根據對象的類型以三種不同方式之一提取方向矢量:
面:使用 normalAt()
Edge 和 geomType() 是“CIRCLE”:使用 normal()
Edge 和 geomType() 不是“CIRCLE”:使用 tangentAt()
使用任何其他類型的對象都會引發 ValueError。 到目前為止,最常見的用例是從面定義軸約束。
import cadquery as cq
from math import cos, sin, pi# Create a sinusoidal surface:
surf = cq.Workplane().parametricSurface(lambda u, v: (u, v, 5 * sin(pi * u / 10) * cos(pi * v / 10)),N=40,start=0,stop=20,
)# Create a cone with a small, flat tip:
cone = (cq.Workplane().add(cq.Solid.makeCone(1, 0.1, 2))# tag the tip for easy reference in the constraint:.faces(">Z").tag("tip").end()
)assy = cq.Assembly()
assy.add(surf, name="surf", color=cq.Color("lightgray"))
assy.add(cone, name="cone", color=cq.Color("green"))
# set the Face on the tip of the cone to point in
# the opposite direction of the center of the surface:
assy.constrain("surf", "cone?tip", "Axis")
# to make the example clearer, move the cone to the center of the face:
assy.constrain("surf", "cone?tip", "Point")
assy.solve()show_object(assy)
assy = cq.Assembly()
assy.add(surf, name="surf", color=cq.Color("lightgray"))
assy.add(cone, name="cone", color=cq.Color("green"))
-# set the Face on the tip of the cone to point in
-# the opposite direction of the center of the surface:
-assy.constrain("surf", "cone?tip", "Axis")
-# to make the example clearer, move the cone to the center of the face:
-assy.constrain("surf", "cone?tip", "Point")
+assy.constrain("surf", "cone?tip", "Plane")
assy.solve()show_object(assy)
import cadquery as cq# Create an L-shaped object:
bracket = (cq.Workplane("YZ").hLine(1).vLine(0.1).hLineTo(0.2).vLineTo(1).hLineTo(0).close().extrude(1)# tag some faces for easy reference:.faces(">Y[1]").tag("inner_vert").end().faces(">Z[1]").tag("inner_horiz").end()
)box = cq.Workplane().box(0.5, 0.5, 0.5)assy = cq.Assembly()
assy.add(bracket, name="bracket", color=cq.Color("gray"))
assy.add(box, name="box", color=cq.Color("green"))# lock bracket orientation:
assy.constrain("bracket@faces@>Z", "box@faces@>Z", "Axis", param=0)
assy.constrain("bracket@faces@>X", "box@faces@>X", "Axis", param=0)# constrain the bottom of the box to be on the plane defined by inner_horiz:
assy.constrain("box@faces@<Z", "bracket?inner_horiz", "PointInPlane")
# constrain the side of the box to be 0.2 units from the plane defined by inner_vert
assy.constrain("box@faces@<Y", "bracket?inner_vert", "PointInPlane", param=0.2)
# constrain the end of the box to be 0.1 units inside the end of the bracket
assy.constrain("box@faces@>X", "bracket@faces@>X", "PointInPlane", param=-0.1)assy.solve()
show_object(assy)
13、點線約束
點線約束將第一個對象的中心定位在第二個對象定義的線上。 成本函數是:
其中:
c是第一個參數的中心,
l是從第二個對象創建的一條線
param是約束的參數,默認為0,
dist(a,b)是點 a 和線 l之間的距離
import cadquery as cqb1 = cq.Workplane().box(1,1,1)
b2 = cq.Workplane().sphere(0.15)assy = (cq.Assembly().add(b1,name='b1').add(b2, loc=cq.Location(cq.Vector(0,0,4)), name='b2', color=cq.Color('red'))
)# fix the position of b1
assy.constrain('b1','Fixed')
# b2 on one of the edges of b1
assy.constrain('b2','b1@edges@>>Z and >>Y','PointOnLine')
# b2 on another of the edges of b1
assy.constrain('b2','b1@edges@>>Z and >>X','PointOnLine')
# effectively b2 will be constrained to be on the intersection of the two edgesassy.solve()
show_object(assy)
import cadquery as cqb1 = cq.Workplane().box(1,1,1)
b2 = cq.Workplane().sphere(0.15)assy = (cq.Assembly().add(b1,name='b1').add(b2, loc=cq.Location(cq.Vector(0,0,4)), name='b2', color=cq.Color('red'))
)# fix the position of b1
assy.constrain('b1','Fixed')
# b2 on one of the edges of b1
assy.constrain('b2','b1@edges@>>Z and >>Y','PointOnLine')
# b2 on another of the edges of b1
assy.constrain('b2','b1@edges@>>Z and >>X','PointOnLine')
# effectively b2 will be constrained to be on the intersection of the two edgesassy.solve()
show_object(assy)
15、固定旋轉約束
固定旋轉約束將給定對象的旋轉固定為等于通過約束參數指定的值。 對象首先繞原點旋轉 Z 角,然后是 Y,最后是 X。
該約束鎖定對象的所有旋轉自由度。 成本函數是:
其中:
R 向量應用于對象的旋轉角度
param是約束參數 - 指定目標旋轉的元組。
import cadquery as cqb1 = cq.Workplane().box(1,1,1)
b2 = cq.Workplane().rect(0.1, 0.1).extrude(1,taper=-15)assy = (cq.Assembly().add(b1,name='b1').add(b2, loc=cq.Location(cq.Vector(0,0,4)), name='b2', color=cq.Color('red'))
)# fix the position of b1
assy.constrain('b1','Fixed')
# fix b2 bottom face position (but not rotation)
assy.constrain('b2@faces@<Z','FixedPoint',(0,0,0.5))
# fix b2 rotational degrees of freedom too
assy.constrain('b2','FixedRotation',(45,0,45))assy.solve()
show_object(assy)
import cadquery as cqb1 = cq.Workplane().box(1,1,1)
b2 = cq.Workplane().rect(0.1, 0.1).extrude(1,taper=-15)assy = (cq.Assembly().add(b1,name='b1').add(b2, loc=cq.Location(cq.Vector(0,0,4)), name='b2', color=cq.Color('red'))
)# fix the position of b1
assy.constrain('b1','Fixed')
# fix b2 bottom face position (but not rotation)
assy.constrain('b2@faces@<Z','FixedPoint',(0,0,0.5))
# fix b2 some rotational degrees of freedom too
assy.constrain('b2@faces@>Z','FixedAxis',(1,0,2))assy.solve()
show_object(assy)