The BinarySelector()
class in CadQuery is used to filter out specific objects based on their properties. It allows the user to select objects that meet specific criteria and exclude objects that don't.
BinarySelector(property, operation, value)
property
(string): The name of the property that the user wants to filter the objects by.operation
(string): The operation that should be used to filter the objects. CadQuery supports the following operations:
"=="
(equal to)"!="
(not equal to)"~"
(contains)"<"
(less than)">"
(greater than)value
(any): The value that the property must meet for a given object to be selected.import cq
# Creates three cylinders with different diameters
cylinder1 = cq.Workplane().circle(10).extrude(30)
cylinder2 = cq.Workplane().circle(20).extrude(30)
cylinder3 = cq.Workplane().circle(30).extrude(30)
# Creates a compound of the cylinders
cylinders = cq.Compound.makeCompound([cylinder1, cylinder2, cylinder3])
# Filters the cylinders based on their diameter
small_cylinders = cylinders.each(lambda obj: obj.faces(">Z").edges().val().distinct().size() == 10)
medium_cylinders = cylinders.each(lambda obj: obj.faces(">Z").edges().val().distinct().size() == 20)
large_cylinders = cylinders.each(lambda obj: obj.faces(">Z").edges().val().distinct().size() == 30)
# Prints out the number of small, medium, and large cylinders
print(f"Small cylinders: {len(small_cylinders.solids())}")
print(f"Medium cylinders: {len(medium_cylinders.solids())}")
print(f"Large cylinders: {len(large_cylinders.solids())}")
In this example, the user can filter the cylinders based on their diameter by using the each()
method and passing it a lambda function that returns True
if the diameter of an object is equal to a certain value. The lambda function is passed as an argument to the BinarySelector()
class, which uses it to filter out the objects that don't meet the criteria.