Macro Draw Parametric 2D Function: Difference between revisions

From FreeCAD Documentation
(Created page with '{{Macro|Name=drawParametric2DFunction|Description=Draws 2-dimensional, parametric and optionally polar equations.|Author=T4b}} Still has some bugs and misses some features. Docum…')
 
(Use {{MacroCode}})
 
(28 intermediate revisions by 5 users not shown)
Line 1: Line 1:
<languages/>
{{Macro|Name=drawParametric2DFunction|Description=Draws 2-dimensional, parametric and optionally polar equations.|Author=T4b}}
<translate>
<!--T:1-->
{{Macro
|Name=Macro Draw Parametric 2D Function
|Icon=Macro_Draw_Parametric_2D_Function.png
|Description=Draws 2-dimensional, parametric and optionally polar equations.
|Author=T4b
|Version=2.0
|Date=2012-08-30
|FCVersion=All
|Download=[https://www.freecadweb.org/wiki/images/0/03/Macro_Draw_Parametric_2D_Function.png ToolBar Icon]
}}

==Description== <!--T:2-->
Draws 2-dimensional, parametric and optionally polar equations.

<!--T:3-->
[[Image:Macro_drawParametric2Dfunction.png|480px]]
{{Caption|DrawParametric2DFunction}}

==Limitations== <!--T:4-->
Still has some bugs and misses some features. Documentation is in the docstrings.
Still has some bugs and misses some features. Documentation is in the docstrings.

<pre>
==Usage== <!--T:5-->
Type :


<!--T:6-->
Example usage:

</translate>
draw2Dfunction(xFunction="0.5*n", yFunction="-0.75*n", n=0, nd=-math.pi, step=50, pol=1)
<translate>

==Script== <!--T:8-->
</translate>

ToolBar Icon [[Image:Macro_Draw_Parametric_2D_Function.png]]

'''Macro_drawParametric2DFunction.FCMacro'''

{{MacroCode|code=

import FreeCAD, FreeCADGui, Part
import FreeCAD, FreeCADGui, Part
import math
import math
Line 22: Line 63:
nStart=n
nStart=n
while math.fabs(n-nd)-1.0/step>0:
while math.fabs(n-nd)-1.0/step>0:
print "n: " + str(n)
print( "n: " + str(n))
x=evalFunction(xFunction, n)
x=evalFunction(xFunction, n)
y=evalFunction(yFunction, n)
y=evalFunction(yFunction, n)
nNext=n+math.copysign(1,nd-n)/step
nNext=n+math.copysign(1,nd-n)/step
print "nNext: " + str(nNext)
print( "nNext: " + str(nNext))
xNext=evalFunction(xFunction, nNext)
xNext=evalFunction(xFunction, nNext)
yNext=evalFunction(yFunction, nNext)
yNext=evalFunction(yFunction, nNext)
Line 33: Line 74:
else:
else:
nextSeg=(x*math.cos(y),x*math.sin(y),z),(xNext*math.cos(yNext),xNext*math.sin(yNext),z)
nextSeg=(x*math.cos(y),x*math.sin(y),z),(xNext*math.cos(yNext),xNext*math.sin(yNext),z)
print "nextSeg: " + str(nextSeg)
print( "nextSeg: " + str(nextSeg))
nomme=Part.makeLine(*nextSeg)
nomme=Part.makeLine(*nextSeg)
if n==nStart:
if n==nStart:
Line 43: Line 84:
#Example usage:
#Example usage:
draw2Dfunction(xFunction="0.5*n", yFunction="-0.75*n", n=0, nd=-math.pi, step=50, pol=1)
draw2Dfunction(xFunction="0.5*n", yFunction="-0.75*n", n=0, nd=-math.pi, step=50, pol=1)

</pre>
}}

Latest revision as of 00:33, 8 May 2020

Macro Draw Parametric 2D Function

Description
Draws 2-dimensional, parametric and optionally polar equations.

Macro version: 2.0
Last modified: 2012-08-30
FreeCAD version: All
Download: ToolBar Icon
Author: T4b
Author
T4b
Download
ToolBar Icon
Links
Macro Version
2.0
Date last modified
2012-08-30
FreeCAD Version(s)
All
Default shortcut
None
See also
None

Description

Draws 2-dimensional, parametric and optionally polar equations.

DrawParametric2DFunction

Limitations

Still has some bugs and misses some features. Documentation is in the docstrings.

Usage

Type :


Example usage:

draw2Dfunction(xFunction="0.5*n", yFunction="-0.75*n", n=0, nd=-math.pi, step=50, pol=1)

Script

ToolBar Icon

Macro_drawParametric2DFunction.FCMacro

import FreeCAD, FreeCADGui, Part
import math

def evalFunction(suppliedFunction, n):
	"""This function uses eval to evaluate suppliedFunction.
	
	It does in no way check whether suppliedFunction is evil, thus it is itself evil!
	"""
	return eval(suppliedFunction)

def draw2Dfunction(xFunction="n", yFunction="n", n=-5, nd=10, step=10, z=0, pol=0):
	"""Draws 2-dimensional mathemathical functions
	
	The function is drawn for n's between n and n+nd, in steps of 1/step, on the z-coordinate z.
	Equations for x and y can be given (xFunction and yFunction arguments), they default to n.
	
	If pol=1 then x is interpreted as r and y is interpreted as t.
	"""
	nStart=n
	while math.fabs(n-nd)-1.0/step>0:
		print( "n: " + str(n))
		x=evalFunction(xFunction, n)
		y=evalFunction(yFunction, n)
		nNext=n+math.copysign(1,nd-n)/step 
		print( "nNext: " + str(nNext))
		xNext=evalFunction(xFunction, nNext)
		yNext=evalFunction(yFunction, nNext)
		if pol==0:
			nextSeg=(x,y,z),(xNext,yNext,z)
		else:
			nextSeg=(x*math.cos(y),x*math.sin(y),z),(xNext*math.cos(yNext),xNext*math.sin(yNext),z)
		print( "nextSeg: " + str(nextSeg))
		nomme=Part.makeLine(*nextSeg)
		if n==nStart:
			WWire=Part.Wire([nomme])
		else:
			WWire=Part.Wire([WWire,nomme])
		n=nNext
	Part.show(WWire)
#Example usage:
draw2Dfunction(xFunction="0.5*n", yFunction="-0.75*n", n=0, nd=-math.pi, step=50, pol=1)