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