KiCad PCB EDA Suite
Loading...
Searching...
No Matches
command_pcb_export_3d.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2022 Mark Roszko <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
22#include <cli/exit_codes.h>
23#include <base_units.h>
24#include <kiface_base.h>
25#include <regex>
26#include <string_utils.h>
27#include <locale_io.h>
28#include <wx/crt.h>
29
30#include <macros.h>
31
32#define ARG_DRILL_ORIGIN "--drill-origin"
33#define ARG_GRID_ORIGIN "--grid-origin"
34#define ARG_NO_UNSPECIFIED "--no-unspecified"
35#define ARG_NO_DNP "--no-dnp"
36#define ARG_SUBST_MODELS "--subst-models"
37#define ARG_FORCE "--force"
38#define ARG_MIN_DISTANCE "--min-distance"
39#define ARG_USER_ORIGIN "--user-origin"
40#define ARG_BOARD_ONLY "--board-only"
41#define ARG_CUT_VIAS_IN_BODY "--cut-vias-in-body"
42#define ARG_NO_BOARD_BODY "--no-board-body"
43#define ARG_NO_COMPONENTS "--no-components"
44#define ARG_INCLUDE_TRACKS "--include-tracks"
45#define ARG_INCLUDE_PADS "--include-pads"
46#define ARG_INCLUDE_ZONES "--include-zones"
47#define ARG_INCLUDE_INNER_COPPER "--include-inner-copper"
48#define ARG_INCLUDE_SILKSCREEN "--include-silkscreen"
49#define ARG_INCLUDE_SOLDERMASK "--include-soldermask"
50#define ARG_FUSE_SHAPES "--fuse-shapes"
51#define ARG_FILL_ALL_VIAS "--fill-all-vias"
52#define ARG_NO_OPTIMIZE_STEP "--no-optimize-step"
53#define ARG_NET_FILTER "--net-filter"
54#define ARG_FORMAT "--format"
55#define ARG_VRML_UNITS "--units"
56#define ARG_VRML_MODELS_DIR "--models-dir"
57#define ARG_VRML_MODELS_RELATIVE "--models-relative"
58#define ARG_COMPONENT_FILTER "--component-filter"
59
60#define REGEX_QUANTITY "([\\s]*[+-]?[\\d]*[.]?[\\d]*)"
61#define REGEX_DELIMITER "(?:[\\s]*x)"
62#define REGEX_UNIT "([m]{2}|(?:in))"
63
65 const std::string& aDescription,
67 COMMAND( aName ),
68 m_format( aFormat )
69{
70 m_argParser.add_description( aDescription );
71 addCommonArgs( true, true, false, false );
73
75 {
76 m_argParser.add_argument( ARG_FORMAT )
77 .default_value( std::string( "step" ) )
78 .help( UTF8STDSTR(
79 _( "Output file format, options: step, brep, xao, glb (binary glTF), ply, stl" ) ) );
80 }
81
82 m_argParser.add_argument( ARG_FORCE, "-f" )
83 .help( UTF8STDSTR( _( "Overwrite output file" ) ) )
84 .flag();
85
86 m_argParser.add_argument( ARG_NO_UNSPECIFIED )
87 .help( UTF8STDSTR(
88 _( "Exclude 3D models for components with 'Unspecified' footprint type" ) ) )
89 .implicit_value( true )
90 .default_value( false );
91
92 m_argParser.add_argument( ARG_NO_DNP )
93 .help( UTF8STDSTR(
94 _( "Exclude 3D models for components with 'Do not populate' attribute" ) ) )
95 .flag();
96
99 {
100 m_argParser.add_argument( ARG_GRID_ORIGIN )
101 .help( UTF8STDSTR( _( "Use Grid Origin for output origin" ) ) )
102 .flag();
103
104 m_argParser.add_argument( ARG_DRILL_ORIGIN )
105 .help( UTF8STDSTR( _( "Use Drill Origin for output origin" ) ) )
106 .flag();
107
108 m_argParser.add_argument( "--subst-models" )
109 .help( UTF8STDSTR( _( "Substitute STEP or IGS models with the same name in place "
110 "of VRML models" ) ) )
111 .flag();
112
113 m_argParser.add_argument( ARG_BOARD_ONLY )
114 .help( UTF8STDSTR( _( "Only generate a board with no components" ) ) )
115 .flag();
116
117 m_argParser.add_argument( ARG_CUT_VIAS_IN_BODY )
118 .help( UTF8STDSTR( _( "Cut via holes in board body even if conductor layers are "
119 "not exported." ) ) )
120 .flag();
121
122 m_argParser.add_argument( ARG_NO_BOARD_BODY )
123 .help( UTF8STDSTR( _( "Exclude board body" ) ) )
124 .flag();
125
126 m_argParser.add_argument( ARG_NO_COMPONENTS )
127 .help( UTF8STDSTR( _( "Exclude 3D models for components" ) ) )
128 .flag();
129
130 m_argParser.add_argument( ARG_COMPONENT_FILTER )
131 .help( UTF8STDSTR( _( "Only include component 3D models matching this list of "
132 "reference designators (comma-separated, wildcards supported)"
133 ) ) )
134 .default_value( std::string() );
135
136 m_argParser.add_argument( ARG_INCLUDE_TRACKS )
137 .help( UTF8STDSTR( _( "Export tracks and vias" ) ) )
138 .flag();
139
140 m_argParser.add_argument( ARG_INCLUDE_PADS )
141 .help( UTF8STDSTR( _( "Export pads" ) ) )
142 .flag();
143
144 m_argParser.add_argument( ARG_INCLUDE_ZONES )
145 .help( UTF8STDSTR( _( "Export zones" ) ) )
146 .flag();
147
149 .help( UTF8STDSTR( _( "Export elements on inner copper layers" ) ) )
150 .flag();
151
152 m_argParser.add_argument( ARG_INCLUDE_SILKSCREEN )
153 .help( UTF8STDSTR( _( "Export silkscreen graphics as a set of flat faces" ) ) )
154 .flag();
155
156 m_argParser.add_argument( ARG_INCLUDE_SOLDERMASK )
157 .help( UTF8STDSTR( _( "Export soldermask layers as a set of flat faces" ) ) )
158 .flag();
159
160 m_argParser.add_argument( ARG_FUSE_SHAPES )
161 .help( UTF8STDSTR( _( "Fuse overlapping geometry together" ) ) )
162 .flag();
163
164 m_argParser.add_argument( ARG_FILL_ALL_VIAS )
165 .help( UTF8STDSTR( _( "Don't cut via holes in conductor layers." ) ) )
166 .flag();
167
168 m_argParser.add_argument( ARG_MIN_DISTANCE )
169 .default_value( std::string( "0.01mm" ) )
170 .help( UTF8STDSTR(
171 _( "Minimum distance between points to treat them as separate ones" ) ) )
172 .metavar( "MIN_DIST" );
173
174 m_argParser.add_argument( ARG_NET_FILTER )
175 .default_value( std::string() )
176 .help( UTF8STDSTR( _(
177 "Only include copper items belonging to nets matching this wildcard" ) ) );
178 }
179
181 {
182 m_argParser.add_argument( ARG_NO_OPTIMIZE_STEP )
183 .help( UTF8STDSTR( _( "Do not optimize STEP file (enables writing parametric "
184 "curves)" ) ) )
185 .flag();
186 }
187
188 m_argParser.add_argument( ARG_USER_ORIGIN )
189 .default_value( std::string() )
190 .help( UTF8STDSTR( _( "User-specified output origin ex. 1x1in, 1x1inch, 25.4x25.4mm "
191 "(default unit mm)" ) ) );
192
194 {
195 m_argParser.add_argument( ARG_VRML_UNITS )
196 .default_value( std::string( "in" ) )
197 .help( UTF8STDSTR(
198 _( "Output units; valid options: mm, m, in, tenths" ) ) );
199
200 m_argParser.add_argument( ARG_VRML_MODELS_DIR )
201 .default_value( std::string( "" ) )
202 .help( UTF8STDSTR(
203 _( "Name of folder to create and store 3d models in, if not specified or "
204 "empty, the models will be embedded in main exported VRML file" ) ) );
205
207 .help( UTF8STDSTR( _( "Used with --models-dir to output relative paths in the "
208 "resulting file" ) ) )
209 .flag();
210 }
211}
212
214{
215 std::unique_ptr<JOB_EXPORT_PCB_3D> step( new JOB_EXPORT_PCB_3D() );
216 EXPORTER_STEP_PARAMS& params = step->m_3dparams;
217
219 && m_format != JOB_EXPORT_PCB_3D::FORMAT::VRML )
220 {
221 params.m_UseDrillOrigin = m_argParser.get<bool>( ARG_DRILL_ORIGIN );
222 params.m_UseGridOrigin = m_argParser.get<bool>( ARG_GRID_ORIGIN );
223 params.m_SubstModels = m_argParser.get<bool>( ARG_SUBST_MODELS );
224 params.m_CutViasInBody = m_argParser.get<bool>( ARG_CUT_VIAS_IN_BODY );
225 params.m_ExportBoardBody = !m_argParser.get<bool>( ARG_NO_BOARD_BODY );
226 params.m_ExportComponents = !m_argParser.get<bool>( ARG_NO_COMPONENTS );
227 params.m_ExportTracksVias = m_argParser.get<bool>( ARG_INCLUDE_TRACKS );
228 params.m_ExportPads = m_argParser.get<bool>( ARG_INCLUDE_PADS );
229 params.m_ExportZones = m_argParser.get<bool>( ARG_INCLUDE_ZONES );
230 params.m_ExportInnerCopper = m_argParser.get<bool>( ARG_INCLUDE_INNER_COPPER );
231 params.m_ExportSilkscreen = m_argParser.get<bool>( ARG_INCLUDE_SILKSCREEN );
232 params.m_ExportSoldermask = m_argParser.get<bool>( ARG_INCLUDE_SOLDERMASK );
233 params.m_FuseShapes = m_argParser.get<bool>( ARG_FUSE_SHAPES );
234 params.m_FillAllVias = m_argParser.get<bool>( ARG_FILL_ALL_VIAS );
235 params.m_BoardOnly = m_argParser.get<bool>( ARG_BOARD_ONLY );
236 params.m_NetFilter = From_UTF8( m_argParser.get<std::string>( ARG_NET_FILTER ).c_str() );
237 params.m_ComponentFilter =
238 From_UTF8( m_argParser.get<std::string>( ARG_COMPONENT_FILTER ).c_str() );
239
240 }
241
242 if( m_format == JOB_EXPORT_PCB_3D::FORMAT::STEP )
243 {
244 params.m_OptimizeStep = !m_argParser.get<bool>( ARG_NO_OPTIMIZE_STEP );
245 }
246
247 params.m_IncludeUnspecified = !m_argParser.get<bool>( ARG_NO_UNSPECIFIED );
248 params.m_IncludeDNP = !m_argParser.get<bool>( ARG_NO_DNP );
249 params.m_Overwrite = m_argParser.get<bool>( ARG_FORCE );
250 step->SetConfiguredOutputPath( m_argOutput );
251
252 step->m_filename = m_argInput;
253 step->m_format = m_format;
254 step->SetVarOverrides( m_argDefineVars );
255
256 if( step->m_format == JOB_EXPORT_PCB_3D::FORMAT::UNKNOWN )
257 {
258 wxString format = From_UTF8( m_argParser.get<std::string>( ARG_FORMAT ).c_str() );
259
260 if( format == wxS( "step" ) )
261 step->m_format = JOB_EXPORT_PCB_3D::FORMAT::STEP;
262 else if( format == wxS( "brep" ) )
263 step->m_format = JOB_EXPORT_PCB_3D::FORMAT::BREP;
264 else if( format == wxS( "xao" ) )
265 step->m_format = JOB_EXPORT_PCB_3D::FORMAT::XAO;
266 else if( format == wxS( "glb" ) )
267 step->m_format = JOB_EXPORT_PCB_3D::FORMAT::GLB;
268 else if( format == wxS( "ply" ) )
269 step->m_format = JOB_EXPORT_PCB_3D::FORMAT::PLY;
270 else if( format == wxS( "stl" ) )
271 step->m_format = JOB_EXPORT_PCB_3D::FORMAT::STL;
272 else
273 {
274 wxFprintf( stderr, _( "Invalid format specified\n" ) );
276 }
277 }
278
279 if( step->m_format == JOB_EXPORT_PCB_3D::FORMAT::VRML )
280 {
281 wxString units = From_UTF8( m_argParser.get<std::string>( ARG_VRML_UNITS ).c_str() );
282
283 if( units == wxS( "in" ) )
284 step->m_vrmlUnits = JOB_EXPORT_PCB_3D::VRML_UNITS::INCHES;
285 else if( units == wxS( "mm" ) )
287 else if( units == wxS( "m" ) )
288 step->m_vrmlUnits = JOB_EXPORT_PCB_3D::VRML_UNITS::METERS;
289 else if( units == wxS( "tenths" ) )
290 step->m_vrmlUnits = JOB_EXPORT_PCB_3D::VRML_UNITS::TENTHS;
291 else
292 {
293 wxFprintf( stderr, _( "Invalid units specified\n" ) );
295 }
296
297 step->m_vrmlModelDir = From_UTF8( m_argParser.get<std::string>( ARG_VRML_MODELS_DIR ).c_str() );
298
299 step->m_vrmlRelativePaths = m_argParser.get<bool>( ARG_VRML_MODELS_RELATIVE );
300 }
301
302 wxString userOrigin = From_UTF8( m_argParser.get<std::string>( ARG_USER_ORIGIN ).c_str() );
303
304 LOCALE_IO dummy; // Switch to "C" locale
305
306 if( !userOrigin.IsEmpty() )
307 {
309 std::regex_constants::icase );
310 std::smatch sm;
311 std::string str( userOrigin.ToUTF8() );
312 std::regex_search( str, sm, re_pattern );
313 step->m_3dparams.m_Origin.x = atof( sm.str( 1 ).c_str() );
314 step->m_3dparams.m_Origin.y = atof( sm.str( 2 ).c_str() );
315
316 // Default unit for m_xOrigin and m_yOrigin is mm.
317 // Convert in to board units. If the value is given in inches, it will be converted later
318 step->m_3dparams.m_Origin.x = pcbIUScale.mmToIU( step->m_3dparams.m_Origin.x );
319 step->m_3dparams.m_Origin.y = pcbIUScale.mmToIU( step->m_3dparams.m_Origin.y );
320
321 std::string tunit( sm[3] );
322
323 if( tunit.size() > 0 ) // unit specified ( default = mm, but can be in, inch or mm )
324 {
325 if( ( !sm.str( 1 ).compare( " " ) || !sm.str( 2 ).compare( " " ) )
326 || ( sm.size() != 4 ) )
327 {
328 std::cout << m_argParser;
330 }
331
332 // only in, inch and mm are valid:
333 if( !tunit.compare( "in" ) || !tunit.compare( "inch" ) )
334 {
335 step->m_3dparams.m_Origin *= 25.4;
336 }
337 else if( tunit.compare( "mm" ) )
338 {
339 std::cout << m_argParser;
341 }
342 }
343
344 step->m_hasUserOrigin = true;
345 }
346
348 && m_format != JOB_EXPORT_PCB_3D::FORMAT::VRML )
349 {
350 wxString minDistance =
351 From_UTF8( m_argParser.get<std::string>( ARG_MIN_DISTANCE ).c_str() );
352
353 if( !minDistance.IsEmpty() )
354 {
355 std::regex re_pattern( REGEX_QUANTITY REGEX_UNIT, std::regex_constants::icase );
356 std::smatch sm;
357 std::string str( minDistance.ToUTF8() );
358 std::regex_search( str, sm, re_pattern );
359 step->m_3dparams.m_BoardOutlinesChainingEpsilon = atof( sm.str( 1 ).c_str() );
360
361 std::string tunit( sm[2] );
362
363 if( tunit.size() > 0 ) // No unit accepted ( default = mm )
364 {
365 if( !tunit.compare( "in" ) || !tunit.compare( "inch" ) )
366 {
367 step->m_3dparams.m_BoardOutlinesChainingEpsilon *= 25.4;
368 }
369 else if( tunit.compare( "mm" ) )
370 {
371 std::cout << m_argParser;
373 }
374 }
375 }
376 }
377
378 int exitCode = aKiway.ProcessJob( KIWAY::FACE_PCB, step.get() );
379
380 return exitCode;
381}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
argparse::ArgumentParser m_argParser
Definition: command.h:100
void addDefineArg()
Set up the drawing sheet arg used by many of the export commands.
Definition: command.cpp:169
void addCommonArgs(bool aInput, bool aOutput, bool aInputIsDir, bool aOutputIsDir)
Set up the most common of args used across cli.
Definition: command.cpp:115
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:285
@ FACE_PCB
pcbnew DSO
Definition: kiway.h:293
int ProcessJob(KIWAY::FACE_T aFace, JOB *aJob, REPORTER *aReporter=nullptr)
Definition: kiway.cpp:711
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:49
#define UTF8STDSTR(s)
Definition: command.h:27
#define ARG_FORCE
#define ARG_FORMAT
#define ARG_VRML_MODELS_DIR
#define ARG_NO_UNSPECIFIED
#define ARG_INCLUDE_SILKSCREEN
#define ARG_INCLUDE_TRACKS
#define ARG_MIN_DISTANCE
#define ARG_INCLUDE_ZONES
#define ARG_NET_FILTER
#define REGEX_QUANTITY
#define ARG_INCLUDE_INNER_COPPER
#define ARG_VRML_MODELS_RELATIVE
#define ARG_NO_OPTIMIZE_STEP
#define REGEX_UNIT
#define ARG_BOARD_ONLY
#define ARG_NO_DNP
#define ARG_CUT_VIAS_IN_BODY
#define ARG_INCLUDE_SOLDERMASK
#define REGEX_DELIMITER
#define ARG_FILL_ALL_VIAS
#define ARG_VRML_UNITS
#define ARG_SUBST_MODELS
#define ARG_FUSE_SHAPES
#define ARG_NO_BOARD_BODY
#define ARG_NO_COMPONENTS
#define ARG_GRID_ORIGIN
#define ARG_USER_ORIGIN
#define ARG_DRILL_ORIGIN
#define ARG_COMPONENT_FILTER
#define ARG_INCLUDE_PADS
#define _(s)
This file contains miscellaneous commonly used macros and functions.
static const int ERR_ARGS
Definition: exit_codes.h:31
std::vector< FAB_LAYER_COLOR > dummy
wxString From_UTF8(const char *cstring)
PCB_EXPORT_3D_COMMAND(const std::string &aName, const std::string &aDescription, JOB_EXPORT_PCB_3D::FORMAT aFormat=JOB_EXPORT_PCB_3D::FORMAT::UNKNOWN)
int doPerform(KIWAY &aKiway) override
The internal handler that should be overloaded to implement command specific processing and work.
JOB_EXPORT_PCB_3D::FORMAT m_format
constexpr int mmToIU(double mm) const
Definition: base_units.h:88