Intermittent constraint solving problem in sketch rectangular array.

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
vectronic
Posts: 33
Joined: Sun Feb 18, 2018 9:44 pm

Intermittent constraint solving problem in sketch rectangular array.

Postby vectronic » Tue May 26, 2020 10:31 am

Hello,

I have successfully been using the python API in a macro to create lego brick models derived from sketches.

https://github.com/vectronic/freecad-legify-macros

I've come across an issue in using "addRectangularArray()" in Sketcher. Intermittently (perhaps 1/3 of the time) some of the copies of the original sketch created by the rectangular array are 'replicated' slightly differently.

This is the line of code where I am using `addRectangularArray`:

https://github.com/vectronic/freecad-le ... uds.py#L57

NOTE: I don't believe this issue is actually related to the fact I am using scripting, it is more about creating a rectangular array in a sketch in general. So I am not asking for help debugging my script! Also, my script works fine 2/3 of the time, so I don't believe it is an issue in my script code.

First screenshot below show an example from one of the times the macro has produced an invalid result. You can see that 2 out of the 8 replicated studs have not rendered properly.

The second screenshot shows the replicated array in the sketch. One stud is the original constructed sketch and then 8 studs are replicated using a rectangular array.

The third screenshot shows a closeup of 2 of the replicated studs, one correct (right) and the other incorrect (left). A line segment on the incorrect stud has been flipped i.e. start point is flipped with end point. This causes the arcs connecting the line segment to be much larger to satisfy the constraints and results in an invalid geometry for padding.

So essentially, about 1/3 of the time when I execute my macro, 1 or 2 of the 8 replicated studs has a line segment flipped...

I have attached the model file.

I am after any tips on what I could do to make sure that when the array instances of the sketch are created, they are completely deterministic.

Currently it seems either to be:

a. a bug in the sketch workspace, or
b. I am not providing the correct info for the constraint resolver to always reach the same solution.

Thanks very much.

OS: macOS 10.15
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.21093 (Git)
Build type: Release
Branch: master
Hash: 31bb4d84b92bc587bd79912dd15e132713610773
Python version: 3.8.2
Qt version: 5.12.5
Coin version: 4.0.0
OCC version: 7.4.0
Locale: C/Default (C)


Screenshot 2020-05-25 at 15.38.14.png
Screenshot 2020-05-25 at 15.38.14.png (135.17 KiB) Viewed 160 times
Screenshot 2020-05-25 at 15.36.46.png
Screenshot 2020-05-25 at 15.36.46.png (317.88 KiB) Viewed 160 times
Screenshot 2020-05-25 at 15.37.18.png
Screenshot 2020-05-25 at 15.37.18.png (123.73 KiB) Viewed 160 times
Attachments
problem.FCStd
(437.92 KiB) Downloaded 3 times
User avatar
papyblaise
Posts: 1950
Joined: Thu Jun 13, 2019 4:28 pm
Location: France

Re: Intermittent constraint solving problem in sketch rectangular array.

Postby papyblaise » Tue May 26, 2020 11:25 am

Hi
it's a autointersection beetween cercle and flat side
Attachments
lego.PNG
lego.PNG (18.6 KiB) Viewed 152 times
Syres
Posts: 704
Joined: Thu Aug 09, 2018 11:14 am

Re: Intermittent constraint solving problem in sketch rectangular array.

Postby Syres » Tue May 26, 2020 11:47 am

Unfortunately as much as I tried with the following three builds I couldn't get your macro to create the faulty sketch.

OS: Windows 7 SP 1 (6.1)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.21097 (Git)
Build type: Release
Branch: master
Hash: a5ab09aa0941cc945e59848897b160f8cbfc0831
Python version: 3.6.8
Qt version: 5.12.1
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/United Kingdom (en_GB)

OS: Windows 7 SP 1 (6.1)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.21049 (Git)
Build type: Release
Branch: master
Hash: 9c27f1078e5ec516fe882aac1a27f5c6c6174554
Python version: 3.8.2
Qt version: 5.12.5
Coin version: 4.0.0
OCC version: 7.4.0
Locale: English/United Kingdom (en_GB)

OS: Windows 7 SP 1 (6.1)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.21255 (Git)
Build type: Release
Branch: master
Hash: dfdcd5351b686f1a452eb840b90cffff1b93c112
Python version: 3.6.8
Qt version: 5.12.1
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/United Kingdom (en_GB)
vectronic
Posts: 33
Joined: Sun Feb 18, 2018 9:44 pm

Re: Intermittent constraint solving problem in sketch rectangular array.

Postby vectronic » Tue May 26, 2020 12:14 pm

Syres wrote:
Tue May 26, 2020 11:47 am
Unfortunately as much as I tried with the following three builds I couldn't get your macro to create the faulty sketch.
Hi Syres,

Thanks for the effort in trying, I must admit I wasn't expecting anyone to try it out from the macros... and therefore I didn't say :oops: that the offending code is on a branch: https://github.com/vectronic/freecad-le ... s/tree/dev

If you still want to try again (after the pain of reading that) it would be a git clone, followed by a checkout of the dev branch...

And as I said it is a fairly inconsistent scenario.

To verify I am not going insane, I just retried it before posting this reply and have produced a screen recording here:

https://www.loom.com/share/bbff80657ed7 ... 9b62fcdcb0

As you can see:

