diff --git a/src/Mod/Sketcher/App/CMakeLists.txt b/src/Mod/Sketcher/App/CMakeLists.txt index 04cec9d7b..ca7884a22 100644 --- a/src/Mod/Sketcher/App/CMakeLists.txt +++ b/src/Mod/Sketcher/App/CMakeLists.txt @@ -106,6 +106,7 @@ SET(Sketcher_Scripts TestSketcherApp.py Profiles.py ProfileLib/Hexagon.py + ProfileLib/RegularPolygon.py ProfileLib/__init__.py ) diff --git a/src/Mod/Sketcher/CMakeLists.txt b/src/Mod/Sketcher/CMakeLists.txt index 33dcaae19..a7a954310 100644 --- a/src/Mod/Sketcher/CMakeLists.txt +++ b/src/Mod/Sketcher/CMakeLists.txt @@ -19,6 +19,7 @@ INSTALL( INSTALL( FILES ProfileLib/Hexagon.py + ProfileLib/RegularPolygon.py ProfileLib/__init__.py DESTINATION Mod/Sketcher/ProfileLib diff --git a/src/Mod/Sketcher/Gui/CommandCreateGeo.cpp b/src/Mod/Sketcher/Gui/CommandCreateGeo.cpp index a0bfae955..57754adaa 100644 --- a/src/Mod/Sketcher/Gui/CommandCreateGeo.cpp +++ b/src/Mod/Sketcher/Gui/CommandCreateGeo.cpp @@ -3219,49 +3219,19 @@ public: virtual bool releaseButton(Base::Vector2D onSketchPos) { - static const char* AddLineCommand = - "App.ActiveDocument.%s.addGeometry(Part.Line(App.Vector(%f,%f,0),App.Vector(%f,%f,0)))"; - static const char* AddConstraintCoincideCommand = - "App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Coincident',%i,2,%i,1))"; - static const char* AddConstraintEqualCommand = - "App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Equal',%i,%i)) "; - static const char* AddConstraintAngleCommand = - "App.ActiveDocument.%s.addConstraint(Sketcher.Constraint('Angle',%i,1,%i,2,%f))"; if (Mode==STATUS_End){ unsetCursor(); resetPositionText(); Gui::Command::openCommand("Add hexagon"); - int firstCurve = getHighestCurveIndex() + 1; - // add the geometry to the sketch try { - for( int i = 0; i < static_cast(Corners); i++ ){ - Gui::Command::doCommand(Gui::Command::Doc, AddLineCommand, - sketchgui->getObject()->getNameInDocument(), - EditCurve[i].fX,EditCurve[i].fY,EditCurve[i+1].fX,EditCurve[i+1].fY); - } - // make line ends coincide and all lines equal length - Gui::Command::doCommand(Gui::Command::Doc, AddConstraintCoincideCommand, - sketchgui->getObject()->getNameInDocument(), - firstCurve+(Corners-1),firstCurve); - for( int i = 1; i < static_cast(Corners); i++ ){ - Gui::Command::doCommand(Gui::Command::Doc, AddConstraintCoincideCommand, - sketchgui->getObject()->getNameInDocument(), - firstCurve+(i-1),firstCurve+i); - Gui::Command::doCommand(Gui::Command::Doc,AddConstraintEqualCommand, - sketchgui->getObject()->getNameInDocument(), - firstCurve,firstCurve+i); - } - double angle_constraint_diff = (1. * Corners)/( 1. * Corners - 3.0 ); - for( int i = 0; i < (Corners-3); i++ ){ - int angle_constraint_index = angle_constraint_diff * i; - Gui::Command::doCommand(Gui::Command::Doc,AddConstraintAngleCommand, - sketchgui->getObject()->getNameInDocument(), - firstCurve + ( angle_constraint_index + 1 ), - firstCurve + ( angle_constraint_index ), - ( M_PI - AngleOfSeparation ) ); - } + Gui::Command::doCommand(Gui::Command::Doc, + "import ProfileLib.RegularPolygon\n" + "ProfileLib.RegularPolygon.makeRegularPolygon('%s',%i,App.Vector(%f,%f,0),App.Vector(%f,%f,0))", + sketchgui->getObject()->getNameInDocument(), + Corners, + StartPos.fX,StartPos.fY,EditCurve[0].fX,EditCurve[0].fY); Gui::Command::commitCommand(); Gui::Command::updateActive(); diff --git a/src/Mod/Sketcher/ProfileLib/RegularPolygon.py b/src/Mod/Sketcher/ProfileLib/RegularPolygon.py new file mode 100644 index 000000000..053bbeea5 --- /dev/null +++ b/src/Mod/Sketcher/ProfileLib/RegularPolygon.py @@ -0,0 +1,87 @@ +#*************************************************************************** +#* * +#* Copyright (c) 2014 - Johan Kristensen, * +#* Juergen Riegel * +#* * +#* This program is free software; you can redistribute it and/or modify * +#* it under the terms of the GNU Lesser General Public License (LGPL) * +#* as published by the Free Software Foundation; either version 2 of * +#* the License, or (at your option) any later version. * +#* for detail see the LICENCE text file. * +#* * +#* This program is distributed in the hope that it will be useful, * +#* but WITHOUT ANY WARRANTY; without even the implied warranty of * +#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +#* GNU Library General Public License for more details. * +#* * +#* You should have received a copy of the GNU Library General Public * +#* License along with this program; if not, write to the Free Software * +#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +#* USA * +#* * +#*************************************************************************** + +import FreeCAD, FreeCADGui, Sketcher, Part, math + + +__title__="Regular polygon profile lib" +__author__ = "Johan Kristensen" +__url__ = "http://www.freecadweb.org" + +App = FreeCAD +Gui = FreeCADGui + +def makeRegularPolygon( + sketchName, + sides, + centerPoint=App.Vector(0,0,0), + firstCornerPoint=App.Vector(-20.00,34.64,0)): + + if not sketchName: + App.Console.PrintError("No sketch specified in 'makeRegularPolygon'") + return + if sides < 3: + App.Console.PrintError("Number of sides must be at least 3 in 'makeRegularPolygon'") + return + + sketch = App.ActiveDocument.getObject(sketchName) + + diffVec = firstCornerPoint - centerPoint + diffVec.z = 0 + angular_diff = 2*math.pi/sides + pointList = [] + for i in range(0,sides): + cos_v = math.cos( angular_diff * i ) + sin_v = math.sin( angular_diff * i ) + pointList.append( centerPoint+ + App.Vector( + cos_v * diffVec.x - sin_v * diffVec.y, + cos_v * diffVec.y + sin_v * diffVec.x, + 0 )) + + geoList = [] + for i in range(0,sides-1): + geoList.append(Part.Line(pointList[i],pointList[i+1])) + geoList.append(Part.Line(pointList[sides-1],pointList[0])) + geoList.append(Part.Circle(centerPoint,App.Vector(0,0,1),diffVec.Length)) + geoIndices = sketch.addGeometry(geoList) + + sketch.setConstruction(geoIndices[-1],True) + + conList = [] + for i in range(0,sides-1): + conList.append(Sketcher.Constraint( + 'Coincident', + geoIndices[i],2, + geoIndices[i+1],1)) + conList.append(Sketcher.Constraint( + 'Coincident', + geoIndices[sides-1],2, + geoIndices[0],1)) + for i in range(0,sides-1): + conList.append(Sketcher.Constraint('Equal',geoIndices[0],geoIndices[i+1])) + for i in range(0,sides): + conList.append(Sketcher.Constraint('PointOnObject',geoIndices[i],2,geoIndices[-1])) + sketch.addConstraint(conList) + return +