Browsed by
Tag: 3D

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)
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)
Everyone Loves a Slinky

Everyone Loves a Slinky

Compatible with WizoScript versions: 2.51

Demonstrates how to create a spiral spring. Uses a custom dialog window to get input parameters.

# Everyone Loves a Slinky
# Adapted from:
# http://forum.alibre.com/viewtopic.php?f=9&t=5752&p=30750&hilit=Spring#p30750
 
import sys
import math
 
# create dialog window
Win = Windows()
Options = []
Options.append(["Angle Increment", WindowsInputTypes.Real, 0.05])
Options.append(["Loop Scale", WindowsInputTypes.Real, 0.8])
Options.append(["Height Scale", WindowsInputTypes.Real, 1.0])
Options.append(["Major Helix Width Scale", WindowsInputTypes.Real, 2.0])
Options.append(["Turn Density", WindowsInputTypes.Integer, 25])
 
# show dialog window and get values
Values = Win.OptionsDialog("Everyone Loves a Slinky", Options)
if Values == None:
  sys.exit("User cancelled")
 
AngleIncrement = Values[0]
LoopScale = Values[1]
HeightScale = Values[2]
WidthScale = Values[3]
TurnDensity = Values[4]
print "Angle Increment = %f" % AngleIncrement
print "Loop Scale = %f" % LoopScale
print "Height Scale = %f" % HeightScale
print "Width Scale = %f" % WidthScale
print "Turn Density = %d" % TurnDensity
 
# create list of points for 3d sketch
Points = []
Angle = 0.0
for Pass in range(0, 437):
  X = (WidthScale + LoopScale * math.cos(Angle * TurnDensity)) * math.cos(Angle)
  Y = (WidthScale + LoopScale * math.cos(Angle * TurnDensity)) * math.sin(Angle)
  Z = HeightScale * Angle + LoopScale * math.sin(Angle * TurnDensity)
  Points.extend([X, Y, Z])
  Angle += AngleIncrement
 
# create part and add 3d sketch
Slinky = Part("Slinky")
Path = Slinky.Add3DSketch("Path")
Path.AddBspline(Points)
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)
Creating a 3D Sketch Using a Spline and an Arc

Creating a 3D Sketch Using a Spline and an Arc

Compatible with WizoScript versions: 1.90

Shows how to create a 3D sketch that is comprised of a spline made of three points followed by an arc.

Units.Current = UnitTypes.Inches
 
P = Part("My Part")
 
# create 3d spline from a set of interpolation points
Path = P.Add3DSketch("Path")
Points = [0.6, -0.6625, 0.0]
Points.extend([0.6, -0.6625, -0.2175])
Points.extend([0.6, -0.8125, -0.6795])
Path.AddBspline(Points)
 
# arcs are counter clockwise, so to get a clockwise arc the start and end points are swapped
Path.AddArcCenterStartEnd(-5.6634, -3.92, -0.6795, 0.6, -7.0275, -0.6795, 0.6, -0.8125, -0.6795)
Lofting with Guide Curve

Lofting with Guide Curve

Compatible with WizoScript versions: 1.70

An example of creating a loft with a guide curve. This can easily be extended to use multiple curves. Requires Geomagic Design 16.0.1.16490 or later.

# create part
P = Part("foo")
 
# create sketch for bottom of loft
Bottom = P.AddSketch("Bottom", P.GetPlane("XY-Plane"))
Bottom.AddRectangle(0, 0, 10, 10, False)
 
# create sketch for top of loft
TopPlane = P.AddPlane("Top Plane", P.GetPlane("XY-Plane"), 30)
Top = P.AddSketch("Top", TopPlane)
Top.AddRectangle(0, 0, 50, 50, False)
 
# create guide curve
Guide = P.Add3DSketch("Guide")
Guide.AddBspline([10,10,0, 20,20,5, 45,45,15, 50,50,30])
 
# create loft using guide curve
P.AddLoftBoss("Loft Test", [Bottom, Top], [Guide], GuideCurveTypes.Global, True, False, False, False)
Modify an Existing Part

Modify an Existing Part

Compatible with WizoScript versions: 1.40

Demonstrates how to use scripting to modify an existing part.

# demonstrates opening an existing part and adding a 3d sketch to it
 
# load P:\work\TestPart.AD_PRT
MyPart = Part(r"P:\work", "TestPart")
 
# create a 3D sketch
Route = MyPart.Add3DSketch("Route")
Route.AddBspline([0, 0, 0,    5, 0, 0,    10, 5, 5,    15, 10, 5,    15, 15, 15])
Importer for IGES Point Files with Splitter

Importer for IGES Point Files with Splitter

Compatible with WizoScript versions: 1.40

This script takes an IGES file containing a set of points, identifies where to split the data, imports into multiple 3D sketches and adds bspline through the point in each sketch. For example it can be used with scanning data from a Romer arm.

# imports points from an IGES file into a 3D sketch
# splits the points up into segments and creates bsplines through the points for each segment
# requires wizoscript 1.40 and later
  
# specify which libraries we are going to use
import re
import math
 
# maximum distance between points before we create a new segment
maximumdistance = 3.9
 
