[Merged]Regular polygons

Merged, abandoned or rejected pull requests are moved here to clear the main Pull Requests forum.
User avatar
DevJohan
Posts: 41
Joined: Sun Jul 13, 2014 2:36 pm
Location: Stockholm, Sweden

[Merged]Regular polygons

Postby DevJohan » Sun Jul 13, 2014 7:32 pm

Hi everybody,

I'm new to this forum and pretty new playing with the FreeCAD code.

I've written some code for creating regular polygons in the Sketcher workbench. Added icons for triangle up to octagon. These changes are available on https://github.com/DevJohan/FreeCAD_sf_ ... cherguihex.
regular_polygons_tb2.png
Actual workspace toolbar layout
regular_polygons_tb2.png (15.5 KiB) Viewed 3213 times
I think there might be some opinions on whether these types of objects should be available in the toolbars.
regular_polygons_tb1.png
All toolbar icons added
regular_polygons_tb1.png (3.75 KiB) Viewed 3213 times
Also, I used templates for creating the different versions of the regular polygons, but this should perhaps be made with a integer argument instead

Here are some examles
regular_polygons1.png
Some examples of regular polygons
regular_polygons1.png (177.4 KiB) Viewed 3213 times
regular_polygons_menu1.png
Icons added in sketcher menu
regular_polygons_menu1.png (92.05 KiB) Viewed 3213 times
Cheers,
Johan
wmayer
Site Admin
Posts: 14988
Joined: Thu Feb 19, 2009 10:32 am

Re: Regular polygons

Postby wmayer » Mon Jul 14, 2014 8:15 am

Hi Johan,

thanks for your great contribution!
I think there might be some opinions on whether these types of objects should be available in the toolbars.
In the Sketcher workbench we have the menu "Profiles" and this is the place where these commands are supposed to be added. We could add them also to the toolbar but we should use a drop down button because otherwise the toolbar becomes too large.
Also, I used templates for creating the different versions of the regular polygons, but this should perhaps be made with a integer argument instead
You mean to have a general function to create a n-gon (with n == number of corners)? A few weeks ago I wrote some Python code to create a hexagon and later tried to create another polygon (octagon IIRC) and it appeared that the code, i.e. the number of angle constraints was totally different so that I don't think such a general function can be written.
User avatar
DevJohan
Posts: 41
Joined: Sun Jul 13, 2014 2:36 pm
Location: Stockholm, Sweden

Re: Regular polygons

Postby DevJohan » Mon Jul 14, 2014 10:20 am

Hi,
wmayer wrote:You mean to have a general function to create a n-gon (with n == number of corners)? A few weeks ago I wrote some Python code to create a hexagon and later tried to create another polygon (octagon IIRC) and it appeared that the code, i.e. the number of angle constraints was totally different so that I don't think such a general function can be written.
I think my function is general. Not that I've done extensive testing, but I assume that "number of corners" - 3 is the number of angular constraints needed to make the polygon regular. At least works from 3 to 8 corners. What I meant by template is that the function is a C++ template function with a size_t parameter. This basically means that dedicated code is generated for every declared n-gon. So at runtime this code isn't general.
wmayer wrote:In the Sketcher workbench we have the menu "Profiles" and this is the place where these commands are supposed to be added. We could add them also to the toolbar but we should use a drop down button because otherwise the toolbar becomes too large.
I've added the taskbar items to a drop down button which can be seen in the second image. The pictures with all buttons laid out was just for illustration.

One question I have is if the toolbars shouldn't be linked across different modules? I added some functions in Sketchers "Workspace.h" which sets up the toolbars associated with sketcher and called those functions from the toolbar setup of both Sketcher and PartDesign. This makes the similar code which needs be update for every modification less spread out and without repetition.

/Johan
ian.rees
Posts: 696
Joined: Sun Jun 15, 2014 3:28 am
Contact:

Re: Regular polygons

Postby ian.rees » Tue Jul 15, 2014 2:40 am

