home processing download documents tutorial python tutorial gallery source about
 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.

import processing.opengl.*;
import 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.

import processing.opengl.*;
import 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).

import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

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

IPoint[] pts = IG.points(); //getting points

ICurve[] crvs = IG.curves(); //getting curves

ISurface[] srfs = IG.surfaces(); //getting surfaces

IMesh[] meshes = IG.meshes(); //getting polygon meshes

IBrep[] breps = IG.breps(); //getting Breps

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

import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

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

IPoint[] pts = IG.points(); //getting points
for(int i=0; i < pts.length; i++){
  // do something with pts[i]
}

ICurve[] crvs = IG.curves(); //getting curves
for(int i=0; i < crvs.length; i++){
  // do something with crvs[i]
}

ISurface[] srfs = IG.surfaces(); //getting surfaces
for(int i=0; i < srfs.length; i++){
  // do something with srfs[i]
}

IMesh[] meshes = IG.meshes(); //getting polygon meshes
for(int i=0; i < meshes.length; i++){
  // do something with meshes[i]
}

IBrep[] breps = IG.breps(); //getting BReps
for(int i=0; i < breps.length; i++){
  // do something with breps[i]
}

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.

import processing.opengl.*;
import 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(int i=0; i < IG.pointNum(); i++){
  // do something with IG.point(i)
}

for(int i=0; i < IG.curveNum(); i++){
  // do something with IG.curve(i)
}

for(int i=0; i < IG.surfaceNum(); i++){
  // do something with IG.surface(i)
}

for(int i=0; i < IG.meshNum(); i++){
  // do something with IG.mesh(i)
}

for(int i=0; i < IG.brepNum(); i++){
  // do something with IG.brep(i)
}

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(int i=0; i < IG.pointNum(); i++){
  new 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.

import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

IG.open("test_input_file2.3dm"); 

// first curve in layer 01
ICurve curveInLayer1 = IG.layer("Layer 01").curve(0); 
// first curve in layer 02
ICurve curveInLayer2 = IG.layer("Layer 02").curve(0); 
// first curve in layer 03
ICurve 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.

import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

new IPoint(0,0,0).clr(1.0,0,0).layer("layerA");
new ICurve(10,0,0, 20,20,0).clr(0,0,1.0).layer("layerB");
new 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