Browsed by
Author: admin

Parameters to CSV File

Parameters to CSV File

Compatible with WizoScript 3.00 and later.

Shows how to export parameters from a part to a CSV file.

import csv

# open part
PartA = Part(r'C:\Users\Andy\Desktop', 'PartA')

# write parameters to csv file
with open(r'C:\Users\Andy\Desktop\PartA.csv', 'wb') as f:
  writer = csv.writer(f)
  Row = ['Name', 'Comment', 'Equation', 'Type', 'Value']
  writer.writerow(Row)
  for Parameter in PartA.Parameters:
    Row = [Parameter.Name, Parameter.Comment, Parameter.Equation, Parameter.Type, Parameter.Value]
    writer.writerow(Row)
MidPlane Extrusion

MidPlane Extrusion

Compatible with WizoScript 3.00 and later.

Shows how to extrude from the mid-plane.

# create the part and then a sketch containing a circle
P = Part("Test")
S = P.AddSketch("Shape", P.GetPlane("XY-Plane"))
S.AddCircle(0, 0, 9, False)

# how far we will extrude from mid-plane
ExtrudeLength = 10

# extrude it
P.AddExtrudeBoss("Cyl", S, ExtrudeLength, False, Part.EndCondition.MidPlane, None, 0, Part.DirectionType.Normal, None, 0, False)
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:
  sys.exit()

# 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)
  sys.exit()
if Sk == None:
  Win.ErrorDialog("No sketch selected", ScriptName)
  sys.exit()
if StartX > EndX:
  Win.ErrorDialog("Start X value is greater than end X value", ScriptName)
  sys.exit()
if NumPoints < 2:
  Win.ErrorDialog("Invalid number of points", ScriptName)
  sys.exit()

# 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)
  sys.exit()
if isinstance(Sk.Figures[0], SketchPoint) == False:
  Win.ErrorDialog("Can't find node on sketch", ScriptName)
  sys.exit()

# 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
  else:
    PX = ValX
    PY = ValY
  Points.extend([float(PX + NodeX), float(PY + NodeY)])
  ValX = ValX + StepX

# generate the bspline
Sk.AddBsplineThroughPoints(3, Points, False)
Cylinder Between Two Points II

Cylinder Between Two Points II

Compatible with WizoScript 3.00 and later.

Inserts a cylinder between two points.

from math import sqrt

Win = Windows()

Options = []
Options.append(["Start Point", WindowsInputTypes.Point, None])
Options.append(["End Point", WindowsInputTypes.Point, None])
Options.append(["Diameter", WindowsInputTypes.Real, 5])

Values = Win.OptionsDialog("Cylinder", Options)
if Values == None:
  sys.exit()
if Values[0] == None:
  Win.ErrorDialog("No start point selected", "Cylinder")
  sys.exit()
if Values[1] == None:
  Win.ErrorDialog("No end point selected", "Cylinder")
  sys.exit()
if Values[2] <= 0:
  Win.ErrorDialog("Invalid diameter", "Cylinder")
  sys.exit()

# get the part that defines the first point
# it is possible the second point is on a different part but we will assume
# they are on the same part
P = Values[0].GetPart()

# get points and diameter
cyl_p1 = Values[0].GetCoordinates()
cyl_p2 = Values[1].GetCoordinates()
diameter = Values[2]

# get length of cynlinder using euclidean distance
length = sqrt((cyl_p2[0] - cyl_p1[0])**2 + (cyl_p2[1] - cyl_p1[1])**2 + (cyl_p2[2] - cyl_p1[2])**2)
 
# calculate normal vector for the plane at the first end of the cylinder
normal_vector = [cyl_p2[0] - cyl_p1[0], cyl_p2[1] - cyl_p1[1], cyl_p2[2] - cyl_p1[2]]
 
# create plane for one end of the cylinder
cyl_plane = P.AddPlane("Cyl Start Plane", normal_vector, cyl_p1)
 
P.AddAxis("Cylinder Axis", cyl_p1, cyl_p2)
 