Hi Johan,

Thanks for putting this together! I was thinking a hexagon tool would be handy the other day for drawing wrench flats.
I assume that "number of corners" - 3 is the number of angular constraints needed to make the polygon regular.
This isn't quite right, but there probably is a general rule. For example, imagine a 4-sided polygon with one angle (same argument for two angles) constrained to 90 degrees, and all sides equal length. Either a square or an 'L' could satisfy those requirements.

The dropdown box is really nice, since it lets me pick which N-gon I can get to by default, and gives me an easy way to change N as required, without taking up much more screen space than a single icon. Will include a quick screen capture here for folks that haven't tried it yet:
regular polygon dropdown 20140715.png
regular polygon dropdown 20140715.png (24.51 KiB) Viewed 3167 times
I think it might be handy to have an additional option in your dropdown for an arbitrary N-gon, which might trigger a popup box to get the number of sides desired. It's good to keep a few common polygons though, because that'll allow keyboard shortcuts for them.

Doing the geometry with a parameter rather than a template would probably be good in general, even if you don't add the N-gon feature. Code will be easier to read, and will probably compile into a smaller executable. -Ian-
User avatar
DevJohan
Posts: 41
Joined: Sun Jul 13, 2014 2:36 pm
Location: Stockholm, Sweden

Re: Regular polygons

Postby DevJohan » Tue Jul 15, 2014 6:47 am

Hi Ian
ian.rees wrote:Doing the geometry with a parameter rather than a template would probably be good in general
I made this change to the code so now number of corners is a constructor parameter rather than a class template parameter.
ian.rees wrote:This isn't quite right, but there probably is a general rule. For example, imagine a 4-sided polygon with one angle (same argument for two angles) constrained to 90 degrees, and all sides equal length. Either a square or an 'L' could satisfy those requirements
You are right about that. However, those two cases are the solutions of the general problem, with the sketcher we just care about if the solutions are locally well defined. There is no way of moving from the square to the 'L' in a gradual way that satisfies the constraints.

I think the real question is how we specify the rules of a regular polygon. Below are three different possibilities for the hexagon.
hexagon_constraints.png
hexagon_constraints.png (42.06 KiB) Viewed 3154 times
top left have all sides equal length and all corners on a construction circle.
top right specifies all angles (specifying the last angle is redundant) and four lines equal length ( corners - 2?)
bottom (implemented) all sides equal length and 3 angles ( corners - 3 )

I think the top right is most well specified, but all the angular constraints clutter the sketch more than I like. The bottom one is nice for a hexagon, but as you pointed out not well defined, and for a larger number of corners suffers from the same problems as the top right one. I personally prefer the one inside a construction circle since this gives a center point which I can attach to something. I've used this extensively to create pockets for nuts. However that one isn't well specified either, both a pentagram and a pentagon satisfies these constraints in the case of five corners for example.

/Johan
jmaustpc
Posts: 9566
Joined: Tue Jul 26, 2011 6:28 am
Location: Australia

Re: Regular polygons

Postby jmaustpc » Tue Jul 15, 2014 9:04 am

ian.rees wrote: I was thinking a hexagon tool would be handy the other day for drawing wrench flats.
Hi Ian
Just checking that you know there is a hexagon tool already, in sketcher wb, under "profiles" menu?

It works in two ways. If you have no sketch open in edit mode then it creates a new sketch named "hexagon" with a default hex profile which you can constraint to your requirements. If you have an existing sketch open in edit mode, then the hexagon profile is inserted into the existing sketch.

Here is a simple demo showing the two modes it works in.
hexdemo.fcstd
(8.27 KiB) Downloaded 43 times
here is a screen shot
hexagonginsketcher.jpg
hexagonginsketcher.jpg (89.74 KiB) Viewed 3140 times



