Skip to content

Sketcher: Harden against flip with signed constraints#26518

Merged
chennes merged 3 commits intoFreeCAD:mainfrom
theo-vt:sketcher_oriented_circleline_distance
Mar 30, 2026
Merged

Sketcher: Harden against flip with signed constraints#26518
chennes merged 3 commits intoFreeCAD:mainfrom
theo-vt:sketcher_oriented_circleline_distance

Conversation

@theo-vt
Copy link
Copy Markdown
Contributor

@theo-vt theo-vt commented Dec 29, 2025

This PR adds a concept of orientation to

  • point-line distance
  • circle-circle distance
  • circle-line distance

to make sketches harder to flip

The orientation is saved to file and handled by the App::Constraint rather than the planegcs::Constraint to better capture design intent (it allows to react to external geometry movement, which the solver is blind to)

The orientation is computed on sketch restore so older files are upgraded seamlessly

The constraints can still be made driven or deactivated and their orientation is recomputed when made driving/reactivated so the user can manually flip them

Point-Line distance

This PR makes point-line constraints signed which prevents flips when constraints values change drastically or a reference geometry moves around

Before:

Screencast.From.2025-12-16.15-49-43.mp4

After:

Screencast.From.2025-12-16.15-50-50.mp4

Circle-Circle distance

This PR adds a concept of relative size for circle-circle distance which allows to keep an inner circle and an outer circle when one of the circles is completely within the other

Before:

Screencast.From.2025-12-18.23-16-07.mp4

After:

Screencast.From.2025-12-18.23-15-23.mp4

Circle-Line distance

This PR adds an orientation concept to circle-line distances. 4 possible orientations are established with 2 flags: ccw and internal.

A ccw orientation means that the sequence [line.pt1, line.pt2, circle.center] is and should remain ccw, this is the same as the point-line distance orientation from https://github.com/FreeCAD/FreeCAD/pull/26228/changes

An internal orientation means that the line is and should remain closer to the center then the radius of the circle

Before:

Screencast.From.2025-12-29.10-04-07.mp4

After:

Screencast.From.2025-12-29.10-05-23.mp4

Issues

Fix: #26634
Fix #17579

@github-actions github-actions bot added Mod: Core Issue or PR touches core sections (App, Gui, Base) of FreeCAD Mod: Sketcher Related to the Sketcher Workbench 3rd party component Issue related to 3rd party component labels Dec 29, 2025
@maxwxyz maxwxyz added the Type: Feature FR for improvements or new features label Dec 29, 2025
@maxwxyz maxwxyz added this to the 1.2 milestone Dec 29, 2025
@maxwxyz maxwxyz moved this from Queue to Merge Meeting in Merge Queue Dec 29, 2025
@theo-vt theo-vt changed the title Sketcher: Harden against flip 3 Sketcher: Harden against flip 4 Dec 29, 2025
@theo-vt theo-vt force-pushed the sketcher_oriented_circleline_distance branch 2 times, most recently from be9a4ab to 8757b68 Compare January 4, 2026 14:37
@theo-vt theo-vt marked this pull request as ready for review January 6, 2026 18:29
@maxwxyz maxwxyz requested a review from FlachyJoe January 9, 2026 16:37
@theo-vt theo-vt force-pushed the sketcher_oriented_circleline_distance branch from 18378b1 to f1269e8 Compare February 23, 2026 23:20
@theo-vt theo-vt changed the title Sketcher: Harden against flip 4 Sketcher: Harden against flip with signed constraints Feb 23, 2026
@theo-vt theo-vt marked this pull request as draft February 23, 2026 23:28
@theo-vt theo-vt force-pushed the sketcher_oriented_circleline_distance branch from 2a482d4 to 29ee990 Compare February 24, 2026 03:01
@theo-vt theo-vt marked this pull request as ready for review February 24, 2026 03:26
@belinski-roman
Copy link
Copy Markdown

What about parallel lines? I noticed that the offset is often broken in the sketch. Even for simple rectangles, I'm not talking about polygons.

@hyarion
Copy link
Copy Markdown
Contributor