# calculates distance between two points
# passed are two lists, [x1, y1, z1] and [x2, y2, z2]
def PointDistance(p1, p2):
  return math.sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2 + (p2[2] - p1[2])**2)
 
# adds a sketch to a part
def CreateSketch(part, name, points):
  # add 3d sketch 
  Sketch = part.Add3DSketch(name)
  # create bspline in sketch using points
  Sketch.AddBspline(points)
 
# this pattern matches lines in the IGES file that define a point and specifies where in the line x, y and z are
igespointline = re.compile('^116,([0-9EeDd\.\-]+),([0-9EeDd\.\-]+),([0-9EeDd\.\-]+),.*[0-9]P.*$', re.IGNORECASE)
  
# list of points, empty for now
# points will be stored as [x1,y1,z1, x2,y2,z2, ... , xn,yn,zn]
points = []
previouspoint = []
  
# create part
MyPart = Part("My Part")
  
# open IGES file for reading
f = open(r"C:\Users\Andy\Downloads\IGES_POINTS_TEST_FOR_2.igs")
  
# read in lines from IGES file looking for points
for line in f:
  # check if the line defines a point 
  match = igespointline.match(line)
  if match != None:
    # line defines a point, read out the coordinates
    # note that we replace the 'D' with an 'E' to allow python to understand the value
    x = float(match.group(1).upper().replace('D', 'E'))
    y = float(match.group(2).upper().replace('D', 'E'))
    z = float(match.group(3).upper().replace('D', 'E'))
 
    # creates the new point
    newpoint = [x, y, z]
 
    # get distance from previous point
    distance = 0.0
    if len(previouspoint) > 1:
      distance = PointDistance(previouspoint, newpoint)
  
    # points close together?
    if distance < maximumdistance:
      # add to list of points
      points.extend(newpoint)
      previouspoint = newpoint
    else:
      # points too far apart, create the sketch of what we have so far
      # show number of points found
      print "Found %d points" % (len(points) / 3)
      print "Creating 3D Sketch..."
      CreateSketch(MyPart, "Sketch", points)
      # restart with latest point
      points = list(newpoint)
      previouspoint = newpoint
 
# finished with IGES file
f.close()
 
# create sketch for remainder
if len(points) > 1:
  # show number of points found
  print "Found %d points" % (len(points) / 3)
  print "Creating 3D Sketch..."
  CreateSketch(MyPart, "Sketch", points)
Importer for IGES Point Files

Importer for IGES Point Files

Compatible with WizoScript versions: 1.40

This script takes an IGES file containing a set of points, imports them into a 3D sketch and adds a bspline through them. For example it can be used with scanning data from a Romer arm. It can easily be adapted to manipulate the data (for example splitting it up, rotation, scaling) before importing.

# imports points from an IGES file into a 3D sketch
# and creates a bspline through the points
# requires wizoscript 1.40 and later
 
# specify which libraries we are going to use
import re
 
# this pattern matches lines in the IGES file that define a point and specifies where in the line x, y and z are
igespointline = re.compile('^116,([0-9EeDd\.\-]+),([0-9EeDd\.\-]+),([0-9EeDd\.\-]+),.*[0-9]P.*$', re.IGNORECASE)
 
# list of points, empty for now
# points will be stored as [x1,y1,z1, x2,y2,z2, ... , xn,yn,zn]
points = []
 
# open IGES file for reading
f = open(r"C:\Users\Andy\Downloads\IGES_POINTS_TEST_FOR_2.igs")
 
# read in lines from IGES file looking for points
for line in f:
  # check if the line defines a point 
  match = igespointline.match(line)
  if match != None:
    # line defines a point, read out the coordinates
    # note that we replace the 'D' with an 'E' to allow python to understand the value
    x = float(match.group(1).upper().replace('D', 'E'))
    y = float(match.group(2).upper().replace('D', 'E'))
    z = float(match.group(3).upper().replace('D', 'E'))
 
    # add to list of points
    points.extend([x, y, z])
 
# finished with IGES file
f.close()
 
# show number of points found
print "Found %d points" % (len(points) / 3)
 
print "Creating 3D Sketch. This could take a few minutes..."
 
# create part
MyPart = Part("My Part")
# add 3D sketch
PointSketch = MyPart.Add3DSketch("Point Sketch")
# create bspline in 3D sketch using points
PointSketch.AddBspline(points)
Profile and Sweep Path

Profile and Sweep Path

Compatible with WizoScript versions: 1.40

This script creates a smooth path for a pipe using a 3D sketch and then a circle at one end to define the profile for the sweep. The sweep can then be manually completed.

# create the part and get the yz plane
MyPart = Part("Test")
YZPlane = MyPart.GetPlane("YZ-Plane")
 
# create the route for the pipe
PipeRoute = MyPart.Add3DSketch("Pipe Route")
PipeRoute.AddBspline([0, 0, 0,    5, 0, 0,    10, 5, 5,    15, 10, 5,    15, 15, 15])
 
# create the pipe profile as a circle on the yz plane
StartProfile = MyPart.AddSketch("Start Profile", YZPlane)
StartProfile.AddCircle(0, 0, 5, False)