Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sketch_solver.line_point has incorrect implementation? #1127

Closed
voneiden opened this issue Jul 26, 2022 · 5 comments · Fixed by #1336
Closed

sketch_solver.line_point has incorrect implementation? #1127

voneiden opened this issue Jul 26, 2022 · 5 comments · Fixed by #1336

Comments

@voneiden
Copy link
Contributor

Background

Tested out sketching with constraints and found myself in a situation where it seemed like my Distance constraint between a midpoint of a line and a midpoint of an arc was not respected.

rotary_enc_od = 6
result = (
    cq.Sketch()
        .segment((-1, 0), (1, 0), "seg")
        .arc((0, 0), rotary_enc_od/2, 0, 200, "arc")
        .constrain("arc", "FixedPoint", None)
        .constrain("arc", "Radius", rotary_enc_od/2)
        .constrain("arc", "seg", "Coincident", None)
        .constrain("seg", "arc", "Coincident", None)
        .constrain("seg", "Orientation", (1, 0))
        .constrain("arc", "seg", "Distance", (0.5, 0.5, 4.5))
        .solve()
        .assemble()
)
show_object(result)

Output
image

Expected
image

Difference is subtle, but the current output produces a distance that is about 3.88 (measured from stl output) instead of the constrained 4.5.

Probable cause

After fiddling a bit with a debugger I noticed that line_point returned a rather odd value for the midpoint (-1.64, -0.85) instead of an expected (0, -0.56).

https:/CadQuery/cadquery/blob/master/cadquery/sketch.py#L924 sends lines/segments to the solver as a tuple (p1.x, p1.y, p2.x, p2.y) where p1 is start point and p2 is end point.

https:/CadQuery/cadquery/blob/master/cadquery/occ_impl/sketch_solver.py#L85 appears to behave as if the above tuple is actually (p1.x, p1.y, d.x, d.y) where p1 is start point and d is a vector from the start point to the end point.

Possible fix

I can get expected results if I change the implementation of line_point to

def line_point(x, val):
    return x[:2] + val * (x[2:] - x[:2])

Thoughts?

@fpq473
Copy link
Contributor

fpq473 commented Nov 24, 2022

I tried the suggested fix -- WFM and fwiw, LGTM.

voneiden added a commit to voneiden/cadquery that referenced this issue May 19, 2023
voneiden added a commit to voneiden/cadquery that referenced this issue May 20, 2023
@voneiden
Copy link
Contributor Author

@adam-urbanczyk I've added a test case that uses distance constraint on a rectangle that fails against master and succeeds against the fix.

@adam-urbanczyk
Copy link
Member

Will you open a PR?

@voneiden
Copy link
Contributor Author

Oh dear, have I've opened it one year ago against my own repo? 😂 Yeah I'll open a proper PR later today ..!

@voneiden
Copy link
Contributor Author

@adam-urbanczyk Alrighty: #1336

voneiden added a commit to voneiden/cadquery that referenced this issue Jun 14, 2023
Enable assert that fails without the fix ( issue CadQuery#1127 )
adam-urbanczyk added a commit that referenced this issue Jun 17, 2023
* Sketch: Fix line_point implementation

Enable assert that fails without the fix ( issue #1127 )

* Change channel order

---------

Co-authored-by: AU <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants