-
Notifications
You must be signed in to change notification settings - Fork 12
/
openscad-step-reader.cpp
204 lines (172 loc) · 5.24 KB
/
openscad-step-reader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyright 2019 Assaf Gordon <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
#include <iostream>
#include <string>
#include <err.h>
#include <getopt.h>
#include <STEPControl_Reader.hxx>
#include <StlAPI_Writer.hxx>
#include <BRepMesh_IncrementalMesh.hxx>
#include "triangle.h"
#include "tessellation.h"
#include "openscad-triangle-writer.h"
#include "explore-shape.h"
static struct option options[] = {
{"help", no_argument, 0, 'h' },
{"version", no_argument, 0, 'V' },
{"stl-ascii", no_argument, 0, 'a' },
{"stl-scad", no_argument, 0, 's' },
{"stl-faces", no_argument, 0, 'f' },
{"stl-occt", no_argument, 0, 'o' },
{"stl-lin-tol", required_argument, 0, 'L'},
{"explore", no_argument, 0, 'e' },
};
void show_help()
{
std::cout << "openscad-step-reader\n"
"\n"
"A proof-of-concept program for STEP/OpenSCAD integration\n"
"\n"
"usage: openscad-step-reader [options] INPUT.STEP\n"
"\n"
"Output is written to STDOUT.\n"
"\n"
"options are:\n"
" -h, --help this help screen\n"
" -V, --version version information\n"
"\n"
" -o, --stl-occt convert the input STEP file into ASCII STL file\n"
" using OpenCASCADE code. This should be the baseline\n"
" when debugging/troubleshooting incorrect outputs.\n"
"\n"
" -a, --stl-ascii convert the input STEP file into custom ASCII STL file,\n"
" using our code. This is a good test to check mesh\n"
" triangulation code. EXCEPT for the 'normal' values\n"
" which are not produced, the vertex values should be\n"
" equivalent to those with --stl-occt.\n"
"\n"
" -s, --stl-scad convert the input STEP file into SCAD code, containing\n"
" a single 'polyhedron' call with the STL triangles stored\n"
" in SCAD vectors.\n"
"\n"
" -f, --stl-faces convert the input STEP file into SCAD code, retaining the\n"
" 'face' information from the STEP file. Each face will be rendered\n"
" in a different color in openscad $preview mode.\n"
"\n"
" -e, --explore Work-in-progress code, used for development and exploration\n"
" of OpenCASCADE class hierarchy, e.g.\n"
" Shell->Face->Surface->Wire->Edge->Vertex.\n"
" produces debug messges and no useful output.\n"
"\n"
"Written by Assaf Gordon ([email protected])\n"
"License: LGPLv2.1 or later\n"
"\n";
exit(0);
}
void show_version()
{
std::cout << 42 << endl;
exit(0);
}
int main(int argc, char *argv[])
{
double stl_lin_tol = 0.5 ; //default linear tolerace.
int c;
enum {
OUT_UNDEFINED,
OUT_STL_ASCII,
OUT_STL_SCAD,
OUT_STL_FACES,
OUT_STL_OCCT,
OUT_EXPLORE
} output = OUT_UNDEFINED;
while ((c = getopt_long(argc, argv, "hVasfoL:e", options, NULL))!=-1) {
switch (c)
{
case 'h':
show_help();
break;
case 'v':
show_version();
break;
case 'a':
output = OUT_STL_ASCII;
break;
case 's':
output = OUT_STL_SCAD;
break;
case 'f':
output = OUT_STL_FACES;
break;
case 'o':
output = OUT_STL_OCCT;
break;
case 'e':
output = OUT_EXPLORE;
break;
case 'L':
stl_lin_tol = atof(optarg);
if (stl_lin_tol<=0)
errx(1,"invalid tolerance value '%s'",optarg);
break;
}
}
if (optind >= argc)
errx(1,"missing input STEP filename. Use --help for usage information");
if (output == OUT_UNDEFINED)
errx(1,"missing output format option. Use --help for usage information");
std::string filename(argv[optind]);
/* Load the shape from STEP file.
See https://github.com/miho/OCC-CSG/blob/master/src/occ-csg.cpp#L311
and https://github.com/lvk88/OccTutorial/blob/master/OtherExamples/runners/convertStepToStl.cpp
*/
TopoDS_Shape shape;
STEPControl_Reader Reader;
enum IFSelect_ReturnStatus s = Reader.ReadFile(filename.c_str());
if (s != IFSelect_RetDone)
err(1,"failed to load STEP file '%s'", filename.c_str());
Reader.TransferRoots();
shape = Reader.OneShape();
/* Is this required (for Tessellation and/or StlAPI_Writer?) */
BRepMesh_IncrementalMesh mesh( shape, stl_lin_tol);
mesh.Perform();
Face_vector faces;
if ( (output==OUT_STL_ASCII) || (output==OUT_STL_SCAD) || (output==OUT_STL_FACES) )
faces = tessellate_shape (shape);
switch (output)
{
case OUT_STL_ASCII:
write_triangles_ascii_stl(faces);
break;
case OUT_STL_SCAD:
write_triangle_scad(faces);
break;
case OUT_STL_FACES:
write_faces_scad(faces);
break;
case OUT_STL_OCCT:
try
{
StlAPI_Writer writer;
writer.Write(shape,"/dev/stdout");
} catch(Standard_ConstructionError& e) {
errx(1,"failed to write OCCT/STL: %s", e.GetMessageString());
}
break;
case OUT_EXPLORE:
explore_shape (shape);
break;
}
return 0;
}