Page 1 of 2

Nester

Posted: Tue Jul 18, 2017 9:19 pm
by yorik
My nesting code is finally up enough to be merged, so it is now merged in git commit aba433880.

Warning, it is still very rough, slow and buggy. It will only work with pretty simple shapes like in the example below. It will still need a lot of work to become reasonably fast.

How it works:

1) create a series of flat shapes. They must all, at the moment, contain exactly one face, and have the same normal (same orientation). One of the shapes, that will be the container, must be rectangular. All these restrictions should be removed in the future.
Screenshot from 2017-07-18 17-57-08.png
Screenshot from 2017-07-18 17-57-08.png (105.01 KiB) Viewed 10017 times
2) Press the "Nest" button under the Arch panel tools

3) Add the container shape as Container, and all the others as Shapes

4) Change settings if you wish, then press "Start"
Screenshot from 2017-07-18 17-57-40.png
Screenshot from 2017-07-18 17-57-40.png (176.51 KiB) Viewed 10017 times
5) Wait for the calculation to finish. On my machine, nesting the shapes here take around one minute (I told you, it's slow).

6) Click "Preview"

7) If you like the solution, press "Ok". The preview will disappear and the shapes will all be moved to occupy the correct position.
Screenshot from 2017-07-18 17-59-04.png
Screenshot from 2017-07-18 17-59-04.png (111.8 KiB) Viewed 10017 times
There is still a lot of work to do, allow non-rectangular container, add support for margins/padding, allow some pieces to restrict to certain rotations, etc... Plus a lot of optimizing, and maybe making some of it multi-threaded, since several parts of the process could run in parallel, and after all that, allow to make several passes to refine the solution (by swapping pieces order). But it works already, that's already a big step :)

The code is divided between the task panel, which is in ArchPanel.py, and the nesting algorithm itself, which is in ArchNesting.py. The idea is that it could be used by other workbenches in case they don't like the task panel, they could build other GUI tools around it.

I tried to organize and comment that file a lot, so hopefully other people will be interested im jumping in and helping to optimize it.

Re: Nester

Posted: Wed Jul 19, 2017 3:10 am
by bitacovir
Impressive.
Well done Yorik :D

Re: Nester

Posted: Wed Jul 19, 2017 3:41 am
by looo
Nice to see some work done in this direction.

Just tried it, but the result wasn't that smooth:
nesting.png
nesting.png (6.79 KiB) Viewed 9966 times
Also I first tried it with some custom face-objects, which use ViewProvider.Proxy=0 and therefore have no "getIcon". This makes your script fail. I believe it is in this line:
https://github.com/FreeCAD/FreeCAD/blob ... l.py#L1304

and could be solved by checking for:

Code: Select all

if hasattr(obj.ViewObject,"Proxy") and hasattr(obj.ViewObject.Proxy, "getIcon"):
But anyway, great work!

Once the solver is working it shouldn't be too much work to get it implemented in c++ and use some multi-threading. I believe this will speed up the algorithm quite a bit :D

Re: Nester

Posted: Wed Jul 19, 2017 1:34 pm
by yorik
looo wrote: Wed Jul 19, 2017 3:41 amJust tried it, but the result wasn't that smooth:
Do you still have the file? It's a good, simple test case that fails, it's useful...
looo wrote: Wed Jul 19, 2017 3:41 am could be solved by checking for:

Code: Select all

if hasattr(obj.ViewObject,"Proxy") and hasattr(obj.ViewObject.Proxy, "getIcon"):

But anyway, great work!

Once the solver is working it shouldn't be too much work to get it implemented in c++ and use some multi-threading
Thanks, I'll add that fix. Yeah, the idea obviously is to port it to C++ once it's robust enough. Since a big part of the slowdown is lists processing, only that will make a huge difference. It only uses basic Part stuff so it should be easy. But at the moment it's much easier for me to develop/test in python...

I just found out that the good old DraftGeomUtils.findIntersection() function is faster than Part.distToShape()!

Re: Nester

Posted: Wed Jul 19, 2017 3:07 pm
by cox
Frantic applause, from the North. :-) Nice feature.

Re: Nester

Posted: Wed Jul 19, 2017 4:37 pm
by chrisb
Since a long time I have link bookmarked to http://svgnest.com/. I guess it can be deleted now :)

Re: Nester

Posted: Sat Jul 29, 2017 9:45 pm
by emills2
very nice.
we can already 'pre-pad' the shapes to account for cut width, but do you have any plans to include an adjustable spacing between the parts and the edges?

Re: Nester

Posted: Mon Jul 31, 2017 3:17 pm
by yorik
emills2 wrote: Sat Jul 29, 2017 9:45 pmdo you have any plans to include an adjustable spacing between the parts and the edges?
yes of course... That, and the ability to use non-rectangular container, is what I'll do next

Re: Nester

Posted: Mon Apr 23, 2018 6:27 am
by dschwartfeger
It wouldn't run for me until I changed the following-
I'm not sure if this is the correct way to do things but found a similar problem on the net and it worked!

Code: Select all

        if FreeCAD.GuiUp:
            from PySide import QtGui
            #changed qApp to QApplication.instance() on the next line
            QtGui.QApplication.instance().processEvents()
thanks for the good work.

Re: Nester

Posted: Mon Apr 23, 2018 7:53 am
by looo
I guess this should be included in master already:
https://github.com/FreeCAD/FreeCAD/pull ... 5f3c4R1347