cadquery icon indicating copy to clipboard operation
cadquery copied to clipboard

How to get the absolute location of a tag?

Open lenianiva opened this issue 7 months ago • 7 comments

Consider this example:

import cadquery as cq

def makeBoxWithTag():
    result = (
        cq.Workplane('XY')
        .box(1, 1, 1, centered=False)
    )
    result.vertices(">X and >Y and >Z").tag("v")
    return result
assembly = (
    cq.Assembly()
    .add(cq.Solid.makeBox(1, 1, 1), name="b1")
    .constrain("b1", "Fixed")
    .add(makeBoxWithTag(), name="b2", loc=cq.Location((0,0,1)))
    .constrain("b2", "Fixed")
    .add(cq.Solid.makeBox(1, 1, 1), name="b3", loc=cq.Location((0,0,2)))
    .constrain("b3", "Fixed")
    .add(cq.Solid.makeSphere(0.2, angleDegrees1=-90), name="m")
    .constrain("m", "b2@faces@>Y", "Point")
    .solve()
)

def print_loc(self: cq.Assembly, tag: str) -> cq.Location:
    name, shape = self._query(tag)
    loc_shape = shape.location()
    loc_centre = cq.Location(shape.Center())
    loc_parent, top = self._subloc(name)
    print(f"[{tag}] {name}: {shape}")
    print(f"    Shape: {loc_shape.toTuple()} ({type(loc_shape)})")
    print(f"    Centre: {loc_centre.toTuple()} ({type(loc_centre)})")
    print(f"    Parent: {loc_parent.toTuple()} ({type(loc_parent)})")
    _, top_shape = self._query(top)
    loc_top_shape = top_shape.location()
    print(f"    Top: {loc_top_shape.toTuple()}")
    print(f"    Top.loc: {top_shape.location().toTuple()}")
    loc_self = self.loc
    print(f"    Self: {loc_self.toTuple()}")
    loc = loc_parent * loc_centre
    print(f"    Total: {loc.toTuple()}")
print_loc(assembly, "b2@faces@>Y")
print_loc(assembly, "b2?v")

result = assembly

The location of the middle face has z = 1.5, but none of the functions I've tried in print_loc outputs a location of z = 1.5. How can I get its location?

absloc

lenianiva avatar Jul 21 '24 06:07 lenianiva