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)
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)
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、固定旋轉(zhuǎn)約束
固定旋轉(zhuǎn)約束將給定對(duì)象的旋轉(zhuǎn)固定為等于通過約束參數(shù)指定的值。 對(duì)象首先繞原點(diǎn)旋轉(zhuǎn) Z 角,然后是 Y,最后是 X。
該約束鎖定對(duì)象的所有旋轉(zhuǎn)自由度。 成本函數(shù)是:
其中:
R 向量應(yīng)用于對(duì)象的旋轉(zhuǎn)角度
param是約束參數(shù) - 指定目標(biāo)旋轉(zhuǎn)的元組。
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)