hyarion commented Feb 24, 2026

What about parallel lines? I noticed that the offset is often broken in the sketch. Even for simple rectangles, I'm not talking about polygons.

do you mean like this?
image
the green dimension here is set between the two lines, but is actually a point-line-constraint.
Setting the red dimension to 40 on main branch makes this jump as @belinski-roman explains

To my understanding, that should result in a point-line distance which should be covered by this PR.

@belinski-roman
Copy link
Copy Markdown

but is actually a point-line-constraint

But when setting the size, you can specify two vertices, you can specify a vertex and a straight line, or you can specify two straight lines. Is it always considered as a constraint between a point and a straight line?

@hyarion
Copy link
Copy Markdown
Contributor

hyarion commented Feb 24, 2026

A rectangle with dimensions looks like this when horizontal and vertical constraints are removed and vertices moved around:

image

So a normal rectangle has point-point constraints.
That is not covered as we need three points to have a winding to ensure it is the same.

@theo-vt I think there's a case here we might be able to handle.
If a line (A to B) has horizontal constraint and a point-point dimension for the endpoints, then we could try to ensure that the vector AB still points in the same direction.
The big question is if we can make this connection between constraints.

Another option would be to change so rectangles gets line-point constraints by default.

@belinski-roman
Copy link
Copy Markdown

the current offset tool https://wiki .freecad.org/Sketcher_Offset Additionally, it draws auxiliary geometry — perpendicular segments (if you specify to create parametricity by setting the offset distance by size). In fact, you can force the distance from a point to a straight line to be tracked there. It's worth checking out

@hyarion
Copy link
Copy Markdown
Contributor

hyarion commented Feb 24, 2026

https://wiki.freecad.org/Sketcher_Offset

Yes, the geometries created with this tool already have point-line constraints which are covered :)

@belinski-roman
Copy link
Copy Markdown

Yes, the geometries created with this tool already have point-line constraints which are covered :)

Which does not prevent you from flipping in most cases.

@hyarion
Copy link
Copy Markdown
Contributor

hyarion commented Feb 24, 2026

Yes, the geometries created with this tool already have point-line constraints which are covered :)

Which does not prevent you from flipping in most cases.

Yes, but that's what I think this PR takes care of

@theo-vt
Copy link
Copy Markdown
Contributor Author

theo-vt commented Feb 24, 2026

@theo-vt I think there's a case here we might be able to handle.
If a line (A to B) has horizontal constraint and a point-point dimension for the endpoints, then we could try to ensure that the vector AB still points in the same direction.
The big question is if we can make this connection between constraints.

This is handled in #25645 the constraints are iteratively reduced to simpler constraints and it tries to use as many ConstraintDifference (signed) as possible

Currently this only works for axis aligned lines, but I want to extend it in 2 ways

  • Transform point-point distance into point-line distance if the 2 lines are perpendicular
  • Try to extend line sign when their angle can be deduced before

But I am marking it as draft because after the discussion in the cad-topic, I think this algorithm should be moved to App level rather than solver level to do smarter things, handle external geometries and eventually include other algorithms

@theo-vt theo-vt force-pushed the sketcher_oriented_circleline_distance branch from a7bb8d1 to de5b05c Compare February 25, 2026 17:27
@theo-vt
Copy link
Copy Markdown
Contributor Author

theo-vt commented Mar 3, 2026

Here are builds if people want to test!
https://github.com/theo-vt/FreeCAD/releases/tag/weekly-2026.03.02

@belinski-roman
Copy link
Copy Markdown

Here are builds if people want to test!

https://github.com/theo-vt/FreeCAD/releases/tag/weekly-2026.03.02

I tested it and didn't see any difference

trim.FDA88948-1A6C-4562-B39E-5D09361AD467.MOV

@belinski-roman
Copy link
Copy Markdown

Discord suggested what's the matter, but then you need to redo the Offset tool itself.

2026-03-03.15-23-14.mp4

@hyarion
Copy link
Copy Markdown
Contributor

hyarion commented Mar 3, 2026

These issues are because point-to-point constraints are created and not the constraints modified in this PR.