# draw a circle on the plane
S = P.AddSketch("Cylinder End", cyl_plane)
[cx, cy] = S.GlobaltoPoint(cyl_p1[0], cyl_p1[1], cyl_p1[2])
S.AddCircle(cx, cy, diameter, False)
 
# extrude into a cylinder
P.AddExtrudeBoss("Cylinder", S, length, True)
Speaker Grille from Bitmap Image

Speaker Grille from Bitmap Image

Compatible with WizoScript 3.00 and later.

This script takes an image and looks for the black pixels. It then creates a set of holes in the same pattern as the black pixels.

from __future__ import division

# get access to library in Windows for using images
import clr
clr.AddReference("System.Drawing")
from System.Drawing import Image

ScriptName = "Speaker Grille"

# create a dialog window to allow user to select image and where grille will go
Win = Windows()
Options = []
Options.append(["Image", WindowsInputTypes.File, None])
Options.append(["Face for Grille", WindowsInputTypes.Face, None])
Options.append(["Grille Center Point", WindowsInputTypes.Point, None])
Options.append(["Grille Width (mm)", WindowsInputTypes.Real, 100])
Options.append(["Grille Height (mm)", WindowsInputTypes.Real, 100])
Options.append(["Hole Diameter (mm)", WindowsInputTypes.Real, 4])
Options.append(["Cut Depth (mm)", WindowsInputTypes.Real, 5])
Options.append(["Reverse Cut", WindowsInputTypes.Boolean, False])

# check for cancel
Values = Win.OptionsDialog(ScriptName, Options)
if Values == None:
  sys.exit()

# get path and name of image file
ImageFile = Values[0]
if ImageFile == None or not ImageFile:
  Win.ErrorDialog("No image selected", ScriptName)
  sys.exit()

# get face in part
GrilleFace = Values[1]
if GrilleFace == None:
  Win.ErrorDialog("No face selected", ScriptName)
  sys.exit()

# get center point
CenterPoint = Values[2]
if CenterPoint == None:
  Win.ErrorDialog("No center point selected", ScriptName)
  sys.exit()

# get dimensions
Width = Values[3]
if Width == 0:
  Win.ErrorDialog("Invalid width", ScriptName)
  sys.exit()
Height = Values[4]
if Height == 0:
  Win.ErrorDialog("Invalid height", ScriptName)
  sys.exit()
  
# get hole diameter
HoleDiameter = Values[5]
if HoleDiameter == 0:
  Win.ErrorDialog("Invalid hole diameter", ScriptName)
  sys.exit()

# get cut depth
CutDepth = Values[6]
if CutDepth == 0:
  Win.ErrorDialog("Invalid cut depth", ScriptName)
  sys.exit()
  
# get reverse cut
ReverseCut = Values[7]

# get locations of all the black pixels in the image
# the origin of the image is top left but the origin of the sketch
# will be bottom left so we need to flip the points
Points = []
Img = Image.FromFile(ImageFile)
for x in range(Img.Width):
  for y in range(Img.Height):
    Pixel = Img.GetPixel(x, y)
    # A = alpha channel aka transparency, 255 = opaque
    if Pixel.A == 255 and Pixel.R == 0 and Pixel.G == 0 and Pixel.B == 0:
      y = Img.Height - y - 1
      Points.append([x, y])

# scale points
ScaleX = Width / Img.Width
ScaleY = Height / Img.Height
for P in Points:
  P[0] = P[0] * ScaleX
  P[1] = P[1] * ScaleY

# get scaled size
ScaledWidth = Img.Width * ScaleX
ScaledHeight = Img.Height * ScaleY

# get the part we are editing
Prt = GrilleFace.GetPart()

# create sketch and project center point onto sketch
Sk = Prt.AddSketch("Grille", GrilleFace)
Center = CenterPoint.GetCoordinates()
[cx, cy] = Sk.GlobaltoPoint(Center[0], Center[1], Center[2])

