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.

1093 lines
32 KiB

  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) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  6. * Copyright (C) 2011 Wayne Stambaugh <stambaughw@gmail.com>
  7. * Copyright The 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. #include "pcb_shape.h"
  27. #include <google/protobuf/any.pb.h>
  28. #include <magic_enum.hpp>
  29. #include <bitmaps.h>
  30. #include <macros.h>
  31. #include <pcb_edit_frame.h>
  32. #include <board_design_settings.h>
  33. #include <board.h>
  34. #include <footprint.h>
  35. #include <lset.h>
  36. #include <pad.h>
  37. #include <base_units.h>
  38. #include <geometry/shape_circle.h>
  39. #include <geometry/shape_compound.h>
  40. #include <geometry/point_types.h>
  41. #include <geometry/shape_utils.h>
  42. #include <pcb_painter.h>
  43. #include <api/board/board_types.pb.h>
  44. #include <api/api_enums.h>
  45. #include <api/api_utils.h>
  46. PCB_SHAPE::PCB_SHAPE( BOARD_ITEM* aParent, KICAD_T aItemType, SHAPE_T aShapeType ) :
  47. BOARD_CONNECTED_ITEM( aParent, aItemType ),
  48. EDA_SHAPE( aShapeType, pcbIUScale.mmToIU( DEFAULT_LINE_WIDTH ), FILL_T::NO_FILL )
  49. {
  50. m_hasSolderMask = false;
  51. }
  52. PCB_SHAPE::PCB_SHAPE( BOARD_ITEM* aParent, SHAPE_T shapetype ) :
  53. BOARD_CONNECTED_ITEM( aParent, PCB_SHAPE_T ),
  54. EDA_SHAPE( shapetype, pcbIUScale.mmToIU( DEFAULT_LINE_WIDTH ), FILL_T::NO_FILL )
  55. {
  56. m_hasSolderMask = false;
  57. }
  58. PCB_SHAPE::~PCB_SHAPE()
  59. {
  60. }
  61. void PCB_SHAPE::CopyFrom( const BOARD_ITEM* aOther )
  62. {
  63. wxCHECK( aOther && aOther->Type() == PCB_SHAPE_T, /* void */ );
  64. *this = *static_cast<const PCB_SHAPE*>( aOther );
  65. }
  66. void PCB_SHAPE::Serialize( google::protobuf::Any &aContainer ) const
  67. {
  68. using namespace kiapi::common;
  69. using namespace kiapi::board::types;
  70. BoardGraphicShape msg;
  71. msg.set_layer( ToProtoEnum<PCB_LAYER_ID, BoardLayer>( GetLayer() ) );
  72. msg.mutable_net()->mutable_code()->set_value( GetNetCode() );
  73. msg.mutable_net()->set_name( GetNetname() );
  74. msg.mutable_id()->set_value( m_Uuid.AsStdString() );
  75. msg.set_locked( IsLocked() ? types::LockedState::LS_LOCKED : types::LockedState::LS_UNLOCKED );
  76. google::protobuf::Any any;
  77. EDA_SHAPE::Serialize( any );
  78. any.UnpackTo( msg.mutable_shape() );
  79. // TODO m_hasSolderMask and m_solderMaskMargin
  80. aContainer.PackFrom( msg );
  81. }
  82. bool PCB_SHAPE::Deserialize( const google::protobuf::Any &aContainer )
  83. {
  84. using namespace kiapi::common;
  85. using namespace kiapi::board::types;
  86. BoardGraphicShape msg;
  87. if( !aContainer.UnpackTo( &msg ) )
  88. return false;
  89. // Initialize everything to a known state that doesn't get touched by every
  90. // codepath below, to make sure the equality operator is consistent
  91. m_start = {};
  92. m_end = {};
  93. m_arcCenter = {};
  94. m_arcMidData = {};
  95. m_bezierC1 = {};
  96. m_bezierC2 = {};
  97. m_editState = 0;
  98. m_proxyItem = false;
  99. m_endsSwapped = false;
  100. const_cast<KIID&>( m_Uuid ) = KIID( msg.id().value() );
  101. SetLocked( msg.locked() == types::LS_LOCKED );
  102. SetLayer( FromProtoEnum<PCB_LAYER_ID, BoardLayer>( msg.layer() ) );
  103. SetNetCode( msg.net().code().value() );
  104. google::protobuf::Any any;
  105. any.PackFrom( msg.shape() );
  106. EDA_SHAPE::Deserialize( any );
  107. // TODO m_hasSolderMask and m_solderMaskMargin
  108. return true;
  109. }
  110. bool PCB_SHAPE::IsType( const std::vector<KICAD_T>& aScanTypes ) const
  111. {
  112. if( BOARD_ITEM::IsType( aScanTypes ) )
  113. return true;
  114. bool sametype = false;
  115. for( KICAD_T scanType : aScanTypes )
  116. {
  117. if( scanType == PCB_LOCATE_BOARD_EDGE_T )
  118. sametype = m_layer == Edge_Cuts;
  119. else if( scanType == PCB_SHAPE_LOCATE_ARC_T )
  120. sametype = m_shape == SHAPE_T::ARC;
  121. else if( scanType == PCB_SHAPE_LOCATE_CIRCLE_T )
  122. sametype = m_shape == SHAPE_T::CIRCLE;
  123. else if( scanType == PCB_SHAPE_LOCATE_RECT_T )
  124. sametype = m_shape == SHAPE_T::RECTANGLE;
  125. else if( scanType == PCB_SHAPE_LOCATE_SEGMENT_T )
  126. sametype = m_shape == SHAPE_T::SEGMENT;
  127. else if( scanType == PCB_SHAPE_LOCATE_POLY_T )
  128. sametype = m_shape == SHAPE_T::POLY;
  129. else if( scanType == PCB_SHAPE_LOCATE_BEZIER_T )
  130. sametype = m_shape == SHAPE_T::BEZIER;
  131. if( sametype )
  132. return true;
  133. }
  134. return false;
  135. }
  136. bool PCB_SHAPE::IsConnected() const
  137. {
  138. // Only board-level copper shapes are connectable
  139. return IsOnCopperLayer() && !GetParentFootprint();
  140. }
  141. void PCB_SHAPE::SetLayer( PCB_LAYER_ID aLayer )
  142. {
  143. BOARD_ITEM::SetLayer( aLayer );
  144. if( !IsOnCopperLayer() )
  145. SetNetCode( -1 );
  146. }
  147. int PCB_SHAPE::GetSolderMaskExpansion() const
  148. {
  149. int margin = m_solderMaskMargin.value_or( 0 );
  150. // If no local margin is set, get the board's solder mask expansion value
  151. if( !m_solderMaskMargin.has_value() )
  152. {
  153. const BOARD* board = GetBoard();
  154. if( board )
  155. margin = board->GetDesignSettings().m_SolderMaskExpansion;
  156. }
  157. // Ensure the resulting mask opening has a non-negative size
  158. if( margin < 0 && !IsSolidFill() )
  159. margin = std::max( margin, -GetWidth() / 2 );
  160. return margin;
  161. }
  162. bool PCB_SHAPE::IsOnLayer( PCB_LAYER_ID aLayer ) const
  163. {
  164. if( aLayer == m_layer )
  165. {
  166. return true;
  167. }
  168. if( m_hasSolderMask
  169. && ( ( aLayer == F_Mask && m_layer == F_Cu )
  170. || ( aLayer == B_Mask && m_layer == B_Cu ) ) )
  171. {
  172. return true;
  173. }
  174. return false;
  175. }
  176. LSET PCB_SHAPE::GetLayerSet() const
  177. {
  178. LSET layermask( { m_layer } );
  179. if( m_hasSolderMask )
  180. {
  181. if( layermask.test( F_Cu ) )
  182. layermask.set( F_Mask );
  183. if( layermask.test( B_Cu ) )
  184. layermask.set( B_Mask );
  185. }
  186. return layermask;
  187. }
  188. void PCB_SHAPE::SetLayerSet( const LSET& aLayerSet )
  189. {
  190. aLayerSet.RunOnLayers(
  191. [&]( PCB_LAYER_ID layer )
  192. {
  193. if( IsCopperLayer( layer ) )
  194. SetLayer( layer );
  195. else if( IsSolderMaskLayer( layer ) )
  196. SetHasSolderMask( true );
  197. } );
  198. }
  199. std::vector<VECTOR2I> PCB_SHAPE::GetConnectionPoints() const
  200. {
  201. std::vector<VECTOR2I> ret;
  202. // For filled shapes, we may as well use a centroid
  203. if( IsSolidFill() )
  204. {
  205. ret.emplace_back( GetCenter() );
  206. return ret;
  207. }
  208. switch( m_shape )
  209. {
  210. case SHAPE_T::CIRCLE:
  211. {
  212. const CIRCLE circle( GetCenter(), GetRadius() );
  213. for( const TYPED_POINT2I& pt : KIGEOM::GetCircleKeyPoints( circle, false ) )
  214. ret.emplace_back( pt.m_point );
  215. break;
  216. }
  217. case SHAPE_T::ARC:
  218. ret.emplace_back( GetArcMid() );
  219. KI_FALLTHROUGH;
  220. case SHAPE_T::SEGMENT:
  221. case SHAPE_T::BEZIER:
  222. ret.emplace_back( GetStart() );
  223. ret.emplace_back( GetEnd() );
  224. break;
  225. case SHAPE_T::POLY:
  226. for( auto iter = GetPolyShape().CIterate(); iter; ++iter )
  227. ret.emplace_back( *iter );
  228. break;
  229. case SHAPE_T::RECTANGLE:
  230. for( const VECTOR2I& pt : GetRectCorners() )
  231. ret.emplace_back( pt );
  232. break;
  233. case SHAPE_T::UNDEFINED:
  234. UNIMPLEMENTED_FOR( SHAPE_T_asString() );
  235. break;
  236. }
  237. return ret;
  238. }
  239. void PCB_SHAPE::UpdateHatching() const
  240. {
  241. // Force update; we don't bother to propagate damage from all the things that might
  242. // knock-out parts of our hatching.
  243. m_hatchingDirty = true;
  244. EDA_SHAPE::UpdateHatching();
  245. if( !m_hatching.IsEmpty() )
  246. {
  247. PCB_LAYER_ID layer = GetLayer();
  248. BOX2I bbox = GetBoundingBox();
  249. SHAPE_POLY_SET holes;
  250. int maxError = ARC_LOW_DEF;
  251. auto knockoutItem =
  252. [&]( BOARD_ITEM* item )
  253. {
  254. int margin = GetHatchLineSpacing() / 2;
  255. if( item->Type() == PCB_TEXTBOX_T )
  256. margin = 0;
  257. item->TransformShapeToPolygon( holes, layer, margin, maxError, ERROR_OUTSIDE );
  258. };
  259. for( BOARD_ITEM* item : GetBoard()->Drawings() )
  260. {
  261. if( item == this )
  262. continue;
  263. if( item->Type() == PCB_FIELD_T
  264. || item->Type() == PCB_TEXT_T
  265. || item->Type() == PCB_TEXTBOX_T
  266. || item->Type() == PCB_SHAPE_T )
  267. {
  268. if( item->GetLayer() == layer && item->GetBoundingBox().Intersects( bbox ) )
  269. knockoutItem( item );
  270. }
  271. }
  272. for( FOOTPRINT* footprint : GetBoard()->Footprints() )
  273. {
  274. if( footprint == GetParentFootprint() )
  275. continue;
  276. // Knockout footprint courtyard
  277. holes.Append( footprint->GetCourtyard( layer ) );
  278. // Knockout footprint fields
  279. footprint->RunOnChildren(
  280. [&]( BOARD_ITEM* item )
  281. {
  282. if( ( item->Type() == PCB_FIELD_T || item->Type() == PCB_SHAPE_T )
  283. && item->GetLayer() == layer
  284. && item->GetBoundingBox().Intersects( bbox ) )
  285. {
  286. knockoutItem( item );
  287. }
  288. },
  289. RECURSE_MODE::RECURSE );
  290. }
  291. if( !holes.IsEmpty() )
  292. {
  293. m_hatching.BooleanSubtract( holes );
  294. m_hatching.Fracture();
  295. }
  296. }
  297. }
  298. int PCB_SHAPE::GetWidth() const
  299. {
  300. // A stroke width of 0 in PCBNew means no-border, but negative stroke-widths are only used
  301. // in EEschema (see SCH_SHAPE::GetPenWidth()).
  302. // Since negative stroke widths can trip up down-stream code (such as the Gerber plotter), we
  303. // weed them out here.
  304. return std::max( EDA_SHAPE::GetWidth(), 0 );
  305. }
  306. void PCB_SHAPE::StyleFromSettings( const BOARD_DESIGN_SETTINGS& settings )
  307. {
  308. m_stroke.SetWidth( settings.GetLineThickness( GetLayer() ) );
  309. }
  310. const VECTOR2I PCB_SHAPE::GetFocusPosition() const
  311. {
  312. // For some shapes return the visual center, but for not filled polygonal shapes,
  313. // the center is usually far from the shape: a point on the outline is better
  314. switch( m_shape )
  315. {
  316. case SHAPE_T::CIRCLE:
  317. if( !IsAnyFill() )
  318. return VECTOR2I( GetCenter().x + GetRadius(), GetCenter().y );
  319. else
  320. return GetCenter();
  321. case SHAPE_T::RECTANGLE:
  322. if( !IsAnyFill() )
  323. return GetStart();
  324. else
  325. return GetCenter();
  326. case SHAPE_T::POLY:
  327. if( !IsAnyFill() )
  328. {
  329. VECTOR2I pos = GetPolyShape().Outline(0).CPoint(0);
  330. return VECTOR2I( pos.x, pos.y );
  331. }
  332. else
  333. {
  334. return GetCenter();
  335. }
  336. case SHAPE_T::ARC:
  337. return GetArcMid();
  338. case SHAPE_T::BEZIER:
  339. return GetStart();
  340. default:
  341. return GetCenter();
  342. }
  343. }
  344. std::vector<VECTOR2I> PCB_SHAPE::GetCorners() const
  345. {
  346. std::vector<VECTOR2I> pts;
  347. if( GetShape() == SHAPE_T::RECTANGLE )
  348. {
  349. pts = GetRectCorners();
  350. }
  351. else if( GetShape() == SHAPE_T::POLY )
  352. {
  353. for( int ii = 0; ii < GetPolyShape().OutlineCount(); ++ii )
  354. {
  355. for( const VECTOR2I& pt : GetPolyShape().Outline( ii ).CPoints() )
  356. pts.emplace_back( pt );
  357. }
  358. }
  359. else
  360. {
  361. UNIMPLEMENTED_FOR( SHAPE_T_asString() );
  362. }
  363. while( pts.size() < 4 )
  364. pts.emplace_back( pts.back() + VECTOR2I( 10, 10 ) );
  365. return pts;
  366. }
  367. void PCB_SHAPE::Move( const VECTOR2I& aMoveVector )
  368. {
  369. move( aMoveVector );
  370. }
  371. void PCB_SHAPE::Scale( double aScale )
  372. {
  373. scale( aScale );
  374. }
  375. void PCB_SHAPE::Normalize()
  376. {
  377. if( m_shape == SHAPE_T::RECTANGLE )
  378. {
  379. VECTOR2I start = GetStart();
  380. VECTOR2I end = GetEnd();
  381. BOX2I rect( start, end - start );
  382. rect.Normalize();
  383. SetStart( rect.GetPosition() );
  384. SetEnd( rect.GetEnd() );
  385. }
  386. else if( m_shape == SHAPE_T::POLY )
  387. {
  388. auto horizontal =
  389. []( const SEG& seg )
  390. {
  391. return seg.A.y == seg.B.y;
  392. };
  393. auto vertical =
  394. []( const SEG& seg )
  395. {
  396. return seg.A.x == seg.B.x;
  397. };
  398. // Convert a poly back to a rectangle if appropriate
  399. if( m_poly.OutlineCount() == 1 && m_poly.Outline( 0 ).SegmentCount() == 4 )
  400. {
  401. SHAPE_LINE_CHAIN& outline = m_poly.Outline( 0 );
  402. if( horizontal( outline.Segment( 0 ) )
  403. && vertical( outline.Segment( 1 ) )
  404. && horizontal( outline.Segment( 2 ) )
  405. && vertical( outline.Segment( 3 ) ) )
  406. {
  407. m_shape = SHAPE_T::RECTANGLE;
  408. m_start.x = std::min( outline.Segment( 0 ).A.x, outline.Segment( 0 ).B.x );
  409. m_start.y = std::min( outline.Segment( 1 ).A.y, outline.Segment( 1 ).B.y );
  410. m_end.x = std::max( outline.Segment( 0 ).A.x, outline.Segment( 0 ).B.x );
  411. m_end.y = std::max( outline.Segment( 1 ).A.y, outline.Segment( 1 ).B.y );
  412. }
  413. else if( vertical( outline.Segment( 0 ) )
  414. && horizontal( outline.Segment( 1 ) )
  415. && vertical( outline.Segment( 2 ) )
  416. && horizontal( outline.Segment( 3 ) ) )
  417. {
  418. m_shape = SHAPE_T::RECTANGLE;
  419. m_start.x = std::min( outline.Segment( 1 ).A.x, outline.Segment( 1 ).B.x );
  420. m_start.y = std::min( outline.Segment( 0 ).A.y, outline.Segment( 0 ).B.y );
  421. m_end.x = std::max( outline.Segment( 1 ).A.x, outline.Segment( 1 ).B.x );
  422. m_end.y = std::max( outline.Segment( 0 ).A.y, outline.Segment( 0 ).B.y );
  423. }
  424. }
  425. }
  426. }
  427. void PCB_SHAPE::NormalizeForCompare()
  428. {
  429. if( m_shape == SHAPE_T::SEGMENT )
  430. {
  431. // we want start point the top left point and end point the bottom right
  432. // (more easy to compare 2 segments: we are seeing them as equivalent if
  433. // they have the same end points, not necessary the same order)
  434. VECTOR2I start = GetStart();
  435. VECTOR2I end = GetEnd();
  436. if( ( start.x > end.x )
  437. || ( start.x == end.x && start.y < end.y ) )
  438. {
  439. SetStart( end );
  440. SetEnd( start );
  441. }
  442. }
  443. else
  444. Normalize();
  445. }
  446. void PCB_SHAPE::Rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle )
  447. {
  448. rotate( aRotCentre, aAngle );
  449. }
  450. void PCB_SHAPE::Flip( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipDirection )
  451. {
  452. flip( aCentre, aFlipDirection );
  453. SetLayer( GetBoard()->FlipLayer( GetLayer() ) );
  454. }
  455. void PCB_SHAPE::Mirror( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipDirection )
  456. {
  457. // Mirror an edge of the footprint. the layer is not modified
  458. // This is a footprint shape modification.
  459. switch( GetShape() )
  460. {
  461. case SHAPE_T::ARC:
  462. case SHAPE_T::SEGMENT:
  463. case SHAPE_T::RECTANGLE:
  464. case SHAPE_T::CIRCLE:
  465. case SHAPE_T::BEZIER:
  466. MIRROR( m_start, aCentre, aFlipDirection );
  467. MIRROR( m_end, aCentre, aFlipDirection );
  468. MIRROR( m_arcCenter, aCentre, aFlipDirection );
  469. MIRROR( m_bezierC1, aCentre, aFlipDirection );
  470. MIRROR( m_bezierC2, aCentre, aFlipDirection );
  471. if( GetShape() == SHAPE_T::ARC )
  472. std::swap( m_start, m_end );
  473. if( GetShape() == SHAPE_T::BEZIER )
  474. RebuildBezierToSegmentsPointsList( ARC_HIGH_DEF );
  475. break;
  476. case SHAPE_T::POLY:
  477. m_poly.Mirror( aCentre, aFlipDirection );
  478. break;
  479. default:
  480. UNIMPLEMENTED_FOR( SHAPE_T_asString() );
  481. }
  482. }
  483. void PCB_SHAPE::SetIsProxyItem( bool aIsProxy )
  484. {
  485. PAD* parentPad = nullptr;
  486. if( GetBoard() && GetBoard()->IsFootprintHolder() )
  487. {
  488. for( FOOTPRINT* fp : GetBoard()->Footprints() )
  489. {
  490. for( PAD* pad : fp->Pads() )
  491. {
  492. if( pad->IsEntered() )
  493. {
  494. parentPad = pad;
  495. break;
  496. }
  497. }
  498. }
  499. }
  500. if( aIsProxy && !m_proxyItem )
  501. {
  502. if( GetShape() == SHAPE_T::SEGMENT )
  503. {
  504. if( parentPad && parentPad->GetLocalThermalSpokeWidthOverride().has_value() )
  505. SetWidth( parentPad->GetLocalThermalSpokeWidthOverride().value() );
  506. else
  507. SetWidth( pcbIUScale.mmToIU( ZONE_THERMAL_RELIEF_COPPER_WIDTH_MM ) );
  508. }
  509. else
  510. {
  511. SetWidth( 1 );
  512. }
  513. }
  514. else if( m_proxyItem && !aIsProxy )
  515. {
  516. SetWidth( pcbIUScale.mmToIU( DEFAULT_LINE_WIDTH ) );
  517. }
  518. m_proxyItem = aIsProxy;
  519. }
  520. double PCB_SHAPE::ViewGetLOD( int aLayer, const KIGFX::VIEW* aView ) const
  521. {
  522. KIGFX::PCB_PAINTER& painter = static_cast<KIGFX::PCB_PAINTER&>( *aView->GetPainter() );
  523. KIGFX::PCB_RENDER_SETTINGS& renderSettings = *painter.GetSettings();
  524. if( aLayer == LAYER_LOCKED_ITEM_SHADOW )
  525. {
  526. // Hide shadow if the main layer is not shown
  527. if( !aView->IsLayerVisible( m_layer ) )
  528. return LOD_HIDE;
  529. // Hide shadow on dimmed tracks
  530. if( renderSettings.GetHighContrast() )
  531. {
  532. if( m_layer != renderSettings.GetPrimaryHighContrastLayer() )
  533. return LOD_HIDE;
  534. }
  535. }
  536. if( FOOTPRINT* parent = GetParentFootprint() )
  537. {
  538. if( parent->GetLayer() == F_Cu && !aView->IsLayerVisible( LAYER_FOOTPRINTS_FR ) )
  539. return LOD_HIDE;
  540. if( parent->GetLayer() == B_Cu && !aView->IsLayerVisible( LAYER_FOOTPRINTS_BK ) )
  541. return LOD_HIDE;
  542. }
  543. return LOD_SHOW;
  544. }
  545. std::vector<int> PCB_SHAPE::ViewGetLayers() const
  546. {
  547. std::vector<int> layers;
  548. layers.reserve( 4 );
  549. layers.push_back( GetLayer() );
  550. if( IsOnCopperLayer() )
  551. {
  552. layers.push_back( GetNetnameLayer( GetLayer() ) );
  553. if( m_hasSolderMask )
  554. {
  555. if( m_layer == F_Cu )
  556. layers.push_back( F_Mask );
  557. else if( m_layer == B_Cu )
  558. layers.push_back( B_Mask );
  559. }
  560. }
  561. if( IsLocked() || ( GetParentFootprint() && GetParentFootprint()->IsLocked() ) )
  562. layers.push_back( LAYER_LOCKED_ITEM_SHADOW );
  563. return layers;
  564. }
  565. void PCB_SHAPE::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  566. {
  567. if( aFrame->GetName() == PCB_EDIT_FRAME_NAME )
  568. {
  569. if( FOOTPRINT* parent = GetParentFootprint() )
  570. aList.emplace_back( _( "Footprint" ), parent->GetReference() );
  571. }
  572. aList.emplace_back( _( "Type" ), _( "Drawing" ) );
  573. if( aFrame->GetName() == PCB_EDIT_FRAME_NAME && IsLocked() )
  574. aList.emplace_back( _( "Status" ), _( "Locked" ) );
  575. ShapeGetMsgPanelInfo( aFrame, aList );
  576. aList.emplace_back( _( "Layer" ), GetLayerName() );
  577. }
  578. wxString PCB_SHAPE::GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const
  579. {
  580. FOOTPRINT* parentFP = GetParentFootprint();
  581. // Don't report parent footprint info from footprint editor, viewer, etc.
  582. if( GetBoard() && GetBoard()->GetBoardUse() == BOARD_USE::FPHOLDER )
  583. parentFP = nullptr;
  584. if( IsOnCopperLayer() )
  585. {
  586. if( parentFP )
  587. {
  588. return wxString::Format( _( "%s %s of %s on %s" ),
  589. GetFriendlyName(),
  590. GetNetnameMsg(),
  591. parentFP->GetReference(),
  592. GetLayerName() );
  593. }
  594. else
  595. {
  596. return wxString::Format( _( "%s %s on %s" ),
  597. GetFriendlyName(),
  598. GetNetnameMsg(),
  599. GetLayerName() );
  600. }
  601. }
  602. else
  603. {
  604. if( parentFP )
  605. {
  606. return wxString::Format( _( "%s of %s on %s" ),
  607. GetFriendlyName(),
  608. parentFP->GetReference(),
  609. GetLayerName() );
  610. }
  611. else
  612. {
  613. return wxString::Format( _( "%s on %s" ),
  614. GetFriendlyName(),
  615. GetLayerName() );
  616. }
  617. }
  618. }
  619. BITMAPS PCB_SHAPE::GetMenuImage() const
  620. {
  621. if( GetParentFootprint() )
  622. return BITMAPS::show_mod_edge;
  623. else
  624. return BITMAPS::add_dashed_line;
  625. }
  626. EDA_ITEM* PCB_SHAPE::Clone() const
  627. {
  628. return new PCB_SHAPE( *this );
  629. }
  630. const BOX2I PCB_SHAPE::ViewBBox() const
  631. {
  632. BOX2I return_box = EDA_ITEM::ViewBBox();
  633. // Inflate the bounding box by just a bit more for safety.
  634. return_box.Inflate( GetWidth() );
  635. return return_box;
  636. }
  637. std::shared_ptr<SHAPE> PCB_SHAPE::GetEffectiveShape( PCB_LAYER_ID aLayer, FLASHING aFlash ) const
  638. {
  639. return std::make_shared<SHAPE_COMPOUND>( MakeEffectiveShapes() );
  640. }
  641. void PCB_SHAPE::swapData( BOARD_ITEM* aImage )
  642. {
  643. PCB_SHAPE* image = dynamic_cast<PCB_SHAPE*>( aImage );
  644. wxCHECK( image, /* void */ );
  645. SwapShape( image );
  646. // Swap params not handled by SwapShape( image )
  647. std::swap( m_layer, image->m_layer );
  648. std::swap( m_isKnockout, image->m_isKnockout );
  649. std::swap( m_isLocked, image->m_isLocked );
  650. std::swap( m_flags, image->m_flags );
  651. std::swap( m_parent, image->m_parent );
  652. std::swap( m_forceVisible, image->m_forceVisible );
  653. std::swap( m_netinfo, image->m_netinfo );
  654. std::swap( m_hasSolderMask, image->m_hasSolderMask );
  655. std::swap( m_solderMaskMargin, image->m_solderMaskMargin );
  656. }
  657. bool PCB_SHAPE::cmp_drawings::operator()( const BOARD_ITEM* aFirst,
  658. const BOARD_ITEM* aSecond ) const
  659. {
  660. if( aFirst->Type() != aSecond->Type() )
  661. return aFirst->Type() < aSecond->Type();
  662. if( aFirst->GetLayer() != aSecond->GetLayer() )
  663. return aFirst->GetLayer() < aSecond->GetLayer();
  664. if( aFirst->Type() == PCB_SHAPE_T )
  665. {
  666. const PCB_SHAPE* dwgA = static_cast<const PCB_SHAPE*>( aFirst );
  667. const PCB_SHAPE* dwgB = static_cast<const PCB_SHAPE*>( aSecond );
  668. if( dwgA->GetShape() != dwgB->GetShape() )
  669. return dwgA->GetShape() < dwgB->GetShape();
  670. }
  671. return aFirst->m_Uuid < aSecond->m_Uuid;
  672. }
  673. void PCB_SHAPE::TransformShapeToPolygon( SHAPE_POLY_SET& aBuffer, PCB_LAYER_ID aLayer,
  674. int aClearance, int aError, ERROR_LOC aErrorLoc,
  675. bool ignoreLineWidth ) const
  676. {
  677. EDA_SHAPE::TransformShapeToPolygon( aBuffer, aClearance, aError, aErrorLoc, ignoreLineWidth,
  678. false );
  679. }
  680. void PCB_SHAPE::TransformShapeToPolySet( SHAPE_POLY_SET& aBuffer, PCB_LAYER_ID aLayer,
  681. int aClearance, int aError, ERROR_LOC aErrorLoc ) const
  682. {
  683. EDA_SHAPE::TransformShapeToPolygon( aBuffer, aClearance, aError, aErrorLoc, false, true );
  684. }
  685. bool PCB_SHAPE::operator==( const BOARD_ITEM& aOther ) const
  686. {
  687. if( aOther.Type() != Type() )
  688. return false;
  689. const PCB_SHAPE& other = static_cast<const PCB_SHAPE&>( aOther );
  690. return *this == other;
  691. }
  692. bool PCB_SHAPE::operator==( const PCB_SHAPE& aOther ) const
  693. {
  694. if( aOther.Type() != Type() )
  695. return false;
  696. const PCB_SHAPE& other = static_cast<const PCB_SHAPE&>( aOther );
  697. if( m_layer != other.m_layer )
  698. return false;
  699. if( m_isKnockout != other.m_isKnockout )
  700. return false;
  701. if( m_isLocked != other.m_isLocked )
  702. return false;
  703. if( m_flags != other.m_flags )
  704. return false;
  705. if( m_forceVisible != other.m_forceVisible )
  706. return false;
  707. if( m_netinfo->GetNetCode() != other.m_netinfo->GetNetCode() )
  708. return false;
  709. if( m_hasSolderMask != other.m_hasSolderMask )
  710. return false;
  711. if( m_solderMaskMargin != other.m_solderMaskMargin )
  712. return false;
  713. return EDA_SHAPE::operator==( other );
  714. }
  715. double PCB_SHAPE::Similarity( const BOARD_ITEM& aOther ) const
  716. {
  717. if( aOther.Type() != Type() )
  718. return 0.0;
  719. const PCB_SHAPE& other = static_cast<const PCB_SHAPE&>( aOther );
  720. double similarity = 1.0;
  721. if( GetLayer() != other.GetLayer() )
  722. similarity *= 0.9;
  723. if( m_isKnockout != other.m_isKnockout )
  724. similarity *= 0.9;
  725. if( m_isLocked != other.m_isLocked )
  726. similarity *= 0.9;
  727. if( m_flags != other.m_flags )
  728. similarity *= 0.9;
  729. if( m_forceVisible != other.m_forceVisible )
  730. similarity *= 0.9;
  731. if( m_netinfo->GetNetCode() != other.m_netinfo->GetNetCode() )
  732. similarity *= 0.9;
  733. if( m_hasSolderMask != other.m_hasSolderMask )
  734. similarity *= 0.9;
  735. if( m_solderMaskMargin != other.m_solderMaskMargin )
  736. similarity *= 0.9;
  737. similarity *= EDA_SHAPE::Similarity( other );
  738. return similarity;
  739. }
  740. static struct PCB_SHAPE_DESC
  741. {
  742. PCB_SHAPE_DESC()
  743. {
  744. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  745. REGISTER_TYPE( PCB_SHAPE );
  746. propMgr.AddTypeCast( new TYPE_CAST<PCB_SHAPE, BOARD_CONNECTED_ITEM> );
  747. propMgr.AddTypeCast( new TYPE_CAST<PCB_SHAPE, EDA_SHAPE> );
  748. propMgr.InheritsAfter( TYPE_HASH( PCB_SHAPE ), TYPE_HASH( BOARD_CONNECTED_ITEM ) );
  749. propMgr.InheritsAfter( TYPE_HASH( PCB_SHAPE ), TYPE_HASH( EDA_SHAPE ) );
  750. // Need to initialise enum_map before we can use a Property enum for it
  751. ENUM_MAP<PCB_LAYER_ID>& layerEnum = ENUM_MAP<PCB_LAYER_ID>::Instance();
  752. if( layerEnum.Choices().GetCount() == 0 )
  753. {
  754. layerEnum.Undefined( UNDEFINED_LAYER );
  755. for( PCB_LAYER_ID layer : LSET::AllLayersMask().Seq() )
  756. layerEnum.Map( layer, LSET::Name( layer ) );
  757. }
  758. void ( PCB_SHAPE::*shapeLayerSetter )( PCB_LAYER_ID ) = &PCB_SHAPE::SetLayer;
  759. PCB_LAYER_ID ( PCB_SHAPE::*shapeLayerGetter )() const = &PCB_SHAPE::GetLayer;
  760. auto layerProperty = new PROPERTY_ENUM<PCB_SHAPE, PCB_LAYER_ID>(
  761. _HKI( "Layer" ), shapeLayerSetter, shapeLayerGetter );
  762. propMgr.ReplaceProperty( TYPE_HASH( BOARD_CONNECTED_ITEM ), _HKI( "Layer" ), layerProperty );
  763. // Only polygons have meaningful Position properties.
  764. // On other shapes, these are duplicates of the Start properties.
  765. auto isPolygon =
  766. []( INSPECTABLE* aItem ) -> bool
  767. {
  768. if( PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( aItem ) )
  769. return shape->GetShape() == SHAPE_T::POLY;
  770. return false;
  771. };
  772. propMgr.OverrideAvailability( TYPE_HASH( PCB_SHAPE ), TYPE_HASH( BOARD_ITEM ),
  773. _HKI( "Position X" ), isPolygon );
  774. propMgr.OverrideAvailability( TYPE_HASH( PCB_SHAPE ), TYPE_HASH( BOARD_ITEM ),
  775. _HKI( "Position Y" ), isPolygon );
  776. propMgr.Mask( TYPE_HASH( PCB_SHAPE ), TYPE_HASH( EDA_SHAPE ), _HKI( "Line Color" ) );
  777. propMgr.Mask( TYPE_HASH( PCB_SHAPE ), TYPE_HASH( EDA_SHAPE ), _HKI( "Fill Color" ) );
  778. auto isCopper =
  779. []( INSPECTABLE* aItem ) -> bool
  780. {
  781. if( PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( aItem ) )
  782. return shape->IsOnCopperLayer();
  783. return false;
  784. };
  785. propMgr.OverrideAvailability( TYPE_HASH( PCB_SHAPE ), TYPE_HASH( BOARD_CONNECTED_ITEM ),
  786. _HKI( "Net" ), isCopper );
  787. auto isPadEditMode =
  788. []( BOARD* aBoard ) -> bool
  789. {
  790. if( aBoard && aBoard->IsFootprintHolder() )
  791. {
  792. for( FOOTPRINT* fp : aBoard->Footprints() )
  793. {
  794. for( PAD* pad : fp->Pads() )
  795. {
  796. if( pad->IsEntered() )
  797. return true;
  798. }
  799. }
  800. }
  801. return false;
  802. };
  803. auto showNumberBoxProperty =
  804. [&]( INSPECTABLE* aItem ) -> bool
  805. {
  806. if( PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( aItem ) )
  807. {
  808. if( shape->GetShape() == SHAPE_T::RECTANGLE )
  809. return isPadEditMode( shape->GetBoard() );
  810. }
  811. return false;
  812. };
  813. auto showSpokeTemplateProperty =
  814. [&]( INSPECTABLE* aItem ) -> bool
  815. {
  816. if( PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( aItem ) )
  817. {
  818. if( shape->GetShape() == SHAPE_T::SEGMENT )
  819. return isPadEditMode( shape->GetBoard() );
  820. }
  821. return false;
  822. };
  823. const wxString groupPadPrimitives = _HKI( "Pad Primitives" );
  824. propMgr.AddProperty( new PROPERTY<PCB_SHAPE, bool>( _HKI( "Number Box" ),
  825. &PCB_SHAPE::SetIsProxyItem,
  826. &PCB_SHAPE::IsProxyItem ),
  827. groupPadPrimitives )
  828. .SetAvailableFunc( showNumberBoxProperty )
  829. .SetIsHiddenFromRulesEditor();
  830. propMgr.AddProperty( new PROPERTY<PCB_SHAPE, bool>( _HKI( "Thermal Spoke Template" ),
  831. &PCB_SHAPE::SetIsProxyItem,
  832. &PCB_SHAPE::IsProxyItem ),
  833. groupPadPrimitives )
  834. .SetAvailableFunc( showSpokeTemplateProperty )
  835. .SetIsHiddenFromRulesEditor();
  836. const wxString groupTechLayers = _HKI( "Technical Layers" );
  837. auto isExternalCuLayer =
  838. []( INSPECTABLE* aItem )
  839. {
  840. if( auto shape = dynamic_cast<PCB_SHAPE*>( aItem ) )
  841. return IsExternalCopperLayer( shape->GetLayer() );
  842. return false;
  843. };
  844. propMgr.AddProperty( new PROPERTY<PCB_SHAPE, bool>( _HKI( "Soldermask" ),
  845. &PCB_SHAPE::SetHasSolderMask,
  846. &PCB_SHAPE::HasSolderMask ),
  847. groupTechLayers )
  848. .SetAvailableFunc( isExternalCuLayer );
  849. propMgr.AddProperty( new PROPERTY<PCB_SHAPE, std::optional<int>>(
  850. _HKI( "Soldermask Margin Override" ),
  851. &PCB_SHAPE::SetLocalSolderMaskMargin,
  852. &PCB_SHAPE::GetLocalSolderMaskMargin,
  853. PROPERTY_DISPLAY::PT_SIZE ),
  854. groupTechLayers )
  855. .SetAvailableFunc( isExternalCuLayer );
  856. }
  857. } _PCB_SHAPE_DESC;