I think we should change how the constraints are set up for rectangles, offsets etc, but that would be best in a follow up PR

theo-vt added 2 commits March 16, 2026 10:19
Sketcher: Make circle-circle distance signed

Sketcher: Make circle-line distance signed

Apply review suggestions
@theo-vt theo-vt force-pushed the sketcher_oriented_circleline_distance branch from d6e429f to 7771ecf Compare March 16, 2026 16:05
@maxwxyz maxwxyz added Requires: Testing The PR needs testing by users and developers Approved: Code Quality The PR was checked for code quality and approved labels Mar 27, 2026
@maxwxyz maxwxyz moved this from Merge Meeting to Approved in Merge Queue Mar 27, 2026
Copy link
Copy Markdown
Collaborator

@maxwxyz maxwxyz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have played around a bit and did not find any issues

@maxwxyz maxwxyz added Approved: Tested The PR was manually tested and approved and removed Requires: Testing The PR needs testing by users and developers labels Mar 30, 2026
@chennes chennes merged commit 4a7f6ae into FreeCAD:main Mar 30, 2026
24 checks passed
@github-project-automation github-project-automation bot moved this from Approved to Done in Merge Queue Mar 30, 2026
@FEA-eng
Copy link
Copy Markdown
Contributor

FEA-eng commented Mar 30, 2026

I've tried testing it with the first example shown here, and it still flipped, but maybe I used the wrong constraints?

Flipping.FCStd.zip

OS: Windows 11 build 26200
Architecture: x86_64
Version: 1.2.0dev.46046 (Git)
Build date: 2026/03/30 15:55:23
Build type: Release
Branch: main
Hash: 8cc01fa8bd5c0ef9cbd74b4dd0d1bed7d79f7272
Python 3.11.14, Qt 6.8.3, Coin 4.0.3, Vtk 9.3.0, boost 1_84, Eigen3 3.4.0, PySide 6.8.3
shiboken 6.8.3, xerces-c 3.3.0, IfcOpenShell 0.0.0, OCC 7.8.1
Locale: Polish/Poland (pl_PL)
Stylesheet/Theme/QtStyle: FreeCAD.qss/FreeCAD Light/
Navigation Style/Orbit Style/Rotation Mode: Blender/Rounded Arcball/Window center
Logical DPI/Physical DPI/Pixel Ratio: 96/81.5973/1

@theo-vt
Copy link
Copy Markdown
Contributor Author

theo-vt commented Mar 30, 2026

This is expected! Flipping is pretty vaporous problem so it can't be solved with a single pr, the example flip still does flip and I have future plans to solve it somewhat!

Reqrefusion pushed a commit to Reqrefusion/FreeCAD-Documentation-Project that referenced this pull request Mar 30, 2026
Reqrefusion pushed a commit to Reqrefusion/FreeCAD-Documentation-html that referenced this pull request Mar 30, 2026
@chennes
Copy link
Copy Markdown
Member

chennes commented Apr 1, 2026

@theo-vt this is breaking two of my integration test files. Can you look at these two cases?

Recompute fails:
https://github.com/chennes/IntegrationTests/blob/main/Data/CADFiles/FL_AP3030S8_TNutM6.FCStd

Large volume change:
https://github.com/chennes/IntegrationTests/blob/main/Data/CADFiles/MG_KidToy.FCStd

@hyarion
Copy link
Copy Markdown
Contributor

hyarion commented Apr 1, 2026

I've tried testing it with the first example shown here, and it still flipped, but maybe I used the wrong constraints?

this PR fixes flipping when constraints are based on more than two points (three points or point+line)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3rd party component Issue related to 3rd party component Approved: Code Quality The PR was checked for code quality and approved Approved: Tested The PR was manually tested and approved Mod: Core Issue or PR touches core sections (App, Gui, Base) of FreeCAD Mod: Sketcher Related to the Sketcher Workbench Type: Feature FR for improvements or new features

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Sketcher: circle-line distance fails when circle radius is free in some orientations Sketcher: Solver Issues with Distant Constraints flip direction

7 participants