Browsed by
Tag: import

Import Files

Import Files

Compatible with WizoScript versions: 2.50

Shows how to import various file types into Geomagic Design.

# import a step file
MyPart1 = Part(r'P:\temp\Corner.stp', Part.FileTypes.STEP)
 
# import a STL mesh which uses millimeters for it's units
MyPart2 = Part(r'P:\temp\Mitre.stl', Part.FileTypes.STL_mm)
 
# import an IGES file
MyPart3 = Part(r'P:\temp\Foo.iges', Part.FileTypes.IGES)
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)
CSV Import with Point Rotation

CSV Import with Point Rotation

Compatible with WizoScript versions: 1.40

The following script shows how to import a set of 2D points from a CSV file, rotate them about a arbitrary point and then add them to a 2D sketch.

# imports a set of 2D points from a csv file, rotates them and then
# adds them to a 2D sketch with lines connecting the points
 
# specify which libraries we are going to use
import csv
import math
 
# configuration
angle = 30.5
rotationpoint = [5.0, 5.0]
csvfile = r"C:\Users\Andy\Example.csv"
 
# rotates a point around another point
# passed is the angle, the point to rotate and the origin of the rotation
# copied from http://ubuntuforums.org/showthread.php?t=975315&p=8618044#post8618044
def rotate2d(degrees,point,origin):
  x = point[0] - origin[0]
  yorz = point[1] - origin[1]
  newx = (x*math.cos(math.radians(degrees))) - (yorz*math.sin(math.radians(degrees)))
  newyorz = (x*math.sin(math.radians(degrees))) + (yorz*math.cos(math.radians(degrees)))
  newx += origin[0]
  newyorz += origin[1] 
  return newx,newyorz
 
# list of points, empty for now
# points will be stored as [x1,y1, x2,y2, ... ,xn,yn]
points = []
 
# open csv file
f = open(csvfile)
 
# create csv reader and read in lines
reader = csv.reader(f)
for row in reader:
  # column 0 contains x, column 1 contains y
  x = float(row[0])
  y = float(row[1])
 
  # rotate point and add to list of points
  points.extend(rotate2d(angle, [x, y], rotationpoint))
 
# finished with csv file
f.close()
 
# show number of points found
print "Found %d points" % (len(points) / 2)
 
# create part
MyPart = Part("My Part")
# add sketch on XY plane
PointSketch = MyPart.AddSketch("Point Sketch", MyPart.GetPlane("XY-Plane"))
# add points with lines connecting them
PointSketch.AddLines(points, False)
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)