KiCad PCB EDA Suite
export_vrml.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) 2009-2013 Lorenzo Mercantonio
5  * Copyright (C) 2014-2017 Cirilo Bernardo
6  * Copyright (C) 2018 Jean-Pierre Charras jp.charras at wanadoo.fr
7  * Copyright (C) 2004-2018 KiCad Developers, see AUTHORS.txt for contributors.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
27 #include <cmath>
28 #include <exception>
29 #include <fstream>
30 #include <iomanip>
31 #include <vector>
32 #include <wx/dir.h>
33 
34 #include "3d_cache/3d_cache.h"
35 #include "3d_cache/3d_info.h"
36 #include "class_board.h"
37 #include "class_edge_mod.h"
38 #include "class_module.h"
39 #include "class_pcb_text.h"
40 #include "class_track.h"
41 #include "class_zone.h"
42 #include "convert_to_biu.h"
43 #include <filename_resolver.h>
44 #include "gr_text.h"
45 #include "macros.h"
46 #include "pgm_base.h"
47 #include "plugins/3dapi/ifsg_all.h"
48 #include "streamwrapper.h"
49 #include "vrml_layer.h"
50 #include "pcb_edit_frame.h"
51 
54 
55 #include <zone_filler.h>
56 
57 // minimum width (mm) of a VRML line
58 #define MIN_VRML_LINEWIDTH 0.05 // previously 0.12
59 
60 // offset for art layers, mm (silk, paste, etc)
61 #define ART_OFFSET 0.025
62 // offset for plating
63 #define PLATE_OFFSET 0.005
64 
65 static S3D_CACHE* cache;
66 static bool USE_INLINES; // true to use legacy inline{} behavior
67 static bool USE_DEFS; // true to reuse component definitions
68 static bool USE_RELPATH; // true to use relative paths in VRML inline{}
69 static double WORLD_SCALE = 1.0; // scaling from 0.1 in to desired VRML unit
70 static double BOARD_SCALE; // scaling from mm to desired VRML world scale
71 static const int PRECISION = 6; // legacy precision factor (now set to 6)
72 static wxString SUBDIR_3D; // legacy 3D subdirectory
73 static wxString PROJ_DIR; // project directory
74 
75 struct VRML_COLOR
76 {
77  float diffuse_red;
78  float diffuse_grn;
79  float diffuse_blu;
80 
81  float spec_red;
82  float spec_grn;
83  float spec_blu;
84 
85  float emit_red;
86  float emit_grn;
87  float emit_blu;
88 
89  float ambient;
90  float transp;
91  float shiny;
92 
94  {
95  // default green
96  diffuse_red = 0.13f;
97  diffuse_grn = 0.81f;
98  diffuse_blu = 0.22f;
99  spec_red = 0.01f;
100  spec_grn = 0.08f;
101  spec_blu = 0.02f;
102  emit_red = 0.0f;
103  emit_grn = 0.0f;
104  emit_blu = 0.0f;
105 
106  ambient = 0.8f;
107  transp = 0.0f;
108  shiny = 0.02f;
109  }
110 
111  VRML_COLOR( float dr, float dg, float db,
112  float sr, float sg, float sb,
113  float er, float eg, float eb,
114  float am, float tr, float sh )
115  {
116  diffuse_red = dr;
117  diffuse_grn = dg;
118  diffuse_blu = db;
119  spec_red = sr;
120  spec_grn = sg;
121  spec_blu = sb;
122  emit_red = er;
123  emit_grn = eg;
124  emit_blu = eb;
125 
126  ambient = am;
127  transp = tr;
128  shiny = sh;
129  }
130 };
131 
133 {
140 };
141 
144 
146 {
147 private:
149 
150  int m_iMaxSeg; // max. sides to a small circle
151  double m_arcMinLen, m_arcMaxLen; // min and max lengths of an arc chord
152 
153 public:
155  VRML_LAYER m_holes;
156  VRML_LAYER m_board;
157  VRML_LAYER m_top_copper;
158  VRML_LAYER m_bot_copper;
159  VRML_LAYER m_top_silk;
160  VRML_LAYER m_bot_silk;
161  VRML_LAYER m_top_tin;
162  VRML_LAYER m_bot_tin;
163  VRML_LAYER m_plated_holes;
164 
165  std::list< SGNODE* > m_components;
166 
168 
169  double m_minLineWidth; // minimum width of a VRML line segment
170 
171  double m_tx; // global translation along X
172  double m_ty; // global translation along Y
173 
174  double m_brd_thickness; // depth of the PCB
175 
178 
180  {
181  for( unsigned i = 0; i < arrayDim( m_layer_z ); ++i )
182  m_layer_z[i] = 0;
183 
184  m_holes.GetArcParams( m_iMaxSeg, m_arcMinLen, m_arcMaxLen );
185 
186  // this default only makes sense if the output is in mm
187  m_brd_thickness = 1.6;
188 
189  // pcb green
191  0.07f, 0.3f, 0.12f, 0.01f, 0.03f, 0.01f, 0.0f, 0.0f, 0.0f, 0.8f, 0.0f, 0.02f );
192  // track green
194  0.08f, 0.5f, 0.1f, 0.01f, 0.05f, 0.01f, 0.0f, 0.0f, 0.0f, 0.8f, 0.0f, 0.02f );
195  // silkscreen white
197  0.9f, 0.9f, 0.9f, 0.1f, 0.1f, 0.1f, 0.0f, 0.0f, 0.0f, 0.9f, 0.0f, 0.02f );
198  // pad silver
199  colors[VRML_COLOR_TIN] = VRML_COLOR( 0.749f, 0.756f, 0.761f, 0.749f, 0.756f, 0.761f, 0.0f,
200  0.0f, 0.0f, 0.8f, 0.0f, 0.8f );
201 
202  m_plainPCB = false;
203  SetOffset( 0.0, 0.0 );
204  m_text_layer = F_Cu;
205  m_text_width = 1;
207  }
208 
210  {
211  // destroy any unassociated material appearances
212  for( int j = 0; j < VRML_COLOR_LAST; ++j )
213  {
214  if( sgmaterial[j] && NULL == S3D::GetSGNodeParent( sgmaterial[j] ) )
216 
217  sgmaterial[j] = NULL;
218  }
219 
220  if( !m_components.empty() )
221  {
222  IFSG_TRANSFORM tmp( false );
223 
224  for( auto i : m_components )
225  {
226  tmp.Attach( i );
227  tmp.SetParent( NULL );
228  }
229 
230  m_components.clear();
232  }
233  }
234 
236  {
237  return colors[aIndex];
238  }
239 
240  void SetOffset( double aXoff, double aYoff )
241  {
242  m_tx = aXoff;
243  m_ty = -aYoff;
244 
245  m_holes.SetVertexOffsets( aXoff, aYoff );
246  m_board.SetVertexOffsets( aXoff, aYoff );
247  m_top_copper.SetVertexOffsets( aXoff, aYoff );
248  m_bot_copper.SetVertexOffsets( aXoff, aYoff );
249  m_top_silk.SetVertexOffsets( aXoff, aYoff );
250  m_bot_silk.SetVertexOffsets( aXoff, aYoff );
251  m_top_tin.SetVertexOffsets( aXoff, aYoff );
252  m_bot_tin.SetVertexOffsets( aXoff, aYoff );
253  m_plated_holes.SetVertexOffsets( aXoff, aYoff );
254  }
255 
256  double GetLayerZ( LAYER_NUM aLayer )
257  {
258  if( unsigned( aLayer ) >= arrayDim( m_layer_z ) )
259  return 0;
260 
261  return m_layer_z[ aLayer ];
262  }
263 
264  void SetLayerZ( LAYER_NUM aLayer, double aValue )
265  {
266  m_layer_z[aLayer] = aValue;
267  }
268 
269  // set the scaling of the VRML world
270  bool SetScale( double aWorldScale )
271  {
272  if( aWorldScale < 0.001 || aWorldScale > 10.0 )
273  throw( std::runtime_error( "WorldScale out of range (valid range is 0.001 to 10.0)" ) );
274 
275  m_OutputPCB.SetScale( aWorldScale * 2.54 );
276  WORLD_SCALE = aWorldScale * 2.54;
277 
278  return true;
279  }
280 
281 };
282 
283 
284 // static var. for dealing with text
286 
287 
288 // select the VRML layer object to draw on; return true if
289 // a layer has been selected.
290 static bool GetLayer( MODEL_VRML& aModel, LAYER_NUM layer, VRML_LAYER** vlayer )
291 {
292  switch( layer )
293  {
294  case B_Cu:
295  *vlayer = &aModel.m_bot_copper;
296  break;
297 
298  case F_Cu:
299  *vlayer = &aModel.m_top_copper;
300  break;
301 
302  case B_SilkS:
303  *vlayer = &aModel.m_bot_silk;
304  break;
305 
306  case F_SilkS:
307  *vlayer = &aModel.m_top_silk;
308  break;
309 
310  default:
311  return false;
312  }
313 
314  return true;
315 }
316 
317 static void create_vrml_shell( IFSG_TRANSFORM& PcbOutput, VRML_COLOR_INDEX colorID,
318  VRML_LAYER* layer, double top_z, double bottom_z );
319 
320 static void create_vrml_plane( IFSG_TRANSFORM& PcbOutput, VRML_COLOR_INDEX colorID,
321  VRML_LAYER* layer, double aHeight, bool aTopPlane );
322 
323 static void write_triangle_bag( std::ostream& aOut_file, VRML_COLOR& aColor,
324  VRML_LAYER* aLayer, bool aPlane, bool aTop,
325  double aTop_z, double aBottom_z )
326 {
327  /* A lot of nodes are not required, but blender sometimes chokes
328  * without them */
329  static const char* shape_boiler[] =
330  {
331  "Transform {\n",
332  " children [\n",
333  " Group {\n",
334  " children [\n",
335  " Shape {\n",
336  " appearance Appearance {\n",
337  " material Material {\n",
338  0, // Material marker
339  " }\n",
340  " }\n",
341  " geometry IndexedFaceSet {\n",
342  " solid TRUE\n",
343  " coord Coordinate {\n",
344  " point [\n",
345  0, // Coordinates marker
346  " ]\n",
347  " }\n",
348  " coordIndex [\n",
349  0, // Index marker
350  " ]\n",
351  " }\n",
352  " }\n",
353  " ]\n",
354  " }\n",
355  " ]\n",
356  "}\n",
357  0 // End marker
358  };
359 
360  int marker_found = 0, lineno = 0;
361 
362  while( marker_found < 4 )
363  {
364  if( shape_boiler[lineno] )
365  aOut_file << shape_boiler[lineno];
366  else
367  {
368  marker_found++;
369 
370  switch( marker_found )
371  {
372  case 1: // Material marker
373  aOut_file << " diffuseColor " << std::setprecision(3);
374  aOut_file << aColor.diffuse_red << " ";
375  aOut_file << aColor.diffuse_grn << " ";
376  aOut_file << aColor.diffuse_blu << "\n";
377 
378  aOut_file << " specularColor ";
379  aOut_file << aColor.spec_red << " ";
380  aOut_file << aColor.spec_grn << " ";
381  aOut_file << aColor.spec_blu << "\n";
382 
383  aOut_file << " emissiveColor ";
384  aOut_file << aColor.emit_red << " ";
385  aOut_file << aColor.emit_grn << " ";
386  aOut_file << aColor.emit_blu << "\n";
387 
388  aOut_file << " ambientIntensity " << aColor.ambient << "\n";
389  aOut_file << " transparency " << aColor.transp << "\n";
390  aOut_file << " shininess " << aColor.shiny << "\n";
391  break;
392 
393  case 2:
394 
395  if( aPlane )
396  aLayer->WriteVertices( aTop_z, aOut_file, PRECISION );
397  else
398  aLayer->Write3DVertices( aTop_z, aBottom_z, aOut_file, PRECISION );
399 
400  aOut_file << "\n";
401  break;
402 
403  case 3:
404 
405  if( aPlane )
406  aLayer->WriteIndices( aTop, aOut_file );
407  else
408  aLayer->Write3DIndices( aOut_file );
409 
410  aOut_file << "\n";
411  break;
412 
413  default:
414  break;
415  }
416  }
417 
418  lineno++;
419  }
420 }
421 
422 
423 static void write_layers( MODEL_VRML& aModel, BOARD* aPcb,
424  const char* aFileName, OSTREAM* aOutputFile )
425 {
426  // VRML_LAYER board;
427  aModel.m_board.Tesselate( &aModel.m_holes );
428  double brdz = aModel.m_brd_thickness / 2.0
429  - ( Millimeter2iu( ART_OFFSET / 2.0 ) ) * BOARD_SCALE;
430 
431  if( USE_INLINES )
432  {
433  write_triangle_bag( *aOutputFile, aModel.GetColor( VRML_COLOR_PCB ),
434  &aModel.m_board, false, false, brdz, -brdz );
435  }
436  else
437  {
438  create_vrml_shell( aModel.m_OutputPCB, VRML_COLOR_PCB, &aModel.m_board, brdz, -brdz );
439  }
440 
441  if( aModel.m_plainPCB )
442  {
443  if( !USE_INLINES )
444  S3D::WriteVRML( aFileName, true, aModel.m_OutputPCB.GetRawPtr(), USE_DEFS, true );
445 
446  return;
447  }
448 
449  // VRML_LAYER m_top_copper;
450  aModel.m_top_copper.Tesselate( &aModel.m_holes );
451 
452  if( USE_INLINES )
453  {
454  write_triangle_bag( *aOutputFile, aModel.GetColor( VRML_COLOR_TRACK ),
455  &aModel.m_top_copper, true, true,
456  aModel.GetLayerZ( F_Cu ), 0 );
457  }
458  else
459  {
461  aModel.GetLayerZ( F_Cu ), true );
462  }
463 
464  // VRML_LAYER m_top_tin;
465  aModel.m_top_tin.Tesselate( &aModel.m_holes );
466 
467  if( USE_INLINES )
468  {
469  write_triangle_bag( *aOutputFile, aModel.GetColor( VRML_COLOR_TIN ),
470  &aModel.m_top_tin, true, true,
471  aModel.GetLayerZ( F_Cu ) + Millimeter2iu( ART_OFFSET / 2.0 ) * BOARD_SCALE,
472  0 );
473  }
474  else
475  {
477  aModel.GetLayerZ( F_Cu ) + Millimeter2iu( ART_OFFSET / 2.0 ) * BOARD_SCALE,
478  true );
479  }
480 
481  // VRML_LAYER m_bot_copper;
482  aModel.m_bot_copper.Tesselate( &aModel.m_holes );
483 
484  if( USE_INLINES )
485  {
486  write_triangle_bag( *aOutputFile, aModel.GetColor( VRML_COLOR_TRACK ),
487  &aModel.m_bot_copper, true, false,
488  aModel.GetLayerZ( B_Cu ), 0 );
489  }
490  else
491  {
493  aModel.GetLayerZ( B_Cu ), false );
494  }
495 
496  // VRML_LAYER m_bot_tin;
497  aModel.m_bot_tin.Tesselate( &aModel.m_holes );
498 
499  if( USE_INLINES )
500  {
501  write_triangle_bag( *aOutputFile, aModel.GetColor( VRML_COLOR_TIN ),
502  &aModel.m_bot_tin, true, false,
503  aModel.GetLayerZ( B_Cu )
504  - Millimeter2iu( ART_OFFSET / 2.0 ) * BOARD_SCALE,
505  0 );
506  }
507  else
508  {
510  aModel.GetLayerZ( B_Cu ) - Millimeter2iu( ART_OFFSET / 2.0 ) * BOARD_SCALE,
511  false );
512  }
513 
514  // VRML_LAYER PTH;
515  aModel.m_plated_holes.Tesselate( NULL, true );
516 
517  if( USE_INLINES )
518  {
519  write_triangle_bag( *aOutputFile, aModel.GetColor( VRML_COLOR_TIN ),
520  &aModel.m_plated_holes, false, false,
521  aModel.GetLayerZ( F_Cu ) + Millimeter2iu( ART_OFFSET / 2.0 ) * BOARD_SCALE,
522  aModel.GetLayerZ( B_Cu ) - Millimeter2iu( ART_OFFSET / 2.0 ) * BOARD_SCALE );
523  }
524  else
525  {
527  aModel.GetLayerZ( F_Cu ) + Millimeter2iu( ART_OFFSET / 2.0 ) * BOARD_SCALE,
528  aModel.GetLayerZ( B_Cu ) - Millimeter2iu( ART_OFFSET / 2.0 ) * BOARD_SCALE );
529  }
530 
531  // VRML_LAYER m_top_silk;
532  aModel.m_top_silk.Tesselate( &aModel.m_holes );
533 
534  if( USE_INLINES )
535  {
536  write_triangle_bag( *aOutputFile, aModel.GetColor( VRML_COLOR_SILK ), &aModel.m_top_silk,
537  true, true, aModel.GetLayerZ( F_SilkS ), 0 );
538  }
539  else
540  {
542  aModel.GetLayerZ( F_SilkS ), true );
543  }
544 
545  // VRML_LAYER m_bot_silk;
546  aModel.m_bot_silk.Tesselate( &aModel.m_holes );
547 
548  if( USE_INLINES )
549  {
550  write_triangle_bag( *aOutputFile, aModel.GetColor( VRML_COLOR_SILK ), &aModel.m_bot_silk,
551  true, false, aModel.GetLayerZ( B_SilkS ), 0 );
552  }
553  else
554  {
556  aModel.GetLayerZ( B_SilkS ), false );
557  }
558 
559  if( !USE_INLINES )
560  S3D::WriteVRML( aFileName, true, aModel.m_OutputPCB.GetRawPtr(), true, true );
561 }
562 
563 
564 static void compute_layer_Zs( MODEL_VRML& aModel, BOARD* pcb )
565 {
566  int copper_layers = pcb->GetCopperLayerCount();
567 
568  // We call it 'layer' thickness, but it's the whole board thickness!
570  double half_thickness = aModel.m_brd_thickness / 2;
571 
572  // Compute each layer's Z value, more or less like the 3d view
573  for( LSEQ seq = LSET::AllCuMask().Seq(); seq; ++seq )
574  {
575  PCB_LAYER_ID i = *seq;
576 
577  if( i < copper_layers )
578  aModel.SetLayerZ( i, half_thickness - aModel.m_brd_thickness * i / (copper_layers - 1) );
579  else
580  aModel.SetLayerZ( i, - half_thickness ); // bottom layer
581  }
582 
583  /* To avoid rounding interference, we apply an epsilon to each
584  * successive layer */
585  double epsilon_z = Millimeter2iu( ART_OFFSET ) * BOARD_SCALE;
586  aModel.SetLayerZ( B_Paste, -half_thickness - epsilon_z * 4 );
587  aModel.SetLayerZ( B_Adhes, -half_thickness - epsilon_z * 3 );
588  aModel.SetLayerZ( B_SilkS, -half_thickness - epsilon_z * 2 );
589  aModel.SetLayerZ( B_Mask, -half_thickness - epsilon_z );
590  aModel.SetLayerZ( F_Mask, half_thickness + epsilon_z );
591  aModel.SetLayerZ( F_SilkS, half_thickness + epsilon_z * 2 );
592  aModel.SetLayerZ( F_Adhes, half_thickness + epsilon_z * 3 );
593  aModel.SetLayerZ( F_Paste, half_thickness + epsilon_z * 4 );
594  aModel.SetLayerZ( Dwgs_User, half_thickness + epsilon_z * 5 );
595  aModel.SetLayerZ( Cmts_User, half_thickness + epsilon_z * 6 );
596  aModel.SetLayerZ( Eco1_User, half_thickness + epsilon_z * 7 );
597  aModel.SetLayerZ( Eco2_User, half_thickness + epsilon_z * 8 );
598  aModel.SetLayerZ( Edge_Cuts, 0 );
599 }
600 
601 
602 static void export_vrml_line( MODEL_VRML& aModel, LAYER_NUM layer,
603  double startx, double starty,
604  double endx, double endy, double width )
605 {
606  VRML_LAYER* vlayer;
607 
608  if( !GetLayer( aModel, layer, &vlayer ) )
609  return;
610 
611  if( width < aModel.m_minLineWidth)
612  width = aModel.m_minLineWidth;
613 
614  starty = -starty;
615  endy = -endy;
616 
617  double angle = atan2( endy - starty, endx - startx ) * 180.0 / M_PI;
618  double length = Distance( startx, starty, endx, endy ) + width;
619  double cx = ( startx + endx ) / 2.0;
620  double cy = ( starty + endy ) / 2.0;
621 
622  if( !vlayer->AddSlot( cx, cy, length, width, angle, false ) )
623  throw( std::runtime_error( vlayer->GetError() ) );
624 }
625 
626 
627 static void export_vrml_circle( MODEL_VRML& aModel, LAYER_NUM layer,
628  double startx, double starty,
629  double endx, double endy, double width )
630 {
631  VRML_LAYER* vlayer;
632 
633  if( !GetLayer( aModel, layer, &vlayer ) )
634  return;
635 
636  if( width < aModel.m_minLineWidth )
637  width = aModel.m_minLineWidth;
638 
639  starty = -starty;
640  endy = -endy;
641 
642  double hole, radius;
643 
644  radius = Distance( startx, starty, endx, endy ) + ( width / 2);
645  hole = radius - width;
646 
647  if( !vlayer->AddCircle( startx, starty, radius, false ) )
648  throw( std::runtime_error( vlayer->GetError() ) );
649 
650  if( hole > 0.0001 )
651  {
652  if( !vlayer->AddCircle( startx, starty, hole, true ) )
653  throw( std::runtime_error( vlayer->GetError() ) );
654  }
655 }
656 
657 
658 static void export_vrml_arc( MODEL_VRML& aModel, LAYER_NUM layer,
659  double centerx, double centery,
660  double arc_startx, double arc_starty,
661  double width, double arc_angle )
662 {
663  VRML_LAYER* vlayer;
664 
665  if( !GetLayer( aModel, layer, &vlayer ) )
666  return;
667 
668  if( width < aModel.m_minLineWidth )
669  width = aModel.m_minLineWidth;
670 
671  centery = -centery;
672  arc_starty = -arc_starty;
673 
674  if( !vlayer->AddArc( centerx, centery, arc_startx, arc_starty, width, -arc_angle, false ) )
675  throw( std::runtime_error( vlayer->GetError() ) );
676 
677 }
678 
679 
680 static void export_vrml_polygon( MODEL_VRML& aModel, LAYER_NUM layer,
681  DRAWSEGMENT *aOutline, double aOrientation, wxPoint aPos )
682 {
683  if( aOutline->IsPolyShapeValid() )
684  {
685  SHAPE_POLY_SET shape = aOutline->GetPolyShape();
686  VRML_LAYER* vlayer;
687 
688  if( !GetLayer( aModel, layer, &vlayer ) )
689  return;
690 
691  if( aOutline->GetWidth() )
692  {
693  int numSegs = std::max(
694  GetArcToSegmentCount( aOutline->GetWidth() / 2, ARC_HIGH_DEF, 360.0 ), 6 );
695  shape.Inflate( aOutline->GetWidth() / 2, numSegs );
697  }
698 
699  shape.Rotate( -aOrientation, VECTOR2I( 0, 0 ) );
700  shape.Move( aPos );
701  const SHAPE_LINE_CHAIN& outline = shape.COutline( 0 );
702 
703  int seg = vlayer->NewContour();
704 
705  for( int j = 0; j < outline.PointCount(); j++ )
706  {
707  if( !vlayer->AddVertex( seg, outline.CPoint( j ).x * BOARD_SCALE,
708  -outline.CPoint( j ).y * BOARD_SCALE ) )
709  throw( std::runtime_error( vlayer->GetError() ) );
710  }
711 
712  vlayer->EnsureWinding( seg, false );
713  }
714 }
715 
716 
717 static void export_vrml_drawsegment( MODEL_VRML& aModel, DRAWSEGMENT* drawseg )
718 {
719  LAYER_NUM layer = drawseg->GetLayer();
720  double w = drawseg->GetWidth() * BOARD_SCALE;
721  double x = drawseg->GetStart().x * BOARD_SCALE;
722  double y = drawseg->GetStart().y * BOARD_SCALE;
723  double xf = drawseg->GetEnd().x * BOARD_SCALE;
724  double yf = drawseg->GetEnd().y * BOARD_SCALE;
725  double r = sqrt( pow( x - xf, 2 ) + pow( y - yf, 2 ) );
726 
727  // Items on the edge layer are handled elsewhere; just return
728  if( layer == Edge_Cuts )
729  return;
730 
731  switch( drawseg->GetShape() )
732  {
733  case S_ARC:
734  export_vrml_arc( aModel, layer,
735  (double) drawseg->GetCenter().x * BOARD_SCALE,
736  (double) drawseg->GetCenter().y * BOARD_SCALE,
737  (double) drawseg->GetArcStart().x * BOARD_SCALE,
738  (double) drawseg->GetArcStart().y * BOARD_SCALE,
739  w, drawseg->GetAngle() / 10 );
740  break;
741 
742  case S_CIRCLE:
743  // Break circles into two 180 arcs to prevent the vrml hole from obscuring objects
744  // within the hole area of the circle.
745  export_vrml_arc( aModel, layer, x, y, x, y-r, w, 180.0 );
746  export_vrml_arc( aModel, layer, x, y, x, y+r, w, 180.0 );
747  break;
748 
749  case S_POLYGON:
750  export_vrml_polygon( aModel, layer, drawseg, 0.0, wxPoint( 0, 0 ) );
751  break;
752 
753  default:
754  export_vrml_line( aModel, layer, x, y, xf, yf, w );
755  break;
756  }
757 }
758 
759 
760 /* C++ doesn't have closures and neither continuation forms... this is
761  * for coupling the vrml_text_callback with the common parameters */
762 static void vrml_text_callback( int x0, int y0, int xf, int yf, void* aData )
763 {
764  LAYER_NUM m_text_layer = model_vrml->m_text_layer;
765  int m_text_width = model_vrml->m_text_width;
766 
767  export_vrml_line( *model_vrml, m_text_layer,
768  x0 * BOARD_SCALE, y0 * BOARD_SCALE,
769  xf * BOARD_SCALE, yf * BOARD_SCALE,
770  m_text_width * BOARD_SCALE );
771 }
772 
773 
774 static void export_vrml_pcbtext( MODEL_VRML& aModel, TEXTE_PCB* text )
775 {
776  model_vrml->m_text_layer = text->GetLayer();
778 
779  wxSize size = text->GetTextSize();
780 
781  if( text->IsMirrored() )
782  size.x = -size.x;
783 
784  COLOR4D color = COLOR4D::BLACK; // not actually used, but needed by GRText
785 
786  if( text->IsMultilineAllowed() )
787  {
788  wxArrayString strings_list;
789  wxStringSplit( text->GetShownText(), strings_list, '\n' );
790  std::vector<wxPoint> positions;
791  positions.reserve( strings_list.Count() );
792  text->GetPositionsOfLinesOfMultilineText( positions, strings_list.Count() );
793 
794  for( unsigned ii = 0; ii < strings_list.Count(); ii++ )
795  {
796  wxString& txt = strings_list.Item( ii );
797  GRText( NULL, positions[ii], color, txt, text->GetTextAngle(), size,
798  text->GetHorizJustify(), text->GetVertJustify(), text->GetThickness(),
799  text->IsItalic(), true, vrml_text_callback );
800  }
801  }
802  else
803  {
804  GRText( NULL, text->GetTextPos(), color, text->GetShownText(), text->GetTextAngle(),
805  size, text->GetHorizJustify(), text->GetVertJustify(), text->GetThickness(),
806  text->IsItalic(), true, vrml_text_callback );
807  }
808 }
809 
810 
811 static void export_vrml_drawings( MODEL_VRML& aModel, BOARD* pcb )
812 {
813  // draw graphic items
814  for( auto drawing : pcb->Drawings() )
815  {
816  PCB_LAYER_ID layer = drawing->GetLayer();
817 
818  if( layer != F_Cu && layer != B_Cu && layer != B_SilkS && layer != F_SilkS )
819  continue;
820 
821  switch( drawing->Type() )
822  {
823  case PCB_LINE_T:
824  export_vrml_drawsegment( aModel, (DRAWSEGMENT*) drawing );
825  break;
826 
827  case PCB_TEXT_T:
828  export_vrml_pcbtext( aModel, (TEXTE_PCB*) drawing );
829  break;
830 
831  default:
832  break;
833  }
834  }
835 }
836 
837 
838 // board edges and cutouts
839 static void export_vrml_board( MODEL_VRML& aModel, BOARD* aPcb )
840 {
841  SHAPE_POLY_SET pcbOutlines; // stores the board main outlines
842  wxString msg;
843 
844  if( !aPcb->GetBoardPolygonOutlines( pcbOutlines, &msg ) )
845  {
846  msg << "\n\n" <<
847  _( "Unable to calculate the board outlines; fall back to using the board boundary box." );
848  wxMessageBox( msg );
849  }
850 
851  int seg;
852 
853  for( int cnt = 0; cnt < pcbOutlines.OutlineCount(); cnt++ )
854  {
855  const SHAPE_LINE_CHAIN& outline = pcbOutlines.COutline( cnt );
856 
857  seg = aModel.m_board.NewContour();
858 
859  for( int j = 0; j < outline.PointCount(); j++ )
860  {
861  aModel.m_board.AddVertex( seg, (double)outline.CPoint(j).x * BOARD_SCALE,
862  -((double)outline.CPoint(j).y * BOARD_SCALE ) );
863 
864  }
865 
866  aModel.m_board.EnsureWinding( seg, false );
867 
868  // Generate holes:
869  for( int ii = 0; ii < pcbOutlines.HoleCount( cnt ); ii++ )
870  {
871  const SHAPE_LINE_CHAIN& hole = pcbOutlines.Hole( cnt, ii );
872 
873  seg = aModel.m_holes.NewContour();
874 
875  if( seg < 0 )
876  {
877  msg << "\n\n" <<
878  _( "VRML Export Failed: Could not add holes to contours." );
879  wxMessageBox( msg );
880 
881  return;
882  }
883 
884  for( int j = 0; j < hole.PointCount(); j++ )
885  {
886  aModel.m_holes.AddVertex( seg, (double)hole.CPoint(j).x * BOARD_SCALE,
887  -((double)hole.CPoint(j).y * BOARD_SCALE ) );
888 
889  }
890 
891  aModel.m_holes.EnsureWinding( seg, true );
892  }
893  }
894 }
895 
896 
897 static void export_round_padstack( MODEL_VRML& aModel, BOARD* pcb,
898  double x, double y, double r,
899  LAYER_NUM bottom_layer, LAYER_NUM top_layer,
900  double hole )
901 {
902  LAYER_NUM layer = top_layer;
903  bool thru = true;
904 
905  // if not a thru hole do not put a hole in the board
906  if( top_layer != F_Cu || bottom_layer != B_Cu )
907  thru = false;
908 
909  if( thru && hole > 0 )
910  aModel.m_holes.AddCircle( x, -y, hole, true );
911 
912  if( aModel.m_plainPCB )
913  return;
914 
915  while( 1 )
916  {
917  if( layer == B_Cu )
918  {
919  aModel.m_bot_copper.AddCircle( x, -y, r );
920 
921  if( hole > 0 && !thru )
922  aModel.m_bot_copper.AddCircle( x, -y, hole, true );
923 
924  }
925  else if( layer == F_Cu )
926  {
927  aModel.m_top_copper.AddCircle( x, -y, r );
928 
929  if( hole > 0 && !thru )
930  aModel.m_top_copper.AddCircle( x, -y, hole, true );
931 
932  }
933 
934  if( layer == bottom_layer )
935  break;
936 
937  layer = bottom_layer;
938  }
939 }
940 
941 
942 static void export_vrml_via( MODEL_VRML& aModel, BOARD* aPcb, const VIA* aVia )
943 {
944  double x, y, r, hole;
945  PCB_LAYER_ID top_layer, bottom_layer;
946 
947  hole = aVia->GetDrillValue() * BOARD_SCALE / 2.0;
948  r = aVia->GetWidth() * BOARD_SCALE / 2.0;
949  x = aVia->GetStart().x * BOARD_SCALE;
950  y = aVia->GetStart().y * BOARD_SCALE;
951  aVia->LayerPair( &top_layer, &bottom_layer );
952 
953  // do not render a buried via
954  if( top_layer != F_Cu && bottom_layer != B_Cu )
955  return;
956 
957  // Export the via padstack
958  export_round_padstack( aModel, aPcb, x, y, r, bottom_layer, top_layer, hole );
959 }
960 
961 
962 static void export_vrml_tracks( MODEL_VRML& aModel, BOARD* pcb )
963 {
964  for( auto track : pcb->Tracks() )
965  {
966  if( track->Type() == PCB_VIA_T )
967  {
968  export_vrml_via( aModel, pcb, (const VIA*) track );
969  }
970  else if( ( track->GetLayer() == B_Cu || track->GetLayer() == F_Cu )
971  && !aModel.m_plainPCB )
972  export_vrml_line( aModel, track->GetLayer(),
973  track->GetStart().x * BOARD_SCALE,
974  track->GetStart().y * BOARD_SCALE,
975  track->GetEnd().x * BOARD_SCALE,
976  track->GetEnd().y * BOARD_SCALE,
977  track->GetWidth() * BOARD_SCALE );
978  }
979 }
980 
981 
982 static void export_vrml_zones( MODEL_VRML& aModel, BOARD* aPcb )
983 {
984 
985  for( int ii = 0; ii < aPcb->GetAreaCount(); ii++ )
986  {
987  ZONE_CONTAINER* zone = aPcb->GetArea( ii );
988 
989  VRML_LAYER* vl;
990 
991  if( !GetLayer( aModel, zone->GetLayer(), &vl ) )
992  continue;
993 
994  // fixme: this modifies the board where it shouldn't, but I don't have the time
995  // to clean this up - TW
996  if( !zone->IsFilled() )
997  {
998  ZONE_FILLER filler( aPcb );
999  zone->SetFillMode( ZONE_FILL_MODE::POLYGONS ); // use filled polygons
1000  filler.Fill( { zone } );
1001  }
1002 
1003  const SHAPE_POLY_SET& poly = zone->GetFilledPolysList();
1004 
1005  for( int i = 0; i < poly.OutlineCount(); i++ )
1006  {
1007  const SHAPE_LINE_CHAIN& outline = poly.COutline( i );
1008 
1009  int seg = vl->NewContour();
1010 
1011  for( int j = 0; j < outline.PointCount(); j++ )
1012  {
1013  if( !vl->AddVertex( seg, (double)outline.CPoint( j ).x * BOARD_SCALE,
1014  -((double)outline.CPoint( j ).y * BOARD_SCALE ) ) )
1015  throw( std::runtime_error( vl->GetError() ) );
1016 
1017  }
1018 
1019  vl->EnsureWinding( seg, false );
1020  }
1021  }
1022 }
1023 
1024 
1026 {
1027  if( item->IsVisible() )
1028  {
1029  wxSize size = item->GetTextSize();
1030 
1031  if( item->IsMirrored() )
1032  size.x = -size.x; // Text is mirrored
1033 
1034  model_vrml->m_text_layer = item->GetLayer();
1036 
1037  GRText( NULL, item->GetTextPos(), BLACK, item->GetShownText(), item->GetDrawRotation(),
1038  size, item->GetHorizJustify(), item->GetVertJustify(), item->GetThickness(),
1039  item->IsItalic(), true, vrml_text_callback );
1040  }
1041 }
1042 
1043 
1044 static void export_vrml_edge_module( MODEL_VRML& aModel, EDGE_MODULE* aOutline,
1045  MODULE* aModule )
1046 {
1047  LAYER_NUM layer = aOutline->GetLayer();
1048  double x = aOutline->GetStart().x * BOARD_SCALE;
1049  double y = aOutline->GetStart().y * BOARD_SCALE;
1050  double xf = aOutline->GetEnd().x * BOARD_SCALE;
1051  double yf = aOutline->GetEnd().y * BOARD_SCALE;
1052  double w = aOutline->GetWidth() * BOARD_SCALE;
1053 
1054  switch( aOutline->GetShape() )
1055  {
1056  case S_SEGMENT:
1057  export_vrml_line( aModel, layer, x, y, xf, yf, w );
1058  break;
1059 
1060  case S_ARC:
1061  export_vrml_arc( aModel, layer, x, y, xf, yf, w, aOutline->GetAngle() / 10 );
1062  break;
1063 
1064  case S_CIRCLE:
1065  export_vrml_circle( aModel, layer, x, y, xf, yf, w );
1066  break;
1067 
1068  case S_POLYGON:
1069  export_vrml_polygon( aModel, layer, aOutline, aModule->GetOrientationRadians(),
1070  aModule->GetPosition() );
1071  break;
1072 
1073  default:
1074  break;
1075  }
1076 }
1077 
1078 
1079 static void export_vrml_padshape( MODEL_VRML& aModel, VRML_LAYER* aTinLayer, D_PAD* aPad )
1080 {
1081  // The (maybe offset) pad position
1082  wxPoint pad_pos = aPad->ShapePos();
1083  double pad_x = pad_pos.x * BOARD_SCALE;
1084  double pad_y = pad_pos.y * BOARD_SCALE;
1085  wxSize pad_delta = aPad->GetDelta();
1086 
1087  double pad_dx = pad_delta.x * BOARD_SCALE / 2.0;
1088  double pad_dy = pad_delta.y * BOARD_SCALE / 2.0;
1089 
1090  double pad_w = aPad->GetSize().x * BOARD_SCALE / 2.0;
1091  double pad_h = aPad->GetSize().y * BOARD_SCALE / 2.0;
1092 
1093  switch( aPad->GetShape() )
1094  {
1095  case PAD_SHAPE_CIRCLE:
1096 
1097  if( !aTinLayer->AddCircle( pad_x, -pad_y, pad_w, false ) )
1098  throw( std::runtime_error( aTinLayer->GetError() ) );
1099 
1100  break;
1101 
1102  case PAD_SHAPE_OVAL:
1103 
1104  if( !aTinLayer->AddSlot( pad_x, -pad_y, pad_w * 2.0, pad_h * 2.0,
1105  aPad->GetOrientation()/10.0, false ) )
1106  throw( std::runtime_error( aTinLayer->GetError() ) );
1107 
1108  break;
1109 
1110  case PAD_SHAPE_ROUNDRECT:
1112  {
1113  SHAPE_POLY_SET polySet;
1114  const int corner_radius = aPad->GetRoundRectCornerRadius( aPad->GetSize() );
1115  TransformRoundChamferedRectToPolygon( polySet, wxPoint( 0, 0 ), aPad->GetSize(),
1116  0.0, corner_radius, 0.0, 0, ARC_HIGH_DEF );
1117  std::vector< wxRealPoint > cornerList;
1118  // TransformRoundChamferedRectToPolygon creates only one convex polygon
1119  SHAPE_LINE_CHAIN poly( polySet.Outline( 0 ) );
1120 
1121  cornerList.reserve( poly.PointCount() );
1122  for( int ii = 0; ii < poly.PointCount(); ++ii )
1123  cornerList.emplace_back(
1124  poly.CPoint( ii ).x * BOARD_SCALE, -poly.CPoint( ii ).y * BOARD_SCALE );
1125 
1126  // Close polygon
1127  cornerList.push_back( cornerList[0] );
1128  if( !aTinLayer->AddPolygon( cornerList, pad_x, -pad_y, aPad->GetOrientation() ) )
1129  throw( std::runtime_error( aTinLayer->GetError() ) );
1130 
1131  break;
1132  }
1133 
1134  case PAD_SHAPE_CUSTOM:
1135  {
1136  SHAPE_POLY_SET polySet;
1137  std::vector< wxRealPoint > cornerList;
1138  aPad->MergePrimitivesAsPolygon( &polySet );
1139 
1140  for( int cnt = 0; cnt < polySet.OutlineCount(); ++cnt )
1141  {
1142  SHAPE_LINE_CHAIN& poly = polySet.Outline( cnt );
1143  cornerList.clear();
1144 
1145  for( int ii = 0; ii < poly.PointCount(); ++ii )
1146  cornerList.emplace_back(
1147  poly.CPoint( ii ).x * BOARD_SCALE, -poly.CPoint( ii ).y * BOARD_SCALE );
1148 
1149  // Close polygon
1150  cornerList.push_back( cornerList[0] );
1151 
1152  if( !aTinLayer->AddPolygon( cornerList, pad_x, -pad_y, aPad->GetOrientation() ) )
1153  throw( std::runtime_error( aTinLayer->GetError() ) );
1154  }
1155 
1156  break;
1157  }
1158 
1159  case PAD_SHAPE_RECT:
1160  // Just to be sure :D
1161  pad_dx = 0;
1162  pad_dy = 0;
1163 
1164  // Intentionally fall through and treat a rectangle as a trapezoid with no sloped sides
1165 
1166  case PAD_SHAPE_TRAPEZOID:
1167  {
1168  double coord[8] =
1169  {
1170  -pad_w + pad_dy, -pad_h - pad_dx,
1171  -pad_w - pad_dy, pad_h + pad_dx,
1172  +pad_w - pad_dy, -pad_h + pad_dx,
1173  +pad_w + pad_dy, pad_h - pad_dx
1174  };
1175 
1176  for( int i = 0; i < 4; i++ )
1177  {
1178  RotatePoint( &coord[i * 2], &coord[i * 2 + 1], aPad->GetOrientation() );
1179  coord[i * 2] += pad_x;
1180  coord[i * 2 + 1] += pad_y;
1181  }
1182 
1183  int lines;
1184 
1185  lines = aTinLayer->NewContour();
1186 
1187  if( lines < 0 )
1188  throw( std::runtime_error( aTinLayer->GetError() ) );
1189 
1190  if( !aTinLayer->AddVertex( lines, coord[0], -coord[1] ) )
1191  throw( std::runtime_error( aTinLayer->GetError() ) );
1192 
1193  if( !aTinLayer->AddVertex( lines, coord[4], -coord[5] ) )
1194  throw( std::runtime_error( aTinLayer->GetError() ) );
1195 
1196  if( !aTinLayer->AddVertex( lines, coord[6], -coord[7] ) )
1197  throw( std::runtime_error( aTinLayer->GetError() ) );
1198 
1199  if( !aTinLayer->AddVertex( lines, coord[2], -coord[3] ) )
1200  throw( std::runtime_error( aTinLayer->GetError() ) );
1201 
1202  if( !aTinLayer->EnsureWinding( lines, false ) )
1203  throw( std::runtime_error( aTinLayer->GetError() ) );
1204 
1205  break;
1206  }
1207  }
1208 }
1209 
1210 
1211 static void export_vrml_pad( MODEL_VRML& aModel, BOARD* aPcb, D_PAD* aPad )
1212 {
1213  double hole_drill_w = (double) aPad->GetDrillSize().x * BOARD_SCALE / 2.0;
1214  double hole_drill_h = (double) aPad->GetDrillSize().y * BOARD_SCALE / 2.0;
1215  double hole_drill = std::min( hole_drill_w, hole_drill_h );
1216  double hole_x = aPad->GetPosition().x * BOARD_SCALE;
1217  double hole_y = aPad->GetPosition().y * BOARD_SCALE;
1218 
1219  // Export the hole on the edge layer
1220  if( hole_drill > 0 )
1221  {
1222  bool pth = false;
1223 
1224  if( ( aPad->GetAttribute() != PAD_ATTRIB_HOLE_NOT_PLATED )
1225  && !aModel.m_plainPCB )
1226  pth = true;
1227 
1228  if( aPad->GetDrillShape() == PAD_DRILL_SHAPE_OBLONG )
1229  {
1230  // Oblong hole (slot)
1231 
1232  if( pth )
1233  {
1234  aModel.m_holes.AddSlot( hole_x, -hole_y, hole_drill_w * 2.0 + PLATE_OFFSET,
1235  hole_drill_h * 2.0 + PLATE_OFFSET,
1236  aPad->GetOrientation()/10.0, true, true );
1237 
1238  aModel.m_plated_holes.AddSlot( hole_x, -hole_y,
1239  hole_drill_w * 2.0, hole_drill_h * 2.0,
1240  aPad->GetOrientation()/10.0, true, false );
1241  }
1242  else
1243  {
1244  aModel.m_holes.AddSlot( hole_x, -hole_y, hole_drill_w * 2.0, hole_drill_h * 2.0,
1245  aPad->GetOrientation()/10.0, true, false );
1246 
1247  }
1248  }
1249  else
1250  {
1251  // Drill a round hole
1252 
1253  if( pth )
1254  {
1255  aModel.m_holes.AddCircle( hole_x, -hole_y, hole_drill + PLATE_OFFSET, true, true );
1256  aModel.m_plated_holes.AddCircle( hole_x, -hole_y, hole_drill, true, false );
1257  }
1258  else
1259  {
1260  aModel.m_holes.AddCircle( hole_x, -hole_y, hole_drill, true, false );
1261  }
1262 
1263  }
1264  }
1265 
1266  if( aModel.m_plainPCB )
1267  return;
1268 
1269  // The pad proper, on the selected layers
1270  LSET layer_mask = aPad->GetLayerSet();
1271 
1272  if( layer_mask[B_Cu] )
1273  {
1274  if( layer_mask[B_Mask] )
1275  export_vrml_padshape( aModel, &aModel.m_bot_tin, aPad );
1276  else
1277  export_vrml_padshape( aModel, &aModel.m_bot_copper, aPad );
1278  }
1279  if( layer_mask[F_Cu] )
1280  {
1281  if( layer_mask[F_Mask] )
1282  export_vrml_padshape( aModel, &aModel.m_top_tin, aPad );
1283  else
1284  export_vrml_padshape( aModel, &aModel.m_top_copper, aPad );
1285  }
1286 
1287 }
1288 
1289 
1290 // From axis/rot to quaternion
1291 static void build_quat( double x, double y, double z, double a, double q[4] )
1292 {
1293  double sina = sin( a / 2 );
1294 
1295  q[0] = x * sina;
1296  q[1] = y * sina;
1297  q[2] = z * sina;
1298  q[3] = cos( a / 2 );
1299 }
1300 
1301 
1302 // From quaternion to axis/rot
1303 static void from_quat( double q[4], double rot[4] )
1304 {
1305  rot[3] = acos( q[3] ) * 2;
1306 
1307  for( int i = 0; i < 3; i++ )
1308  {
1309  rot[i] = q[i] / sin( rot[3] / 2 );
1310  }
1311 }
1312 
1313 
1314 // Quaternion composition
1315 static void compose_quat( double q1[4], double q2[4], double qr[4] )
1316 {
1317  double tmp[4];
1318 
1319  tmp[0] = q2[3] * q1[0] + q2[0] * q1[3] + q2[1] * q1[2] - q2[2] * q1[1];
1320  tmp[1] = q2[3] * q1[1] + q2[1] * q1[3] + q2[2] * q1[0] - q2[0] * q1[2];
1321  tmp[2] = q2[3] * q1[2] + q2[2] * q1[3] + q2[0] * q1[1] - q2[1] * q1[0];
1322  tmp[3] = q2[3] * q1[3] - q2[0] * q1[0] - q2[1] * q1[1] - q2[2] * q1[2];
1323 
1324  qr[0] = tmp[0];
1325  qr[1] = tmp[1];
1326  qr[2] = tmp[2];
1327  qr[3] = tmp[3];
1328 }
1329 
1330 
1331 static void export_vrml_module( MODEL_VRML& aModel, BOARD* aPcb,
1332  MODULE* aModule, std::ostream* aOutputFile )
1333 {
1334  if( !aModel.m_plainPCB )
1335  {
1336  // Reference and value
1337  if( aModule->Reference().IsVisible() )
1338  export_vrml_text_module( &aModule->Reference() );
1339 
1340  if( aModule->Value().IsVisible() )
1341  export_vrml_text_module( &aModule->Value() );
1342 
1343  // Export module edges
1344 
1345  for( auto item : aModule->GraphicalItems() )
1346  {
1347  switch( item->Type() )
1348  {
1349  case PCB_MODULE_TEXT_T:
1350  export_vrml_text_module( static_cast<TEXTE_MODULE*>( item ) );
1351  break;
1352 
1353  case PCB_MODULE_EDGE_T:
1354  export_vrml_edge_module( aModel, static_cast<EDGE_MODULE*>( item ),
1355  aModule );
1356  break;
1357 
1358  default:
1359  break;
1360  }
1361  }
1362  }
1363 
1364  // Export pads
1365  for( auto pad : aModule->Pads() )
1366  export_vrml_pad( aModel, aPcb, pad );
1367 
1368  bool isFlipped = aModule->GetLayer() == B_Cu;
1369 
1370  // Export the object VRML model(s)
1371  auto sM = aModule->Models().begin();
1372  auto eM = aModule->Models().end();
1373 
1374  wxFileName subdir( SUBDIR_3D, "" );
1375 
1376  while( sM != eM )
1377  {
1378  SGNODE* mod3d = (SGNODE*) cache->Load( sM->m_Filename );
1379 
1380  if( NULL == mod3d )
1381  {
1382  ++sM;
1383  continue;
1384  }
1385 
1386  /* Calculate 3D shape rotation:
1387  * this is the rotation parameters, with an additional 180 deg rotation
1388  * for footprints that are flipped
1389  * When flipped, axis rotation is the horizontal axis (X axis)
1390  */
1391  double rotx = -sM->m_Rotation.x;
1392  double roty = -sM->m_Rotation.y;
1393  double rotz = -sM->m_Rotation.z;
1394 
1395  if( isFlipped )
1396  {
1397  rotx += 180.0;
1398  roty = -roty;
1399  rotz = -rotz;
1400  }
1401 
1402  // Do some quaternion munching
1403  double q1[4], q2[4], rot[4];
1404  build_quat( 1, 0, 0, DEG2RAD( rotx ), q1 );
1405  build_quat( 0, 1, 0, DEG2RAD( roty ), q2 );
1406  compose_quat( q1, q2, q1 );
1407  build_quat( 0, 0, 1, DEG2RAD( rotz ), q2 );
1408  compose_quat( q1, q2, q1 );
1409 
1410  // Note here aModule->GetOrientation() is in 0.1 degrees,
1411  // so module rotation has to be converted to radians
1412  build_quat( 0, 0, 1, DECIDEG2RAD( aModule->GetOrientation() ), q2 );
1413  compose_quat( q1, q2, q1 );
1414  from_quat( q1, rot );
1415 
1416  double offsetFactor = 1000.0f * IU_PER_MILS / 25.4f;
1417 
1418  // adjust 3D shape local offset position
1419  // they are given in mm, so they are converted in board IU.
1420  double offsetx = sM->m_Offset.x * offsetFactor;
1421  double offsety = sM->m_Offset.y * offsetFactor;
1422  double offsetz = sM->m_Offset.z * offsetFactor;
1423 
1424  if( isFlipped )
1425  offsetz = -offsetz;
1426  else // In normal mode, Y axis is reversed in Pcbnew.
1427  offsety = -offsety;
1428 
1429  RotatePoint( &offsetx, &offsety, aModule->GetOrientation() );
1430 
1431  SGPOINT trans;
1432  trans.x = ( offsetx + aModule->GetPosition().x ) * BOARD_SCALE + aModel.m_tx;
1433  trans.y = -(offsety + aModule->GetPosition().y) * BOARD_SCALE - aModel.m_ty;
1434  trans.z = (offsetz * BOARD_SCALE ) + aModel.GetLayerZ( aModule->GetLayer() );
1435 
1436  if( USE_INLINES )
1437  {
1438  wxFileName srcFile = cache->GetResolver()->ResolvePath( sM->m_Filename );
1439  wxFileName dstFile;
1440  dstFile.SetPath( SUBDIR_3D );
1441  dstFile.SetName( srcFile.GetName() );
1442  dstFile.SetExt( "wrl" );
1443 
1444  // copy the file if necessary
1445  wxDateTime srcModTime = srcFile.GetModificationTime();
1446  wxDateTime destModTime = srcModTime;
1447 
1448  destModTime.SetToCurrent();
1449 
1450  if( dstFile.FileExists() )
1451  destModTime = dstFile.GetModificationTime();
1452 
1453  if( srcModTime != destModTime )
1454  {
1455  wxLogDebug( "Copying 3D model %s to %s.",
1456  GetChars( srcFile.GetFullPath() ),
1457  GetChars( dstFile.GetFullPath() ) );
1458 
1459  wxString fileExt = srcFile.GetExt();
1460  fileExt.LowerCase();
1461 
1462  // copy VRML models and use the scenegraph library to
1463  // translate other model types
1464  if( fileExt == "wrl" )
1465  {
1466  if( !wxCopyFile( srcFile.GetFullPath(), dstFile.GetFullPath() ) )
1467  continue;
1468  }
1469  else
1470  {
1471  if( !S3D::WriteVRML( dstFile.GetFullPath().ToUTF8(), true, mod3d, USE_DEFS, true ) )
1472  continue;
1473  }
1474  }
1475 
1476  (*aOutputFile) << "Transform {\n";
1477 
1478  // only write a rotation if it is >= 0.1 deg
1479  if( std::abs( rot[3] ) > 0.0001745 )
1480  {
1481  (*aOutputFile) << " rotation " << std::setprecision( 5 );
1482  (*aOutputFile) << rot[0] << " " << rot[1] << " " << rot[2] << " " << rot[3] << "\n";
1483  }
1484 
1485  (*aOutputFile) << " translation " << std::setprecision( PRECISION );
1486  (*aOutputFile) << trans.x << " ";
1487  (*aOutputFile) << trans.y << " ";
1488  (*aOutputFile) << trans.z << "\n";
1489 
1490  (*aOutputFile) << " scale ";
1491  (*aOutputFile) << sM->m_Scale.x << " ";
1492  (*aOutputFile) << sM->m_Scale.y << " ";
1493  (*aOutputFile) << sM->m_Scale.z << "\n";
1494 
1495  (*aOutputFile) << " children [\n Inline {\n url \"";
1496 
1497  if( USE_RELPATH )
1498  {
1499  wxFileName tmp = dstFile;
1500  tmp.SetExt( "" );
1501  tmp.SetName( "" );
1502  tmp.RemoveLastDir();
1503  dstFile.MakeRelativeTo( tmp.GetPath() );
1504  }
1505 
1506  wxString fn = dstFile.GetFullPath();
1507  fn.Replace( "\\", "/" );
1508  (*aOutputFile) << TO_UTF8( fn ) << "\"\n } ]\n";
1509  (*aOutputFile) << " }\n";
1510  }
1511  else
1512  {
1513  IFSG_TRANSFORM* modelShape = new IFSG_TRANSFORM( aModel.m_OutputPCB.GetRawPtr() );
1514 
1515  // only write a rotation if it is >= 0.1 deg
1516  if( std::abs( rot[3] ) > 0.0001745 )
1517  modelShape->SetRotation( SGVECTOR( rot[0], rot[1], rot[2] ), rot[3] );
1518 
1519  modelShape->SetTranslation( trans );
1520  modelShape->SetScale( SGPOINT( sM->m_Scale.x, sM->m_Scale.y, sM->m_Scale.z ) );
1521 
1522  if( NULL == S3D::GetSGNodeParent( mod3d ) )
1523  {
1524  aModel.m_components.push_back( mod3d );
1525  modelShape->AddChildNode( mod3d );
1526  }
1527  else
1528  {
1529  modelShape->AddRefNode( mod3d );
1530  }
1531 
1532  }
1533 
1534  ++sM;
1535  }
1536 }
1537 
1538 
1539 bool PCB_EDIT_FRAME::ExportVRML_File( const wxString& aFullFileName, double aMMtoWRMLunit,
1540  bool aExport3DFiles, bool aUseRelativePaths,
1541  bool aUsePlainPCB, const wxString& a3D_Subdir,
1542  double aXRef, double aYRef )
1543 {
1544  BOARD* pcb = GetBoard();
1545  bool ok = true;
1546 
1547  USE_INLINES = aExport3DFiles;
1548  USE_DEFS = true;
1549  USE_RELPATH = aUseRelativePaths;
1550 
1551  cache = Prj().Get3DCacheManager();
1552  PROJ_DIR = Prj().GetProjectPath();
1553  SUBDIR_3D = a3D_Subdir;
1554  MODEL_VRML model3d;
1555  model_vrml = &model3d;
1556  model3d.SetScale( aMMtoWRMLunit );
1557 
1558  if( USE_INLINES )
1559  {
1560  BOARD_SCALE = MM_PER_IU / 2.54;
1561  model3d.SetOffset( -aXRef / 2.54, aYRef / 2.54 );
1562  }
1563  else
1564  {
1565  BOARD_SCALE = MM_PER_IU;
1566  model3d.SetOffset( -aXRef, aYRef );
1567  }
1568 
1569  // plain PCB or else PCB with copper and silkscreen
1570  model3d.m_plainPCB = aUsePlainPCB;
1571 
1572  try
1573  {
1574 
1575  // Preliminary computation: the z value for each layer
1576  compute_layer_Zs(model3d, pcb);
1577 
1578  // board edges and cutouts
1579  export_vrml_board(model3d, pcb);
1580 
1581  // Drawing and text on the board
1582  if( !aUsePlainPCB )
1583  export_vrml_drawings( model3d, pcb );
1584 
1585  // Export vias and trackage
1586  export_vrml_tracks( model3d, pcb );
1587 
1588  // Export zone fills
1589  if( !aUsePlainPCB )
1590  export_vrml_zones( model3d, pcb);
1591 
1592  if( USE_INLINES )
1593  {
1594  // check if the 3D Subdir exists - create if not
1595  wxFileName subdir( SUBDIR_3D, "" );
1596 
1597  if( ! subdir.DirExists() )
1598  {
1599  if( !wxDir::Make( subdir.GetFullPath() ) )
1600  throw( std::runtime_error( "Could not create 3D model subdirectory" ) );
1601  }
1602 
1603  OPEN_OSTREAM( output_file, TO_UTF8( aFullFileName ) );
1604 
1605  if( output_file.fail() )
1606  {
1607  std::ostringstream ostr;
1608  ostr << "Could not open file '" << TO_UTF8( aFullFileName ) << "'";
1609  throw( std::runtime_error( ostr.str().c_str() ) );
1610  }
1611 
1612  output_file.imbue( std::locale( "C" ) );
1613 
1614  // Begin with the usual VRML boilerplate
1615  wxString fn = aFullFileName;
1616  fn.Replace( "\\" , "/" );
1617  output_file << "#VRML V2.0 utf8\n";
1618  output_file << "WorldInfo {\n";
1619  output_file << " title \"" << TO_UTF8( fn ) << " - Generated by Pcbnew\"\n";
1620  output_file << "}\n";
1621  output_file << "Transform {\n";
1622  output_file << " scale " << std::setprecision( PRECISION );
1623  output_file << WORLD_SCALE << " ";
1624  output_file << WORLD_SCALE << " ";
1625  output_file << WORLD_SCALE << "\n";
1626  output_file << " children [\n";
1627 
1628  // Export footprints
1629  for( auto module : pcb->Modules() )
1630  export_vrml_module( model3d, pcb, module, &output_file );
1631 
1632  // write out the board and all layers
1633  write_layers( model3d, pcb, TO_UTF8( aFullFileName ), &output_file );
1634 
1635  // Close the outer 'transform' node
1636  output_file << "]\n}\n";
1637 
1638  CLOSE_STREAM( output_file );
1639  }
1640  else
1641  {
1642  // Export footprints
1643  for( auto module : pcb->Modules() )
1644  export_vrml_module( model3d, pcb, module, NULL );
1645 
1646  // write out the board and all layers
1647  write_layers( model3d, pcb, TO_UTF8( aFullFileName ), NULL );
1648  }
1649  }
1650  catch( const std::exception& e )
1651  {
1652  wxString msg;
1653  msg << _( "IDF Export Failed:\n" ) << FROM_UTF8( e.what() );
1654  wxMessageBox( msg );
1655 
1656  ok = false;
1657  }
1658 
1659  return ok;
1660 }
1661 
1662 
1664 {
1665  if( colorIdx == -1 )
1666  colorIdx = VRML_COLOR_PCB;
1667  else if( colorIdx == VRML_COLOR_LAST )
1668  return NULL;
1669 
1670  if( sgmaterial[colorIdx] )
1671  return sgmaterial[colorIdx];
1672 
1673  IFSG_APPEARANCE vcolor( (SGNODE*) NULL );
1674  VRML_COLOR* cp = &colors[colorIdx];
1675 
1676  vcolor.SetSpecular( cp->spec_red, cp->spec_grn, cp->spec_blu );
1677  vcolor.SetDiffuse( cp->diffuse_red, cp->diffuse_grn, cp->diffuse_blu );
1678  vcolor.SetShininess( cp->shiny );
1679  // NOTE: XXX - replace with a better equation; using this definition
1680  // of ambient will not yield the best results
1681  vcolor.SetAmbient( cp->ambient, cp->ambient, cp->ambient );
1682  vcolor.SetTransparency( cp->transp );
1683 
1684  sgmaterial[colorIdx] = vcolor.GetRawPtr();
1685 
1686  return sgmaterial[colorIdx];
1687 }
1688 
1689 
1690 static void create_vrml_plane( IFSG_TRANSFORM& PcbOutput, VRML_COLOR_INDEX colorID,
1691  VRML_LAYER* layer, double top_z, bool aTopPlane )
1692 {
1693  std::vector< double > vertices;
1694  std::vector< int > idxPlane;
1695 
1696  if( !( *layer ).Get2DTriangles( vertices, idxPlane, top_z, aTopPlane ) )
1697  {
1698 #ifdef DEBUG
1699  do {
1700  std::ostringstream ostr;
1701  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
1702  ostr << " * [INFO] no vertex data";
1703  wxLogDebug( "%s\n", ostr.str().c_str() );
1704  } while( 0 );
1705 #endif
1706 
1707  return;
1708  }
1709 
1710  if( ( idxPlane.size() % 3 ) )
1711  {
1712 #ifdef DEBUG
1713  do {
1714  std::ostringstream ostr;
1715  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
1716  ostr << " * [BUG] index lists are not a multiple of 3 (not a triangle list)";
1717  wxLogDebug( "%s\n", ostr.str().c_str() );
1718  } while( 0 );
1719 #endif
1720 
1721  throw( std::runtime_error( "[BUG] index lists are not a multiple of 3 (not a triangle list)" ) );
1722  }
1723 
1724  std::vector< SGPOINT > vlist;
1725  size_t nvert = vertices.size() / 3;
1726  size_t j = 0;
1727 
1728  for( size_t i = 0; i < nvert; ++i, j+= 3 )
1729  vlist.emplace_back( vertices[j], vertices[j+1], vertices[j+2] );
1730 
1731  // create the intermediate scenegraph
1732  IFSG_TRANSFORM tx0( PcbOutput.GetRawPtr() ); // tx0 = Transform for this outline
1733  IFSG_SHAPE shape( tx0 ); // shape will hold (a) all vertices and (b) a local list of normals
1734  IFSG_FACESET face( shape ); // this face shall represent the top and bottom planes
1735  IFSG_COORDS cp( face ); // coordinates for all faces
1736  cp.SetCoordsList( nvert, &vlist[0] );
1737  IFSG_COORDINDEX coordIdx( face ); // coordinate indices for top and bottom planes only
1738  coordIdx.SetIndices( idxPlane.size(), &idxPlane[0] );
1739  IFSG_NORMALS norms( face ); // normals for the top and bottom planes
1740 
1741  // set the normals
1742  if( aTopPlane )
1743  {
1744  for( size_t i = 0; i < nvert; ++i )
1745  norms.AddNormal( 0.0, 0.0, 1.0 );
1746  }
1747  else
1748  {
1749  for( size_t i = 0; i < nvert; ++i )
1750  norms.AddNormal( 0.0, 0.0, -1.0 );
1751  }
1752 
1753  // assign a color from the palette
1754  SGNODE* modelColor = getSGColor( colorID );
1755 
1756  if( NULL != modelColor )
1757  {
1758  if( NULL == S3D::GetSGNodeParent( modelColor ) )
1759  shape.AddChildNode( modelColor );
1760  else
1761  shape.AddRefNode( modelColor );
1762  }
1763 
1764  return;
1765 }
1766 
1767 
1768 static void create_vrml_shell( IFSG_TRANSFORM& PcbOutput, VRML_COLOR_INDEX colorID,
1769  VRML_LAYER* layer, double top_z, double bottom_z )
1770 {
1771  std::vector< double > vertices;
1772  std::vector< int > idxPlane;
1773  std::vector< int > idxSide;
1774 
1775  if( top_z < bottom_z )
1776  {
1777  double tmp = top_z;
1778  top_z = bottom_z;
1779  bottom_z = tmp;
1780  }
1781 
1782  if( !( *layer ).Get3DTriangles( vertices, idxPlane, idxSide, top_z, bottom_z )
1783  || idxPlane.empty() || idxSide.empty() )
1784  {
1785 #ifdef DEBUG
1786  do {
1787  std::ostringstream ostr;
1788  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
1789  ostr << " * [INFO] no vertex data";
1790  wxLogDebug( "%s\n", ostr.str().c_str() );
1791  } while( 0 );
1792 #endif
1793 
1794  return;
1795  }
1796 
1797  if( ( idxPlane.size() % 3 ) || ( idxSide.size() % 3 ) )
1798  {
1799 #ifdef DEBUG
1800  do {
1801  std::ostringstream ostr;
1802  ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
1803  ostr << " * [BUG] index lists are not a multiple of 3 (not a triangle list)";
1804  wxLogDebug( "%s\n", ostr.str().c_str() );
1805  } while( 0 );
1806 #endif
1807 
1808  throw( std::runtime_error( "[BUG] index lists are not a multiple of 3 (not a triangle list)" ) );
1809  }
1810 
1811  std::vector< SGPOINT > vlist;
1812  size_t nvert = vertices.size() / 3;
1813  size_t j = 0;
1814 
1815  for( size_t i = 0; i < nvert; ++i, j+= 3 )
1816  vlist.emplace_back( vertices[j], vertices[j+1], vertices[j+2] );
1817 
1818  // create the intermediate scenegraph
1819  IFSG_TRANSFORM tx0( PcbOutput.GetRawPtr() ); // tx0 = Transform for this outline
1820  IFSG_SHAPE shape( tx0 ); // shape will hold (a) all vertices and (b) a local list of normals
1821  IFSG_FACESET face( shape ); // this face shall represent the top and bottom planes
1822  IFSG_COORDS cp( face ); // coordinates for all faces
1823  cp.SetCoordsList( nvert, &vlist[0] );
1824  IFSG_COORDINDEX coordIdx( face ); // coordinate indices for top and bottom planes only
1825  coordIdx.SetIndices( idxPlane.size(), &idxPlane[0] );
1826  IFSG_NORMALS norms( face ); // normals for the top and bottom planes
1827 
1828  // number of TOP (and bottom) vertices
1829  j = nvert / 2;
1830 
1831  // set the TOP normals
1832  for( size_t i = 0; i < j; ++i )
1833  norms.AddNormal( 0.0, 0.0, 1.0 );
1834 
1835  // set the BOTTOM normals
1836  for( size_t i = 0; i < j; ++i )
1837  norms.AddNormal( 0.0, 0.0, -1.0 );
1838 
1839  // assign a color from the palette
1840  SGNODE* modelColor = getSGColor( colorID );
1841 
1842  if( NULL != modelColor )
1843  {
1844  if( NULL == S3D::GetSGNodeParent( modelColor ) )
1845  shape.AddChildNode( modelColor );
1846  else
1847  shape.AddRefNode( modelColor );
1848  }
1849 
1850  // create a second shape describing the vertical walls of the extrusion
1851  // using per-vertex-per-face-normals
1852  shape.NewNode( tx0 );
1853  shape.AddRefNode( modelColor ); // set the color to be the same as the top/bottom
1854  face.NewNode( shape );
1855  cp.NewNode( face ); // new vertex list
1856  norms.NewNode( face ); // new normals list
1857  coordIdx.NewNode( face ); // new index list
1858 
1859  // populate the new per-face vertex list and its indices and normals
1860  std::vector< int >::iterator sI = idxSide.begin();
1861  std::vector< int >::iterator eI = idxSide.end();
1862 
1863  size_t sidx = 0; // index to the new coord set
1864  SGPOINT p1, p2, p3;
1865  SGVECTOR vnorm;
1866 
1867  while( sI != eI )
1868  {
1869  p1 = vlist[*sI];
1870  cp.AddCoord( p1 );
1871  ++sI;
1872 
1873  p2 = vlist[*sI];
1874  cp.AddCoord( p2 );
1875  ++sI;
1876 
1877  p3 = vlist[*sI];
1878  cp.AddCoord( p3 );
1879  ++sI;
1880 
1881  vnorm.SetVector( S3D::CalcTriNorm( p1, p2, p3 ) );
1882  norms.AddNormal( vnorm );
1883  norms.AddNormal( vnorm );
1884  norms.AddNormal( vnorm );
1885 
1886  coordIdx.AddIndex( (int)sidx );
1887  ++sidx;
1888  coordIdx.AddIndex( (int)sidx );
1889  ++sidx;
1890  coordIdx.AddIndex( (int)sidx );
1891  ++sidx;
1892  }
1893 }
void SetLayerZ(LAYER_NUM aLayer, double aValue)
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
Definition: common.cpp:336
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Function AllCuMask returns a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:686
bool SetTranslation(const SGPOINT &aTranslation)
void Rotate(double aAngle, const VECTOR2I &aCenter={ 0, 0 })
Function Rotate rotates all vertices by a given angle.
static double BOARD_SCALE
Definition: export_vrml.cpp:70
static void vrml_text_callback(int x0, int y0, int xf, int yf, void *aData)
static void export_vrml_pad(MODEL_VRML &aModel, BOARD *aPcb, D_PAD *aPad)
SGNODE * GetRawPtr(void)
Function GetRawPtr() returns the raw internal SGNODE pointer.
Definition: ifsg_node.cpp:66
ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:60
void LayerPair(PCB_LAYER_ID *top_layer, PCB_LAYER_ID *bottom_layer) const
Function LayerPair Return the 2 layers used by the via (the via actually uses all layers between thes...
double GetOrientation() const
Definition: class_module.h:215
bool IsFilled() const
Definition: class_zone.h:188
TEXTE_MODULE & Reference()
Definition: class_module.h:477
static S3D_CACHE * cache
Definition: export_vrml.cpp:65
int OutlineCount() const
Returns the number of outlines in the set
EDA_TEXT_VJUSTIFY_T GetVertJustify() const
Definition: eda_text.h:184
double x
Definition: sg_base.h:70
VRML_COLOR_INDEX
bool IsMirrored() const
Definition: eda_text.h:173
bool NewNode(SGNODE *aParent) override
Function NewNode creates a new node to associate with this wrapper.
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:62
static void compose_quat(double q1[4], double q2[4], double qr[4])
TEXTE_PCB class definition.
static double WORLD_SCALE
Definition: export_vrml.cpp:69
IFSG_COORDS is the wrapper for SGCOORDS.
Definition: ifsg_coords.h:40
IFSG_COORDINDEX is the wrapper for SGCOORDINDEX.
like PAD_STANDARD, but not plated mechanical use only, no connection allowed
Definition: pad_shapes.h:66
static bool USE_INLINES
Definition: export_vrml.cpp:66
static void export_vrml_text_module(TEXTE_MODULE *item)
const wxPoint GetCenter() const override
Function GetCenter()
bool Attach(SGNODE *aNode) override
Function Attach associates a given SGNODE* with this wrapper.
const wxPoint & GetStart() const
Definition: class_track.h:111
float transp
Definition: export_vrml.cpp:90
STROKE_T GetShape() const
VRML_LAYER m_bot_tin
VRML_LAYER m_plated_holes
VRML_LAYER m_holes
bool IsVisible() const
Definition: eda_text.h:170
int color
Definition: DXF_plotter.cpp:61
polygon (not yet used for tracks, but could be in microwave apps)
bool SetDiffuse(float aRVal, float aGVal, float aBVal)
bool SetTransparency(float aTransparency)
static void create_vrml_plane(IFSG_TRANSFORM &PcbOutput, VRML_COLOR_INDEX colorID, VRML_LAYER *layer, double aHeight, bool aTopPlane)
#define OSTREAM
Definition: streamwrapper.h:99
virtual PCB_LAYER_ID GetLayer() const override
Function GetLayer returns the primary layer this item is on.
Definition: class_zone.cpp:213
PADS & Pads()
Definition: class_module.h:173
provides an extensible class to resolve 3D model paths.
double GetTextAngle() const
Definition: eda_text.h:158
bool AddRefNode(SGNODE *aNode)
Function AddRefNode adds a reference to an existing node which is not owned by (not a child of) this ...
Definition: ifsg_node.cpp:199
class TEXTE_PCB, text on a layer
Definition: typeinfo.h:92
static MODEL_VRML * model_vrml
void SetVector(double aXVal, double aYVal, double aZVal)
Definition: sg_base.cpp:292
SHAPE_LINE_CHAIN & Hole(int aOutline, int aHole)
Returns the reference to aHole-th hole in the aIndex-th outline
void SetOffset(double aXoff, double aYoff)
static void compute_layer_Zs(MODEL_VRML &aModel, BOARD *pcb)
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Function GetDesignSettings.
Definition: class_board.h:512
int GetThickness() const
Return the pen width.
Definition: eda_text.h:148
usual segment : line with rounded ends
SGLIB_API SGNODE * GetSGNodeParent(SGNODE *aNode)
Definition: ifsg_api.cpp:636
static void write_triangle_bag(std::ostream &aOut_file, VRML_COLOR &aColor, VRML_LAYER *aLayer, bool aPlane, bool aTop, double aTop_z, double aBottom_z)
bool SetParent(SGNODE *aParent)
Function SetParent sets the parent SGNODE of this object.
Definition: ifsg_node.cpp:108
DRAWINGS & GraphicalItems()
Definition: class_module.h:183
static SGNODE * sgmaterial[VRML_COLOR_LAST]
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
SGNODE represents the base class of all Scene Graph nodes.
Definition: sg_node.h:76
S3D_CACHE.
Definition: 3d_cache.h:54
void Inflate(int aAmount, int aCircleSegmentsCount, CORNER_STRATEGY aCornerStrategy=ROUND_ALL_CORNERS)
Performs outline inflation/deflation.
collects header files for all SG* wrappers and the API
virtual wxString GetShownText() const override
Return the string actually shown after processing of the base text.
float spec_blu
Definition: export_vrml.cpp:83
bool SetScale(const SGPOINT &aScale)
IFSG_TRANSFORM m_OutputPCB
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
const wxPoint & GetEnd() const
Function GetEnd returns the ending point of the graphic.
defines the basic data associated with a single 3D model.
int PointCount() const
Function PointCount()
bool SetScale(double aWorldScale)
VTBL_ENTRY const wxString GetProjectPath() const
Function GetProjectPath returns the full path of the project.
Definition: project.cpp:102
double m_layer_z[PCB_LAYER_ID_COUNT]
class EDGE_MODULE, a footprint edge
Definition: typeinfo.h:94
bool AddChildNode(SGNODE *aNode)
Function AddChildNode adds a node as a child owned by this node.
Definition: ifsg_node.cpp:249
PAD_ATTR_T GetAttribute() const
Definition: class_pad.h:449
Definitions for tracks, vias and zones.
bool AddIndex(int aIndex)
Function AddIndex adds a single index to the list.
Definition: ifsg_index.cpp:83
SGLIB_API bool WriteVRML(const char *filename, bool overwrite, SGNODE *aTopNode, bool reuse, bool renameNodes)
Function WriteVRML writes out the given node and its subnodes to a VRML2 file.
Definition: ifsg_api.cpp:81
This file contains miscellaneous commonly used macros and functions.
static void export_vrml_module(MODEL_VRML &aModel, BOARD *aPcb, MODULE *aModule, std::ostream *aOutputFile)
bool IsItalic() const
Definition: eda_text.h:164
static void build_quat(double x, double y, double z, double a, double q[4])
static void export_vrml_circle(MODEL_VRML &aModel, LAYER_NUM layer, double startx, double starty, double endx, double endy, double width)
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:48
bool AddNormal(double aXValue, double aYValue, double aZValue)
static void export_vrml_padshape(MODEL_VRML &aModel, VRML_LAYER *aTinLayer, D_PAD *aPad)
const VECTOR2I & CPoint(int aIndex) const
Function Point()
float ambient
Definition: export_vrml.cpp:89
float diffuse_grn
Definition: export_vrml.cpp:78
PCB_LAYER_ID
A quick note on layer IDs:
VRML_LAYER m_bot_silk
int GetAreaCount() const
Function GetAreaCount.
Definition: class_board.h:919
LSET is a set of PCB_LAYER_IDs.
static void export_vrml_drawsegment(MODEL_VRML &aModel, DRAWSEGMENT *drawseg)
IFSG_NORMALS is the wrapper for the SGNORMALS class.
Definition: ifsg_normals.h:40
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:183
VRML_LAYER m_top_silk
#define NULL
bool SetAmbient(float aRVal, float aGVal, float aBVal)
void Move(const VECTOR2I &aVector) override
int GetDrillValue() const
Function GetDrillValue "calculates" the drill value for vias (m-Drill if > 0, or default drill value ...
MODULES & Modules()
Definition: class_board.h:229
bool MergePrimitivesAsPolygon(SHAPE_POLY_SET *aMergedPolygon=NULL)
Merge all basic shapes, converted to a polygon in one polygon, in m_customShapeAsPolygon.
SHAPE_POLY_SET.
TEXTE_MODULE & Value()
read/write accessors:
Definition: class_module.h:476
SHAPE_LINE_CHAIN & Outline(int aIndex)
Returns the reference to aIndex-th outline in the set
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
const wxSize & GetTextSize() const
Definition: eda_text.h:223
Arcs (with rounded ends)
PAD_DRILL_SHAPE_T GetDrillShape() const
Definition: class_pad.h:432
LSET GetLayerSet() const override
Function GetLayerSet returns a "layer mask", which is a bitmap of all layers on which the TRACK segme...
Definition: class_pad.h:446
static void export_round_padstack(MODEL_VRML &aModel, BOARD *pcb, double x, double y, double r, LAYER_NUM bottom_layer, LAYER_NUM top_layer, double hole)
VRML_LAYER m_board
#define MIN_VRML_LINEWIDTH
Definition: export_vrml.cpp:58
static bool USE_RELPATH
Definition: export_vrml.cpp:68
static SGNODE * getSGColor(VRML_COLOR_INDEX colorIdx)
#define OPEN_OSTREAM(var, name)
double GetOrientationRadians() const
Definition: class_module.h:217
float spec_red
Definition: export_vrml.cpp:81
bool SetRotation(const SGVECTOR &aRotationAxis, double aAngle)
static void export_vrml_arc(MODEL_VRML &aModel, LAYER_NUM layer, double centerx, double centery, double arc_startx, double arc_starty, double width, double arc_angle)
static wxString SUBDIR_3D
Definition: export_vrml.cpp:72
a few functions useful in geometry calculations.
const wxPoint & GetArcStart() const
static void export_vrml_pcbtext(MODEL_VRML &aModel, TEXTE_PCB *text)
bool GetBoardPolygonOutlines(SHAPE_POLY_SET &aOutlines, wxString *aErrorText=nullptr, wxPoint *aErrorLocation=nullptr)
Function GetBoardPolygonOutlines Extracts the board outlines and build a closed polygon from lines,...
SHAPE_POLY_SET & GetPolyShape()
bool Fill(const std::vector< ZONE_CONTAINER * > &aZones, bool aCheck=false)
static void export_vrml_tracks(MODEL_VRML &aModel, BOARD *pcb)
static void export_vrml_board(MODEL_VRML &aModel, BOARD *aPcb)
const wxSize & GetDelta() const
Definition: class_pad.h:303
static void export_vrml_drawings(MODEL_VRML &aModel, BOARD *pcb)
int HoleCount(int aOutline) const
Returns the number of holes in a given outline
float spec_grn
Definition: export_vrml.cpp:82
VRML_LAYER m_bot_copper
void Fracture(POLYGON_MODE aFastMode)
Converts a set of polygons with holes to a singe outline with "slits"/"fractures" connecting the oute...
LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
int GetWidth() const
bool IsMultilineAllowed() const
Definition: eda_text.h:181
int LAYER_NUM
Type LAYER_NUM can be replaced with int and removed.
LAYER_NUM m_text_layer
static void write_layers(MODEL_VRML &aModel, BOARD *aPcb, const char *aFileName, OSTREAM *aOutputFile)
float emit_grn
Definition: export_vrml.cpp:86
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Definition: macros.h:108
void GRText(wxDC *aDC, const wxPoint &aPos, COLOR4D aColor, const wxString &aText, double aOrient, const wxSize &aSize, enum EDA_TEXT_HJUSTIFY_T aH_justify, enum EDA_TEXT_VJUSTIFY_T aV_justify, int aWidth, bool aItalic, bool aBold, void(*aCallback)(int x0, int y0, int xf, int yf, void *aData), void *aCallbackData, PLOTTER *aPlotter)
Function GRText Draw a graphic text (like module texts)
Definition: gr_text.cpp:143
std::list< SGNODE * > m_components
void TransformRoundChamferedRectToPolygon(SHAPE_POLY_SET &aCornerBuffer, const wxPoint &aPosition, const wxSize &aSize, double aRotation, int aCornerRadius, double aChamferRatio, int aChamferCorners, int aApproxErrorMax, int aMinSegPerCircleCount=16)
convert a rectangle with rounded corners and/or chamfered corners to a polygon Convert rounded corner...
bool NewNode(SGNODE *aParent) override
Function NewNode creates a new node to associate with this wrapper.
wxString ResolvePath(const wxString &aFileName)
Function ResolvePath determines the full path of the given file name.
static wxString PROJ_DIR
Definition: export_vrml.cpp:73
bool SetIndices(size_t nIndices, int *aIndexList)
Function SetIndices sets the number of indices and creates a copy of the given index data.
Definition: ifsg_index.cpp:63
class TEXTE_MODULE, text in a footprint
Definition: typeinfo.h:93
SGLIB_API SGVECTOR CalcTriNorm(const SGPOINT &p1, const SGPOINT &p2, const SGPOINT &p3)
Function CalcTriNorm returns the normal vector of a triangle described by vertices p1,...
Definition: ifsg_api.cpp:606
double GetAngle() const
static void export_vrml_polygon(MODEL_VRML &aModel, LAYER_NUM layer, DRAWSEGMENT *aOutline, double aOrientation, wxPoint aPos)
bool SetSpecular(float aRVal, float aGVal, float aBVal)
std::list< MODULE_3D_SETTINGS > & Models()
Definition: class_module.h:205
static void export_vrml_zones(MODEL_VRML &aModel, BOARD *aPcb)
void SetFillMode(ZONE_FILL_MODE aFillMode)
Definition: class_zone.h:155
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:101
see class PGM_BASE
bool SetCoordsList(size_t aListSize, const SGPOINT *aCoordsList)
int GetWidth() const
Definition: class_track.h:105
bool ExportVRML_File(const wxString &aFullFileName, double aMMtoWRMLunit, bool aExport3DFiles, bool aUseRelativePaths, bool aUsePlainPCB, const wxString &a3D_Subdir, double aXRef, double aYRef)
Function ExportVRML_File Creates the file(s) exporting current BOARD to a VRML file.
float emit_blu
Definition: export_vrml.cpp:87
bool SetShininess(float aShininess)
double DEG2RAD(double deg)
Definition: trigo.h:214
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:163
#define _(s)
Definition: 3d_actions.cpp:33
float diffuse_red
Definition: export_vrml.cpp:77
SHAPE_LINE_CHAIN.
double m_arcMinLen
double GetOrientation() const
Function GetOrientation returns the rotation angle of the pad in tenths of degrees,...
Definition: class_pad.h:426
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
IFSG_FACESET is the wrapper for the SGFACESET class.
Definition: ifsg_faceset.h:40
double m_arcMaxLen
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
bool NewNode(SGNODE *aParent) override
Function NewNode creates a new node to associate with this wrapper.
IFSG_TRANSFORM is the wrapper for the VRML compatible TRANSFORM block class SCENEGRAPH.
SGLIB_API void DestroyNode(SGNODE *aNode)
Function DestroyNode deletes the given SG* class node.
Definition: ifsg_api.cpp:210
int GetCopperLayerCount() const
Function GetCopperLayerCount.
static void create_vrml_shell(IFSG_TRANSFORM &PcbOutput, VRML_COLOR_INDEX colorID, VRML_LAYER *layer, double top_z, double bottom_z)
const wxSize & GetDrillSize() const
Definition: class_pad.h:306
wxPoint ShapePos() const
Definition: class_pad.cpp:570
double DECIDEG2RAD(double deg)
Definition: trigo.h:218
static void from_quat(double q[4], double rot[4])
#define IU_PER_MILS
Definition: plotter.cpp:137
defines the display data cache manager for 3D models
double m_brd_thickness
bool IsPolyShapeValid() const
static const int PRECISION
Definition: export_vrml.cpp:71
int GetRoundRectCornerRadius() const
Function GetRoundRectCornerRadius Has meaning only for rounded rect pads.
Definition: class_pad.h:577
static bool GetLayer(MODEL_VRML &aModel, LAYER_NUM layer, VRML_LAYER **vlayer)
double m_minLineWidth
static bool USE_DEFS
Definition: export_vrml.cpp:67
const wxPoint & GetTextPos() const
Definition: eda_text.h:232
FILENAME_RESOLVER * GetResolver(void)
Definition: 3d_cache.cpp:690
void GetPositionsOfLinesOfMultilineText(std::vector< wxPoint > &aPositions, int aLineCount) const
Populate aPositions with the position of each line of a multiline text, according to the vertical jus...
Definition: eda_text.cpp:384
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
#define PLATE_OFFSET
Definition: export_vrml.cpp:63
const wxPoint & GetStart() const
Function GetStart returns the starting point of the graphic.
static void export_vrml_via(MODEL_VRML &aModel, BOARD *aPcb, const VIA *aVia)
static void export_vrml_edge_module(MODEL_VRML &aModel, EDGE_MODULE *aOutline, MODULE *aModule)
void Destroy(void)
Function Destroy deletes the object held by this wrapper.
Definition: ifsg_node.cpp:54
PAD_SHAPE_T GetShape() const
Function GetShape.
Definition: class_pad.h:237
Module description (excepted pads)
virtual wxString GetShownText() const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:129
Definition: colors.h:45
#define CLOSE_STREAM(var)
ZONE_CONTAINER * GetArea(int index) const
Function GetArea returns the Area (Zone Container) at a given index.
Definition: class_board.h:884
SCENEGRAPH * Load(const wxString &aModelFile)
Function Load attempts to load the scene data for a model; it will consult the internal cache list an...
Definition: 3d_cache.cpp:283
BOARD * GetBoard() const
static void export_vrml_line(MODEL_VRML &aModel, LAYER_NUM layer, double startx, double starty, double endx, double endy, double width)
VRML_LAYER m_top_copper
const wxSize & GetSize() const
Definition: class_pad.h:300
const wxPoint GetPosition() const override
Definition: class_pad.h:241
VRML_LAYER m_top_tin
EDGE_MODULE class definition.
class DRAWSEGMENT, a segment not on copper layers
Definition: typeinfo.h:91
double GetDrawRotation() const
VRML_COLOR & GetColor(VRML_COLOR_INDEX aIndex)
virtual PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
float emit_red
Definition: export_vrml.cpp:85
const wxPoint GetPosition() const override
Definition: class_module.h:210
DRAWINGS & Drawings()
Definition: class_board.h:238
int GetArcToSegmentCount(int aRadius, int aErrorMax, double aArcAngleDegree)
bool AddCoord(double aXValue, double aYValue, double aZValue)
float diffuse_blu
Definition: export_vrml.cpp:79
double GetLayerZ(LAYER_NUM aLayer)
static VRML_COLOR colors[VRML_COLOR_LAST]
TRACKS & Tracks()
Definition: class_board.h:220
#define ART_OFFSET
Definition: export_vrml.cpp:61
IFSG_SHAPE is the wrapper for the SGSHAPE class.
Definition: ifsg_shape.h:40
bool NewNode(SGNODE *aParent) override
Function NewNode creates a new node to associate with this wrapper.
const SHAPE_POLY_SET & GetFilledPolysList() const
Function GetFilledPolysList returns a reference to the list of filled polygons.
Definition: class_zone.h:571
VRML_COLOR(float dr, float dg, float db, float sr, float sg, float sb, float er, float eg, float eb, float am, float tr, float sh)
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:40