Squashed commit of the following:

commit 6e78052d325eadcb212a6d50c6cd09cc29ac7870
Author: Johan K <johankristensen@gmail.com>
Date:   Tue Jul 29 14:52:08 2014 +0200

    Adding missing file RegularPolygon.py

commit 416396283032a31a1ea307df23d33ecfccf37935
Author: Johan K <johankristensen@gmail.com>
Date:   Mon Jul 28 22:51:28 2014 +0200

    Replaced sketcher implementation of RegularPolygons with a Python script version.
This commit is contained in:
wmayer 2014-07-29 17:26:28 +02:00
parent c10ddae02f
commit 65c974ed2d
4 changed files with 95 additions and 36 deletions

View File

@ -106,6 +106,7 @@ SET(Sketcher_Scripts
TestSketcherApp.py
Profiles.py
ProfileLib/Hexagon.py
ProfileLib/RegularPolygon.py
ProfileLib/__init__.py
)

View File

@ -19,6 +19,7 @@ INSTALL(
INSTALL(
FILES
ProfileLib/Hexagon.py
ProfileLib/RegularPolygon.py
ProfileLib/__init__.py
DESTINATION
Mod/Sketcher/ProfileLib

View File

@ -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<int>(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<int>(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();

View File

@ -0,0 +1,87 @@
#***************************************************************************
#* *
#* Copyright (c) 2014 - Johan Kristensen, *
#* Juergen Riegel <FreeCAD@juergen-riegel.net> *
#* *
#* 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