# center points
for P in Points:
  P[0] = P[0] - (ScaledWidth / 2) + cx
  P[1] = P[1] - (ScaledHeight / 2) + cy

# draw holes
Sk.AutomaticStartEndEditing = False
Sk.StartEditing()
for P in Points:
  Sk.AddCircle(P[0], P[1], HoleDiameter, False)
Sk.StopEditing()
Sk.AutomaticStartEndEditing = True

# cut holes
Prt.AddExtrudeCut("Grille", Sk, CutDepth, ReverseCut)
Tool Cutting

Tool Cutting

Requires Wizoscript 2.66 or later.

Simulates a cylinder being cut into by another cylinder (e.g. an endmill). The “cutting tool” follows a helical path around the stock.

# cylinder dimensions
Diameter = 20
Length = 100

# cutter dimensions
CutterDiameter = 5

# angle to increase by on each pass of the cutter, in degrees
# must be a whole divisor of 180
StepAngle = 10

# total angle of cutting around the cylinder
TotalAngle = 1440

# starting distance from end of cylinder
StartX = 10

# create the cylinder
P = Part("Cylinder")
CylPlane = P.GetPlane("XY-Plane")
CrossSection = P.AddSketch("Cross-Section", CylPlane)
CrossSection.AddCircle(0,0, Diameter, False)
P.AddExtrudeBoss("Cylinder", CrossSection, Length, False)

# create the planes
Planes = []
NumPlanes = 180 / StepAngle
for PlaneIndex in range(0, NumPlanes):
  Angle = PlaneIndex * StepAngle
  Pl = P.AddPlane("P" + str(Angle), P.GetPlane("YZ-Plane"), P.GetAxis("Z-Axis"), Angle)
  Planes.append(Pl)
for PlaneIndex in range(0, NumPlanes):
  Planes.append(Planes[PlaneIndex])
NumPlanes = NumPlanes * 2

# start of helix has no offset along cylinder
XStep = 0

# create circle sketches then extrude cut 'through all'
for Step in range(0, TotalAngle / StepAngle):
  Angle = Step * StepAngle
  NormalizedAngle = Angle % 360
  XStep += (Angle * 0.001)
  if NormalizedAngle < 90:
    X = -(StartX + XStep)
    Y = Diameter / 2.0
  elif NormalizedAngle == 90:
    X = -(Diameter / 2.0)
    Y = -(StartX + XStep)
  elif NormalizedAngle < 180:
    X = (StartX + XStep)
    Y = -(Diameter / 2.0)
  elif NormalizedAngle < 270:
    X = -(StartX + XStep)
    Y = -(Diameter / 2.0)
  elif NormalizedAngle == 270:
    X = (Diameter / 2.0)
    Y = -(StartX + XStep)
  else:
    X = (StartX + XStep)
    Y = Diameter / 2.0
  Sk = P.AddSketch("S" + str(Angle), Planes[Step % NumPlanes])
  Sk.AddCircle(X, Y, CutterDiameter, False)
  P.AddExtrudeCut("S" + str(Angle), Sk, 0, False, Part.EndCondition.ThroughAll, None, 0, Part.DirectionType.Normal, None, 0, False)
Useful Dialogs

Useful Dialogs

Compatible with WizoScript versions: 2.65

Demonstrates how to show information, error and question dialogs.

Win = Windows()
 
Win.InfoDialog("I am about to create a part", "My Script")
 
Win.ErrorDialog("Oops. That didn't go as planned", "My Script")
 
# returns True for 'yes' and False for 'no'
print Win.QuestionDialog("Shall I stop?", "My Script")
Wave Washer

Wave Washer

Compatible with WizoScript versions: 2.60

Generates wave washers. Uses a custom dialog window. Requires Geomagic Design V18 (2015.1.0) or later.

import math
from math import *
 
# radius
R = 100.0
# amplitude
A = 10.0
# number of waves (must be a whole number)
B = 4
 
# width
Width = 10
# thickness
Thickness = 5
 
Win = Windows()
 
