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.

1204 lines
38 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019-2023 KiCad Developers, see AUTHORS.txt for contributors.
  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 <cstdio>
  24. #include <memory>
  25. #include <mutex>
  26. #include <wx/log.h>
  27. #include <board.h>
  28. #include <board_design_settings.h>
  29. #include <drc/drc_rtree.h>
  30. #include <drc/drc_engine.h>
  31. #include <pcb_track.h>
  32. #include <pcb_group.h>
  33. #include <geometry/shape_segment.h>
  34. #include <pcbexpr_evaluator.h>
  35. #include <connectivity/connectivity_data.h>
  36. #include <connectivity/connectivity_algo.h>
  37. #include <connectivity/from_to_cache.h>
  38. bool fromToFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  39. {
  40. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  41. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  42. LIBEVAL::VALUE* result = aCtx->AllocValue();
  43. LIBEVAL::VALUE* argTo = aCtx->Pop();
  44. LIBEVAL::VALUE* argFrom = aCtx->Pop();
  45. result->Set(0.0);
  46. aCtx->Push( result );
  47. if(!item)
  48. return false;
  49. auto ftCache = item->GetBoard()->GetConnectivity()->GetFromToCache();
  50. if( !ftCache )
  51. {
  52. wxLogWarning( wxT( "Attempting to call fromTo() with non-existent from-to cache." ) );
  53. return true;
  54. }
  55. if( ftCache->IsOnFromToPath( static_cast<BOARD_CONNECTED_ITEM*>( item ),
  56. argFrom->AsString(), argTo->AsString() ) )
  57. {
  58. result->Set(1.0);
  59. }
  60. return true;
  61. }
  62. #define MISSING_LAYER_ARG( f ) wxString::Format( _( "Missing layer name argument to %s." ), f )
  63. static void existsOnLayerFunc( LIBEVAL::CONTEXT* aCtx, void *self )
  64. {
  65. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  66. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  67. LIBEVAL::VALUE* arg = aCtx->Pop();
  68. LIBEVAL::VALUE* result = aCtx->AllocValue();
  69. result->Set( 0.0 );
  70. aCtx->Push( result );
  71. if( !item )
  72. return;
  73. if( !arg || arg->AsString().IsEmpty() )
  74. {
  75. if( aCtx->HasErrorCallback() )
  76. aCtx->ReportError( MISSING_LAYER_ARG( wxT( "existsOnLayer()" ) ) );
  77. return;
  78. }
  79. result->SetDeferredEval(
  80. [item, arg, aCtx]() -> double
  81. {
  82. const wxString& layerName = arg->AsString();
  83. wxPGChoices& layerMap = ENUM_MAP<PCB_LAYER_ID>::Instance().Choices();
  84. if( aCtx->HasErrorCallback())
  85. {
  86. /*
  87. * Interpreted version
  88. */
  89. bool anyMatch = false;
  90. for( unsigned ii = 0; ii < layerMap.GetCount(); ++ii )
  91. {
  92. wxPGChoiceEntry& entry = layerMap[ ii ];
  93. if( entry.GetText().Matches( layerName ))
  94. {
  95. anyMatch = true;
  96. if( item->IsOnLayer( ToLAYER_ID( entry.GetValue() ) ) )
  97. return 1.0;
  98. }
  99. }
  100. if( !anyMatch )
  101. {
  102. aCtx->ReportError( wxString::Format( _( "Unrecognized layer '%s'" ),
  103. layerName ) );
  104. }
  105. return 0.0;
  106. }
  107. else
  108. {
  109. /*
  110. * Compiled version
  111. */
  112. BOARD* board = item->GetBoard();
  113. {
  114. std::shared_lock<std::shared_mutex> readLock( board->m_CachesMutex );
  115. auto i = board->m_LayerExpressionCache.find( layerName );
  116. if( i != board->m_LayerExpressionCache.end() )
  117. return ( item->GetLayerSet() & i->second ).any() ? 1.0 : 0.0;
  118. }
  119. LSET mask;
  120. for( unsigned ii = 0; ii < layerMap.GetCount(); ++ii )
  121. {
  122. wxPGChoiceEntry& entry = layerMap[ ii ];
  123. if( entry.GetText().Matches( layerName ) )
  124. mask.set( ToLAYER_ID( entry.GetValue() ) );
  125. }
  126. {
  127. std::unique_lock<std::shared_mutex> writeLock( board->m_CachesMutex );
  128. board->m_LayerExpressionCache[ layerName ] = mask;
  129. }
  130. return ( item->GetLayerSet() & mask ).any() ? 1.0 : 0.0;
  131. }
  132. } );
  133. }
  134. static void isPlatedFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  135. {
  136. LIBEVAL::VALUE* result = aCtx->AllocValue();
  137. result->Set( 0.0 );
  138. aCtx->Push( result );
  139. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  140. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  141. if( !item )
  142. return;
  143. if( item->Type() == PCB_PAD_T && static_cast<PAD*>( item )->GetAttribute() == PAD_ATTRIB::PTH )
  144. result->Set( 1.0 );
  145. else if( item->Type() == PCB_VIA_T )
  146. result->Set( 1.0 );
  147. }
  148. bool collidesWithCourtyard( BOARD_ITEM* aItem, std::shared_ptr<SHAPE>& aItemShape,
  149. PCBEXPR_CONTEXT* aCtx, FOOTPRINT* aFootprint, PCB_LAYER_ID aSide )
  150. {
  151. SHAPE_POLY_SET footprintCourtyard;
  152. footprintCourtyard = aFootprint->GetCourtyard( aSide );
  153. if( !aItemShape )
  154. {
  155. // Since rules are used for zone filling we can't rely on the filled shapes.
  156. // Use the zone outline instead.
  157. if( ZONE* zone = dynamic_cast<ZONE*>( aItem ) )
  158. aItemShape.reset( zone->Outline()->Clone() );
  159. else
  160. aItemShape = aItem->GetEffectiveShape( aCtx->GetLayer() );
  161. }
  162. return footprintCourtyard.Collide( aItemShape.get() );
  163. };
  164. static bool searchFootprints( BOARD* aBoard, const wxString& aArg, PCBEXPR_CONTEXT* aCtx,
  165. const std::function<bool( FOOTPRINT* )>& aFunc )
  166. {
  167. if( aArg == wxT( "A" ) )
  168. {
  169. FOOTPRINT* fp = dynamic_cast<FOOTPRINT*>( aCtx->GetItem( 0 ) );
  170. if( fp && aFunc( fp ) )
  171. return true;
  172. }
  173. else if( aArg == wxT( "B" ) )
  174. {
  175. FOOTPRINT* fp = dynamic_cast<FOOTPRINT*>( aCtx->GetItem( 1 ) );
  176. if( fp && aFunc( fp ) )
  177. return true;
  178. }
  179. else for( FOOTPRINT* fp : aBoard->Footprints() )
  180. {
  181. if( fp->GetReference().Matches( aArg ) )
  182. {
  183. if( aFunc( fp ) )
  184. return true;
  185. }
  186. else if( aArg.Contains( ':' )
  187. && fp->GetFPIDAsString().Matches( aArg ) )
  188. {
  189. if( aFunc( fp ) )
  190. return true;
  191. }
  192. }
  193. return false;
  194. }
  195. #define MISSING_FP_ARG( f ) \
  196. wxString::Format( _( "Missing footprint argument (A, B, or reference designator) to %s." ), f )
  197. static void intersectsCourtyardFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  198. {
  199. PCBEXPR_CONTEXT* context = static_cast<PCBEXPR_CONTEXT*>( aCtx );
  200. LIBEVAL::VALUE* arg = context->Pop();
  201. LIBEVAL::VALUE* result = context->AllocValue();
  202. result->Set( 0.0 );
  203. context->Push( result );
  204. if( !arg || arg->AsString().IsEmpty() )
  205. {
  206. if( context->HasErrorCallback() )
  207. context->ReportError( MISSING_FP_ARG( wxT( "intersectsCourtyard()" ) ) );
  208. return;
  209. }
  210. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  211. BOARD_ITEM* item = vref ? vref->GetObject( context ) : nullptr;
  212. if( !item )
  213. return;
  214. result->SetDeferredEval(
  215. [item, arg, context]() -> double
  216. {
  217. BOARD* board = item->GetBoard();
  218. std::shared_ptr<SHAPE> itemShape;
  219. if( searchFootprints( board, arg->AsString(), context,
  220. [&]( FOOTPRINT* fp )
  221. {
  222. PTR_PTR_CACHE_KEY key = { fp, item };
  223. if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
  224. {
  225. std::shared_lock<std::shared_mutex> readLock( board->m_CachesMutex );
  226. auto i = board->m_IntersectsCourtyardCache.find( key );
  227. if( i != board->m_IntersectsCourtyardCache.end() )
  228. return i->second;
  229. }
  230. bool res = collidesWithCourtyard( item, itemShape, context, fp, F_Cu )
  231. || collidesWithCourtyard( item, itemShape, context, fp, B_Cu );
  232. if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
  233. {
  234. std::unique_lock<std::shared_mutex> cacheLock( board->m_CachesMutex );
  235. board->m_IntersectsCourtyardCache[ key ] = res;
  236. }
  237. return res;
  238. } ) )
  239. {
  240. return 1.0;
  241. }
  242. return 0.0;
  243. } );
  244. }
  245. static void intersectsFrontCourtyardFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  246. {
  247. PCBEXPR_CONTEXT* context = static_cast<PCBEXPR_CONTEXT*>( aCtx );
  248. LIBEVAL::VALUE* arg = context->Pop();
  249. LIBEVAL::VALUE* result = context->AllocValue();
  250. result->Set( 0.0 );
  251. context->Push( result );
  252. if( !arg || arg->AsString().IsEmpty() )
  253. {
  254. if( context->HasErrorCallback() )
  255. context->ReportError( MISSING_FP_ARG( wxT( "intersectsFrontCourtyard()" ) ) );
  256. return;
  257. }
  258. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  259. BOARD_ITEM* item = vref ? vref->GetObject( context ) : nullptr;
  260. if( !item )
  261. return;
  262. result->SetDeferredEval(
  263. [item, arg, context]() -> double
  264. {
  265. BOARD* board = item->GetBoard();
  266. std::shared_ptr<SHAPE> itemShape;
  267. if( searchFootprints( board, arg->AsString(), context,
  268. [&]( FOOTPRINT* fp )
  269. {
  270. PTR_PTR_CACHE_KEY key = { fp, item };
  271. if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
  272. {
  273. std::shared_lock<std::shared_mutex> readLock( board->m_CachesMutex );
  274. auto i = board->m_IntersectsFCourtyardCache.find( key );
  275. if( i != board->m_IntersectsFCourtyardCache.end() )
  276. return i->second;
  277. }
  278. bool res = collidesWithCourtyard( item, itemShape, context, fp, F_Cu );
  279. if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
  280. {
  281. std::unique_lock<std::shared_mutex> writeLock( board->m_CachesMutex );
  282. board->m_IntersectsFCourtyardCache[ key ] = res;
  283. }
  284. return res;
  285. } ) )
  286. {
  287. return 1.0;
  288. }
  289. return 0.0;
  290. } );
  291. }
  292. static void intersectsBackCourtyardFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  293. {
  294. PCBEXPR_CONTEXT* context = static_cast<PCBEXPR_CONTEXT*>( aCtx );
  295. LIBEVAL::VALUE* arg = context->Pop();
  296. LIBEVAL::VALUE* result = context->AllocValue();
  297. result->Set( 0.0 );
  298. context->Push( result );
  299. if( !arg || arg->AsString().IsEmpty() )
  300. {
  301. if( context->HasErrorCallback() )
  302. context->ReportError( MISSING_FP_ARG( wxT( "intersectsBackCourtyard()" ) ) );
  303. return;
  304. }
  305. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  306. BOARD_ITEM* item = vref ? vref->GetObject( context ) : nullptr;
  307. if( !item )
  308. return;
  309. result->SetDeferredEval(
  310. [item, arg, context]() -> double
  311. {
  312. BOARD* board = item->GetBoard();
  313. std::shared_ptr<SHAPE> itemShape;
  314. if( searchFootprints( board, arg->AsString(), context,
  315. [&]( FOOTPRINT* fp )
  316. {
  317. PTR_PTR_CACHE_KEY key = { fp, item };
  318. if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
  319. {
  320. std::shared_lock<std::shared_mutex> readLock( board->m_CachesMutex );
  321. auto i = board->m_IntersectsBCourtyardCache.find( key );
  322. if( i != board->m_IntersectsBCourtyardCache.end() )
  323. return i->second;
  324. }
  325. bool res = collidesWithCourtyard( item, itemShape, context, fp, B_Cu );
  326. if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
  327. {
  328. std::unique_lock<std::shared_mutex> writeLock( board->m_CachesMutex );
  329. board->m_IntersectsBCourtyardCache[ key ] = res;
  330. }
  331. return res;
  332. } ) )
  333. {
  334. return 1.0;
  335. }
  336. return 0.0;
  337. } );
  338. }
  339. bool collidesWithArea( BOARD_ITEM* aItem, PCBEXPR_CONTEXT* aCtx, ZONE* aArea )
  340. {
  341. BOARD* board = aArea->GetBoard();
  342. BOX2I areaBBox = aArea->GetBoundingBox();
  343. std::shared_ptr<SHAPE> shape;
  344. // Collisions include touching, so we need to deflate outline by enough to exclude it.
  345. // This is particularly important for detecting copper fills as they will be exactly
  346. // touching along the entire exclusion border.
  347. SHAPE_POLY_SET areaOutline = aArea->Outline()->CloneDropTriangulation();
  348. areaOutline.ClearArcs();
  349. areaOutline.Deflate( board->GetDesignSettings().GetDRCEpsilon(),
  350. CORNER_STRATEGY::ALLOW_ACUTE_CORNERS, ARC_LOW_DEF );
  351. if( aItem->GetFlags() & HOLE_PROXY )
  352. {
  353. if( aItem->Type() == PCB_PAD_T )
  354. {
  355. return areaOutline.Collide( aItem->GetEffectiveHoleShape().get() );
  356. }
  357. else if( aItem->Type() == PCB_VIA_T )
  358. {
  359. LSET overlap = aItem->GetLayerSet() & aArea->GetLayerSet();
  360. /// Avoid buried vias that don't overlap the zone's layers
  361. if( overlap.any() )
  362. {
  363. if( aCtx->GetLayer() == UNDEFINED_LAYER || overlap.Contains( aCtx->GetLayer() ) )
  364. return areaOutline.Collide( aItem->GetEffectiveHoleShape().get() );
  365. }
  366. }
  367. return false;
  368. }
  369. if( aItem->Type() == PCB_FOOTPRINT_T )
  370. {
  371. FOOTPRINT* footprint = static_cast<FOOTPRINT*>( aItem );
  372. if( ( footprint->GetFlags() & MALFORMED_COURTYARDS ) != 0 )
  373. {
  374. if( aCtx->HasErrorCallback() )
  375. aCtx->ReportError( _( "Footprint's courtyard is not a single, closed shape." ) );
  376. return false;
  377. }
  378. if( ( aArea->GetLayerSet() & LSET::FrontMask() ).any() )
  379. {
  380. const SHAPE_POLY_SET& courtyard = footprint->GetCourtyard( F_CrtYd );
  381. if( courtyard.OutlineCount() == 0 )
  382. {
  383. if( aCtx->HasErrorCallback() )
  384. aCtx->ReportError( _( "Footprint has no front courtyard." ) );
  385. }
  386. else if( areaOutline.Collide( &courtyard.Outline( 0 ) ) )
  387. {
  388. return true;
  389. }
  390. }
  391. if( ( aArea->GetLayerSet() & LSET::BackMask() ).any() )
  392. {
  393. const SHAPE_POLY_SET& courtyard = footprint->GetCourtyard( B_CrtYd );
  394. if( courtyard.OutlineCount() == 0 )
  395. {
  396. if( aCtx->HasErrorCallback() )
  397. aCtx->ReportError( _( "Footprint has no back courtyard." ) );
  398. }
  399. else if( areaOutline.Collide( &courtyard.Outline( 0 ) ) )
  400. {
  401. return true;
  402. }
  403. }
  404. return false;
  405. }
  406. if( aItem->Type() == PCB_ZONE_T )
  407. {
  408. ZONE* zone = static_cast<ZONE*>( aItem );
  409. if( !zone->IsFilled() )
  410. return false;
  411. DRC_RTREE* zoneRTree = board->m_CopperZoneRTreeCache[ zone ].get();
  412. if( zoneRTree )
  413. {
  414. for( size_t ii = 0; ii < aArea->GetLayerSet().size(); ++ii )
  415. {
  416. if( aArea->GetLayerSet().test( ii ) )
  417. {
  418. PCB_LAYER_ID layer = PCB_LAYER_ID( ii );
  419. if( aCtx->GetLayer() == layer || aCtx->GetLayer() == UNDEFINED_LAYER )
  420. {
  421. if( zoneRTree->QueryColliding( areaBBox, &areaOutline, layer ) )
  422. return true;
  423. }
  424. }
  425. }
  426. }
  427. return false;
  428. }
  429. else
  430. {
  431. PCB_LAYER_ID layer = aCtx->GetLayer();
  432. if( layer != UNDEFINED_LAYER && !( aArea->GetLayerSet().Contains( layer ) ) )
  433. return false;
  434. if( !shape )
  435. shape = aItem->GetEffectiveShape( layer );
  436. return areaOutline.Collide( shape.get() );
  437. }
  438. }
  439. bool searchAreas( BOARD* aBoard, const wxString& aArg, PCBEXPR_CONTEXT* aCtx,
  440. const std::function<bool( ZONE* )>& aFunc )
  441. {
  442. if( aArg == wxT( "A" ) )
  443. {
  444. return aFunc( dynamic_cast<ZONE*>( aCtx->GetItem( 0 ) ) );
  445. }
  446. else if( aArg == wxT( "B" ) )
  447. {
  448. return aFunc( dynamic_cast<ZONE*>( aCtx->GetItem( 1 ) ) );
  449. }
  450. else if( KIID::SniffTest( aArg ) )
  451. {
  452. KIID target( aArg );
  453. for( ZONE* area : aBoard->Zones() )
  454. {
  455. // Only a single zone can match the UUID; exit once we find a match whether
  456. // "inside" or not
  457. if( area->m_Uuid == target )
  458. return aFunc( area );
  459. }
  460. for( FOOTPRINT* footprint : aBoard->Footprints() )
  461. {
  462. for( ZONE* area : footprint->Zones() )
  463. {
  464. // Only a single zone can match the UUID; exit once we find a match
  465. // whether "inside" or not
  466. if( area->m_Uuid == target )
  467. return aFunc( area );
  468. }
  469. }
  470. return false;
  471. }
  472. else // Match on zone name
  473. {
  474. for( ZONE* area : aBoard->Zones() )
  475. {
  476. if( area->GetZoneName().Matches( aArg ) )
  477. {
  478. // Many zones can match the name; exit only when we find an "inside"
  479. if( aFunc( area ) )
  480. return true;
  481. }
  482. }
  483. for( FOOTPRINT* footprint : aBoard->Footprints() )
  484. {
  485. for( ZONE* area : footprint->Zones() )
  486. {
  487. // Many zones can match the name; exit only when we find an "inside"
  488. if( area->GetZoneName().Matches( aArg ) )
  489. {
  490. if( aFunc( area ) )
  491. return true;
  492. }
  493. }
  494. }
  495. return false;
  496. }
  497. }
  498. class SCOPED_LAYERSET
  499. {
  500. public:
  501. SCOPED_LAYERSET( BOARD_ITEM* aItem )
  502. {
  503. m_item = aItem;
  504. m_layers = aItem->GetLayerSet();
  505. }
  506. ~SCOPED_LAYERSET()
  507. {
  508. m_item->SetLayerSet( m_layers );
  509. }
  510. void Add( PCB_LAYER_ID aLayer )
  511. {
  512. m_item->SetLayerSet( m_item->GetLayerSet().set( aLayer ) );
  513. }
  514. private:
  515. BOARD_ITEM* m_item;
  516. LSET m_layers;
  517. };
  518. #define MISSING_AREA_ARG( f ) \
  519. wxString::Format( _( "Missing rule-area argument (A, B, or rule-area name) to %s." ), f )
  520. static void intersectsAreaFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  521. {
  522. PCBEXPR_CONTEXT* context = static_cast<PCBEXPR_CONTEXT*>( aCtx );
  523. LIBEVAL::VALUE* arg = aCtx->Pop();
  524. LIBEVAL::VALUE* result = aCtx->AllocValue();
  525. result->Set( 0.0 );
  526. aCtx->Push( result );
  527. if( !arg || arg->AsString().IsEmpty() )
  528. {
  529. if( aCtx->HasErrorCallback() )
  530. aCtx->ReportError( MISSING_AREA_ARG( wxT( "intersectsArea()" ) ) );
  531. return;
  532. }
  533. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  534. BOARD_ITEM* item = vref ? vref->GetObject( context ) : nullptr;
  535. if( !item )
  536. return;
  537. result->SetDeferredEval(
  538. [item, arg, context]() -> double
  539. {
  540. BOARD* board = item->GetBoard();
  541. PCB_LAYER_ID aLayer = context->GetLayer();
  542. BOX2I itemBBox = item->GetBoundingBox();
  543. if( searchAreas( board, arg->AsString(), context,
  544. [&]( ZONE* aArea )
  545. {
  546. if( !aArea || aArea == item || aArea->GetParent() == item )
  547. return false;
  548. SCOPED_LAYERSET scopedLayerSet( aArea );
  549. if( context->GetConstraint() == SILK_CLEARANCE_CONSTRAINT )
  550. {
  551. // Silk clearance tests are run across layer pairs
  552. if( ( aArea->IsOnLayer( F_SilkS ) && IsFrontLayer( aLayer ) )
  553. || ( aArea->IsOnLayer( B_SilkS ) && IsBackLayer( aLayer ) ) )
  554. {
  555. scopedLayerSet.Add( aLayer );
  556. }
  557. }
  558. LSET commonLayers = aArea->GetLayerSet() & item->GetLayerSet();
  559. if( !commonLayers.any() )
  560. return false;
  561. if( !aArea->GetBoundingBox().Intersects( itemBBox ) )
  562. return false;
  563. LSET testLayers;
  564. if( aLayer != UNDEFINED_LAYER )
  565. testLayers.set( aLayer );
  566. else
  567. testLayers = commonLayers;
  568. for( PCB_LAYER_ID layer : testLayers.UIOrder() )
  569. {
  570. PTR_PTR_LAYER_CACHE_KEY key = { aArea, item, layer };
  571. if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
  572. {
  573. std::shared_lock<std::shared_mutex> readLock( board->m_CachesMutex );
  574. auto i = board->m_IntersectsAreaCache.find( key );
  575. if( i != board->m_IntersectsAreaCache.end() && i->second )
  576. return true;
  577. }
  578. bool collides = collidesWithArea( item, context, aArea );
  579. if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
  580. {
  581. std::unique_lock<std::shared_mutex> writeLock( board->m_CachesMutex );
  582. board->m_IntersectsAreaCache[ key ] = collides;
  583. }
  584. if( collides )
  585. return true;
  586. }
  587. return false;
  588. } ) )
  589. {
  590. return 1.0;
  591. }
  592. return 0.0;
  593. } );
  594. }
  595. static void enclosedByAreaFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  596. {
  597. PCBEXPR_CONTEXT* context = static_cast<PCBEXPR_CONTEXT*>( aCtx );
  598. LIBEVAL::VALUE* arg = aCtx->Pop();
  599. LIBEVAL::VALUE* result = aCtx->AllocValue();
  600. result->Set( 0.0 );
  601. aCtx->Push( result );
  602. if( !arg || arg->AsString().IsEmpty() )
  603. {
  604. if( aCtx->HasErrorCallback() )
  605. aCtx->ReportError( MISSING_AREA_ARG( wxT( "enclosedByArea()" ) ) );
  606. return;
  607. }
  608. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  609. BOARD_ITEM* item = vref ? vref->GetObject( context ) : nullptr;
  610. if( !item )
  611. return;
  612. result->SetDeferredEval(
  613. [item, arg, context]() -> double
  614. {
  615. BOARD* board = item->GetBoard();
  616. int maxError = board->GetDesignSettings().m_MaxError;
  617. PCB_LAYER_ID layer = context->GetLayer();
  618. BOX2I itemBBox = item->GetBoundingBox();
  619. if( searchAreas( board, arg->AsString(), context,
  620. [&]( ZONE* aArea )
  621. {
  622. if( !aArea || aArea == item || aArea->GetParent() == item )
  623. return false;
  624. if( !( aArea->GetLayerSet() & item->GetLayerSet() ).any() )
  625. return false;
  626. if( !aArea->GetBoundingBox().Intersects( itemBBox ) )
  627. return false;
  628. PTR_PTR_LAYER_CACHE_KEY key = { aArea, item, layer };
  629. if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
  630. {
  631. std::shared_lock<std::shared_mutex> readLock( board->m_CachesMutex );
  632. auto i = board->m_EnclosedByAreaCache.find( key );
  633. if( i != board->m_EnclosedByAreaCache.end() )
  634. return i->second;
  635. }
  636. SHAPE_POLY_SET itemShape;
  637. bool enclosedByArea;
  638. if( item->Type() == PCB_ZONE_T )
  639. {
  640. itemShape = *static_cast<ZONE*>( item )->Outline();
  641. }
  642. else
  643. {
  644. item->TransformShapeToPolygon( itemShape, layer, 0, maxError,
  645. ERROR_OUTSIDE );
  646. }
  647. if( itemShape.IsEmpty() )
  648. {
  649. // If it's already empty then our test will have no meaning.
  650. enclosedByArea = false;
  651. }
  652. else
  653. {
  654. itemShape.BooleanSubtract( *aArea->Outline(),
  655. SHAPE_POLY_SET::PM_FAST );
  656. enclosedByArea = itemShape.IsEmpty();
  657. }
  658. if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
  659. {
  660. std::unique_lock<std::shared_mutex> writeLock( board->m_CachesMutex );
  661. board->m_EnclosedByAreaCache[ key ] = enclosedByArea;
  662. }
  663. return enclosedByArea;
  664. } ) )
  665. {
  666. return 1.0;
  667. }
  668. return 0.0;
  669. } );
  670. }
  671. #define MISSING_GROUP_ARG( f ) \
  672. wxString::Format( _( "Missing group name argument to %s." ), f )
  673. static void memberOfGroupFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  674. {
  675. LIBEVAL::VALUE* arg = aCtx->Pop();
  676. LIBEVAL::VALUE* result = aCtx->AllocValue();
  677. result->Set( 0.0 );
  678. aCtx->Push( result );
  679. if( !arg || arg->AsString().IsEmpty() )
  680. {
  681. if( aCtx->HasErrorCallback() )
  682. aCtx->ReportError( MISSING_GROUP_ARG( wxT( "memberOfGroup()" ) ) );
  683. return;
  684. }
  685. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  686. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  687. if( !item )
  688. return;
  689. result->SetDeferredEval(
  690. [item, arg]() -> double
  691. {
  692. PCB_GROUP* group = item->GetParentGroup();
  693. if( !group && item->GetParent() && item->GetParent()->Type() == PCB_FOOTPRINT_T )
  694. group = item->GetParent()->GetParentGroup();
  695. while( group )
  696. {
  697. if( group->GetName().Matches( arg->AsString() ) )
  698. return 1.0;
  699. group = group->GetParentGroup();
  700. }
  701. return 0.0;
  702. } );
  703. }
  704. #define MISSING_SHEET_ARG( f ) \
  705. wxString::Format( _( "Missing sheet name argument to %s." ), f )
  706. static void memberOfSheetFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  707. {
  708. LIBEVAL::VALUE* arg = aCtx->Pop();
  709. LIBEVAL::VALUE* result = aCtx->AllocValue();
  710. result->Set( 0.0 );
  711. aCtx->Push( result );
  712. if( !arg || arg->AsString().IsEmpty() )
  713. {
  714. if( aCtx->HasErrorCallback() )
  715. aCtx->ReportError( MISSING_SHEET_ARG( wxT( "memberOfSheet()" ) ) );
  716. return;
  717. }
  718. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  719. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  720. if( !item )
  721. return;
  722. result->SetDeferredEval(
  723. [item, arg]() -> double
  724. {
  725. FOOTPRINT* fp = dyn_cast<FOOTPRINT*>( item );
  726. if( !fp )
  727. fp = item->GetParentFootprint();
  728. if( !fp )
  729. return 0.0;
  730. if( fp->GetSheetname().Matches( arg->AsString() ) )
  731. return 1.0;
  732. if( ( arg->AsString().Matches( wxT( "/" ) ) || arg->AsString().IsEmpty() )
  733. && fp->GetSheetname() == wxT( "Root" ) )
  734. {
  735. return 1.0;
  736. }
  737. return 0.0;
  738. } );
  739. }
  740. #define MISSING_REF_ARG( f ) \
  741. wxString::Format( _( "Missing footprint argument (reference designator) to %s." ), f )
  742. static void memberOfFootprintFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  743. {
  744. LIBEVAL::VALUE* arg = aCtx->Pop();
  745. LIBEVAL::VALUE* result = aCtx->AllocValue();
  746. result->Set( 0.0 );
  747. aCtx->Push( result );
  748. if( !arg || arg->AsString().IsEmpty() )
  749. {
  750. if( aCtx->HasErrorCallback() )
  751. aCtx->ReportError( MISSING_REF_ARG( wxT( "memberOfFootprint()" ) ) );
  752. return;
  753. }
  754. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  755. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  756. if( !item )
  757. return;
  758. result->SetDeferredEval(
  759. [item, arg]() -> double
  760. {
  761. ;
  762. if( FOOTPRINT* parentFP = item->GetParentFootprint() )
  763. {
  764. if( parentFP->GetReference().Matches( arg->AsString() ) )
  765. return 1.0;
  766. if( arg->AsString().Contains( ':' )
  767. && parentFP->GetFPIDAsString().Matches( arg->AsString() ) )
  768. {
  769. return 1.0;
  770. }
  771. }
  772. return 0.0;
  773. } );
  774. }
  775. static void isMicroVia( LIBEVAL::CONTEXT* aCtx, void* self )
  776. {
  777. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  778. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  779. LIBEVAL::VALUE* result = aCtx->AllocValue();
  780. result->Set( 0.0 );
  781. aCtx->Push( result );
  782. if( item && item->Type() == PCB_VIA_T
  783. && static_cast<PCB_VIA*>( item )->GetViaType() == VIATYPE::MICROVIA )
  784. {
  785. result->Set ( 1.0 );
  786. }
  787. }
  788. static void isBlindBuriedViaFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  789. {
  790. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  791. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  792. LIBEVAL::VALUE* result = aCtx->AllocValue();
  793. result->Set( 0.0 );
  794. aCtx->Push( result );
  795. if( item && item->Type() == PCB_VIA_T
  796. && static_cast<PCB_VIA*>( item )->GetViaType() == VIATYPE::BLIND_BURIED )
  797. {
  798. result->Set ( 1.0 );
  799. }
  800. }
  801. static void isCoupledDiffPairFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  802. {
  803. PCBEXPR_CONTEXT* context = static_cast<PCBEXPR_CONTEXT*>( aCtx );
  804. BOARD_CONNECTED_ITEM* a = dynamic_cast<BOARD_CONNECTED_ITEM*>( context->GetItem( 0 ) );
  805. BOARD_CONNECTED_ITEM* b = dynamic_cast<BOARD_CONNECTED_ITEM*>( context->GetItem( 1 ) );
  806. LIBEVAL::VALUE* result = aCtx->AllocValue();
  807. result->Set( 0.0 );
  808. aCtx->Push( result );
  809. result->SetDeferredEval(
  810. [a, b, context]() -> double
  811. {
  812. NETINFO_ITEM* netinfo = a ? a->GetNet() : nullptr;
  813. if( !netinfo )
  814. return 0.0;
  815. wxString coupledNet;
  816. wxString dummy;
  817. if( !DRC_ENGINE::MatchDpSuffix( netinfo->GetNetname(), coupledNet, dummy ) )
  818. return 0.0;
  819. if( context->GetConstraint() == DRC_CONSTRAINT_T::LENGTH_CONSTRAINT
  820. || context->GetConstraint() == DRC_CONSTRAINT_T::SKEW_CONSTRAINT )
  821. {
  822. // DRC engine evaluates these singly, so we won't have a B item
  823. return 1.0;
  824. }
  825. return b && b->GetNetname() == coupledNet;
  826. } );
  827. }
  828. #define MISSING_DP_ARG( f ) \
  829. wxString::Format( _( "Missing diff-pair name argument to %s." ), f )
  830. static void inDiffPairFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  831. {
  832. LIBEVAL::VALUE* argv = aCtx->Pop();
  833. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  834. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  835. LIBEVAL::VALUE* result = aCtx->AllocValue();
  836. result->Set( 0.0 );
  837. aCtx->Push( result );
  838. if( !argv || argv->AsString().IsEmpty() )
  839. {
  840. if( aCtx->HasErrorCallback() )
  841. aCtx->ReportError( MISSING_DP_ARG( wxT( "inDiffPair()" ) ) );
  842. return;
  843. }
  844. if( !item || !item->GetBoard() )
  845. return;
  846. result->SetDeferredEval(
  847. [item, argv]() -> double
  848. {
  849. if( item && item->IsConnected() )
  850. {
  851. NETINFO_ITEM* netinfo = static_cast<BOARD_CONNECTED_ITEM*>( item )->GetNet();
  852. if( !netinfo )
  853. return 0.0;
  854. wxString refName = netinfo->GetNetname();
  855. wxString arg = argv->AsString();
  856. wxString baseName, coupledNet;
  857. int polarity = DRC_ENGINE::MatchDpSuffix( refName, coupledNet, baseName );
  858. if( polarity != 0 && item->GetBoard()->FindNet( coupledNet ) )
  859. {
  860. if( baseName.Matches( arg ) )
  861. return 1.0;
  862. if( baseName.EndsWith( "_" ) && baseName.BeforeLast( '_' ).Matches( arg ) )
  863. return 1.0;
  864. }
  865. }
  866. return 0.0;
  867. } );
  868. }
  869. static void getFieldFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  870. {
  871. LIBEVAL::VALUE* arg = aCtx->Pop();
  872. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  873. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  874. LIBEVAL::VALUE* result = aCtx->AllocValue();
  875. result->Set( "" );
  876. aCtx->Push( result );
  877. if( !arg )
  878. {
  879. if( aCtx->HasErrorCallback() )
  880. {
  881. aCtx->ReportError( wxString::Format( _( "Missing field name argument to %s." ),
  882. wxT( "getField()" ) ) );
  883. }
  884. return;
  885. }
  886. if( !item || !item->GetBoard() )
  887. return;
  888. result->SetDeferredEval(
  889. [item, arg]() -> wxString
  890. {
  891. if( item && item->Type() == PCB_FOOTPRINT_T )
  892. {
  893. FOOTPRINT* fp = static_cast<FOOTPRINT*>( item );
  894. PCB_FIELD* field = fp->GetFieldByName( arg->AsString() );
  895. if( field )
  896. return field->GetText();
  897. }
  898. return "";
  899. } );
  900. }
  901. PCBEXPR_BUILTIN_FUNCTIONS::PCBEXPR_BUILTIN_FUNCTIONS()
  902. {
  903. RegisterAllFunctions();
  904. }
  905. void PCBEXPR_BUILTIN_FUNCTIONS::RegisterAllFunctions()
  906. {
  907. m_funcs.clear();
  908. RegisterFunc( wxT( "existsOnLayer('x')" ), existsOnLayerFunc );
  909. RegisterFunc( wxT( "isPlated()" ), isPlatedFunc );
  910. RegisterFunc( wxT( "insideCourtyard('x') DEPRECATED" ), intersectsCourtyardFunc );
  911. RegisterFunc( wxT( "insideFrontCourtyard('x') DEPRECATED" ), intersectsFrontCourtyardFunc );
  912. RegisterFunc( wxT( "insideBackCourtyard('x') DEPRECATED" ), intersectsBackCourtyardFunc );
  913. RegisterFunc( wxT( "intersectsCourtyard('x')" ), intersectsCourtyardFunc );
  914. RegisterFunc( wxT( "intersectsFrontCourtyard('x')" ), intersectsFrontCourtyardFunc );
  915. RegisterFunc( wxT( "intersectsBackCourtyard('x')" ), intersectsBackCourtyardFunc );
  916. RegisterFunc( wxT( "insideArea('x') DEPRECATED" ), intersectsAreaFunc );
  917. RegisterFunc( wxT( "intersectsArea('x')" ), intersectsAreaFunc );
  918. RegisterFunc( wxT( "enclosedByArea('x')" ), enclosedByAreaFunc );
  919. RegisterFunc( wxT( "isMicroVia()" ), isMicroVia );
  920. RegisterFunc( wxT( "isBlindBuriedVia()" ), isBlindBuriedViaFunc );
  921. RegisterFunc( wxT( "memberOf('x') DEPRECATED" ), memberOfGroupFunc );
  922. RegisterFunc( wxT( "memberOfGroup('x')" ), memberOfGroupFunc );
  923. RegisterFunc( wxT( "memberOfFootprint('x')" ), memberOfFootprintFunc );
  924. RegisterFunc( wxT( "memberOfSheet('x')" ), memberOfSheetFunc );
  925. RegisterFunc( wxT( "fromTo('x','y')" ), fromToFunc );
  926. RegisterFunc( wxT( "isCoupledDiffPair()" ), isCoupledDiffPairFunc );
  927. RegisterFunc( wxT( "inDiffPair('x')" ), inDiffPairFunc );
  928. RegisterFunc( wxT( "getField('x')" ), getFieldFunc );
  929. }