[Merged]Regular polygons
[Merged]Regular polygons
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. I think there might be some opinions on whether these types of objects should be available in the toolbars. 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 Cheers,
Johan
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. I think there might be some opinions on whether these types of objects should be available in the toolbars. 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 Cheers,
Johan
Re: Regular polygons
Hi Johan,
thanks for your great contribution!
thanks for your great contribution!
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 think there might be some opinions on whether these types of objects should be available in the toolbars.
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.Also, I used templates for creating the different versions of the regular polygons, but this should perhaps be made with a integer argument instead
Re: Regular polygons
Hi,
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
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: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'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.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.
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
Re: Regular polygons
Hi Johan,
Thanks for putting this together! I was thinking a hexagon tool would be handy the other day for drawing wrench flats.
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: 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-
Thanks for putting this together! I was thinking a hexagon tool would be handy the other day for drawing wrench flats.
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.I assume that "number of corners" - 3 is the number of angular constraints needed to make the polygon regular.
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: 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-
Re: Regular polygons
Hi Ian
I think the real question is how we specify the rules of a regular polygon. Below are three different possibilities for the hexagon.
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
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:Doing the geometry with a parameter rather than a template would probably be good in general
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.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
I think the real question is how we specify the rules of a regular polygon. Below are three different possibilities for the hexagon.
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
Re: Regular polygons
Hi Ianian.rees wrote: I was thinking a hexagon tool would be handy the other day for drawing wrench flats.
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. here is a screen shot
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
Re: Regular polygons
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.Just checking that you know there is a hexagon tool already, in sketcher wb, under "profiles" menu?
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.I personally prefer the one inside a construction circle since this gives a center point which I can attach to something.
Re: Regular polygons
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.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.
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: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.
Code: Select all
angle = 180 - 360/n
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).
Re: Regular polygons
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
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
Re: [Merged]Regular polygons
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...
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
I wonder, should icons appear besides the labels in the drop down menu? I'm guessing from a previous screen capture that they should...
I agree, I like to have a construction circle so the polygon can be centered.wmayer wrote:+ maybe we should think about creating the regular polygon using the construction circle.
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 (22.3 KiB) Viewed 5241 times