home processing download documents tutorial python tutorial gallery source about
 Python Tutorials (back to the list of tutorials)

File I/O

     Open Files

Currently iGeo can read most of types of geometries in Wavefront OBJ file (.obj) and Rhinoceros 3dm file in version 4.0 (.3dm). Known types of geometries iGeo currently cannot read are BRep solid geometries and poly-curves (polylines can be read).

To open file, please use IG.open() method, specifying the file path in its argument.

The path can be either an absolute path or a relative path.

An absolute path is a path starts with "drive name letter:\".. like "C:\Users\username\My Documents\" in Windows or a path starts with "/" like "/home/user/Documents/" in MacOSX. Note that in case of Windows, because the separator "\" is used as an escape character in Java String, you need to put double back slash to put one back slash in Java String, like this; "C:\\User\\username\\My Documents\\".

When it is not an absolute path, it's read as a relative path and it is relative to Processing sketch's data folder, which is a subfolder named "data" inside saved sketch folder and typically it looks like this; in Windows,
My Documents\Processing\sketch_sep01a\data\,
and in MacOSX,
~/Documents/Processing/sketch_sp01a/data/.

Processing recommends you to put data files inside the "data" folder inside the sketch folder. In this case, first, please save the sketch and second, either create a subfolder named "data" inside the sketch folder where the saved .pde file exist and put the input 3D model file into the "data folder" or go to the menu of "Sketch > Add File..." and select the input file and then Processing automatically create the "data" subfolder and copy the input file under the "data" folder.

Once you put the input in the Processing sketch's data folder, you can open file like below.

add_library('igeo')

size( 480, 360, IG.GL )

#IG.open("test_input_file.obj") # Wavefront OBJ format
IG.open("test_input_file.3dm") # Rhinoceros 3dm format

     Save Files

In the same way with opening files, you can save geometries in files by IG.save() method, specifying the file path in its argument. If the file path is a relative path, it's saved in Processing sketch's data folder. You need to save Processing sketch file (.pde) before you save geometries.

add_library('igeo')

size( 480, 360, IG.GL )

#IG.save("test_output_file.obj"); # Wavefront OBJ format
IG.save("test_output_file.3dm") # Rhinoceros 3dm format

     Import Geometries

You can select geometries which are imported into iGeo's server with those methods.
IG.points() for getting an array of points.
IG.curves() for getting an array of curves.
IG.surfaces() for getting an array of surfaces.
IG.meshes() for getting an array of polygon meshes.
IG.breps() for getting an array of BReps (polysurfaces).

add_library('igeo')

size( 480, 360, IG.GL )

#IG.open("test_input_file.obj") # Wavefront OBJ format
IG.open("test_input_file.3dm") # Rhinoceros 3dm format

pts = IG.points() #getting points

crvs = IG.curves() #getting curves

srfs = IG.surfaces() #getting surfaces

meshes = IG.meshes() #getting polygon meshes

breps = IG.breps() #getting Breps

To access each member of the array, usually you use for-loop like the following code.

add_library('igeo')

size( 480, 360, IG.GL )

#IG.open("test_input_file.obj") # Wavefront OBJ format
IG.open("test_input_file.3dm") # Rhinoceros 3dm format

pts = IG.points() #getting points
for pt in pts :
  # do something with pt (one of points in pts)
  pt.clr(1.0,0,0) # changing color for example

crvs = IG.curves() #getting curves
for crv in crvs :
  # do something with crv (one of curves in crvs)
  crv.clr(0,1.0,1.0) # changing color for example

srfs = IG.surfaces() #getting surfaces
for srf in srfs :
  # do something with srf (one of surfaces in srfs)
  srf.clr(0,0,0.5) # changing color for example

meshes = IG.meshes() #getting polygon meshes
for mesh in meshes : 
  # do something with mesh (one of meshes in mesh)
  mesh.clr(0,0,1.0) # changing color for example

breps = IG.breps() #getting BReps
for brep in breps : 
  # do something with brep (one of breps in breps)
  brep.clr(0.5,0,1.0) # changing color for example

There are other sets of methods to access imported geometries. You can get a total number of each type of geometry objects by the following methods.

IG.pointNum() number of all points in the scene.
IG.curveNum() number of all curves in the scene.
IG.surfaceNum() number of all surfaces in the scene.
IG.mesheNum() number of all meshes in the scene.
IG.brepNum() number of all BReps in the scene.

And the following is to access to each geometry with an integer index number starting from zero, upto the total number - 1.

