Equation Sketcher

Equation Sketcher

Compatible with WizoScript 3.00 and later.

Enter a formula and a Bspline is added to your chosen sketch with the mathematical shape. X and Y can be swapped if needed. The number of points for calculation is controllable. The start of the line is offset to begin at a node manually placed on the sketch before running the script.

from __future__ import division
from math import sqrt

Win = Windows()

ScriptName = "Equation Sketch"

Options = []
Options.append([None, WindowsInputTypes.Label, "The sketch must have a node added already that marks the start of the line"])
Options.append(["Equation y = ", WindowsInputTypes.String, "0.1*x**2"])
Options.append(["Sketch", WindowsInputTypes.Sketch, None])
Options.append(["Start X (at node)", WindowsInputTypes.Real, 0])
Options.append(["End X", WindowsInputTypes.Real, 10])
Options.append(["Number of points", WindowsInputTypes.Integer, 10])
Options.append(["Swap X and Y", WindowsInputTypes.Boolean, False])

Values = Win.OptionsDialog(ScriptName, Options)
if Values == None:

# get the inputs
Equation = Values[0]
Sk = Values[1]
StartX = Values[2]
EndX = Values[3]
NumPoints = Values[4]
SwapXY = Values[5]

# validate
if not Equation:
  Win.ErrorDialog("No equation entered", ScriptName)
if Sk == None:
  Win.ErrorDialog("No sketch selected", ScriptName)
if StartX > EndX:
  Win.ErrorDialog("Start X value is greater than end X value", ScriptName)
if NumPoints < 2:
  Win.ErrorDialog("Invalid number of points", ScriptName)

# get the part that defines the sketch
Prt = Sk.GetPart()

# check sketch defines a node
# we assume first sketch figure is the node
if len(Sk.Figures) == 0:
  Win.ErrorDialog("The sketch doesn't have a node defined", ScriptName)
if isinstance(Sk.Figures[0], SketchPoint) == False:
  Win.ErrorDialog("Can't find node on sketch", ScriptName)

# get location of node
NodeX = Sk.Figures[0].X
NodeY = Sk.Figures[0].Y

# we only import this now because it can cause a bit of a delay
from sympy import *

# parse equation
x = Symbol("x")
Eq = sympify(Equation)

# work out how much we increase x between points
StepX = (EndX - StartX) / NumPoints

# calculate the points
Points = []
ValX = StartX
for p in xrange(NumPoints):
  ValY = Eq.subs(x, ValX)
  if SwapXY == True:
    PY = ValX
    PX = ValY
    PX = ValX
    PY = ValY
  Points.extend([float(PX + NodeX), float(PY + NodeY)])
  ValX = ValX + StepX

# generate the bspline
Sk.AddBsplineThroughPoints(3, Points, False)

Leave a Reply

Your email address will not be published. Required fields are marked *