OS: Ubuntu 14.04 LTS
Word size: 64-bit
Version: 0.15.3731 (Git)
Branch: master
Hash: bd6a5cea8d039cf09cc778e3f66e0ea80c322b4b
Python version: 2.7.6
Qt version: 4.8.6
Coin version: 4.0.0a
SoQt version: 1.6.0a
OCC version: 6.7.0
ian.rees
Posts: 696
Joined: Sun Jun 15, 2014 3:28 am
Contact:

Re: Regular polygons

Postby ian.rees » Tue Jul 15, 2014 10:05 am

Just checking that you know there is a hexagon tool already, in sketcher wb, under "profiles" menu?
Yep, figured that out after reading wmayer's post ;). I do like the idea of a regular tool for does regular polygons though, like what Johan has done. Seems like the "Profiles" section would be better suited for shapes that are a bit more specialized, maybe like standard types/dimensions of Al extrusion, standard pulleys, NEMA mount patterns, that sort of thing.
I personally prefer the one inside a construction circle since this gives a center point which I can attach to something.
Same here, and the circle might be a handy visual cue too depending on how the resulting part will be built. I haven't thought about this much (and that might not help anyways...), but it seems like the ambiguity in your "pentagon in a circle" problem could be resolved by adding a constraint to one of the inside angles.
wmayer
Site Admin
Posts: 14988
Joined: Thu Feb 19, 2009 10:32 am

Re: Regular polygons

Postby wmayer » Sat Jul 26, 2014 11:28 pm

I personally prefer the one inside a construction circle since this gives a center point which I can attach to something. I've used this extensively to create pockets for nuts.
I agree that this will be the best choice to use the construction circle. And the nice thing with this solution is that you can handle any kind of regular n-polygon without any distinction of cases.
However that one isn't well specified either, both a pentagram and a pentagon satisfies these constraints in the case of five corners for example.
This isn't only true for five corners but for all polygons with uneven numbers of corners (except triangle). However, this can be easily avoided. All what you have to add is one angle constraint:

Code: Select all

angle = 180 - 360/n
with n=number of corners
But I think in reality we can omit this angle constraint because the solver may jump to the "bad" solution for big differences of the parameters (e.g. change length of an edge from something small to something big).
wmayer
Site Admin
Posts: 14988
Joined: Thu Feb 19, 2009 10:32 am

Re: Regular polygons

Postby wmayer » Sun Jul 27, 2014 9:11 am

OK, I merged your changes into master now.

Two points:
+ maybe we should think about creating the regular polygon using the construction circle. Then we can support arbitrary regular polygons.
+ I noticed that after adding a few profiles the adding of new elements becomes very slow. You can even see that new lines and constraints are added step by step. To solve this issue I extended the interface of addGeometry() and addConstraint() a while ago so that all geometries and all constraints can be added in one step. This reduces the required time dramatically.
Here is an example of my hexagon which I wrote in Python a few weeks ago: https://github.com/FreeCAD/FreeCAD_sf_m ... Hexagon.py
User avatar
NormandC
Posts: 18534
Joined: Sat Feb 06, 2010 9:52 pm
Location: Québec, Canada

Re: [Merged]Regular polygons

Postby NormandC » Sun Jul 27, 2014 6:00 pm

Very nice, thank you for your work.

I wonder, should icons appear besides the labels in the drop down menu? I'm guessing from a previous screen capture that they should... :?
wmayer wrote:+ maybe we should think about creating the regular polygon using the construction circle.
I agree, I like to have a construction circle so the polygon can be centered.

OS: Ubuntu 14.04.1 LTS
Word size: 64-bit
Version: 0.15.3789 (Git)
Branch: master
Hash: a1da4bb4fbd0c715edf7b9eecc9d19c0a84511b0
Python version: 2.7.6
Qt version: 4.8.6
Coin version: 4.0.0a
SoQt version: 1.6.0a
OCC version: 6.7.0
Attachments
FC_Sketcher_Polygons.png
FC_Sketcher_Polygons.png (22.3 KiB) Viewed 3029 times