- the first time I run the macro it works fine.
- the second time the result is fine, but there are constraint errors in the logs.
- the last time, there are constraint errors and nothing gets rendered. The sketch says it is constrained properly but visually you can see it is invalid. Then after closing the sketch, the geometry is rendered. Upon re-opening the sketch, it is constrained and visually correct.

In the last scenario the sketch errors were different to what I described in my first post, so maybe it is less something to with individual constraints or geometry I am creating and perhaps more to do with how the rectangular array or constraint solver feels on a particular day :)

confused...
Syres
Posts: 704
Joined: Thu Aug 09, 2018 11:14 am

Re: Intermittent constraint solving problem in sketch rectangular array.

Postby Syres » Tue May 26, 2020 1:21 pm

vectronic wrote:
Tue May 26, 2020 12:14 pm
offending code is on a branch: https://github.com/vectronic/freecad-le ... s/tree/dev
The good news is......confirmed after 5 attempts using:

OS: Windows 7 SP 1 (6.1)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.21049 (Git)
Build type: Release
Branch: master
Hash: 9c27f1078e5ec516fe882aac1a27f5c6c6174554
Python version: 3.8.2
Qt version: 5.12.5
Coin version: 4.0.0
OCC version: 7.4.0
Locale: English/United Kingdom (en_GB)

The better news is.......it looks like it's already fixed. I carried out 24 attempts error free using:

OS: Windows 7 SP 1 (6.1)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.21255 (Git)
Build type: Release
Branch: master
Hash: dfdcd5351b686f1a452eb840b90cffff1b93c112
Python version: 3.6.8
Qt version: 5.12.1
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/United Kingdom (en_GB)


I can't confirm whether it's fixed in 019.21125 (the newest Mac build available) but hopefully you'll get a fixed build at the latest next week, hopefully confusion will be no longer!!
chrisb
Posts: 25155
Joined: Tue Mar 17, 2015 9:14 am

Re: Intermittent constraint solving problem in sketch rectangular array.

Postby chrisb » Tue May 26, 2020 2:56 pm

We see this behaviour now and then in manual sketching as well. It can occur when the solution of an even fully constrained sketch is not unique. You can most probably prevent it from happening if you specify the angles of the arcs and not some lengths. If you do it from a script anyway you could calculate the arc length from the dimensions you currently have.
vectronic
Posts: 33
Joined: Sun Feb 18, 2018 9:44 pm

Re: Intermittent constraint solving problem in sketch rectangular array.

Postby vectronic » Tue Jun 16, 2020 7:33 pm

Just an update on this as I got a bit of free time.

I tried with the latest build on MacOS and it still proved intermittent (e.g. 0.19 21514)

I can see one thing I could do is try and constrain the angle of the lines that are 'flipping' i.e. currently I constrain them to parallel to a construction line, instead I could set their angle. This might indeed reduce the number of possible constraint solutions and hence help me out.

One question if anyone knows:

Would it make any different to the constraint solver if I choose either of these two approaches in python:

1. Add all geometries and constraints at once:

Code: Select all

   geometries = []
    constraints = []

    geometries.append(Part.LineSegment(....))
    constraints.append(Sketcher.Constraint(....))
    ....
    geometries.append(Part.LineSegment(....))
    constraints.append(Sketcher.Constraint(....))
    ....
    geometries.append(Part.LineSegment(....))
    constraints.append(Sketcher.Constraint(....))
    ....
    geometries.append(Part.LineSegment(....))
    constraints.append(Sketcher.Constraint(....))
    
    sketch.addGeometry(geometries, False)
    sketch.addConstraint(constraints)
or

2. Add geometries and constraints bit by bit:

Code: Select all

   geometries = []
    constraints = []

    geometries.append(Part.LineSegment(....))
    constraints.append(Sketcher.Constraint(....))
    ....
    geometries.append(Part.LineSegment(....))
    constraints.append(Sketcher.Constraint(....))

    sketch.addGeometry(geometries, False)
    sketch.addConstraint(constraints)

   geometries = []
    constraints = []

    geometries.append(Part.LineSegment(....))
    constraints.append(Sketcher.Constraint(....))
    ....
    geometries.append(Part.LineSegment(....))
    constraints.append(Sketcher.Constraint(....))

    sketch.addGeometry(geometries, False)
    sketch.addConstraint(constraints)
[code]

- Possibly after each partial step of sketch creation in the second case I should invoke recompute() on the document?


I have discovered another issue which is probably unrelated but similar in that it is completely intermittent...

I have python code which selects edges to fillet via various matching logic on plane normals, number of vertex etc.. (the search and filter is due to the lack of consistent shape naming...) 

The same code, on the same generated shape, sometimes finds the edges and sometimes doesn't. A seemingly random portion of the time the macro does one thing or another. See screenshot where in this attempt the front facing 'stud' is not filleted. On another run, it might the left one which isn't filleted...

At a high level, for this one, I guess my question is:

- Are there any threading issues at play which I need to be aware of when scripting? 
- After every feature creation step, I invoke recompute() on the document. Is there anything else I should do to make sure, the geometry state is always consistent?
- Is it possible that the scripting layer just isn't able to consistently create the same geometry for some reason? (I doubt it... but...)

[attachment=0]Screenshot 2020-06-16 at 20.24.41.png[/attachment]
Attachments
Screenshot 2020-06-16 at 20.24.41.png
Screenshot 2020-06-16 at 20.24.41.png (67.38 KiB) Viewed 49 times