Options = []
Options.append(["Radius", WindowsInputTypes.Real, R])
Options.append(["Amplitude", WindowsInputTypes.Real, A])
Options.append(["Number of Waves", WindowsInputTypes.Integer, B])
Options.append(["Width", WindowsInputTypes.Real, Width])
Options.append(["Thickness", WindowsInputTypes.Real, Thickness])
 
Values = Win.OptionsDialog("Wave Washer Generator", Options)
if Values == None:
  sys.exit()
 
R = Values[0]
A = Values[1]
B = Values[2]
Width = Values[3]
Thickness = Values[4]
 
# path accuracy = lower number = more points calculated
t_step = 0.1
 
# complete circle = PI x 2
t_max = 3.141592 * 2
 
# keep track of the total points we have calculated
TotalPoints = 0
 
# calculate points for 3D sketch
PathPoints = []
t = 0
while True:
  X = R * sin(t)
  Y = R * cos(t)
  Z = A * sin(B * t)
  PathPoints.extend([X, Y, Z])
  
  if TotalPoints == 0:
    P1 = [X, Y, Z]
  elif TotalPoints == 1:
    P2 = [X, Y, Z]
 
  t = t + t_step
  TotalPoints = TotalPoints + 1
  if t >= t_max:
    break
 
# close path
PathPoints.extend([PathPoints[0], PathPoints[1], PathPoints[2]])
 
# create part and add 3d sketch
P = Part("Wave Washer")
Path = P.Add3DSketch("Path")
Path.AddBspline(PathPoints)
 
# calculate normal vector for the plane at the start of the path
normal_vector = [P2[0] - P1[0], P2[1] - P1[1], P2[2] - P1[2]]
 
# create plane at the start of the path
Plane = P.AddPlane("Start Plane", normal_vector, P1)
 
CrossSection = P.AddSketch("Cross Section", Plane)
 
Origin = CrossSection.GlobaltoPoint(P1[0], P1[1], P1[2])
 
CrossSection.AddRectangle(Origin[0] - (Thickness / 2.0), Origin[1] - (Width / 2.0), Origin[0] + (Thickness / 2.0), Origin[1] + (Width / 2.0), False)
 
P.AddSweepBoss("Washer", CrossSection, Path, False, Part.EndCondition.EntirePath, None, 0, 0, False)
Polygon Incircle

Polygon Incircle

Compatible with WizoScript versions: 2.51

Polygons in Geomagic Design can be defined by an “exterior” or “interior” circle. The interior circle is typically used for bolt heads. The circle is called an “incircle”. This script shows how to draw a polygon by defining the incircle diameter.

import math
 
# diameter of circle that fits inside polygon
Diameter = 100
# number of sides
Sides = 6
 
# calculate exterior diameter of polygon
EDia = Diameter / math.cos(math.pi / Sides)
 
# create part, create polygon sketch, extrude
P = Part("Hex")
S = P.AddSketch("Hexagon", P.GetPlane("XY-Plane"))
S.AddPolygon(0, 0, EDia, Sides, False)
P.AddExtrudeBoss("Hex Head", S, 10, False)
Triangle

Triangle

Compatible with WizoScript versions: 2.51

Simple sketch of a triangle with angles 90, 15, 75 degrees.

# Create triangle with angles 90, 15, 75
 
import math
 
# set up parameters
Theta = 15.0
Adjacent = 100.0
 
# calculate side
Opposite = Adjacent * math.tan(math.radians(15.0))
 
# generate three vertices of triangle
P1_X = 0
P1_Y = 0
 
P2_X = Adjacent
P2_Y = 0
 
P3_X = Adjacent
P3_Y = Opposite
 
# create part and sketch
P = Part("Foo")
S = P.AddSketch("Shape", P.GetPlane("XY-Plane"))
 
# draw it
S.AddLine(P1_X, P1_Y, P2_X, P2_Y, False)
S.AddLine(P2_X, P2_Y, P3_X, P3_Y, False)
S.AddLine(P3_X, P3_Y, P1_X, P1_Y, False)