Import and Export Data
This module allows users to export/import NURBS shapes in common CAD exchange formats. The functions starting with import_ are used for generating B-spline and NURBS objects from the input files. The functions starting with export_ are used for saving B-spline and NURBS objects as files.
The following functions import/export control points or export evaluated points:
The following functions work with single or multiple surfaces:
The following functions work with single or multiple volumes:
The following functions can be used to import/export rational or non-rational spline geometries:
The following functions work with single or multiple curves and surfaces:
Function Reference
- geomdl.exchange.export_3dm(obj, file_name, **kwargs)
Exports NURBS curves and surfaces to Rhinoceros/OpenNURBS .3dm files.
Deprecated since version 5.2.2:
rw3dm
Python module is replaced byjson2on
. It can be used to convert geomdl JSON format to .3dm files. Please refer to https://github.com/orbingol/rw3dm for more details.- Parameters:
obj (abstract.Curve, abstract.Surface, multi.CurveContainer, multi.SurfaceContainer) – curves/surfaces to be exported
file_name (str) – file name
- geomdl.exchange.export_cfg(obj, file_name)
Exports curves and surfaces in libconfig format.
Note
Requires libconf package.
- Parameters:
obj (abstract.SplineGeometry, multi.AbstractContainer) – input geometry
file_name (str) – name of the output file
- Raises:
GeomdlException – an error occurred writing the file
- geomdl.exchange.export_csv(obj, file_name, point_type='evalpts', **kwargs)
Exports control points or evaluated points as a CSV file.
- Parameters:
obj (abstract.SplineGeometry) – a spline geometry object
file_name (str) – output file name
point_type (str) –
ctrlpts
for control points orevalpts
for evaluated points
- Raises:
GeomdlException – an error occurred writing the file
- geomdl.exchange.export_json(obj, file_name)
Exports curves and surfaces in JSON format.
- Parameters:
obj (abstract.SplineGeometry, multi.AbstractContainer) – input geometry
file_name (str) – name of the output file
- Raises:
GeomdlException – an error occurred writing the file
- geomdl.exchange.export_obj(surface, file_name, **kwargs)
Exports surface(s) as a .obj file.
- Keyword Arguments:
vertex_spacing
: size of the triangle edge in terms of surface points sampled. Default: 2vertex_normals
: if True, then computes vertex normals. Default: Falseparametric_vertices
: if True, then adds parameter space vertices. Default: Falseupdate_delta
: use multi-surface evaluation delta for all surfaces. Default: True
- Parameters:
surface (abstract.Surface or multi.SurfaceContainer) – surface or surfaces to be saved
file_name (str) – name of the output file
- Raises:
GeomdlException – an error occurred writing the file
- geomdl.exchange.export_off(surface, file_name, **kwargs)
Exports surface(s) as a .off file.
- Keyword Arguments:
vertex_spacing
: size of the triangle edge in terms of points sampled on the surface. Default: 1update_delta
: use multi-surface evaluation delta for all surfaces. Default: True
- Parameters:
surface (abstract.Surface or multi.SurfaceContainer) – surface or surfaces to be saved
file_name (str) – name of the output file
- Raises:
GeomdlException – an error occurred writing the file
- geomdl.exchange.export_smesh(surface, file_name, **kwargs)
Exports surface(s) as surface mesh (smesh) files.
Please see
import_smesh()
for details on the file format.- Parameters:
surface (abstract.Surface or multi.SurfaceContainer) – surface(s) to be exported
file_name (str) – name of the output file
- Raises:
GeomdlException – an error occurred writing the file
- geomdl.exchange.export_stl(surface, file_name, **kwargs)
Exports surface(s) as a .stl file in plain text or binary format.
- Keyword Arguments:
binary
: flag to generate a binary STL file. Default: Truevertex_spacing
: size of the triangle edge in terms of points sampled on the surface. Default: 1update_delta
: use multi-surface evaluation delta for all surfaces. Default: True
- Parameters:
surface (abstract.Surface or multi.SurfaceContainer) – surface or surfaces to be saved
file_name (str) – name of the output file
- Raises:
GeomdlException – an error occurred writing the file
- geomdl.exchange.export_txt(obj, file_name, two_dimensional=False, **kwargs)
Exports control points as a text file.
For curves the output is always a list of control points. For surfaces, it is possible to generate a 2-dimensional control point output file using
two_dimensional
.Please see
exchange.import_txt()
for detailed description of the keyword arguments.- Parameters:
obj (abstract.SplineGeometry) – a spline geometry object
file_name (str) – file name of the text file to be saved
two_dimensional (bool) – type of the text file (only works for Surface objects)
- Raises:
GeomdlException – an error occurred writing the file
- geomdl.exchange.export_vmesh(volume, file_name, **kwargs)
Exports volume(s) as volume mesh (vmesh) files.
- Parameters:
volume (abstract.Volume) – volume(s) to be exported
file_name (str) – name of the output file
- Raises:
GeomdlException – an error occurred writing the file
- geomdl.exchange.export_yaml(obj, file_name)
Exports curves and surfaces in YAML format.
Note
Requires ruamel.yaml package.
- Parameters:
obj (abstract.SplineGeometry, multi.AbstractContainer) – input geometry
file_name (str) – name of the output file
- Raises:
GeomdlException – an error occurred writing the file
- geomdl.exchange.import_3dm(file_name, **kwargs)
Imports curves and surfaces from Rhinoceros/OpenNURBS .3dm files.
Deprecated since version 5.2.2:
rw3dm
Python module is replaced byon2json
. It can be used to convert .3dm files to geomdl JSON format. Please refer to https://github.com/orbingol/rw3dm for more details.- Parameters:
file_name (str) – input file name
- geomdl.exchange.import_cfg(file_name, **kwargs)
Imports curves and surfaces from files in libconfig format.
Note
Requires libconf package.
Use
jinja2=True
to activate Jinja2 template processing. Please refer to the documentation for details.- Parameters:
file_name (str) – name of the input file
- Returns:
a list of rational spline geometries
- Return type:
list
- Raises:
GeomdlException – an error occurred writing the file
- geomdl.exchange.import_csv(file_name, **kwargs)
Reads control points from a CSV file and generates a 1-dimensional list of control points.
It is possible to use a different value separator via
separator
keyword argument. The following code segment illustrates the usage ofseparator
keyword argument.1# By default, import_csv uses 'comma' as the value separator 2ctrlpts = exchange.import_csv("control_points.csv") 3 4# Alternatively, it is possible to import a file containing tab-separated values 5ctrlpts = exchange.import_csv("control_points.csv", separator="\t")
The only difference of this function from
exchange.import_txt()
is skipping the first line of the input file which generally contains the column headings.- Parameters:
file_name (str) – file name of the text file
- Returns:
list of control points
- Return type:
list
- Raises:
GeomdlException – an error occurred reading the file
- geomdl.exchange.import_json(file_name, **kwargs)
Imports curves and surfaces from files in JSON format.
Use
jinja2=True
to activate Jinja2 template processing. Please refer to the documentation for details.- Parameters:
file_name (str) – name of the input file
- Returns:
a list of rational spline geometries
- Return type:
list
- Raises:
GeomdlException – an error occurred reading the file
- geomdl.exchange.import_obj(file_name, **kwargs)
Reads .obj files and generates faces.
- Keyword Arguments:
callback
: reference to the function that processes the faces for customized output
The structure of the callback function is shown below:
def my_callback_function(face_list): # "face_list" will be a list of elements.Face class instances # The function should return a list return list()
- Parameters:
file_name (str) – file name
- Returns:
output of the callback function (default is a list of faces)
- Return type:
list
- geomdl.exchange.import_smesh(file)
Generates NURBS surface(s) from surface mesh (smesh) file(s).
smesh files are some text files which contain a set of NURBS surfaces. Each file in the set corresponds to one NURBS surface. Most of the time, you receive multiple smesh files corresponding to an complete object composed of several NURBS surfaces. The files have the extensions of
txt
ordat
and they are named assmesh.X.Y.txt
smesh.X.dat
where X and Y correspond to some integer value which defines the set the surface belongs to and part number of the surface inside the complete object.
- Parameters:
file (str) – path to a directory containing mesh files or a single mesh file
- Returns:
list of NURBS surfaces
- Return type:
list
- Raises:
GeomdlException – an error occurred reading the file
- geomdl.exchange.import_txt(file_name, two_dimensional=False, **kwargs)
Reads control points from a text file and generates a 1-dimensional list of control points.
The following code examples illustrate importing different types of text files for curves and surfaces:
1# Import curve control points from a text file 2curve_ctrlpts = exchange.import_txt(file_name="control_points.txt") 3 4# Import surface control points from a text file (1-dimensional file) 5surf_ctrlpts = exchange.import_txt(file_name="control_points.txt") 6 7# Import surface control points from a text file (2-dimensional file) 8surf_ctrlpts, size_u, size_v = exchange.import_txt(file_name="control_points.txt", two_dimensional=True)
If argument
jinja2=True
is set, then the input file is processed as a Jinja2 template. You can also use the following convenience template functions which correspond to the given mathematical equations:sqrt(x)
:cubert(x)
:pow(x, y)
:
You may set the file delimiters using the keyword arguments
separator
andcol_separator
, respectively.separator
is the delimiter between the coordinates of the control points. It could be comma1, 2, 3
or space1 2 3
or something else.col_separator
is the delimiter between the control points and is only valid whentwo_dimensional
isTrue
. Assuming thatseparator
is set to space, thencol_operator
could be semi-colon1 2 3; 4 5 6
or pipe1 2 3| 4 5 6
or comma1 2 3, 4 5 6
or something else.The defaults for
separator
andcol_separator
are comma (,) and semi-colon (;), respectively.The following code examples illustrate the usage of the keyword arguments discussed above.
1# Import curve control points from a text file delimited with space 2curve_ctrlpts = exchange.import_txt(file_name="control_points.txt", separator=" ") 3 4# Import surface control points from a text file (2-dimensional file) w/ space and comma delimiters 5surf_ctrlpts, size_u, size_v = exchange.import_txt(file_name="control_points.txt", two_dimensional=True, 6 separator=" ", col_separator=",")
Please note that this function does not check whether the user set delimiters to the same value or not.
- Parameters:
file_name (str) – file name of the text file
two_dimensional (bool) – type of the text file
- Returns:
list of control points, if two_dimensional, then also returns size in u- and v-directions
- Return type:
list
- Raises:
GeomdlException – an error occurred reading the file
- geomdl.exchange.import_vmesh(file)
Imports NURBS volume(s) from volume mesh (vmesh) file(s).
- Parameters:
file (str) – path to a directory containing mesh files or a single mesh file
- Returns:
list of NURBS volumes
- Return type:
list
- Raises:
GeomdlException – an error occurred reading the file
- geomdl.exchange.import_yaml(file_name, **kwargs)
Imports curves and surfaces from files in YAML format.
Note
Requires ruamel.yaml package.
Use
jinja2=True
to activate Jinja2 template processing. Please refer to the documentation for details.- Parameters:
file_name (str) – name of the input file
- Returns:
a list of rational spline geometries
- Return type:
list
- Raises:
GeomdlException – an error occurred reading the file
VTK Support
The following functions export control points and evaluated points as VTK files (in legacy format).
- geomdl.exchange_vtk.export_polydata(obj, file_name, **kwargs)
Exports control points or evaluated points in VTK Polydata format.
Please see the following document for details: http://www.vtk.org/VTK/img/file-formats.pdf
- Keyword Arguments:
point_type
: ctrlpts for control points or evalpts for evaluated pointstessellate
: tessellates the points (works only for surfaces)
- Parameters:
obj (abstract.SplineGeometry, multi.AbstractContainer) – geometry object
file_name (str) – output file name
- Raises:
GeomdlException – an error occurred writing the file