A prime ingredient of any 3D design flow is the ability to import from and export to other tools. TheSTL file format is currently the most common format used.
In older versions of the application files of each format were imported using a module specific to it.These older modules are deprecated in newer versions but are still documented in this page.
Currently theimport()
Object Module processes each type of file according to its file extension to instantiate imported objects.[Note:Requires version2015]
There are some other, purpose specific, import methods in the app:
include <filename.csg>
statement.A.csg
format file is a type of geometry file exported from OpenSCAD using a subset of its programming language.As such the shapes described by it may be imported into a program using theinclude <filename.csg>
statement.
It is also a format recognized by the import() module .
This is anObject Module that imports geometry for use in the script.The extension on the filename informs the module as to which input processor to use.
Normally modules do not return a value when called, but if theimport-function
feature is enabled inPreferences>Features
the data imported from certain of the file formats may be assigned to a variable for further processing.
import( filename );import_results = import(filename);
There is a concise comparison of the commonly used file formats in 3D printing on theAnyCubic website that may be of interest.
These are the file formats recognized byimport()
:
DXF and SVG are the only drawing file formats that may be directly imported but we can offer some tips for usingother 2D formats.
Read a layer of a 2D DXF file and create a 3D shape.
linear_extrude(height = 5, center = true, convexity = 10)import_dxf(file = "example009.dxf", layer = "plate");
This image shows a 2D shape with a convexity of 2, as the ray indicated in red intersects with the 2D shape in at most two sections. The convexity of a 3D shape would be determined in a similar way. Setting it to 10 should work fine for most cases.
It is strongly recommended that a mesh described in an STL file should be be manifold, not contain holes, nor intersection itself.The import process may well succeed with such issues present and show the results in the preview panel.However combining that object with something else, or doing a full render, will likely emit warnings about it not being manifold, cause it to disappear, or to emit CGAL errors like:
CGAL error in CGAL_Build_PolySet: CGAL ERROR: assertion violation! Expr: check_protocoll == 0 File: /user/openscad_deps/../CGAL/Polyhedron_incremental_builder_3.h
or
CGAL error in CGAL_Nef_polyhedron3(): CGAL ERROR: assertion violation! Expr: pe_prev->is_border() || !internal::Plane_constructor<Plane>::get_plane(pe_prev->facet(),pe_prev->facet()->plane()).is_degenerate()
In order to clean the STL file, you have the following options:
Using MeshLab, you can do:
Next, you can click the icon 'Fill Hole', select all the holes and click Fill and then Accept. You might have to redo this action a few times.
Use File - Export Mesh to save the STL.
If Meshlab can't fill the last hole then Blender might help:
This requires enablingimport-function feature in development build. If you import a file with the suffix "json" or "csv", import returns a JSON-object datatype which there is not a way to express as a literal value -- it can only be imported.
Note: Files with the ".csv" file suffix are also treated as JSON files, though these formats are not the same -- a CSV file saved from a spreadsheet program cannot be used here.
/* input file contains:{"people":[{"name":"Helen", "age":19}, {"name":"Chris", "age":32}]}*/t=import("people.json");echo(t);people=t.people;for(i=[0:len(people)-1]){person=people[i];echo(str(person.name,": ",person.age));}
Which results in this output:
ECHO: { people = [{ age = 19; name = "Helen"; }, { age = 32; name = "Chris"; }]; }ECHO: "Helen: 19"ECHO: "Chris: 32"
[Deprecated:import_dxf() will be removed in future releases. Useimport() instead.]
Read a DXF file and create a 3D shape.
linear_extrude(height = 5, center = true, convexity = 10)import_dxf(file = "example009.dxf", layer = "plate");
[Deprecated:import_stl() will be removed in future releases. Useimport() instead. See above.]
Imports an STL file for use in the current OpenSCAD model
import_stl("body.stl", convexity = 5);
surface()
readsHeightmap information from text or image files.It can readPNG files.
The format for text based heightmaps is a matrix of numbers that represent the height for a specific point. Rows are mapped to the Y-axis, columns to the X axis.The numbers must be separated by spaces or tabs. Empty lines and lines starting with a # character are ignored.
[Note:Requires version2015.03]
Currently only PNG images are supported. Alpha channel information of the image is ignored and the height for the pixel is determined by converting the color value toGrayscale using the linear luminance for the sRGB color space (Y = 0.2126R + 0.7152G + 0.0722B). The gray scale values are scaled to be in the range 0 to 100.
Example 1:
//surface.scadsurface(file = "surface.dat", center = true, convexity = 5);%translate([0,0,5])cube([10,10,10], center =true);
#surface.dat10 9 8 7 6 5 5 5 5 5 9 8 7 6 6 4 3 2 1 0 8 7 6 6 4 3 2 1 0 07 6 6 4 3 2 1 0 0 06 6 4 3 2 1 1 0 0 06 6 3 2 1 1 1 0 0 06 6 2 1 1 1 1 0 0 06 6 1 0 0 0 0 0 0 03 1 0 0 0 0 0 0 0 03 0 0 0 0 0 0 0 0 0
Result:
Example 2
// example010.dat generated using octave: // d = (sin(1:0.2:10)' * cos(1:0.2:10)) * 10; // save("example010.dat", "d"); intersection() { surface(file = "example010.dat", center = true, convexity = 5); rotate(45, [0, 0, 1]) surface(file = "example010.dat", center = true, convexity = 5); }
Example 3:
[Note:Requires version2015.03]
// Example 3ascale([1, 1, 0.1]) surface(file = "smiley.png", center = true);
// Example 3bscale([1, 1, 0.1]) surface(file = "smiley.png", center = true, invert = true);
On the "File" menu select the "Export" item to open the file type selector and tap "Export as STL..."
A file dialog box will open to place the exported file into.The ".stl" extension must be explicitly added.
Aftercompile and render CGAL (F6), you may see that your design issimple: no.
When you try to export this to .STL, this message appears:
Object isn't a valid 2-manifold! Modify your design..
// Parametersmain_diameter = 70;main_thickness = 30;hole_diameter = 5;num_holes = 3;hole_radius = main_diameter / 2 - 5; // Inset from edgedifference() { // Main cylinder cylinder(d=main_diameter, h=main_thickness, $fn=100); // Holes for (i = [0 : 360 / num_holes : 359]) { angle = i; x = hole_radius * cos(angle); y = hole_radius * sin(angle); translate([x, y, 0]) // Add height to ensure full cut cylinder(d=hole_diameter, h=main_thickness + 1, $fn=60); }}
"Manifold" means that it is "water tight" and that there are no holes in the geometry.In a valid 2-manifold each edge must connect exactly two facets. That means that the program must be able to connect a face with an object. E.g. if you use a cube of height 10 to carve out something from a wider cube of height 10, it is not clear to which cube the top or the bottom belongs. So make the small extracting cube a bit "longer" (or "shorter"):
difference(){// originalcube(size=[2,2,2]);// object that carves out#translate([0.5,0.5,-0.5]){cube(size=[1,1,3]);}}
Here is a more tricky little example taken from theOpenSCAD Forum (retrieved 15:13, 22 March 2010 (UTC)):
moduleexample1(){cube([20,20,20]);translate([-20,-20,0])cube([20,20,20]);cube([50,50,5],center=true);}moduleexample2(){cube([20.1,20.1,20]);translate([-20,-20,0])cube([20.1,20.1,20]);cube([50,50,5],center=true);}
Example1 would render like this:
Theexample1 module is not a valid 2-manifold because both cubes aresharing one edge. They touch each other but do not intersect.
Example2 is a valid 2-manifold because there is an intersection. Now the construct meets the 2-manifold constraint stipulating thateach edge must connect exactly two facets.
Pieces you are subtracting must extend past the original part. (OpenSCAD Tip: Manifold Space and Time, retrieved 18:40, 22 March 2010 (UTC)).
For reference, another situation that causes the design to be non-exportable is when two faces that are each the result of a subtraction touch. Then the error message comes up.
difference () { cube ([20,10,10]); translate ([10,0,0]) cube (10);}difference () { cube ([20,10,10]); cube (10);}
simply touching surfaces is correctly handled.
translate ([10,0,0]) cube (10);cube (10);