KiCad PCB EDA Suite
dxf_import_plugin.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) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2018 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 // The DXF reader lib (libdxfrw) comes from dxflib project used in QCAD
26 // See http://www.ribbonsoft.com
27 // Each time a dxf entity is read, a "call back" fuction is called
28 // like void DXF_IMPORT_PLUGIN::addLine( const DL_LineData& data ) when a line is read.
29 // this function just add the BOARD entity from dxf parameters (start and end point ...)
30 
31 
32 #include "dxf_import_plugin.h"
33 #include <wx/arrstr.h>
34 #include <wx/regex.h>
35 
36 #include <trigo.h>
37 #include <macros.h>
38 #include <class_board.h>
39 #include <class_drawsegment.h>
40 #include <class_edge_mod.h>
41 #include <class_pcb_text.h>
42 #include <class_text_mod.h>
43 #include "common.h"
44 
45 
46 /*
47  * Important note: all DXF coordinates and sizes are converted to mm.
48  * they will be converted to internal units later.
49  */
50 
51 
52 // minimum bulge value before resorting to a line segment;
53 // the value 0.0218 is equivalent to about 5 degrees arc,
54 #define MIN_BULGE 0.0218
55 
56 //#define SCALE_FACTOR(x) millimeter2iu(x) /* no longer used */
57 #define SCALE_FACTOR(x) (x)
58 
59 DXF_IMPORT_PLUGIN::DXF_IMPORT_PLUGIN() : DL_CreationAdapter()
60 {
61  m_xOffset = 0.0; // X coord offset for conversion (in mm)
62  m_yOffset = 0.0; // Y coord offset for conversion (in mm)
63  m_DXF2mm = 1.0; // The scale factor to convert DXF units to mm
64  m_version = 0; // the dxf version, not yet used
65  m_defaultThickness = 0.2; // default thickness (in mm)
66  m_brdLayer = Dwgs_User; // The default import layer
70 }
71 
72 
74 {
75 }
76 
77 
78 bool DXF_IMPORT_PLUGIN::Load( const wxString& aFileName )
79 {
80  return ImportDxfFile( aFileName );
81 }
82 
83 
85 {
86  wxCHECK( m_importer, false );
88 
89  return true;
90 }
91 
92 
94 {
95  return m_maxX - m_minX;
96 }
97 
98 
100 {
101  return m_maxY - m_minY;
102 }
103 
104 // coordinate conversions from dxf to internal units
105 double DXF_IMPORT_PLUGIN::mapX( double aDxfCoordX )
106 {
107  return SCALE_FACTOR( m_xOffset + ( aDxfCoordX * m_DXF2mm ) );
108 }
109 
110 
111 double DXF_IMPORT_PLUGIN::mapY( double aDxfCoordY )
112 {
113  return SCALE_FACTOR( m_yOffset - ( aDxfCoordY * m_DXF2mm ) );
114 }
115 
116 
117 double DXF_IMPORT_PLUGIN::mapDim( double aDxfValue )
118 {
119  return SCALE_FACTOR( aDxfValue * m_DXF2mm );
120 }
121 
122 
123 double DXF_IMPORT_PLUGIN::mapWidth( double aDxfWidth )
124 {
125  // Always return the default line width
126 #if 0
127  // mapWidth returns the aDxfValue if aDxfWidth > 0 m_defaultThickness
128  if( aDxfWidth > 0.0 )
129  return SCALE_FACTOR( aDxfWidth * m_DXF2mm );
130 #endif
132 }
133 
134 bool DXF_IMPORT_PLUGIN::ImportDxfFile( const wxString& aFile )
135 {
136  DL_Dxf dxf_reader;
137  std::string filename = TO_UTF8( aFile );
138 
139  // wxFopen takes care of unicode filenames across platforms
140  FILE* fp = wxFopen( aFile, "rt" );
141 
142  if( fp == nullptr )
143  return false;
144 
145  // Note the dxf reader takes care of switching to "C" locale before reading the file
146  // and will close the file after reading
147  bool success = dxf_reader.in( fp, this );
148 
149  return success;
150 }
151 
152 
153 void DXF_IMPORT_PLUGIN::reportMsg( const char* aMessage )
154 {
155  // Add message to keep trace of not handled dxf entities
156  m_messages += aMessage;
157  m_messages += '\n';
158 }
159 
160 
161 void DXF_IMPORT_PLUGIN::addSpline( const DL_SplineData& aData )
162 {
163  // Called when starting reading a spline
166  m_curr_entity.m_EntityFlag = aData.flags;
167  m_curr_entity.m_EntityType = DL_ENTITY_SPLINE;
168  m_curr_entity.m_SplineDegree = aData.degree;
169  m_curr_entity.m_SplineTangentStartX = aData.tangentStartX;
170  m_curr_entity.m_SplineTangentStartY = aData.tangentStartY;
171  m_curr_entity.m_SplineTangentEndX = aData.tangentEndX;
172  m_curr_entity.m_SplineTangentEndY = aData.tangentEndY;
173  m_curr_entity.m_SplineKnotsCount = aData.nKnots;
174  m_curr_entity.m_SplineControlCount = aData.nControl;
175  m_curr_entity.m_SplineFitCount = aData.nFit;
176 }
177 
178 
179 void DXF_IMPORT_PLUGIN::addControlPoint( const DL_ControlPointData& aData )
180 {
181  // Called for every spline control point, when reading a spline entity
182  m_curr_entity.m_SplineControlPointList.push_back( SPLINE_CTRL_POINT( aData.x , aData.y, aData.w ) );
183 }
184 
185 void DXF_IMPORT_PLUGIN::addFitPoint( const DL_FitPointData& aData )
186 {
187  // Called for every spline fit point, when reading a spline entity
188  // we store only the X,Y coord values in a VECTOR2D
189  m_curr_entity.m_SplineFitPointList.push_back( VECTOR2D( aData.x, aData.y ) );
190 }
191 
192 
193 void DXF_IMPORT_PLUGIN::addKnot( const DL_KnotData& aData)
194 {
195  // Called for every spline knot value, when reading a spline entity
196  m_curr_entity.m_SplineKnotsList.push_back( aData.k );
197 }
198 
199 
200 void DXF_IMPORT_PLUGIN::addLayer( const DL_LayerData& aData )
201 {
202  // Not yet useful in Pcbnew.
203 #if 0
204  wxString name = wxString::FromUTF8( aData.name.c_str() );
205  wxLogMessage( name );
206 #endif
207 }
208 
209 
210 void DXF_IMPORT_PLUGIN::addLine( const DL_LineData& aData )
211 {
212  VECTOR2D start( mapX( aData.x1 ), mapY( aData.y1 ) );
213  VECTOR2D end( mapX( aData.x2 ), mapY( aData.y2 ) );
214  double lineWidth = mapWidth( attributes.getWidth() );
215 
216  m_internalImporter.AddLine( start, end, lineWidth );
217 
218  updateImageLimits( start );
219  updateImageLimits( end );
220 }
221 
222 
223 void DXF_IMPORT_PLUGIN::addPolyline(const DL_PolylineData& aData )
224 {
225  // Convert DXF Polylines into a series of KiCad Lines and Arcs.
226  // A Polyline (as opposed to a LWPolyline) may be a 3D line or
227  // even a 3D Mesh. The only type of Polyline which is guaranteed
228  // to import correctly is a 2D Polyline in X and Y, which is what
229  // we assume of all Polylines. The width used is the width of the Polyline.
230  // per-vertex line widths, if present, are ignored.
233  m_curr_entity.m_EntityFlag = aData.flags;
234  m_curr_entity.m_EntityType = DL_ENTITY_POLYLINE;
235 }
236 
237 
238 void DXF_IMPORT_PLUGIN::addVertex( const DL_VertexData& aData )
239 {
241  return; // Error
242 
243  double lineWidth = mapWidth( attributes.getWidth() );
244 
245  const DL_VertexData* vertex = &aData;
246 
247  if( m_curr_entity.m_EntityParseStatus == 1 ) // This is the first vertex of an entity
248  {
252  m_curr_entity.m_BulgeVertex = vertex->bulge;
254  return;
255  }
256 
257  VECTOR2D seg_end( m_xOffset + vertex->x * m_DXF2mm,
258  m_yOffset - vertex->y * m_DXF2mm );
259 
261  insertLine( m_curr_entity.m_LastCoordinate, seg_end, lineWidth );
262  else
264 
266  m_curr_entity.m_BulgeVertex = vertex->bulge;
267 }
268 
269 
271 {
272  if( m_curr_entity.m_EntityType == DL_ENTITY_POLYLINE ||
273  m_curr_entity.m_EntityType == DL_ENTITY_LWPOLYLINE )
274  {
275  // Polyline flags bit 0 indicates closed (1) or open (0) polyline
276  if( m_curr_entity.m_EntityFlag & 1 )
277  {
278  double lineWidth = mapWidth( attributes.getWidth() );
279 
282  else
284  m_curr_entity.m_BulgeVertex, lineWidth );
285  }
286  }
287 
288  if( m_curr_entity.m_EntityType == DL_ENTITY_SPLINE )
289  {
290  double lineWidth = mapWidth( attributes.getWidth() );
291  insertSpline( lineWidth );
292  }
293 
295 }
296 
297 
298 void DXF_IMPORT_PLUGIN::addCircle( const DL_CircleData& aData )
299 {
300  VECTOR2D center( mapX( aData.cx ), mapY( aData.cy ) );
301  double lineWidth = mapWidth( attributes.getWidth() );
302  m_internalImporter.AddCircle( center, mapDim( aData.radius ), lineWidth );
303 
304  VECTOR2D radiusDelta( mapDim( aData.radius ), mapDim( aData.radius ) );
305 
306  updateImageLimits( center + radiusDelta );
307  updateImageLimits( center - radiusDelta );
308 }
309 
310 
311 /*
312  * Import Arc entities.
313  */
314 void DXF_IMPORT_PLUGIN::addArc( const DL_ArcData& aData )
315 {
316  // Init arc centre:
317  VECTOR2D center( mapX( aData.cx ), mapY( aData.cy ) );
318 
319  // Init arc start point
320  double arcStartx = aData.radius;
321  double arcStarty = 0;
322 
323  // aData.anglex is in degrees. Our internal units are 0.1 degree
324  // so convert DXF angles to our units
325  #define DXF2ANGLEUI 10
326  double startangle = aData.angle1 * DXF2ANGLEUI;
327  double endangle = aData.angle2 * DXF2ANGLEUI;
328 
329  RotatePoint( &arcStartx, &arcStarty, -RAD2DECIDEG( startangle ) );
330  VECTOR2D arcStart( mapX( arcStartx + aData.cx ), mapY( arcStarty + aData.cy ) );
331 
332  // calculate arc angle (arcs are CCW, and should be < 0 in Pcbnew)
333  double angle = -( endangle - startangle );
334 
335  if( angle > 0.0 )
336  angle -= 3600.0;
337 
338  double lineWidth = mapWidth( attributes.getWidth() );
339  m_internalImporter.AddArc( center, arcStart, angle, lineWidth );
340 
341  VECTOR2D radiusDelta( mapDim( aData.radius ), mapDim( aData.radius ) );
342 
343  updateImageLimits( center + radiusDelta );
344  updateImageLimits( center - radiusDelta );
345 }
346 
347 
348 void DXF_IMPORT_PLUGIN::addText( const DL_TextData& aData )
349 {
350  VECTOR2D refPoint( mapX( aData.ipx ), mapY( aData.ipy ) );
351  VECTOR2D secPoint( mapX( aData.apx ), mapY( aData.apy ) );
352 
353  if( aData.vJustification != 0 || aData.hJustification != 0 || aData.hJustification == 4 )
354  {
355  if( aData.hJustification != 3 && aData.hJustification != 5 )
356  {
357  VECTOR2D tmp = secPoint;
358  secPoint = refPoint;
359  refPoint = tmp;
360  }
361  }
362 
363  wxString text = toNativeString( wxString::FromUTF8( aData.text.c_str() ) );
364 
365  double textHeight = mapDim( aData.height );
366  // The 0.9 factor gives a better height/width ratio with our font
367  double charWidth = textHeight * 0.9;
368  double textWidth = charWidth * text.length(); // Rough approximation
369  double textThickness = textHeight/8.0; // Use a reasonable line thickness for this text
370 
371  VECTOR2D bottomLeft(0.0, 0.0);
372  VECTOR2D bottomRight(0.0, 0.0);
373  VECTOR2D topLeft(0.0, 0.0);
374  VECTOR2D topRight(0.0, 0.0);
375 
378 
379  switch( aData.vJustification )
380  {
381  case 0: //DRW_Text::VBaseLine:
382  case 1: //DRW_Text::VBottom:
383  vJustify = GR_TEXT_VJUSTIFY_BOTTOM;
384 
385  topLeft.y = textHeight;
386  topRight.y = textHeight;
387  break;
388 
389  case 2: //DRW_Text::VMiddle:
390  vJustify = GR_TEXT_VJUSTIFY_CENTER;
391 
392  bottomRight.y = -textHeight / 2.0;
393  bottomLeft.y = -textHeight / 2.0;
394  topLeft.y = textHeight / 2.0;
395  topRight.y = textHeight / 2.0;
396  break;
397 
398  case 3: //DRW_Text::VTop:
399  vJustify = GR_TEXT_VJUSTIFY_TOP;
400 
401  bottomLeft.y = -textHeight;
402  bottomRight.y = -textHeight;
403  break;
404  }
405 
406  switch( aData.hJustification )
407  {
408  case 0: //DRW_Text::HLeft:
409  case 3: //DRW_Text::HAligned: // no equivalent options in text pcb.
410  case 5: //DRW_Text::HFit: // no equivalent options in text pcb.
411  hJustify = GR_TEXT_HJUSTIFY_LEFT;
412 
413  bottomRight.x = textWidth;
414  topRight.x = textWidth;
415  break;
416 
417  case 1: //DRW_Text::HCenter:
418  case 4: //DRW_Text::HMiddle: // no equivalent options in text pcb.
419  hJustify = GR_TEXT_HJUSTIFY_CENTER;
420 
421  bottomLeft.x = -textWidth / 2.0;
422  topLeft.x = -textWidth / 2.0;
423  bottomRight.x = textWidth / 2.0;
424  topRight.x = textWidth / 2.0;
425  break;
426 
427  case 2: //DRW_Text::HRight:
428  hJustify = GR_TEXT_HJUSTIFY_RIGHT;
429 
430  bottomLeft.x = -textWidth;
431  topLeft.x = -textWidth;
432  break;
433  }
434 
435 #if 0
436  wxString sty = wxString::FromUTF8( aData.style.c_str() );
437  sty = sty.ToLower();
438 
439  if( aData.textgen == 2 )
440  {
441  // Text dir = left to right;
442  } else if( aData.textgen == 4 )
443  {
444  // Text dir = top to bottom;
445  } else
446  {
447  }
448 #endif
449 
450  double angle = aData.angle * 10;
451  double angleInRads = angle / 10.0 * M_PI / 180.0;
452  double cosine = cos(angleInRads);
453  double sine = sin(angleInRads);
454 
455  m_internalImporter.AddText( refPoint, text, textHeight, charWidth, textThickness, angle,
456  hJustify, vJustify );
457 
458  // Calculate the boundary box and update the image limits:
459  bottomLeft.x = bottomLeft.x * cosine - bottomLeft.y * sine;
460  bottomLeft.y = bottomLeft.x * sine + bottomLeft.y * cosine;
461 
462  bottomRight.x = bottomRight.x * cosine - bottomRight.y * sine;
463  bottomRight.y = bottomRight.x * sine + bottomRight.y * cosine;
464 
465  topLeft.x = topLeft.x * cosine - topLeft.y * sine;
466  topLeft.y = topLeft.x * sine + topLeft.y * cosine;
467 
468  topRight.x = topRight.x * cosine - topRight.y * sine;
469  topRight.y = topRight.x * sine + topRight.y * cosine;
470 
471  bottomLeft += refPoint;
472  bottomRight += refPoint;
473  topLeft += refPoint;
474  topRight += refPoint;
475 
476  updateImageLimits( bottomLeft );
477  updateImageLimits( bottomRight );
478  updateImageLimits( topLeft );
479  updateImageLimits( topRight );
480 
481 }
482 
483 
484 void DXF_IMPORT_PLUGIN::addMText( const DL_MTextData& aData )
485 {
486  wxString text = toNativeString( wxString::FromUTF8( aData.text.c_str() ) );
487  wxString attrib, tmp;
488 
489  double textHeight = mapDim( aData.height );
490  // The 0.9 factor gives a better height/width ratio with our font
491  double charWidth = textHeight * 0.9;
492  double textWidth = charWidth * text.length(); // Rough approximation
493  double textThickness = textHeight/8.0; // Use a reasonable line thickness for this text
494 
495  VECTOR2D bottomLeft(0.0, 0.0);
496  VECTOR2D bottomRight(0.0, 0.0);
497  VECTOR2D topLeft(0.0, 0.0);
498  VECTOR2D topRight(0.0, 0.0);
499 
500  /* Some texts start by '\' and have formating chars (font name, font option...)
501  * ending with ';'
502  * Here are some mtext formatting codes:
503  * Format code Purpose
504  * \0...\o Turns overline on and off
505  * \L...\l Turns underline on and off
506  * \~ Inserts a nonbreaking space
507  \\ Inserts a backslash
508  \\\{...\} Inserts an opening and closing brace
509  \\ \File name; Changes to the specified font file
510  \\ \Hvalue; Changes to the text height specified in drawing units
511  \\ \Hvaluex; Changes the text height to a multiple of the current text height
512  \\ \S...^...; Stacks the subsequent text at the \, #, or ^ symbol
513  \\ \Tvalue; Adjusts the space between characters, from.75 to 4 times
514  \\ \Qangle; Changes obliquing angle
515  \\ \Wvalue; Changes width factor to produce wide text
516  \\ \A Sets the alignment value; valid values: 0, 1, 2 (bottom, center, top) while( text.StartsWith( wxT("\\") ) )
517  */
518  while( text.StartsWith( wxT( "\\" ) ) )
519  {
520  attrib << text.BeforeFirst( ';' );
521  tmp = text.AfterFirst( ';' );
522  text = tmp;
523  }
524 
525  VECTOR2D textpos( mapX( aData.ipx ), mapY( aData.ipy ) );
526 
527  // Initialize text justifications:
530 
531  if( aData.attachmentPoint <= 3 )
532  {
533  vJustify = GR_TEXT_VJUSTIFY_TOP;
534 
535  bottomLeft.y = -textHeight;
536  bottomRight.y = -textHeight;
537  }
538  else if( aData.attachmentPoint <= 6 )
539  {
540  vJustify = GR_TEXT_VJUSTIFY_CENTER;
541 
542  bottomRight.y = -textHeight / 2.0;
543  bottomLeft.y = -textHeight / 2.0;
544  topLeft.y = textHeight / 2.0;
545  topRight.y = textHeight / 2.0;
546  }
547  else
548  {
549  vJustify = GR_TEXT_VJUSTIFY_BOTTOM;
550 
551  topLeft.y = textHeight;
552  topRight.y = textHeight;
553  }
554 
555  if( aData.attachmentPoint % 3 == 1 )
556  {
557  hJustify = GR_TEXT_HJUSTIFY_LEFT;
558 
559  bottomRight.x = textWidth;
560  topRight.x = textWidth;
561  }
562  else if( aData.attachmentPoint % 3 == 2 )
563  {
564  hJustify = GR_TEXT_HJUSTIFY_CENTER;
565 
566  bottomLeft.x = -textWidth / 2.0;
567  topLeft.x = -textWidth / 2.0;
568  bottomRight.x = textWidth / 2.0;
569  topRight.x = textWidth / 2.0;
570  }
571  else
572  {
573  hJustify = GR_TEXT_HJUSTIFY_RIGHT;
574 
575  bottomLeft.x = -textWidth;
576  topLeft.x = -textWidth;
577  }
578 
579 #if 0 // These setting have no mening in Pcbnew
580  if( data.alignH == 1 )
581  {
582  // Text is left to right;
583  }
584  else if( data.alignH == 3 )
585  {
586  // Text is top to bottom;
587  }
588  else
589  {
590  // use ByStyle;
591  }
592 
593  if( aData.alignV == 1 )
594  {
595  // use AtLeast;
596  }
597  else
598  {
599  // useExact;
600  }
601 #endif
602 
603  double angle = aData.angle * 10;
604  double angleInRads = angle / 10.0 * M_PI / 180.0;
605  double cosine = cos(angleInRads);
606  double sine = sin(angleInRads);
607 
608  m_internalImporter.AddText( textpos, text, textHeight, charWidth,
609  textThickness, angle, hJustify, vJustify );
610 
611  bottomLeft.x = bottomLeft.x * cosine - bottomLeft.y * sine;
612  bottomLeft.y = bottomLeft.x * sine + bottomLeft.y * cosine;
613 
614  bottomRight.x = bottomRight.x * cosine - bottomRight.y * sine;
615  bottomRight.y = bottomRight.x * sine + bottomRight.y * cosine;
616 
617  topLeft.x = topLeft.x * cosine - topLeft.y * sine;
618  topLeft.y = topLeft.x * sine + topLeft.y * cosine;
619 
620  topRight.x = topRight.x * cosine - topRight.y * sine;
621  topRight.y = topRight.x * sine + topRight.y * cosine;
622 
623  bottomLeft += textpos;
624  bottomRight += textpos;
625  topLeft += textpos;
626  topRight += textpos;
627 
628  updateImageLimits( bottomLeft );
629  updateImageLimits( bottomRight );
630  updateImageLimits( topLeft );
631  updateImageLimits( topRight );
632 
633 }
634 
635 
636 void DXF_IMPORT_PLUGIN::setVariableInt( const std::string& key, int value, int code )
637 {
638  // Called for every int variable in the DXF file (e.g. "$INSUNITS").
639 
640  if( key == "$DWGCODEPAGE" )
641  {
642  m_codePage = value;
643  return;
644  }
645 
646  if( key == "$INSUNITS" ) // Drawing units
647  {
648  switch( value )
649  {
650  case 1: // inches
651  m_DXF2mm = 25.4;
652  break;
653 
654  case 2: // feet
655  m_DXF2mm = 304.8;
656  break;
657 
658  case 4: // mm
659  m_DXF2mm = 1.0;
660  break;
661 
662  case 5: // centimeters
663  m_DXF2mm = 10.0;
664  break;
665 
666  case 6: // meters
667  m_DXF2mm = 1000.0;
668  break;
669 
670  case 8: // microinches
671  m_DXF2mm = 2.54e-5;
672  break;
673 
674  case 9: // mils
675  m_DXF2mm = 0.0254;
676  break;
677 
678  case 10: // yards
679  m_DXF2mm = 914.4;
680  break;
681 
682  case 11: // Angstroms
683  m_DXF2mm = 1.0e-7;
684  break;
685 
686  case 12: // nanometers
687  m_DXF2mm = 1.0e-6;
688  break;
689 
690  case 13: // micrometers
691  m_DXF2mm = 1.0e-3;
692  break;
693 
694  case 14: // decimeters
695  m_DXF2mm = 100.0;
696  break;
697 
698  default:
699  // use the default of 1.0 for:
700  // 0: Unspecified Units
701  // 3: miles
702  // 7: kilometers
703  // 15: decameters
704  // 16: hectometers
705  // 17: gigameters
706  // 18: AU
707  // 19: lightyears
708  // 20: parsecs
709  m_DXF2mm = 1.0;
710  break;
711  }
712 
713  return;
714  }
715 }
716 
717 
718 void DXF_IMPORT_PLUGIN::setVariableString( const std::string& key, const std::string& value,
719  int code )
720 {
721  // Called for every string variable in the DXF file (e.g. "$ACADVER").
722 }
723 
724 
725 wxString DXF_IMPORT_PLUGIN::toDxfString( const wxString& aStr )
726 {
727  wxString res;
728  int j = 0;
729 
730  for( unsigned i = 0; i<aStr.length(); ++i )
731  {
732  int c = aStr[i];
733 
734  if( c > 175 || c < 11 )
735  {
736  res.append( aStr.Mid( j, i - j ) );
737  j = i;
738 
739  switch( c )
740  {
741  case 0x0A:
742  res += wxT( "\\P" );
743  break;
744 
745  // diameter:
746 #ifdef __WINDOWS_
747  // windows, as always, is special.
748  case 0x00D8:
749 #else
750  case 0x2205:
751 #endif
752  res += wxT( "%%C" );
753  break;
754 
755  // degree:
756  case 0x00B0:
757  res += wxT( "%%D" );
758  break;
759 
760  // plus/minus
761  case 0x00B1:
762  res += wxT( "%%P" );
763  break;
764 
765  default:
766  j--;
767  break;
768  }
769 
770  j++;
771  }
772  }
773 
774  res.append( aStr.Mid( j ) );
775  return res;
776 }
777 
778 
779 wxString DXF_IMPORT_PLUGIN::toNativeString( const wxString& aData )
780 {
781  wxString res;
782 
783  // Ignore font tags:
784  int j = 0;
785 
786  for( unsigned i = 0; i < aData.length(); ++i )
787  {
788  if( aData[ i ] == 0x7B ) // is '{' ?
789  {
790  if( aData[ i + 1 ] == 0x5c && aData[ i + 2 ] == 0x66 ) // is "\f" ?
791  {
792  // found font tag, append parsed part
793  res.append( aData.Mid( j, i - j ) );
794 
795  // skip to ';'
796  for( unsigned k = i + 3; k < aData.length(); ++k )
797  {
798  if( aData[ k ] == 0x3B )
799  {
800  i = j = ++k;
801  break;
802  }
803  }
804 
805  // add to '}'
806  for( unsigned k = i; k < aData.length(); ++k )
807  {
808  if( aData[ k ] == 0x7D )
809  {
810  res.append( aData.Mid( i, k - i ) );
811  i = j = ++k;
812  break;
813  }
814  }
815  }
816  }
817  }
818 
819  res.append( aData.Mid( j ) );
820 
821 #if 1
822  wxRegEx regexp;
823  // Line feed:
824  regexp.Compile( wxT( "\\\\P" ) );
825  regexp.Replace( &res, wxT( "\n" ) );
826 
827  // Space:
828  regexp.Compile( wxT( "\\\\~" ) );
829  regexp.Replace( &res, wxT( " " ) );
830 
831  // diameter:
832  regexp.Compile( wxT( "%%[cC]" ) );
833 #ifdef __WINDOWS__
834  // windows, as always, is special.
835  regexp.Replace( &res, wxChar( 0xD8 ) );
836 #else
837  // Empty_set, diameter is 0x2300
838  regexp.Replace( &res, wxChar( 0x2205 ) );
839 #endif
840 
841  // degree:
842  regexp.Compile( wxT( "%%[dD]" ) );
843  regexp.Replace( &res, wxChar( 0x00B0 ) );
844  // plus/minus
845  regexp.Compile( wxT( "%%[pP]" ) );
846  regexp.Replace( &res, wxChar( 0x00B1 ) );
847 #endif
848 
849  return res;
850 }
851 
852 
853 void DXF_IMPORT_PLUGIN::addTextStyle( const DL_StyleData& aData )
854 {
855  // TODO
856 }
857 
858 
860  const VECTOR2D& aSegEnd, int aWidth )
861 {
862  VECTOR2D origin( SCALE_FACTOR( aSegStart.x ), SCALE_FACTOR( aSegStart.y ) );
863  VECTOR2D end( SCALE_FACTOR( aSegEnd.x ), SCALE_FACTOR( aSegEnd.y ) );
864  m_internalImporter.AddLine( origin, end, aWidth );
865 
866  updateImageLimits( origin );
867  updateImageLimits( end );
868 }
869 
870 
871 void DXF_IMPORT_PLUGIN::insertArc( const VECTOR2D& aSegStart, const VECTOR2D& aSegEnd,
872  double aBulge, int aWidth )
873 {
874  VECTOR2D segment_startpoint( SCALE_FACTOR( aSegStart.x ), SCALE_FACTOR( aSegStart.y ) );
875  VECTOR2D segment_endpoint( SCALE_FACTOR( aSegEnd.x ), SCALE_FACTOR( aSegEnd.y ) );
876 
877  // ensure aBulge represents an angle from +/- ( 0 .. approx 359.8 deg )
878  if( aBulge < -2000.0 )
879  aBulge = -2000.0;
880  else if( aBulge > 2000.0 )
881  aBulge = 2000.0;
882 
883  double ang = 4.0 * atan( aBulge );
884 
885  // reflect the Y values to put everything in a RHCS
886  VECTOR2D sp( aSegStart.x, -aSegStart.y );
887  VECTOR2D ep( aSegEnd.x, -aSegEnd.y );
888  // angle from end->start
889  double offAng = atan2( ep.y - sp.y, ep.x - sp.x );
890  // length of subtended segment = 1/2 distance between the 2 points
891  double d = 0.5 * sqrt( (sp.x - ep.x) * (sp.x - ep.x) + (sp.y - ep.y) * (sp.y - ep.y) );
892  // midpoint of the subtended segment
893  double xm = ( sp.x + ep.x ) * 0.5;
894  double ym = ( sp.y + ep.y ) * 0.5;
895  double radius = d / sin( ang * 0.5 );
896 
897  if( radius < 0.0 )
898  radius = -radius;
899 
900  // calculate the height of the triangle with base d and hypotenuse r
901  double dh2 = radius * radius - d * d;
902 
903  // this should only ever happen due to rounding errors when r == d
904  if( dh2 < 0.0 )
905  dh2 = 0.0;
906 
907  double h = sqrt( dh2 );
908 
909  if( ang < 0.0 )
910  offAng -= M_PI_2;
911  else
912  offAng += M_PI_2;
913 
914  // for angles greater than 180 deg we need to flip the
915  // direction in which the arc center is found relative
916  // to the midpoint of the subtended segment.
917  if( ang < -M_PI )
918  offAng += M_PI;
919  else if( ang > M_PI )
920  offAng -= M_PI;
921 
922  // center point
923  double cx = h * cos( offAng ) + xm;
924  double cy = h * sin( offAng ) + ym;
925  VECTOR2D center( SCALE_FACTOR( cx ), SCALE_FACTOR( -cy ) );
926  VECTOR2D arc_start;
927  double angle = RAD2DECIDEG( ang );
928 
929  if( ang < 0.0 )
930  {
931  arc_start = VECTOR2D( SCALE_FACTOR( ep.x ), SCALE_FACTOR( -ep.y ) );
932  }
933  else
934  {
935  arc_start = VECTOR2D( SCALE_FACTOR( sp.x ), SCALE_FACTOR( -sp.y ) );
936  angle = -angle;
937  }
938 
939  m_internalImporter.AddArc( center, arc_start, angle, aWidth );
940 
941  VECTOR2D radiusDelta( SCALE_FACTOR( radius ), SCALE_FACTOR( radius ) );
942 
943  updateImageLimits( center + radiusDelta );
944  updateImageLimits( center - radiusDelta );
945  return;
946 }
947 
948 
949 #include "tinyspline_lib/tinysplinecpp.h"
950 
952 {
953  #if 0 // Debug only
954  wxLogMessage("spl deg %d kn %d ctr %d fit %d",
959  #endif
960 
961  unsigned imax = m_curr_entity.m_SplineControlPointList.size();
962 
963  if( imax < 2 ) // malformed spline
964  return;
965 
966 #if 0 // set to 1 to approximate the spline by segments between 2 control points
967  VECTOR2D startpoint( mapX( m_curr_entity.m_SplineControlPointList[0].m_x ),
969 
970  for( unsigned int ii = 1; ii < imax; ++ii )
971  {
974 
975  if( startpoint != endpoint )
976  {
977  m_internalImporter.AddLine( startpoint, endpoint );
978 
979  updateImageLimits( startpoint );
980  updateImageLimits( endpoint );
981 
982  startpoint = endpoint;
983  }
984  }
985 #else // Use bezier curves, supported by pcbnew, to approximate the spline
986  tinyspline::BSpline dxfspline( m_curr_entity.m_SplineControlPointList.size(),
987  /* coord dim */ 2, m_curr_entity.m_SplineDegree );
988  std::vector<double> ctrlp;
989 
990  for( unsigned ii = 0; ii < imax; ++ii )
991  {
992  ctrlp.push_back( m_curr_entity.m_SplineControlPointList[ii].m_x );
993  ctrlp.push_back( m_curr_entity.m_SplineControlPointList[ii].m_y );
994  }
995 
996  dxfspline.setCtrlp( ctrlp );
997  dxfspline.setKnots( m_curr_entity.m_SplineKnotsList );
998  tinyspline::BSpline beziers( dxfspline.toBeziers() );
999 
1000  std::vector<double> coords = beziers.ctrlp();
1001 
1002  // Each Bezier curve uses 4 vertices (a start point, 2 control points and a end point).
1003  // So we can have more than one Bezier curve ( there are one curve each four vertices)
1004  for( unsigned ii = 0; ii < coords.size(); ii += 8 )
1005  {
1006  VECTOR2D start( mapX( coords[ii] ), mapY( coords[ii+1] ) );
1007  VECTOR2D bezierControl1( mapX( coords[ii+2] ), mapY( coords[ii+3] ) );
1008  VECTOR2D bezierControl2( mapX( coords[ii+4] ), mapY( coords[ii+5] ) );
1009  VECTOR2D end( mapX( coords[ii+6] ), mapY( coords[ii+7] ) );
1010  m_internalImporter.AddSpline( start, bezierControl1, bezierControl2, end , aWidth );
1011  }
1012 #endif
1013 }
1014 
1015 
1017 {
1018  m_minX = std::min( aPoint.x, m_minX );
1019  m_maxX = std::max( aPoint.x, m_maxX );
1020 
1021  m_minY = std::min( aPoint.y, m_minY );
1022  m_maxY = std::max( aPoint.y, m_maxY );
1023 }
DXF2BRD_ENTITY_DATA m_curr_entity
void insertArc(const VECTOR2D &aSegStart, const VECTOR2D &aSegEnd, double aBulge, int aWidth)
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:62
void AddLine(const VECTOR2D &aStart, const VECTOR2D &aEnd, double aWidth) override
Creates an object representing a line segment.
double GetImageHeight() const override
Return image height from original imported file.
TEXTE_PCB class definition.
virtual void addTextStyle(const DL_StyleData &aData) override
virtual void addLine(const DL_LineData &aData) override
virtual void addPolyline(const DL_PolylineData &aData) override
static wxString toDxfString(const wxString &aStr)
Converts a native unicode string into a DXF encoded string.
#define MIN_BULGE
unsigned int m_SplineFitCount
Class BOARD to handle a board.
double GetImageWidth() const override
Return image width from original imported file.
void AddArc(const VECTOR2D &aCenter, const VECTOR2D &aStart, double aAngle, double aWidth) override
Creates an object representing an arc.
bool Load(const wxString &aFileName) override
Loads file for import.
unsigned int m_SplineControlCount
A helper class to store a spline control point (in X,Y plane only)
double mapX(double aDxfCoordX)
virtual void addFitPoint(const DL_FitPointData &aData) override
Called for every spline fit point.
double RAD2DECIDEG(double rad)
Definition: trigo.h:204
virtual void addSpline(const DL_SplineData &aData) override
Called for every spline.
std::vector< VECTOR2D > m_SplineFitPointList
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:216
#define abs(a)
Definition: auxiliary.h:84
void updateImageLimits(const VECTOR2D &aPoint)
#define SCALE_FACTOR(x)
This file contains miscellaneous commonly used macros and functions.
virtual void endEntity() override
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
virtual void setVariableInt(const std::string &key, int value, int code) override
Called for every int variable in the DXF file (e.g.
bool Import() override
Actually imports the file.
Footprint text class description.
void AddSpline(const VECTOR2D &aStart, const VECTOR2D &BezierControl1, const VECTOR2D &BezierControl2, const VECTOR2D &aEnd, double aWidth) override
Creates an object representing an arc.
VECTOR2< double > VECTOR2D
Definition: vector2d.h:586
virtual void addVertex(const DL_VertexData &aData) override
Called for every polyline vertex.
unsigned int m_SplineKnotsCount
unsigned int m_SplineDegree
virtual void addKnot(const DL_KnotData &aData) override
Called for every spline knot value.
static wxString toNativeString(const wxString &aData)
Converts a DXF encoded string into a native Unicode string.
void AddCircle(const VECTOR2D &aCenter, double aRadius, double aWidth) override
Creates an object representing a circle.
double mapDim(double aDxfValue)
void AddText(const VECTOR2D &aOrigin, const wxString &aText, double aHeight, double aWidth, double aThickness, double aOrientation, EDA_TEXT_HJUSTIFY_T aHJustify, EDA_TEXT_VJUSTIFY_T aVJustify) override
Creates an object representing a text.
EDA_TEXT_VJUSTIFY_T
Definition: eda_text.h:69
double mapWidth(double aDxfWidth)
virtual void addControlPoint(const DL_ControlPointData &aData) override
Called for every spline control point.
virtual void addMText(const DL_MTextData &aData) override
Class to handle a graphic segment.
const char * name
Definition: DXF_plotter.cpp:61
#define max(a, b)
Definition: auxiliary.h:86
#define DXF2ANGLEUI
void insertSpline(int aWidth)
bool ImportDxfFile(const wxString &aFile)
Implementation of the method used for communicate with this filter.
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
size_t i
Definition: json11.cpp:597
void insertLine(const VECTOR2D &aSegStart, const VECTOR2D &aSegEnd, int aWidth)
The common library.
GRAPHICS_IMPORTER * m_importer
Importer used to create objects representing the imported shapes.
virtual void addLayer(const DL_LayerData &aData) override
virtual void addArc(const DL_ArcData &aData) override
double mapY(double aDxfCoordY)
virtual void addText(const DL_TextData &aData) override
GRAPHICS_IMPORTER_BUFFER m_internalImporter
EDGE_MODULE class definition.
std::vector< double > m_SplineKnotsList
void ImportTo(GRAPHICS_IMPORTER &aImporter)
virtual void setVariableString(const std::string &key, const std::string &value, int code) override
Called for every string variable in the DXF file (e.g.
std::vector< SPLINE_CTRL_POINT > m_SplineControlPointList
void reportMsg(const char *aMessage)
#define min(a, b)
Definition: auxiliary.h:85
virtual void addCircle(const DL_CircleData &aData) override