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