Is it possible to create a sketch using a formula

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
Post Reply
User avatar
iplayfast
Posts: 256
Joined: Sat Sep 07, 2019 6:55 am

Is it possible to create a sketch using a formula

Post by iplayfast »

I'm thinking of cycloidal gears which are created using math to describe the radius. https://en.wikipedia.org/wiki/Cycloidal_drive

I've seen it done in F360 and it would be really cool if I could just plug in the python script or dimension with the formula.
chrisb
Veteran
Posts: 53935
Joined: Tue Mar 17, 2015 9:14 am

Re: Is it possible to create a sketch using a formula

Post by chrisb »

IIRC then exists a macro somewhere in the forum.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
Syres
Veteran
Posts: 2893
Joined: Thu Aug 09, 2018 11:14 am

Re: Is it possible to create a sketch using a formula

Post by Syres »

iplayfast wrote: Wed Dec 01, 2021 9:47 pm it would be really cool if I could just plug in the python script or dimension with the formula.
Firstly any thanks should go to @DjangoFreeCAD who wrote the main code, I've just done tweaks for my own requirements. If you start a new project file and create a new Sketch (I always use X-Y plane) and while in Sketch Editing mode run the macro below (it take about 32 seconds on my Win partition and usually half that on my Linux partition):

Code: Select all

import time
start_time = time.time()
LargeRadius = 560.0
SmallRadius = 20.0
nbPas = 480
excentrique = 12.0
EpiCycloidAngle = 0.0
# Large line length= sum of the radiuses
ActiveSketch.addGeometry(Part.LineSegment(App.Vector(10,23,0),App.Vector(27.354679,55.016010,0)),False)
ActiveSketch.addConstraint(Sketcher.Constraint('Coincident',0,1,-1,1)) 
c=ActiveSketch.addConstraint(Sketcher.Constraint('Distance',0,LargeRadius+SmallRadius))
ActiveSketch.setVirtualSpace(c, True) 
La=ActiveSketch.addConstraint(Sketcher.Constraint('Angle',-1,1,0,1,2*3.141592653589793*EpiCycloidAngle/360.0))
ActiveSketch.setVirtualSpace(La, True) 
#ActiveSketch.setDatum(La,App.Units.Quantity('0.000000 deg'))

# arc de cercle small radius
ActiveSketch.addGeometry(Part.ArcOfCircle(Part.Circle(App.Vector(25,31,0),App.Vector(0,0,1),12),-2,0.4),False)
c=ActiveSketch.addConstraint(Sketcher.Constraint('PointOnObject',1,1,0))  # start of the Arc on the large line
ActiveSketch.setVirtualSpace(c, True) 
ActiveSketch.addConstraint(Sketcher.Constraint('Coincident',1,3,0,2)) #center at end of the large line
c=ActiveSketch.addConstraint(Sketcher.Constraint('Radius',1,SmallRadius))
ActiveSketch.setVirtualSpace(c, True) 
 
#  Small line
ActiveSketch.addGeometry(Part.LineSegment(App.Vector(26,35,0),App.Vector(34,38,0)),False)
ActiveSketch.addConstraint(Sketcher.Constraint('Coincident',0,2,2,1)) #start at the end of the large line
c=ActiveSketch.addConstraint(Sketcher.Constraint('Distance',2,excentrique))
ActiveSketch.setVirtualSpace(c, True)
c=App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('PointOnObject',1,2,2))
ActiveSketch.setVirtualSpace(c, True)
Sa=ActiveSketch.addConstraint(Sketcher.Constraint('Angle',0,2,2,1,0)) #creer contrainte a 0 (radian))
ActiveSketch.setVirtualSpace(Sa, True)          # make the angle constraint invisible to improve drawing understanding  

n=0
Sv= ActiveSketch.Geometry[2].EndPoint
Lad = (360.000000/nbPas)
Sad = Lad * (LargeRadius/SmallRadius)
if True:
  while n < nbPas :
    n += 1
    App.ActiveDocument.Sketch.setDatum(La,App.Units.Quantity(str(Lad*n+EpiCycloidAngle) + ' deg'))
    App.ActiveDocument.Sketch.setDatum(Sa,App.Units.Quantity(str(Sad*n) + ' deg'))
    Ev= App.ActiveDocument.Sketch.Geometry[2].EndPoint
    App.ActiveDocument.Sketch.addGeometry(Part.LineSegment(Sv,Ev),False)
    Sv=Ev
    
    #clean up of the geometries used to buid the epicycloid
  App.ActiveDocument.Sketch.delGeometry(2)
  App.ActiveDocument.Sketch.delGeometry(1)
  App.ActiveDocument.Sketch.delGeometry(0)

print("--- %s seconds ---" % (time.time() - start_time))
FreeCADGui.SendMsgToActiveView("ViewFit")

You can then tweak the parameters to hopefully get what you want, please not this is not a constrained sketch. Tested using a pre-release of 0.19.3

OS: Windows 7 Version 6.1 (Build 7601: SP 1)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.24365 (Git)
Build type: Release
Branch: releases/FreeCAD-0-19
Hash: c5eca93a2070fe67957350ef8371e53027364c69
Python version: 3.8.6+
Qt version: 5.15.2
Coin version: 4.0.1
OCC version: 7.5.3
Locale: English/United Kingdom (en_GB)
Post Reply