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.

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