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 (C) 1992-2024 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_NO_BOARD_BODY "--no-board-body"
42#define ARG_NO_COMPONENTS "--no-components"
43#define ARG_INCLUDE_TRACKS "--include-tracks"
44#define ARG_INCLUDE_PADS "--include-pads"
45#define ARG_INCLUDE_ZONES "--include-zones"
46#define ARG_INCLUDE_INNER_COPPER "--include-inner-copper"
47#define ARG_INCLUDE_SILKSCREEN "--include-silkscreen"
48#define ARG_INCLUDE_SOLDERMASK "--include-soldermask"
49#define ARG_FUSE_SHAPES "--fuse-shapes"
50#define ARG_NO_OPTIMIZE_STEP "--no-optimize-step"
51#define ARG_NET_FILTER "--net-filter"
52#define ARG_FORMAT "--format"
53#define ARG_VRML_UNITS "--units"
54#define ARG_VRML_MODELS_DIR "--models-dir"
55#define ARG_VRML_MODELS_RELATIVE "--models-relative"
56#define ARG_COMPONENT_FILTER "--component-filter"
57
58#define REGEX_QUANTITY "([\\s]*[+-]?[\\d]*[.]?[\\d]*)"
59#define REGEX_DELIMITER "(?:[\\s]*x)"
60#define REGEX_UNIT "([m]{2}|(?:in))"
61
63 const std::string& aDescription,
65 COMMAND( aName ),
66 m_format( aFormat )
67{
68 m_argParser.add_description( aDescription );
69 addCommonArgs( true, true, false, false );
71
73 {
74 m_argParser.add_argument( ARG_FORMAT )
75 .default_value( std::string( "step" ) )
76 .help( UTF8STDSTR(
77 _( "Output file format, options: step, brep, xao, glb (binary glTF)" ) ) );
78 }
79
80 m_argParser.add_argument( ARG_FORCE, "-f" )
81 .help( UTF8STDSTR( _( "Overwrite output file" ) ) )
82 .flag();
83
84 m_argParser.add_argument( ARG_NO_UNSPECIFIED )
85 .help( UTF8STDSTR(
86 _( "Exclude 3D models for components with 'Unspecified' footprint type" ) ) )
87 .implicit_value( true )
88 .default_value( false );
89
90 m_argParser.add_argument( ARG_NO_DNP )
91 .help( UTF8STDSTR(
92 _( "Exclude 3D models for components with 'Do not populate' attribute" ) ) )
93 .flag();
94
98 {
99 m_argParser.add_argument( ARG_GRID_ORIGIN )
100 .help( UTF8STDSTR( _( "Use Grid Origin for output origin" ) ) )
101 .flag();
102
103 m_argParser.add_argument( ARG_DRILL_ORIGIN )
104 .help( UTF8STDSTR( _( "Use Drill Origin for output origin" ) ) )
105 .flag();
106
107 m_argParser.add_argument( "--subst-models" )
108 .help( UTF8STDSTR( _( "Substitute STEP or IGS models with the same name in place "
109 "of VRML models" ) ) )
110 .flag();
111
112 m_argParser.add_argument( ARG_BOARD_ONLY )
113 .help( UTF8STDSTR( _( "Only generate a board with no components" ) ) )
114 .flag();
115
116 m_argParser.add_argument( ARG_NO_BOARD_BODY )
117 .help( UTF8STDSTR( _( "Exclude board body" ) ) )
118 .flag();
119
120 m_argParser.add_argument( ARG_NO_COMPONENTS )
121 .help( UTF8STDSTR( _( "Exclude 3D models for components" ) ) )
122 .flag();
123
124 m_argParser.add_argument( ARG_COMPONENT_FILTER )
125 .help( UTF8STDSTR( _( "Only include component 3D models matching this list of "
126 "reference designators (comma-separated, wildcards supported)"
127 ) ) )
128 .default_value( std::string() );
129
130 m_argParser.add_argument( ARG_INCLUDE_TRACKS )
131 .help( UTF8STDSTR( _( "Export tracks and vias" ) ) )
132 .flag();
133
134 m_argParser.add_argument( ARG_INCLUDE_PADS )
135 .help( UTF8STDSTR( _( "Export pads" ) ) )
136 .flag();
137
138 m_argParser.add_argument( ARG_INCLUDE_ZONES )
139 .help( UTF8STDSTR( _( "Export zones" ) ) )
140 .flag();
141
143 .help( UTF8STDSTR( _( "Export elements on inner copper layers" ) ) )
144 .flag();
145
146 m_argParser.add_argument( ARG_INCLUDE_SILKSCREEN )
147 .help( UTF8STDSTR( _( "Export silkscreen graphics as a set of flat faces" ) ) )
148 .flag();
149
150 m_argParser.add_argument( ARG_INCLUDE_SOLDERMASK )
151 .help( UTF8STDSTR( _( "Export soldermask layers as a set of flat faces" ) ) )
152 .flag();
153
154 m_argParser.add_argument( ARG_FUSE_SHAPES )
155 .help( UTF8STDSTR( _( "Fuse overlapping geometry together" ) ) )
156 .flag();
157
158 m_argParser.add_argument( ARG_MIN_DISTANCE )
159 .default_value( std::string( "0.01mm" ) )
160 .help( UTF8STDSTR(
161 _( "Minimum distance between points to treat them as separate ones" ) ) )
162 .metavar( "MIN_DIST" );
163
164 m_argParser.add_argument( ARG_NET_FILTER )
165 .default_value( std::string() )
166 .help( UTF8STDSTR( _(
167 "Only include copper items belonging to nets matching this wildcard" ) ) );
168 }
169
171 {
172 m_argParser.add_argument( ARG_NO_OPTIMIZE_STEP )
173 .help( UTF8STDSTR( _( "Do not optimize STEP file (enables writing parametric "
174 "curves)" ) ) )
175 .flag();
176 }
177
178 m_argParser.add_argument( ARG_USER_ORIGIN )
179 .default_value( std::string() )
180 .help( UTF8STDSTR( _( "User-specified output origin ex. 1x1in, 1x1inch, 25.4x25.4mm "
181 "(default unit mm)" ) ) );
182
184 {
185 m_argParser.add_argument( ARG_VRML_UNITS )
186 .default_value( std::string( "in" ) )
187 .help( UTF8STDSTR(
188 _( "Output units; valid options: mm, m, in, tenths" ) ) );
189
190 m_argParser.add_argument( ARG_VRML_MODELS_DIR )
191 .default_value( std::string( "" ) )
192 .help( UTF8STDSTR(
193 _( "Name of folder to create and store 3d models in, if not specified or "
194 "empty, the models will be embedded in main exported VRML file" ) ) );
195
197 .help( UTF8STDSTR( _( "Used with --models-dir to output relative paths in the "
198 "resulting file" ) ) )
199 .flag();
200 }
201}
202
204{
205 std::unique_ptr<JOB_EXPORT_PCB_3D> step( new JOB_EXPORT_PCB_3D( true ) );
206 EXPORTER_STEP_PARAMS& params = step->m_3dparams;
207
209 || m_format == JOB_EXPORT_PCB_3D::FORMAT::XAO
210 || m_format == JOB_EXPORT_PCB_3D::FORMAT::GLB )
211 {
212 params.m_UseDrillOrigin = m_argParser.get<bool>( ARG_DRILL_ORIGIN );
213 params.m_UseGridOrigin = m_argParser.get<bool>( ARG_GRID_ORIGIN );
214 params.m_SubstModels = m_argParser.get<bool>( ARG_SUBST_MODELS );
215 params.m_ExportBoardBody = !m_argParser.get<bool>( ARG_NO_BOARD_BODY );
216 params.m_ExportComponents = !m_argParser.get<bool>( ARG_NO_COMPONENTS );
217 params.m_ExportTracksVias = m_argParser.get<bool>( ARG_INCLUDE_TRACKS );
218 params.m_ExportPads = m_argParser.get<bool>( ARG_INCLUDE_PADS );
219 params.m_ExportZones = m_argParser.get<bool>( ARG_INCLUDE_ZONES );
220 params.m_ExportInnerCopper = m_argParser.get<bool>( ARG_INCLUDE_INNER_COPPER );
221 params.m_ExportSilkscreen = m_argParser.get<bool>( ARG_INCLUDE_SILKSCREEN );
222 params.m_ExportSoldermask = m_argParser.get<bool>( ARG_INCLUDE_SOLDERMASK );
223 params.m_FuseShapes = m_argParser.get<bool>( ARG_FUSE_SHAPES );
224 params.m_BoardOnly = m_argParser.get<bool>( ARG_BOARD_ONLY );
225 params.m_NetFilter = From_UTF8( m_argParser.get<std::string>( ARG_NET_FILTER ).c_str() );
226 params.m_ComponentFilter =
227 From_UTF8( m_argParser.get<std::string>( ARG_COMPONENT_FILTER ).c_str() );
228
229 }
230
231 if( m_format == JOB_EXPORT_PCB_3D::FORMAT::STEP )
232 {
233 params.m_OptimizeStep = !m_argParser.get<bool>( ARG_NO_OPTIMIZE_STEP );
234 }
235
236 params.m_IncludeUnspecified = !m_argParser.get<bool>( ARG_NO_UNSPECIFIED );
237 params.m_IncludeDNP = !m_argParser.get<bool>( ARG_NO_DNP );
238 params.m_Overwrite = m_argParser.get<bool>( ARG_FORCE );
239 step->SetOutputPath( m_argOutput );
240
241 step->m_filename = m_argInput;
242 step->m_format = m_format;
243 step->SetVarOverrides( m_argDefineVars );
244
245 if( step->m_format == JOB_EXPORT_PCB_3D::FORMAT::UNKNOWN )
246 {
247 wxString format = From_UTF8( m_argParser.get<std::string>( ARG_FORMAT ).c_str() );
248
249 if( format == wxS( "step" ) )
250 step->m_format = JOB_EXPORT_PCB_3D::FORMAT::STEP;
251 else if( format == wxS( "brep" ) )
252 step->m_format = JOB_EXPORT_PCB_3D::FORMAT::BREP;
253 else if( format == wxS( "xao" ) )
254 step->m_format = JOB_EXPORT_PCB_3D::FORMAT::XAO;
255 else if( format == wxS( "glb" ) )
256 step->m_format = JOB_EXPORT_PCB_3D::FORMAT::GLB;
257 else
258 {
259 wxFprintf( stderr, _( "Invalid format specified\n" ) );
261 }
262 }
263
264 if( step->m_format == JOB_EXPORT_PCB_3D::FORMAT::VRML )
265 {
266 wxString units = From_UTF8( m_argParser.get<std::string>( ARG_VRML_UNITS ).c_str() );
267
268 if( units == wxS( "in" ) )
269 step->m_vrmlUnits = JOB_EXPORT_PCB_3D::VRML_UNITS::INCHES;
270 else if( units == wxS( "mm" ) )
272 else if( units == wxS( "m" ) )
273 step->m_vrmlUnits = JOB_EXPORT_PCB_3D::VRML_UNITS::METERS;
274 else if( units == wxS( "tenths" ) )
275 step->m_vrmlUnits = JOB_EXPORT_PCB_3D::VRML_UNITS::TENTHS;
276 else
277 {
278 wxFprintf( stderr, _( "Invalid units specified\n" ) );
280 }
281
282 step->m_vrmlModelDir = From_UTF8( m_argParser.get<std::string>( ARG_VRML_MODELS_DIR ).c_str() );
283
284 step->m_vrmlRelativePaths = m_argParser.get<bool>( ARG_VRML_MODELS_RELATIVE );
285 }
286
287 wxString userOrigin = From_UTF8( m_argParser.get<std::string>( ARG_USER_ORIGIN ).c_str() );
288
289 LOCALE_IO dummy; // Switch to "C" locale
290
291 if( !userOrigin.IsEmpty() )
292 {
294 std::regex_constants::icase );
295 std::smatch sm;
296 std::string str( userOrigin.ToUTF8() );
297 std::regex_search( str, sm, re_pattern );
298 step->m_3dparams.m_Origin.x = atof( sm.str( 1 ).c_str() );
299 step->m_3dparams.m_Origin.y = atof( sm.str( 2 ).c_str() );
300
301 // Default unit for m_xOrigin and m_yOrigin is mm.
302 // Convert in to board units. If the value is given in inches, it will be converted later
303 step->m_3dparams.m_Origin.x = pcbIUScale.mmToIU( step->m_3dparams.m_Origin.x );
304 step->m_3dparams.m_Origin.y = pcbIUScale.mmToIU( step->m_3dparams.m_Origin.y );
305
306 std::string tunit( sm[3] );
307
308 if( tunit.size() > 0 ) // unit specified ( default = mm, but can be in, inch or mm )
309 {
310 if( ( !sm.str( 1 ).compare( " " ) || !sm.str( 2 ).compare( " " ) )
311 || ( sm.size() != 4 ) )
312 {
313 std::cout << m_argParser;
315 }
316
317 // only in, inch and mm are valid:
318 if( !tunit.compare( "in" ) || !tunit.compare( "inch" ) )
319 {
320 step->m_3dparams.m_Origin *= 25.4;
321 }
322 else if( tunit.compare( "mm" ) )
323 {
324 std::cout << m_argParser;
326 }
327 }
328
329 step->m_hasUserOrigin = true;
330 }
331
333 || m_format == JOB_EXPORT_PCB_3D::FORMAT::XAO
334 || m_format == JOB_EXPORT_PCB_3D::FORMAT::GLB )
335 {
336 wxString minDistance =
337 From_UTF8( m_argParser.get<std::string>( ARG_MIN_DISTANCE ).c_str() );
338
339 if( !minDistance.IsEmpty() )
340 {
341 std::regex re_pattern( REGEX_QUANTITY REGEX_UNIT, std::regex_constants::icase );
342 std::smatch sm;
343 std::string str( minDistance.ToUTF8() );
344 std::regex_search( str, sm, re_pattern );
345 step->m_3dparams.m_BoardOutlinesChainingEpsilon = atof( sm.str( 1 ).c_str() );
346
347 std::string tunit( sm[2] );
348
349 if( tunit.size() > 0 ) // No unit accepted ( default = mm )
350 {
351 if( !tunit.compare( "in" ) || !tunit.compare( "inch" ) )
352 {
353 step->m_3dparams.m_BoardOutlinesChainingEpsilon *= 25.4;
354 }
355 else if( tunit.compare( "mm" ) )
356 {
357 std::cout << m_argParser;
359 }
360 }
361 }
362 }
363
364 int exitCode = aKiway.ProcessJob( KIWAY::FACE_PCB, step.get() );
365
366 return exitCode;
367}
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:284
int ProcessJob(KIWAY::FACE_T aFace, JOB *aJob)
Definition: kiway.cpp:709
@ FACE_PCB
pcbnew DSO
Definition: kiway.h:292
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_INCLUDE_SOLDERMASK
#define REGEX_DELIMITER
#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