Browsed by
Tag: extrude cut

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)
Slice a Part

Slice a Part

Compatible with WizoScript versions: 1.95

Slices a part at a plane called “Slice”

# open part
P = Part(r"P:\temp\GDForum", "Test")
 
# get bounding box of part - eight points, one for each corner
# of the bounding box
Bounds = P.GetBoundingBox()
 
# get the plane that the part will be sliced on
SlicePlane = P.GetPlane("Slice")
 
# create a sketch on the slicing plane
S = P.AddSketch("SliceSketch", SlicePlane)
 
# empty list
Proj = []
 
# for each corner of the part bounding box, map that 3D point into
# a 2D point on the sketch
# this doesn't create the points in the sketch, but is only a mathematical
# operation
for i in range(0, 8):
  Proj.append(S.GlobaltoPoint(Bounds[i][0], Bounds[i][1], Bounds[i][2]))
 
# go through the eight 2D points and find the maximum and minimum
# X and Y values
MaxX = Proj[0][0]
for i in range (0, 8):
  if Proj[i][0] >= MaxX :
    MaxX = Proj[i][0]
 
MaxY = Proj[0][1]
for i in range (0, 8):
  if Proj[i][1] >= MaxY :
    MaxY = Proj[i][1]
 
MinX = Proj[0][0]
for i in range (0, 8):
  if Proj[i][0] < MinX :
    MinX = Proj[i][0]
 
MinY = Proj[0][1]
for i in range (0, 8):
  if Proj[i][1] < MinY :
    MinY = Proj[i][1]
 
# draw a rectangle on the sketch which will cover the entire part when viewed
# perpendicular to the slicing plane
S.AddRectangle(MinX, MinY, MaxX, MaxY, False)
 
# cut the part using the rectangle
P.AddExtrudeCut("Cut", S, 100, False)