IG.point(int) number of all points in the scene.
IG.curve(int) number of all curves in the scene.
IG.surface(int) number of all surfaces in the scene.
IG.meshe(int) number of all meshes in the scene.
IG.brep(int) number of all breps in the scene.

With those methods, the example code with for-loops above can be rewritten as following.

add_library('igeo')

size( 480, 360, IG.GL )

#IG.open("test_input_file.obj") # Wavefront OBJ format
IG.open("test_input_file.3dm")  # Rhinoceros 3dm format

for i in range(IG.pointNum()) : 
  # do something with IG.point(i)
  IG.point(i).clr(1.0,0,0) # changing color for example

for i in range(IG.curveNum()) : 
  # do something with IG.curve(i)
  IG.curve(i).clr(0,1.0,1.0) # changing color for example

for i in range(IG.surfaceNum()) : 
  # do something with IG.surface(i)
  IG.surface(i).clr(0,0,0.5) # changing color for example

for i in range(IG.meshNum()) : 
  # do something with IG.mesh(i)
  IG.mesh(i).clr(0,0,1.0) # changing color for example

for i in range(IG.brepNum()) : 
  # do something with IG.brep(i)
  IG.brep(i).clr(0.5,0,1.0) # changing color for example

Note that when you use IG.pointNum() / IG.curveNum() / IG.surfaceNum() / IG.meshNum() inside for-loops, you need to careful about creating or deleting the same type of geometry because those methods returns not only the number of imported geometry from a file but total number of geometry in the current execution of Processing. For example, creating a new IPoint inside a for-loop with IG.pointNum() like the following, it causes an infinite loop because IG.pointNum() keeps increasing in the iteration.

for i in range(IG.pointNum()) :
  IPoint(IG.point(i)) # duplicating each point

In those case, it's safer to use methods to return an array such as IG.points() / IG.curves() / IG.surfaces() / IG.meshes() / IG.breps() .


     Import Geometries by Layers

If the input file contains layer information (like Rhino 3dm file), you can choose geometry objects by the layer names. IG.layer("layer name") is a method to specify a layer with a layer name as a String value. You can add geometry object selection methods after the layer method as following.

IG.layer("layer name").points() for getting an array of points in a layer.
IG.layer("layer name").curves() for getting an array of curves in a layer.
IG.layer("layer name").surfaces() for getting an array of surfaces in a layer.
IG.layer("layer name").meshes() for getting an array of polygon meshes in a layer.
IG.layer("layer name").breps() for getting an array of BReps (polysurfaces) in a layer.

Methods to get a total numbers of geometry in a layer are below.

IG.layer("layer name").pointNum() number of all points in the scene.
IG.layer("layer name").curveNum() number of all curves in the scene.
IG.layer("layer name").surfaceNum() number of all surfaces in the scene.
IG.layer("layer name").mesheNum() number of all meshes in the scene.
IG.layer("layer name").brepNum() number of all BReps in the scene.

And the following is to access to each geometry in a layer with an integer index number.

IG.layer("layer name").point(int) number of all points in the scene.
IG.layer("layer name").curve(int) number of all curves in the scene.
IG.layer("layer name").surface(int) number of all surfaces in the scene.
IG.layer("layer name").meshe(int) number of all meshes in the scene.
IG.layer("layer name").brep(int) number of all breps in the scene.

The below is an example code of selecting 3 curves in different layers. If the input file doesn't contain the specified layer or if the layer doesn't contain any curves, it will cause error.

The link to the test file used here is this; test_input_file2.3dm.

add_library('igeo')

size( 480, 360, IG.GL )

IG.open("test_input_file2.3dm")

# first curve in layer 01
curveInLayer1 = IG.layer("Layer 01").curve(0) 
# first curve in layer 02
curveInLayer2 = IG.layer("Layer 02").curve(0) 
# first curve in layer 03
curveInLayer3 = IG.layer("Layer 03").curve(0)

For exporting geometries in saving, you can put geometries in a specified layer as well. You add a layer method layer("layer name") after geometry objects. If the specified layer doesn't exist, it creates a new layer with the specified name. Here is an example code to put geometry objects in layers.

add_library('igeo')

size( 480, 360, IG.GL )

IPoint(0,0,0).clr(1.0,0,0).layer("layerA")
ICurve(10,0,0, 20,20,0).clr(0,0,1.0).layer("layerB")
ISurface(0,0,5, 5,5,5, -10,0,10).layer("layerC")

(back to the list of tutorials)

HOME
FOR PROCESSING
DOWNLOAD
DOCUMENTS
TUTORIALS (Java / Python)
GALLERY
SOURCE CODE(GitHub)
ABOUT