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