-
Notifications
You must be signed in to change notification settings - Fork 291
/
assy.rst
1413 lines (1157 loc) · 90.1 KB
/
assy.rst
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
.. _assytutorial:
**********
Assemblies
**********
Assembly tutorial
-----------------
The purpose of this section is to demonstrate how to use the assembly and constraints
functionality to build a realistic model. It will be a enclosure door assembly made out of 20x20 v-slot profiles.
Defining parameters
===================
We want to start with defining the model parameters to allow for easy dimension changes later:
.. code-block:: python
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
It is interesting to note that the v-slot profile is imported from a DXF file.
This way it is very easy to change to other aluminum extrusion type, e.g. Item or Bosch.
Vendors usually provide DXF files.
Defining reusable components
============================
Next we want to define functions generating the assembly components based on the specified parameters.
.. code-block:: python
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
Initial assembly
================
Next we want to instantiate all the components and add them to the assembly.
.. code-block:: python
# 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"),
)
)
Constraints definition
======================
Then we want to define all the constraints
.. code-block:: python
# 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")
)
Should you need to do something unusual that is not possible with the string
based selectors (e.g. use :py:class:`cadquery.selectors.BoxSelector` or a user-defined selector class),
it is possible to pass :py:class:`cadquery.Shape` objects to the :py:meth:`cadquery.Assembly.constrain` method directly. For example, the above
.. code-block:: python
.constrain('part1@faces@>Z','part3@faces@<Z','Axis')
is equivalent to
.. code-block:: python
.constrain('part1',part1.faces('>z').val(),'part3',part3.faces('<Z').val(),'Axis')
This method requires a :py:class:`cadquery.Shape` object, so remember to use the :py:meth:`cadquery.Workplane.val`
method to pass a single :py:class:`cadquery.Shape` and not the whole :py:class:`cadquery.Workplane` object.
Final result
============
Below is the complete code including the final solve step.
.. cadquery::
:height: 600px
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')
Data export
===========
The resulting assembly can be exported as a STEP file or in a internal OCCT XML format.
STEP can be loaded in all CAD tool, e.g. in FreeCAD and the XML be used in other applications using OCCT.
.. code-block:: python
:linenos:
door.save('door.step')
door.save('door.xml')
.. image:: _static/door_assy_freecad.png
Object locations
----------------
Objects can be added to an assembly with initial locations supplied, such as:
.. cadquery::
import cadquery as cq
cone = cq.Solid.makeCone(1, 0, 2)
assy = cq.Assembly()
assy.add(
cone,
loc=cq.Location((0, 0, 0), (1, 0, 0), 180),
name="cone0",
color=cq.Color("green"),
)
assy.add(cone, name="cone1", color=cq.Color("blue"))
show_object(assy)
As an alternative to the user calculating locations, constraints and the method
:meth:`~cadquery.Assembly.solve` can be used to position objects in an assembly.
If initial locations and the method :meth:`~cadquery.Assembly.solve` are used the solver will
overwrite these initial locations with it's solution, however initial locations can still affect the
final solution. In an underconstrained system the solver may not move an object if it does not
contribute to the cost function, or if multiple solutions exist (ie. multiple instances
where the cost function is at a minimum) initial locations can cause the solver to converge on one
particular solution. For very complicated assemblies setting approximately correct initial locations
can also reduce the computational time requred.
Constraints
-----------
Constraints are often a better representation of the real world relationship the user wants to
model than directly supplying locations. In the above example the real world relationship is that
the bottom face of each cone should touch, which can be modelled with a Plane constraint. When the
user provides explicit locations (instead of constraints) then they are also reponsible for updating
them when, for example, the location of ``cone1`` changes.
When at least one constraint is supplied and the method :meth:`~cadquery.Assembly.solve` is run, an
optimization problem is set up. Each constraint provides a cost function that depends on the
position and orientation (represented by a :class:`~cadquery.Location`) of the two objects specified
when creating the constraint. The solver varies the location of the assembly's children and attempts
to minimize the sum of all cost functions. Hence by reading the formulae of the cost functions
below, you can understand exactly what each constraint does.
Point
=====
The Point constraint is a frequently used constraint that minimizes the distance between two points.
Some example uses are centering faces or aligning verticies, but it is also useful with dummy
vertices to create offsets between two parts.
The cost function is:
.. math::
( param - \lvert \vec{ c_1 } - \vec{ c_2 } \rvert ) ^2
Where:
- :math:`param` is the parameter of the constraint, which defaults to 0,
- :math:`\vec{ c_i }` is the center of the ith object, and
- :math:`\lvert \vec{ v } \rvert` is the modulus of :math:`\vec{ v }`, ie. the length of
:math:`\vec{ v }`.
When creating a Point constraint, the ``param`` argument can be used to specify a desired offset
between the two centers. This offset does not have a direction associated with it, if you want to
specify an offset in a specific direction then you should use a dummy :class:`~cadquery.Vertex`.
The Point constraint uses the :meth:`~cadquery.Shape.Center` to find the center of the
argument. Hence it will work with all subclasses of :class:`~cadquery.Shape`.
.. cadquery::
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)
Axis
====
The Axis constraint minimizes the angle between two vectors. It is frequently used to align faces
and control the rotation of an object.
The cost function is:
.. math::
( k_{ dir } \times ( param - \vec{ d_1 } \angle \vec{ d_2 } ) ^2
Where:
- :math:`k_{ dir }` is a scaling factor for directional constraints,
- :math:`param` is the parameter of the constraint, which defaults to :math:`\pi` radians,
- :math:`\vec{d_i}` is the direction created from the ith object argument as described below, and
- :math:`\vec{ d_1 } \angle \vec{ d_2 }` is the angle in radians between :math:`\vec{ d_1 }` and
:math:`\vec{ d_2 }`.
The argument ``param`` defaults to :math:`\pi` radians, which sets the two directions opposite
to each other. This represents what is often called a "mate" relationship, where the external faces
of two objects touch.
.. cadquery::
import cadquery as cq
cone = cq.Solid.makeCone(1, 0, 2)
assy = cq.Assembly()
assy.add(cone, name="cone0", color=cq.Color("green"))
assy.add(cone, name="cone1", color=cq.Color("blue"))
assy.constrain("cone0@faces@<Z", "cone1@faces@<Z", "Axis")
assy.solve()
show_object(assy)
If the ``param`` argument is set to zero, then the two objects will point in the same direction.
This is often used when one object goes through another, such as a pin going into a hole in a plate:
.. cadquery::
import cadquery as cq
plate = 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)
In creating an Axis constraint, a direction vector is extracted in one of three different ways,
depending on the object's type.
:class:`~cadquery.Face`:
Using :meth:`~cadquery.Face.normalAt`
:class:`~cadquery.Edge` and :meth:`~cadquery.Shape.geomType` is ``"CIRCLE"``:
Using :meth:`~cadquery.Mixin1D.normal`
:class:`~cadquery.Edge` and :meth:`~cadquery.Shape.geomType` is not ``"CIRCLE"``:
Using :meth:`~cadquery.Mixin1D.tangentAt`
Using any other type of object will raise a :exc:`ValueError`. By far the most common use case
is to define an Axis constraint from a :class:`~cadquery.Face`.
.. cadquery::
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)
Plane
=====
The Plane constraint is simply a combination of both the Point and Axis constraints. It is a
convenient shortcut for a commonly used combination of constraints. It can be used to shorten the
previous example from the two constraints to just one:
.. code-block:: diff
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)
The result of this code is identical to the above two constraint example.
For the cost function of Plane, please see the Point and Axis sections. The ``param`` argument is applied to Axis and should be left as the default value for a "mate" style
constraint (two surfaces touching) or can be set to ``0`` for a through surface constraint (see
description in the Axis constraint section).
PointInPlane
============
PointInPlane positions the center of the first object within the plane defined by the second object.
The cost function is:
.. math::
\operatorname{dist}( \vec{ c }, p_\text{ offset } ) ^2
Where:
- :math:`\vec{ c }` is the center of the first argument,
- :math:`p_\text{ offset }` is a plane created from the second object, offset in the plane's normal
direction by ``param``, and
- :math:`\operatorname{dist}( \vec{ a }, b)` is the distance between point :math:`\vec{ a }` and
plane :math:`b`.
.. cadquery::
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)
PointOnLine
===========
PointOnLine positions the center of the first object on the line defined by the second object.
The cost function is:
.. math::
( param - \operatorname{dist}(\vec{ c }, l ) )^2
Where:
- :math:`\vec{ c }` is the center of the first argument,
- :math:`l` is a line created from the second object
- :math:`param` is the parameter of the constraint, which defaults to 0,
- :math:`\operatorname{dist}( \vec{ a }, b)` is the distance between point :math:`\vec{ a }` and
line :math:`b`.
.. cadquery::
import cadquery as cq
b1 = cq.Workplane().box(1, 1, 1)
b2 = cq.Workplane().sphere(0.15)
assy = (
cq.Assembly()
.add(b1, name="b1")
.add(b2, loc=cq.Location((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 edges
assy.solve()
show_object(assy)
FixedPoint
==========
FixedPoint fixes the position of the given argument to be equal to the given point specified via the parameter of the constraint. This constraint locks all translational degrees of freedom of the argument.
The cost function is:
.. math::
\left\lVert \vec{ c } - \vec{param} \right\rVert ^2
Where:
- :math:`\vec{ c }` is the center of the argument,
- :math:`param` is the parameter of the constraint - tuple specifying the target position.
.. cadquery::
import cadquery as cq
b1 = cq.Workplane().box(1, 1, 1)
b2 = cq.Workplane().sphere(0.15)
assy = (
cq.Assembly()
.add(b1, name="b1")
.add(b2, loc=cq.Location((0, 0, 4)), name="b2", color=cq.Color("red"))
.add(b1, loc=cq.Location((-2, 0, 0)), name="b3", color=cq.Color("red"))
)
pnt = (0.5, 0.5, 0.5)
# fix the position of b1
assy.constrain("b1", "Fixed")
# fix b2 center at point
assy.constrain("b2", "FixedPoint", pnt)
# fix b3 vertex position at point
assy.constrain("b3@vertices@<X and <Y and <Z", "FixedPoint", pnt)
assy.solve()
show_object(assy)
FixedRotation
=============
FixedRotation fixes the rotation of the given argument to be equal to the value specified via the parameter of the constraint.
This constraint locks all rotational degrees of freedom of the argument.
The cost function is:
.. math::
\left\lVert \vec{ R } - \vec{param} \right\rVert ^2
Where:
- :math:`\vec{ R }` vector of the rotation angles of the rotation applied to the argument,
- :math:`param` is the parameter of the constraint - tuple specifying the target rotation.
.. cadquery::
import cadquery as cq
b1 = 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((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)
FixedAxis
=========
FixedAxis fixes the orientation of the given argument's normal or tangent to be equal to the orientation of the vector specified via the parameter of the constraint. This constraint locks two rotational degrees of freedom of the argument.
The cost function is:
.. math::
( \vec{ a } \angle \vec{ param } ) ^2
Where:
- :math:`\vec{ a }` normal or tangent vector of the argument,
- :math:`param` is the parameter of the constraint - tuple specifying the target direction.
.. cadquery::
import cadquery as cq
b1 = 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((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)
Assembly colors
---------------
Aside from RGBA values, the :class:`~cadquery.Color` class can be instantiated from a text name. Valid names are
listed along with a color sample below:
.. raw:: html
<div class="color-grid" style="display:grid;grid-gap:10px;grid-template-columns:repeat(auto-fill, minmax(200px,1fr));">
<div style="background-color:rgba(222,239,255,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">aliceblue</div>
<div style="background-color:rgba(243,211,173,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">antiquewhite</div>
<div style="background-color:rgba(255,220,180,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">antiquewhite1</div>
<div style="background-color:rgba(218,188,153,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">antiquewhite2</div>
<div style="background-color:rgba(155,134,110,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">antiquewhite3</div>
<div style="background-color:rgba(65,57,47,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">antiquewhite4</div>
<div style="background-color:rgba(54,255,167,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">aquamarine1</div>
<div style="background-color:rgba(46,218,144,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">aquamarine2</div>
<div style="background-color:rgba(15,65,44,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">aquamarine4</div>
<div style="background-color:rgba(222,255,255,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">azure</div>
<div style="background-color:rgba(190,218,218,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">azure2</div>
<div style="background-color:rgba(135,155,155,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">azure3</div>
<div style="background-color:rgba(57,65,65,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">azure4</div>
<div style="background-color:rgba(68,10,68,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">beet</div>
<div style="background-color:rgba(232,232,182,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">beige</div>
<div style="background-color:rgba(255,197,140,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">bisque</div>
<div style="background-color:rgba(218,169,120,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">bisque2</div>
<div style="background-color:rgba(155,120,87,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">bisque3</div>
<div style="background-color:rgba(65,52,37,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">bisque4</div>
<div style="background-color:rgba(0,0,0,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">black</div>
<div style="background-color:rgba(255,211,155,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">blanchedalmond</div>
<div style="background-color:rgba(0,0,255,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">blue</div>
<div style="background-color:rgba(0,0,255,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">blue1</div>
<div style="background-color:rgba(0,0,218,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">blue2</div>
<div style="background-color:rgba(0,0,155,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">blue3</div>
<div style="background-color:rgba(0,0,65,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">blue4</div>
<div style="background-color:rgba(64,6,193,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">blueviolet</div>
<div style="background-color:rgba(95,5,5,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">brown</div>
<div style="background-color:rgba(255,13,13,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">brown1</div>
<div style="background-color:rgba(218,11,11,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">brown2</div>
<div style="background-color:rgba(155,8,8,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">brown3</div>
<div style="background-color:rgba(65,4,4,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">brown4</div>
<div style="background-color:rgba(186,122,61,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">burlywood</div>
<div style="background-color:rgba(255,166,83,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">burlywood1</div>
<div style="background-color:rgba(218,142,72,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">burlywood2</div>
<div style="background-color:rgba(155,102,52,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">burlywood3</div>
<div style="background-color:rgba(65,43,23,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">burlywood4</div>
<div style="background-color:rgba(29,87,89,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">cadetblue</div>
<div style="background-color:rgba(80,232,255,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">cadetblue1</div>
<div style="background-color:rgba(68,199,218,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">cadetblue2</div>
<div style="background-color:rgba(49,142,155,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">cadetblue3</div>
<div style="background-color:rgba(22,60,65,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">cadetblue4</div>
<div style="background-color:rgba(54,255,0,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">chartreuse</div>
<div style="background-color:rgba(54,255,0,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">chartreuse1</div>
<div style="background-color:rgba(46,218,0,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">chartreuse2</div>
<div style="background-color:rgba(33,155,0,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">chartreuse3</div>
<div style="background-color:rgba(15,65,0,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">chartreuse4</div>
<div style="background-color:rgba(164,36,3,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">chocolate</div>
<div style="background-color:rgba(255,54,4,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">chocolate1</div>
<div style="background-color:rgba(218,46,3,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">chocolate2</div>
<div style="background-color:rgba(155,33,3,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">chocolate3</div>
<div style="background-color:rgba(65,15,1,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">chocolate4</div>
<div style="background-color:rgba(255,54,20,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">coral</div>
<div style="background-color:rgba(255,42,23,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">coral1</div>
<div style="background-color:rgba(218,36,20,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">coral2</div>
<div style="background-color:rgba(155,26,15,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">coral3</div>
<div style="background-color:rgba(65,12,7,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">coral4</div>
<div style="background-color:rgba(32,76,215,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">cornflowerblue</div>
<div style="background-color:rgba(255,239,182,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">cornsilk1</div>
<div style="background-color:rgba(218,205,155,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">cornsilk2</div>
<div style="background-color:rgba(155,147,112,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">cornsilk3</div>
<div style="background-color:rgba(65,62,47,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">cornsilk4</div>
<div style="background-color:rgba(0,255,255,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">cyan</div>
<div style="background-color:rgba(0,255,255,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">cyan1</div>
<div style="background-color:rgba(0,218,218,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">cyan2</div>
<div style="background-color:rgba(0,155,155,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">cyan3</div>
<div style="background-color:rgba(0,65,65,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">cyan4</div>
<div style="background-color:rgba(122,60,0,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkgoldenrod</div>
<div style="background-color:rgba(255,123,1,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkgoldenrod1</div>
<div style="background-color:rgba(218,106,1,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkgoldenrod2</div>
<div style="background-color:rgba(155,76,0,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkgoldenrod3</div>
<div style="background-color:rgba(65,33,0,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkgoldenrod4</div>
<div style="background-color:rgba(0,32,0,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkgreen</div>
<div style="background-color:rgba(129,120,37,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkkhaki</div>
<div style="background-color:rgba(23,37,7,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkolivegreen</div>
<div style="background-color:rgba(150,255,41,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">darkolivegreen1</div>
<div style="background-color:rgba(128,218,35,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkolivegreen2</div>
<div style="background-color:rgba(92,155,26,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkolivegreen3</div>
<div style="background-color:rgba(39,65,11,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkolivegreen4</div>
<div style="background-color:rgba(255,66,0,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkorange</div>
<div style="background-color:rgba(255,54,0,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkorange1</div>
<div style="background-color:rgba(218,46,0,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkorange2</div>
<div style="background-color:rgba(155,33,0,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkorange3</div>
<div style="background-color:rgba(65,15,0,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkorange4</div>
<div style="background-color:rgba(81,8,153,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkorchid</div>
<div style="background-color:rgba(132,12,255,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">darkorchid1</div>
<div style="background-color:rgba(113,10,218,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkorchid2</div>
<div style="background-color:rgba(82,8,155,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkorchid3</div>
<div style="background-color:rgba(35,4,65,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkorchid4</div>
<div style="background-color:rgba(207,77,49,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darksalmon</div>
<div style="background-color:rgba(70,128,70,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkseagreen</div>
<div style="background-color:rgba(135,255,135,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">darkseagreen1</div>
<div style="background-color:rgba(116,218,116,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">darkseagreen2</div>
<div style="background-color:rgba(83,155,83,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkseagreen3</div>
<div style="background-color:rgba(36,65,36,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkseagreen4</div>
<div style="background-color:rgba(16,11,65,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkslateblue</div>
<div style="background-color:rgba(7,19,19,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkslategray</div>
<div style="background-color:rgba(78,255,255,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">darkslategray1</div>
<div style="background-color:rgba(67,218,218,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">darkslategray2</div>
<div style="background-color:rgba(48,155,155,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkslategray3</div>
<div style="background-color:rgba(21,65,65,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkslategray4</div>
<div style="background-color:rgba(0,157,162,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkturquoise</div>
<div style="background-color:rgba(75,0,166,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">darkviolet</div>
<div style="background-color:rgba(255,1,74,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">deeppink</div>
<div style="background-color:rgba(218,1,63,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">deeppink2</div>
<div style="background-color:rgba(155,1,46,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">deeppink3</div>
<div style="background-color:rgba(65,0,20,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">deeppink4</div>
<div style="background-color:rgba(0,132,255,1.0);padding:10px;border-radius:5px;color:rgba(0,0,0);">deepskyblue1</div>
<div style="background-color:rgba(0,113,218,1.0);padding:10px;border-radius:5px;color:rgba(255,255,255);">deepskyblue2</div>