Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

OpenSCAD User Manual/STL Import and Export

From Wikibooks, open books for an open world
<OpenSCAD User Manual
Thelatest reviewed version waschecked on26 May 2019. There aretemplate/file changes awaiting review.

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.

0% developed  as of November 17, 2009Import Modules

[edit |edit source]

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:

A '.csg' geometry file
may be imported by aninclude <filename.csg> statement.
A '.png' image
Theimport image option of thesurface()Object Module may be used to apply a height map to a surface.
data in a text file
Theimport data file option of thesurface() method may also be used to apply a height map.

Include <"filename.csg">

[edit |edit source]

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 .

Import() Object Module

[edit |edit source]

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);

Parameters

[edit |edit source]
file
A string containing a filename and extension of form "filename.ext". The location of the file is the same as the script's file. Refer to theFile System Issues page for other file path options and platform specific information.
origin
vector of form [xcoord,ycoord]
scale
number. A scale factor to apply to the model
width
number
height
number
dpi
non-negative integer >= 0.001. The Dots Per Inch value in an SVG or DXF file to correctly size the generated geometry.
center
Boolean. When true the object is centered in the X-Y plane at the origin.[Note:Requires versionDevelopment snapshot]
convexity
non-negative integer. The convexity parameter specifies the maximum number of front sides (or back sides) a ray intersecting the object might penetrate. This parameter is needed only for correctly displaying the object in OpenCSG preview mode and has no effect on the polyhedron rendering. Optional.
id
String. For SVG import only, the id of an element or group to import. Optional.[Note:Requires versionDevelopment snapshot]
layername
Deprecated, uselayer.
layer
For DXF and SVG import only, specify a specific layer to import. Optional.
$fn
Double. The number of polygon segments to use when converting circles, arcs, and curves to polygons.[Note:Requires versionDevelopment snapshot]
$fa
Double. The minimum angle step to use when converting circles and arcs to polygons.[Note:Requires versionDevelopment snapshot]
$fs
Double. The minimum segment length to use when converting circles and arcs to polygons.[Note:Requires versionDevelopment snapshot]

3D Geometry Formats

[edit |edit source]

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():

OBJ
OBJect data file by Wavefront
AMF
Additive Manufacturing Files include color, texture, and other meta data in addition to a 3D polygonal geometry but is deprecated as it has not achieved much acceptance in the industry.
3MF
3D Manufacturing Format XML data file
CSG
solid geometry format of OpenSCAD programming language
STL
STereoLithography file format by 3D Systems for describing polygons in a 3D coordinate space. Replaced byAdditive Manufacturing Format (.amf)
OFF
Object File Format
NEF3
aCGAL geometry data format from aNef Polygon object (ref section 15.6). It is used mostly by the OpenSCAD dev group as a debugging tool when building test cases for upstream reporting to CGAL.

2D Geometry Formats

[edit |edit source]

DXF and SVG are the only drawing file formats that may be directly imported but we can offer some tips for usingother 2D formats.

DXF
Drawing Exchange Format developed by AutoCAD
SVG
Scalable Vector Graphics format
previously handled byimport_SVG() module
PS/EPS
Postscript, seeother 2D formats page.
Illustrator
seeother 2D formats page.

Image Formats

[edit |edit source]
PNG
Portable Network Graphics image file. As already mentioned, thesurface() method is able to derive a gray scale value from each pixel to apply to a surface as a height map.

Data formats

[edit |edit source]
JSON
[Note:Requires versionDevelopment snapshot]
data in a text file
Another use of thesurface() method is to import numeric data from a text file to apply to a surface as a height map.

DXF example

[edit |edit source]

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");

Convexity

[edit |edit source]

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.

STL Issues

[edit |edit source]

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:

  • usehttp://wiki.netfabb.com/Semi-Automatic_Repair_Options to repair the holes but not the self-intersections.
  • use netfabb basic. This free software doesn't have the option to close holes nor can it fix the self-intersections.
  • use MeshLab, This free software can fix all the issues.

Using MeshLab, you can do:

  • Render - Show non Manif Edges
  • Render - Show non Manif Vertices
  • if found, use Filters - Selection - Select non Manifold Edges or Select non Manifold Vertices - Apply - Close. Then click button 'Delete the current set of selected vertices...' or checkhttp://www.youtube.com/watch?v=oDx0Tgy0UHo for an instruction video. The screen should show "0 non manifold edges", "0 non manifold vertices"


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:

  1. Start Blender
  2. `X, 1` to remove the default object
  3. File, Import, Stl
  4. `Tab` to edit the mesh
  5. `A` to de-select all vertices
  6. `Alt+Ctrl+Shift+M` to select all non-manifold vertices
  7. `MMB` to rotate, `Shift+MMB` to pan, `wheel` to zoom
  8. `C` for "circle" select, `Esc` to finish
  9. `Alt+M, 1` to merge or `Space` and search for "merge" as alternative
  10. Merging vertices is a useful way of filling holes where the vertices are so closely packed that the slight change in geometry is unimportant compared to the precision of a typical 3D printer

Importing JSON

[edit |edit source]

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"

import_dxf

[edit |edit source]

[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");

import_stl

[edit |edit source]

[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

[edit |edit source]

surface() readsHeightmap information from text or image files.It can readPNG files.

Parameters

[edit |edit source]
file
String. The path to the file containing the heightmap data.
center
Boolean. This determines the positioning of the generated object. If true, object is centered in X- and Y-axis. Otherwise, the object is placed in the positive quadrant. Defaults to false.
invert
Boolean. Inverts how the color values of imported images are translated into height values. This has no effect when importing text data files. Defaults to false.[Note:Requires version2015.03]
convexity
Integer. The convexity parameter specifies the maximum number of front sides (back sides) a ray intersecting the object might penetrate. This parameter is needed only for correct display of the object in OpenCSG preview mode and has no effect on the final rendering.

Text file format

[edit |edit source]

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.

Images

[edit |edit source]

[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.

Examples

[edit |edit source]

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);
Input image
Input image
Surface output
Example 3a: surface(invert = false)
Surface output inverted
Example 3b: surface(invert = true)
Example 3: Using surface() with a PNG image as heightmap input.

0% developed  as of November 17, 2009Export Commands

[edit |edit source]

STL Export

[edit |edit source]

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.

Trouble shooting

[edit |edit source]

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]);}}
Correct use of difference

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:

A not valid 2-manifold cube (simple = no)

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);
Retrieved from "https://en.wikibooks.org/w/index.php?title=OpenSCAD_User_Manual/STL_Import_and_Export&oldid=3392926"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp