You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
4.4 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2016 Cirilo Bernardo <cirilo.bernardo@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include "kicadcurve.h"
  24. #include <sexpr/sexpr.h>
  25. #include <wx/log.h>
  26. #include <iostream>
  27. #include <math.h>
  28. #include <sstream>
  29. KICADCURVE::KICADCURVE()
  30. {
  31. m_form = CURVE_NONE;
  32. m_angle = 0.0;
  33. m_radius = 0.0;
  34. m_layer = LAYER_NONE;
  35. m_startangle = 0.0;
  36. m_endangle = 0.0;
  37. return;
  38. }
  39. KICADCURVE::~KICADCURVE()
  40. {
  41. return;
  42. }
  43. bool KICADCURVE::Read( SEXPR::SEXPR* aEntry, CURVE_TYPE aCurveType )
  44. {
  45. if( CURVE_LINE != aCurveType && CURVE_ARC != aCurveType && CURVE_CIRCLE != aCurveType )
  46. {
  47. std::ostringstream ostr;
  48. ostr << "* Unsupported curve type: " << aCurveType;
  49. wxLogMessage( "%s\n", ostr.str().c_str() );
  50. return false;
  51. }
  52. m_form = aCurveType;
  53. int nchild = aEntry->GetNumberOfChildren();
  54. if( ( CURVE_CIRCLE == aCurveType && nchild < 5 )
  55. || ( CURVE_ARC == aCurveType && nchild < 6 )
  56. || ( CURVE_LINE == aCurveType && nchild < 5 ) )
  57. {
  58. std::ostringstream ostr;
  59. ostr << "* bad curve data; not enough parameters";
  60. wxLogMessage( "%s\n", ostr.str().c_str() );
  61. return false;
  62. }
  63. SEXPR::SEXPR* child;
  64. std::string text;
  65. for( int i = 1; i < nchild; ++i )
  66. {
  67. child = aEntry->GetChild( i );
  68. if( !child->IsList() )
  69. continue;
  70. text = child->GetChild( 0 )->GetSymbol();
  71. if( text == "start" || text == "center" )
  72. {
  73. if( !Get2DCoordinate( child, m_start ) )
  74. return false;
  75. }
  76. else if( text == "end" )
  77. {
  78. if( !Get2DCoordinate( child, m_end ) )
  79. return false;
  80. }
  81. else if( text == "angle" )
  82. {
  83. if( child->GetNumberOfChildren() < 2
  84. || ( !child->GetChild( 1 )->IsDouble()
  85. && !child->GetChild( 1 )->IsInteger() ) )
  86. {
  87. std::ostringstream ostr;
  88. ostr << "* bad angle data";
  89. wxLogMessage( "%s\n", ostr.str().c_str() );
  90. return false;
  91. }
  92. if( child->GetChild( 1 )->IsDouble() )
  93. m_angle = child->GetChild( 1 )->GetDouble();
  94. else
  95. m_angle = child->GetChild( 1 )->GetInteger();
  96. m_angle = m_angle / 180.0 * M_PI;
  97. }
  98. else if( text == "layer" )
  99. {
  100. const OPT<std::string> layer = GetLayerName( *child );
  101. if( !layer )
  102. {
  103. std::ostringstream ostr;
  104. ostr << "* bad layer data: " << child->AsString();
  105. wxLogMessage( "%s\n", ostr.str().c_str() );
  106. return false;
  107. }
  108. // NOTE: for the moment we only process Edge.Cuts
  109. if( *layer == "Edge.Cuts" )
  110. m_layer = LAYER_EDGE;
  111. }
  112. }
  113. return true;
  114. }
  115. std::string KICADCURVE::Describe() const
  116. {
  117. std::ostringstream desc;
  118. switch( m_form )
  119. {
  120. case CURVE_LINE:
  121. desc << "line start: " << m_start << " end: " << m_end;
  122. break;
  123. case CURVE_ARC:
  124. desc << "arc center: " << m_start << " radius: " << m_radius
  125. << " angle: " << 180.0 * m_angle / M_PI;
  126. break;
  127. case CURVE_CIRCLE:
  128. desc << "circle center: " << m_start << " radius: " << m_radius;
  129. break;
  130. default:
  131. desc << "<invalid curve type>";
  132. break;
  133. }
  134. return desc.str();
  135. }