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 <kiface_base.h>
24#include <regex>
25#include <string_utils.h>
26#include <locale_io.h>
27#include <wx/crt.h>
28
29#include <macros.h>
30
31#define ARG_DRILL_ORIGIN "--drill-origin"
32#define ARG_GRID_ORIGIN "--grid-origin"
33#define ARG_NO_UNSPECIFIED "--no-unspecified"
34#define ARG_NO_DNP "--no-dnp"
35#define ARG_SUBST_MODELS "--subst-models"
36#define ARG_FORCE "--force"
37#define ARG_MIN_DISTANCE "--min-distance"
38#define ARG_USER_ORIGIN "--user-origin"
39#define ARG_BOARD_ONLY "--board-only"
40#define ARG_NO_BOARD_BODY "--no-board-body"
41#define ARG_NO_COMPONENTS "--no-components"
42#define ARG_INCLUDE_TRACKS "--include-tracks"
43#define ARG_INCLUDE_ZONES "--include-zones"
44#define ARG_INCLUDE_INNER_COPPER "--include-inner-copper"
45#define ARG_FUSE_SHAPES "--fuse-shapes"
46#define ARG_NO_OPTIMIZE_STEP "--no-optimize-step"
47#define ARG_FORMAT "--format"
48#define ARG_VRML_UNITS "--units"
49#define ARG_VRML_MODELS_DIR "--models-dir"
50#define ARG_VRML_MODELS_RELATIVE "--models-relative"
51
52#define REGEX_QUANTITY "([\\s]*[+-]?[\\d]*[.]?[\\d]*)"
53#define REGEX_DELIMITER "(?:[\\s]*x)"
54#define REGEX_UNIT "([m]{2}|(?:in))"
55
57 const std::string& aDescription,
59 COMMAND( aName ),
60 m_format( aFormat )
61{
62 m_argParser.add_description( aDescription );
63 addCommonArgs( true, true, false, false );
65
67 {
68 m_argParser.add_argument( ARG_FORMAT )
69 .default_value( std::string( "step" ) )
70 .help( UTF8STDSTR(
71 _( "Output file format, options: step, brep, glb (binary glTF)" ) ) );
72 }
73
74 m_argParser.add_argument( ARG_FORCE, "-f" )
75 .help( UTF8STDSTR( _( "Overwrite output file" ) ) )
76 .flag();
77
78 m_argParser.add_argument( ARG_NO_UNSPECIFIED )
79 .help( UTF8STDSTR(
80 _( "Exclude 3D models for components with 'Unspecified' footprint type" ) ) )
81 .implicit_value( true )
82 .default_value( false );
83
84 m_argParser.add_argument( ARG_NO_DNP )
85 .help( UTF8STDSTR(
86 _( "Exclude 3D models for components with 'Do not populate' attribute" ) ) )
87 .flag();
88
91 {
92 m_argParser.add_argument( ARG_GRID_ORIGIN )
93 .help( UTF8STDSTR( _( "Use Grid Origin for output origin" ) ) )
94 .flag();
95
96 m_argParser.add_argument( ARG_DRILL_ORIGIN )
97 .help( UTF8STDSTR( _( "Use Drill Origin for output origin" ) ) )
98 .flag();
99
100 m_argParser.add_argument( "--subst-models" )
101 .help( UTF8STDSTR( _( "Substitute STEP or IGS models with the same name in place "
102 "of VRML models" ) ) )
103 .flag();
104
105 m_argParser.add_argument( ARG_BOARD_ONLY )
106 .help( UTF8STDSTR( _( "Only generate a board with no components" ) ) )
107 .flag();
108
109 m_argParser.add_argument( ARG_NO_BOARD_BODY )
110 .help( UTF8STDSTR( _( "Exclude board body" ) ) )
111 .flag();
112
113 m_argParser.add_argument( ARG_NO_COMPONENTS )
114 .help( UTF8STDSTR( _( "Exclude 3D models for components" ) ) )
115 .flag();
116
117 m_argParser.add_argument( ARG_INCLUDE_TRACKS )
118 .help( UTF8STDSTR( _( "Export tracks" ) ) )
119 .flag();
120
121 m_argParser.add_argument( ARG_INCLUDE_ZONES )
122 .help( UTF8STDSTR( _( "Export zones" ) ) )
123 .flag();
124
126 .help( UTF8STDSTR( _( "Export elements on inner copper layers" ) ) )
127 .flag();
128
129 m_argParser.add_argument( ARG_FUSE_SHAPES )
130 .help( UTF8STDSTR( _( "Fuse overlapping geometry together" ) ) )
131 .flag();
132
133 m_argParser.add_argument( ARG_MIN_DISTANCE )
134 .default_value( std::string( "0.01mm" ) )
135 .help( UTF8STDSTR(
136 _( "Minimum distance between points to treat them as separate ones" ) ) )
137 .metavar( "MIN_DIST" );
138 }
139
141 {
142 m_argParser.add_argument( ARG_NO_OPTIMIZE_STEP )
143 .help( UTF8STDSTR( _( "Do not optimize STEP file (enables writing parametric "
144 "curves)" ) ) )
145 .flag();
146 }
147
148 m_argParser.add_argument( ARG_USER_ORIGIN )
149 .default_value( std::string() )
150 .help( UTF8STDSTR( _( "User-specified output origin ex. 1x1in, 1x1inch, 25.4x25.4mm "
151 "(default unit mm)" ) ) );
152
154 {
155 m_argParser.add_argument( ARG_VRML_UNITS )
156 .default_value( std::string( "in" ) )
157 .help( UTF8STDSTR(
158 _( "Output units; valid options: mm, m, in, tenths" ) ) );
159
160 m_argParser.add_argument( ARG_VRML_MODELS_DIR )
161 .default_value( std::string( "" ) )
162 .help( UTF8STDSTR(
163 _( "Name of folder to create and store 3d models in, if not specified or "
164 "empty, the models will be embedded in main exported VRML file" ) ) );
165
167 .help( UTF8STDSTR( _( "Used with --models-dir to output relative paths in the "
168 "resulting file" ) ) )
169 .flag();
170 }
171}
172
174{
175 std::unique_ptr<JOB_EXPORT_PCB_3D> step( new JOB_EXPORT_PCB_3D( true ) );
176
178 || m_format == JOB_EXPORT_PCB_3D::FORMAT::GLB )
179 {
180 step->m_useDrillOrigin = m_argParser.get<bool>( ARG_DRILL_ORIGIN );
181 step->m_useGridOrigin = m_argParser.get<bool>( ARG_GRID_ORIGIN );
182 step->m_substModels = m_argParser.get<bool>( ARG_SUBST_MODELS );
183 step->m_exportBoardBody = !m_argParser.get<bool>( ARG_NO_BOARD_BODY );
184 step->m_exportComponents = !m_argParser.get<bool>( ARG_NO_COMPONENTS );
185 step->m_exportTracks = m_argParser.get<bool>( ARG_INCLUDE_TRACKS );
186 step->m_exportZones = m_argParser.get<bool>( ARG_INCLUDE_ZONES );
187 step->m_exportInnerCopper = m_argParser.get<bool>( ARG_INCLUDE_INNER_COPPER );
188 step->m_fuseShapes = m_argParser.get<bool>( ARG_FUSE_SHAPES );
189 step->m_boardOnly = m_argParser.get<bool>( ARG_BOARD_ONLY );
190 }
191
192 if( m_format == JOB_EXPORT_PCB_3D::FORMAT::STEP )
193 {
194 step->m_optimizeStep = !m_argParser.get<bool>( ARG_NO_OPTIMIZE_STEP );
195 }
196
197 step->m_includeUnspecified = !m_argParser.get<bool>( ARG_NO_UNSPECIFIED );
198 step->m_includeDNP = !m_argParser.get<bool>( ARG_NO_DNP );
199 step->m_overwrite = m_argParser.get<bool>( ARG_FORCE );
200 step->m_filename = m_argInput;
201 step->m_outputFile = m_argOutput;
202 step->m_format = m_format;
203 step->SetVarOverrides( m_argDefineVars );
204
205 if( step->m_format == JOB_EXPORT_PCB_3D::FORMAT::UNKNOWN )
206 {
207 wxString format = From_UTF8( m_argParser.get<std::string>( ARG_FORMAT ).c_str() );
208
209 if( format == wxS( "step" ) )
210 step->m_format = JOB_EXPORT_PCB_3D::FORMAT::STEP;
211 else if( format == wxS( "brep" ) )
212 step->m_format = JOB_EXPORT_PCB_3D::FORMAT::BREP;
213 else if( format == wxS( "glb" ) )
214 step->m_format = JOB_EXPORT_PCB_3D::FORMAT::GLB;
215 else
216 {
217 wxFprintf( stderr, _( "Invalid format specified\n" ) );
219 }
220 }
221
222 if( step->m_format == JOB_EXPORT_PCB_3D::FORMAT::VRML )
223 {
224 wxString units = From_UTF8( m_argParser.get<std::string>( ARG_VRML_UNITS ).c_str() );
225
226 if( units == wxS( "in" ) )
227 step->m_vrmlUnits = JOB_EXPORT_PCB_3D::VRML_UNITS::INCHES;
228 else if( units == wxS( "mm" ) )
230 else if( units == wxS( "m" ) )
231 step->m_vrmlUnits = JOB_EXPORT_PCB_3D::VRML_UNITS::METERS;
232 else if( units == wxS( "tenths" ) )
233 step->m_vrmlUnits = JOB_EXPORT_PCB_3D::VRML_UNITS::TENTHS;
234 else
235 {
236 wxFprintf( stderr, _( "Invalid units specified\n" ) );
238 }
239
240 step->m_vrmlModelDir = From_UTF8( m_argParser.get<std::string>( ARG_VRML_MODELS_DIR ).c_str() );
241
242 step->m_vrmlRelativePaths = m_argParser.get<bool>( ARG_VRML_MODELS_RELATIVE );
243 }
244
245 wxString userOrigin = From_UTF8( m_argParser.get<std::string>( ARG_USER_ORIGIN ).c_str() );
246
247 LOCALE_IO dummy; // Switch to "C" locale
248
249 if( !userOrigin.IsEmpty() )
250 {
252 std::regex_constants::icase );
253 std::smatch sm;
254 std::string str( userOrigin.ToUTF8() );
255 std::regex_search( str, sm, re_pattern );
256 step->m_xOrigin = atof( sm.str( 1 ).c_str() );
257 step->m_yOrigin = atof( sm.str( 2 ).c_str() );
258
259 // Default unit for m_xOrigin and m_yOrigin is mm.
260 // Convert in to board units. If the value is given in inches, it will be converted later
261 step->m_xOrigin = pcbIUScale.mmToIU( step->m_xOrigin );
262 step->m_yOrigin = pcbIUScale.mmToIU( step->m_yOrigin );
263
264 std::string tunit( sm[3] );
265
266 if( tunit.size() > 0 ) // unit specified ( default = mm, but can be in, inch or mm )
267 {
268 if( ( !sm.str( 1 ).compare( " " ) || !sm.str( 2 ).compare( " " ) )
269 || ( sm.size() != 4 ) )
270 {
271 std::cout << m_argParser;
273 }
274
275 // only in, inch and mm are valid:
276 if( !tunit.compare( "in" ) || !tunit.compare( "inch" ) )
277 {
278 step->m_xOrigin *= 25.4;
279 step->m_yOrigin *= 25.4;
280 }
281 else if( tunit.compare( "mm" ) )
282 {
283 std::cout << m_argParser;
285 }
286 }
287
288 step->m_hasUserOrigin = true;
289 }
290
292 || m_format == JOB_EXPORT_PCB_3D::FORMAT::GLB )
293 {
294 wxString minDistance =
295 From_UTF8( m_argParser.get<std::string>( ARG_MIN_DISTANCE ).c_str() );
296
297 if( !minDistance.IsEmpty() )
298 {
299 std::regex re_pattern( REGEX_QUANTITY REGEX_UNIT, std::regex_constants::icase );
300 std::smatch sm;
301 std::string str( minDistance.ToUTF8() );
302 std::regex_search( str, sm, re_pattern );
303 step->m_BoardOutlinesChainingEpsilon = atof( sm.str( 1 ).c_str() );
304
305 std::string tunit( sm[2] );
306
307 if( tunit.size() > 0 ) // No unit accepted ( default = mm )
308 {
309 if( !tunit.compare( "in" ) || !tunit.compare( "inch" ) )
310 {
311 step->m_BoardOutlinesChainingEpsilon *= 25.4;
312 }
313 else if( tunit.compare( "mm" ) )
314 {
315 std::cout << m_argParser;
317 }
318 }
319 }
320 }
321
322 int exitCode = aKiway.ProcessJob( KIWAY::FACE_PCB, step.get() );
323
324 return exitCode;
325}
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_TRACKS
#define ARG_MIN_DISTANCE
#define ARG_INCLUDE_ZONES
#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 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 _(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