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.

398 lines
11 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2004-2020 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. #include <fctsys.h>
  25. #include <gr_basic.h>
  26. #include <macros.h>
  27. #include <sch_draw_panel.h>
  28. #include <eda_draw_frame.h>
  29. #include <plotter.h>
  30. #include <trigo.h>
  31. #include <base_units.h>
  32. #include <msgpanel.h>
  33. #include <bitmaps.h>
  34. #include <eda_draw_frame.h>
  35. #include <general.h>
  36. #include <lib_polyline.h>
  37. #include <settings/color_settings.h>
  38. #include <transform.h>
  39. LIB_POLYLINE::LIB_POLYLINE( LIB_PART* aParent ) :
  40. LIB_ITEM( LIB_POLYLINE_T, aParent )
  41. {
  42. m_Fill = NO_FILL;
  43. m_Width = 0;
  44. m_isFillable = true;
  45. }
  46. EDA_ITEM* LIB_POLYLINE::Clone() const
  47. {
  48. return new LIB_POLYLINE( *this );
  49. }
  50. int LIB_POLYLINE::compare( const LIB_ITEM& aOther, LIB_ITEM::COMPARE_FLAGS aCompareFlags ) const
  51. {
  52. wxASSERT( aOther.Type() == LIB_POLYLINE_T );
  53. int retv = LIB_ITEM::compare( aOther );
  54. if( retv )
  55. return retv;
  56. const LIB_POLYLINE* tmp = (LIB_POLYLINE*) &aOther;
  57. if( m_PolyPoints.size() != tmp->m_PolyPoints.size() )
  58. return m_PolyPoints.size() - tmp->m_PolyPoints.size();
  59. for( size_t i = 0; i < m_PolyPoints.size(); i++ )
  60. {
  61. if( m_PolyPoints[i].x != tmp->m_PolyPoints[i].x )
  62. return m_PolyPoints[i].x - tmp->m_PolyPoints[i].x;
  63. if( m_PolyPoints[i].y != tmp->m_PolyPoints[i].y )
  64. return m_PolyPoints[i].y - tmp->m_PolyPoints[i].y;
  65. }
  66. return 0;
  67. }
  68. void LIB_POLYLINE::Offset( const wxPoint& aOffset )
  69. {
  70. for( wxPoint& point : m_PolyPoints )
  71. point += aOffset;
  72. }
  73. void LIB_POLYLINE::MoveTo( const wxPoint& aPosition )
  74. {
  75. Offset( aPosition - m_PolyPoints[ 0 ] );
  76. }
  77. void LIB_POLYLINE::MirrorHorizontal( const wxPoint& aCenter )
  78. {
  79. for( wxPoint& point : m_PolyPoints )
  80. {
  81. point.x -= aCenter.x;
  82. point.x *= -1;
  83. point.x += aCenter.x;
  84. }
  85. }
  86. void LIB_POLYLINE::MirrorVertical( const wxPoint& aCenter )
  87. {
  88. for( wxPoint& point : m_PolyPoints )
  89. {
  90. point.y -= aCenter.y;
  91. point.y *= -1;
  92. point.y += aCenter.y;
  93. }
  94. }
  95. void LIB_POLYLINE::Rotate( const wxPoint& aCenter, bool aRotateCCW )
  96. {
  97. for( wxPoint& point : m_PolyPoints )
  98. RotatePoint( &point, aCenter, aRotateCCW ? -900 : 900 );
  99. }
  100. void LIB_POLYLINE::Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
  101. const TRANSFORM& aTransform )
  102. {
  103. wxASSERT( aPlotter != NULL );
  104. static std::vector< wxPoint > cornerList;
  105. cornerList.clear();
  106. for( wxPoint pos : m_PolyPoints )
  107. {
  108. pos = aTransform.TransformCoordinate( pos ) + aOffset;
  109. cornerList.push_back( pos );
  110. }
  111. if( aFill && m_Fill == FILLED_WITH_BG_BODYCOLOR )
  112. {
  113. aPlotter->SetColor( aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE_BACKGROUND ) );
  114. aPlotter->PlotPoly( cornerList, FILLED_WITH_BG_BODYCOLOR, 0 );
  115. }
  116. bool already_filled = m_Fill == FILLED_WITH_BG_BODYCOLOR;
  117. int pen_size = GetPenWidth();
  118. if( !already_filled || pen_size > 0 )
  119. {
  120. pen_size = std::max( pen_size, aPlotter->RenderSettings()->GetDefaultPenWidth() );
  121. aPlotter->SetColor( aPlotter->RenderSettings()->GetLayerColor( LAYER_DEVICE ) );
  122. aPlotter->PlotPoly( cornerList, already_filled ? NO_FILL : m_Fill, pen_size );
  123. }
  124. }
  125. void LIB_POLYLINE::AddPoint( const wxPoint& aPosition )
  126. {
  127. m_PolyPoints.push_back( aPosition );
  128. }
  129. void LIB_POLYLINE::AddCorner( const wxPoint& aPosition )
  130. {
  131. int currentMinDistance = INT_MAX;
  132. int closestLineStart = 0;
  133. for( unsigned i = 0; i < m_PolyPoints.size() - 1; ++i )
  134. {
  135. int distance = (int) DistanceLinePoint( m_PolyPoints[i], m_PolyPoints[i + 1], aPosition );
  136. if( distance < currentMinDistance )
  137. {
  138. currentMinDistance = distance;
  139. closestLineStart = i;
  140. }
  141. }
  142. m_PolyPoints.insert( m_PolyPoints.begin() + closestLineStart, aPosition );
  143. }
  144. void LIB_POLYLINE::RemoveCorner( int aIdx )
  145. {
  146. m_PolyPoints.erase( m_PolyPoints.begin() + aIdx );
  147. }
  148. int LIB_POLYLINE::GetPenWidth() const
  149. {
  150. // Historically 0 meant "default width" and negative numbers meant "don't stroke".
  151. if( m_Width < 0 && GetFillMode() != NO_FILL )
  152. return 0;
  153. else
  154. return std::max( m_Width, 1 );
  155. }
  156. void LIB_POLYLINE::print( RENDER_SETTINGS* aSettings, const wxPoint& aOffset, void* aData,
  157. const TRANSFORM& aTransform )
  158. {
  159. bool forceNoFill = static_cast<bool>( aData );
  160. int penWidth = GetPenWidth();
  161. if( forceNoFill && m_Fill != NO_FILL && penWidth == 0 )
  162. return;
  163. wxDC* DC = aSettings->GetPrintDC();
  164. COLOR4D color = aSettings->GetLayerColor( LAYER_DEVICE );
  165. wxPoint* buffer = new wxPoint[ m_PolyPoints.size() ];
  166. for( unsigned ii = 0; ii < m_PolyPoints.size(); ii++ )
  167. buffer[ii] = aTransform.TransformCoordinate( m_PolyPoints[ii] ) + aOffset;
  168. if( forceNoFill || m_Fill == NO_FILL )
  169. {
  170. penWidth = std::max( penWidth, aSettings->GetDefaultPenWidth() );
  171. GRPoly( nullptr, DC, m_PolyPoints.size(), buffer, false, penWidth, color, color );
  172. }
  173. else
  174. {
  175. if( m_Fill == FILLED_WITH_BG_BODYCOLOR )
  176. color = aSettings->GetLayerColor( LAYER_DEVICE_BACKGROUND );
  177. GRPoly( nullptr, DC, m_PolyPoints.size(), buffer, true, penWidth, color, color );
  178. }
  179. delete[] buffer;
  180. }
  181. bool LIB_POLYLINE::HitTest( const wxPoint& aPosition, int aAccuracy ) const
  182. {
  183. int delta = std::max( aAccuracy + GetPenWidth() / 2, Mils2iu( MINIMUM_SELECTION_DISTANCE ) );
  184. SHAPE_LINE_CHAIN shape;
  185. for( wxPoint pt : m_PolyPoints )
  186. shape.Append( DefaultTransform.TransformCoordinate( pt ) );
  187. if( m_Fill != NO_FILL && m_PolyPoints.size() > 2 )
  188. {
  189. shape.SetClosed( true );
  190. return shape.PointInside( aPosition, delta );
  191. }
  192. else
  193. return shape.PointOnEdge( aPosition, delta );
  194. }
  195. bool LIB_POLYLINE::HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy ) const
  196. {
  197. if( m_Flags & ( STRUCT_DELETED | SKIP_STRUCT ) )
  198. return false;
  199. EDA_RECT sel = aRect;
  200. if ( aAccuracy )
  201. sel.Inflate( aAccuracy );
  202. if( aContained )
  203. return sel.Contains( GetBoundingBox() );
  204. // Fast test: if rect is outside the polygon bounding box, then they cannot intersect
  205. if( !sel.Intersects( GetBoundingBox() ) )
  206. return false;
  207. // Account for the width of the line
  208. sel.Inflate( ( GetPenWidth() / 2 ) + 1 );
  209. // Only test closing segment if the polyline is filled
  210. int count = m_Fill == NO_FILL ? m_PolyPoints.size() - 1 : m_PolyPoints.size();
  211. for( int ii = 0; ii < count; ii++ )
  212. {
  213. wxPoint pt = DefaultTransform.TransformCoordinate( m_PolyPoints[ ii ] );
  214. wxPoint ptNext = DefaultTransform.TransformCoordinate( m_PolyPoints[ (ii+1) % count ] );
  215. // Test if the point is within aRect
  216. if( sel.Contains( pt ) )
  217. return true;
  218. // Test if this edge intersects aRect
  219. if( sel.Intersects( pt, ptNext ) )
  220. return true;
  221. }
  222. return false;
  223. }
  224. const EDA_RECT LIB_POLYLINE::GetBoundingBox() const
  225. {
  226. EDA_RECT rect;
  227. int xmin, xmax, ymin, ymax;
  228. xmin = xmax = m_PolyPoints[0].x;
  229. ymin = ymax = m_PolyPoints[0].y;
  230. for( unsigned ii = 1; ii < GetCornerCount(); ii++ )
  231. {
  232. xmin = std::min( xmin, m_PolyPoints[ii].x );
  233. xmax = std::max( xmax, m_PolyPoints[ii].x );
  234. ymin = std::min( ymin, m_PolyPoints[ii].y );
  235. ymax = std::max( ymax, m_PolyPoints[ii].y );
  236. }
  237. rect.SetOrigin( xmin, ymin );
  238. rect.SetEnd( xmax, ymax );
  239. rect.Inflate( ( GetPenWidth() / 2 ) + 1 );
  240. rect.RevertYAxis();
  241. return rect;
  242. }
  243. void LIB_POLYLINE::DeleteSegment( const wxPoint aPosition )
  244. {
  245. // First segment is kept, only its end point is changed
  246. while( GetCornerCount() > 2 )
  247. {
  248. m_PolyPoints.pop_back();
  249. if( m_PolyPoints[ GetCornerCount() - 1 ] != aPosition )
  250. {
  251. m_PolyPoints[ GetCornerCount() - 1 ] = aPosition;
  252. break;
  253. }
  254. }
  255. }
  256. void LIB_POLYLINE::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, MSG_PANEL_ITEMS& aList )
  257. {
  258. wxString msg;
  259. EDA_RECT bBox = GetBoundingBox();
  260. LIB_ITEM::GetMsgPanelInfo( aFrame, aList );
  261. msg = MessageTextFromValue( aFrame->GetUserUnits(), m_Width, true );
  262. aList.push_back( MSG_PANEL_ITEM( _( "Line Width" ), msg, BLUE ) );
  263. msg.Printf( wxT( "(%d, %d, %d, %d)" ), bBox.GetOrigin().x,
  264. bBox.GetOrigin().y, bBox.GetEnd().x, bBox.GetEnd().y );
  265. aList.push_back( MSG_PANEL_ITEM( _( "Bounding Box" ), msg, BROWN ) );
  266. }
  267. wxString LIB_POLYLINE::GetSelectMenuText( EDA_UNITS aUnits ) const
  268. {
  269. return wxString::Format( _( "Polyline at (%s, %s) with %d points" ),
  270. MessageTextFromValue( aUnits, m_PolyPoints[0].x ),
  271. MessageTextFromValue( aUnits, m_PolyPoints[0].y ),
  272. int( m_PolyPoints.size() ) );
  273. }
  274. BITMAP_DEF LIB_POLYLINE::GetMenuImage() const
  275. {
  276. return add_graphical_segments_xpm;
  277. }
  278. void LIB_POLYLINE::BeginEdit( const wxPoint aPosition )
  279. {
  280. m_PolyPoints.push_back( aPosition ); // Start point of first segment.
  281. m_PolyPoints.push_back( aPosition ); // End point of first segment.
  282. }
  283. bool LIB_POLYLINE::ContinueEdit( const wxPoint aPosition )
  284. {
  285. // do not add zero length segments
  286. if( m_PolyPoints[m_PolyPoints.size() - 2] != m_PolyPoints.back() )
  287. m_PolyPoints.push_back( aPosition );
  288. return true;
  289. }
  290. void LIB_POLYLINE::EndEdit()
  291. {
  292. // do not include last point twice
  293. if( m_PolyPoints.size() > 2 )
  294. {
  295. if( m_PolyPoints[ m_PolyPoints.size() - 2 ] == m_PolyPoints.back() )
  296. m_PolyPoints.pop_back();
  297. }
  298. }
  299. void LIB_POLYLINE::CalcEdit( const wxPoint& aPosition )
  300. {
  301. m_PolyPoints[ GetCornerCount() - 1 ] = aPosition;
  302. }