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.

1148 lines
36 KiB

  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. }
  187. return false;
  188. }
  189. #define MISSING_FP_ARG( f ) \
  190. wxString::Format( _( "Missing footprint argument (A, B, or reference designator) to %s." ), f )
  191. static void intersectsCourtyardFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  192. {
  193. PCBEXPR_CONTEXT* context = static_cast<PCBEXPR_CONTEXT*>( aCtx );
  194. LIBEVAL::VALUE* arg = context->Pop();
  195. LIBEVAL::VALUE* result = context->AllocValue();
  196. result->Set( 0.0 );
  197. context->Push( result );
  198. if( !arg || arg->AsString().IsEmpty() )
  199. {
  200. if( context->HasErrorCallback() )
  201. context->ReportError( MISSING_FP_ARG( wxT( "intersectsCourtyard()" ) ) );
  202. return;
  203. }
  204. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  205. BOARD_ITEM* item = vref ? vref->GetObject( context ) : nullptr;
  206. if( !item )
  207. return;
  208. result->SetDeferredEval(
  209. [item, arg, context]() -> double
  210. {
  211. BOARD* board = item->GetBoard();
  212. std::shared_ptr<SHAPE> itemShape;
  213. if( searchFootprints( board, arg->AsString(), context,
  214. [&]( FOOTPRINT* fp )
  215. {
  216. PTR_PTR_CACHE_KEY key = { fp, item };
  217. if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
  218. {
  219. std::shared_lock<std::shared_mutex> readLock( board->m_CachesMutex );
  220. auto i = board->m_IntersectsCourtyardCache.find( key );
  221. if( i != board->m_IntersectsCourtyardCache.end() )
  222. return i->second;
  223. }
  224. bool res = collidesWithCourtyard( item, itemShape, context, fp, F_Cu )
  225. || collidesWithCourtyard( item, itemShape, context, fp, B_Cu );
  226. if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
  227. {
  228. std::unique_lock<std::shared_mutex> cacheLock( board->m_CachesMutex );
  229. board->m_IntersectsCourtyardCache[ key ] = res;
  230. }
  231. return res;
  232. } ) )
  233. {
  234. return 1.0;
  235. }
  236. return 0.0;
  237. } );
  238. }
  239. static void intersectsFrontCourtyardFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  240. {
  241. PCBEXPR_CONTEXT* context = static_cast<PCBEXPR_CONTEXT*>( aCtx );
  242. LIBEVAL::VALUE* arg = context->Pop();
  243. LIBEVAL::VALUE* result = context->AllocValue();
  244. result->Set( 0.0 );
  245. context->Push( result );
  246. if( !arg || arg->AsString().IsEmpty() )
  247. {
  248. if( context->HasErrorCallback() )
  249. context->ReportError( MISSING_FP_ARG( wxT( "intersectsFrontCourtyard()" ) ) );
  250. return;
  251. }
  252. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  253. BOARD_ITEM* item = vref ? vref->GetObject( context ) : nullptr;
  254. if( !item )
  255. return;
  256. result->SetDeferredEval(
  257. [item, arg, context]() -> double
  258. {
  259. BOARD* board = item->GetBoard();
  260. std::shared_ptr<SHAPE> itemShape;
  261. if( searchFootprints( board, arg->AsString(), context,
  262. [&]( FOOTPRINT* fp )
  263. {
  264. PTR_PTR_CACHE_KEY key = { fp, item };
  265. if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
  266. {
  267. std::shared_lock<std::shared_mutex> readLock( board->m_CachesMutex );
  268. auto i = board->m_IntersectsFCourtyardCache.find( key );
  269. if( i != board->m_IntersectsFCourtyardCache.end() )
  270. return i->second;
  271. }
  272. bool res = collidesWithCourtyard( item, itemShape, context, fp, F_Cu );
  273. if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
  274. {
  275. std::unique_lock<std::shared_mutex> writeLock( board->m_CachesMutex );
  276. board->m_IntersectsFCourtyardCache[ key ] = res;
  277. }
  278. return res;
  279. } ) )
  280. {
  281. return 1.0;
  282. }
  283. return 0.0;
  284. } );
  285. }
  286. static void intersectsBackCourtyardFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  287. {
  288. PCBEXPR_CONTEXT* context = static_cast<PCBEXPR_CONTEXT*>( aCtx );
  289. LIBEVAL::VALUE* arg = context->Pop();
  290. LIBEVAL::VALUE* result = context->AllocValue();
  291. result->Set( 0.0 );
  292. context->Push( result );
  293. if( !arg || arg->AsString().IsEmpty() )
  294. {
  295. if( context->HasErrorCallback() )
  296. context->ReportError( MISSING_FP_ARG( wxT( "intersectsBackCourtyard()" ) ) );
  297. return;
  298. }
  299. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  300. BOARD_ITEM* item = vref ? vref->GetObject( context ) : nullptr;
  301. if( !item )
  302. return;
  303. result->SetDeferredEval(
  304. [item, arg, context]() -> double
  305. {
  306. BOARD* board = item->GetBoard();
  307. std::shared_ptr<SHAPE> itemShape;
  308. if( searchFootprints( board, arg->AsString(), context,
  309. [&]( FOOTPRINT* fp )
  310. {
  311. PTR_PTR_CACHE_KEY key = { fp, item };
  312. if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
  313. {
  314. std::shared_lock<std::shared_mutex> readLock( board->m_CachesMutex );
  315. auto i = board->m_IntersectsBCourtyardCache.find( key );
  316. if( i != board->m_IntersectsBCourtyardCache.end() )
  317. return i->second;
  318. }
  319. bool res = collidesWithCourtyard( item, itemShape, context, fp, B_Cu );
  320. if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
  321. {
  322. std::unique_lock<std::shared_mutex> writeLock( board->m_CachesMutex );
  323. board->m_IntersectsBCourtyardCache[ key ] = res;
  324. }
  325. return res;
  326. } ) )
  327. {
  328. return 1.0;
  329. }
  330. return 0.0;
  331. } );
  332. }
  333. bool collidesWithArea( BOARD_ITEM* aItem, PCBEXPR_CONTEXT* aCtx, ZONE* aArea )
  334. {
  335. BOARD* board = aArea->GetBoard();
  336. BOX2I areaBBox = aArea->GetBoundingBox();
  337. std::shared_ptr<SHAPE> shape;
  338. // Collisions include touching, so we need to deflate outline by enough to exclude it.
  339. // This is particularly important for detecting copper fills as they will be exactly
  340. // touching along the entire exclusion border.
  341. SHAPE_POLY_SET areaOutline = aArea->Outline()->CloneDropTriangulation();
  342. areaOutline.ClearArcs();
  343. areaOutline.Deflate( board->GetDesignSettings().GetDRCEpsilon(),
  344. CORNER_STRATEGY::ALLOW_ACUTE_CORNERS, ARC_LOW_DEF );
  345. if( aItem->GetFlags() & HOLE_PROXY )
  346. {
  347. if( aItem->Type() == PCB_PAD_T )
  348. {
  349. return areaOutline.Collide( aItem->GetEffectiveHoleShape().get() );
  350. }
  351. else if( aItem->Type() == PCB_VIA_T )
  352. {
  353. LSET overlap = aItem->GetLayerSet() & aArea->GetLayerSet();
  354. /// Avoid buried vias that don't overlap the zone's layers
  355. if( overlap.any() )
  356. {
  357. if( aCtx->GetLayer() == UNDEFINED_LAYER || overlap.Contains( aCtx->GetLayer() ) )
  358. return areaOutline.Collide( aItem->GetEffectiveHoleShape().get() );
  359. }
  360. }
  361. return false;
  362. }
  363. if( aItem->Type() == PCB_FOOTPRINT_T )
  364. {
  365. FOOTPRINT* footprint = static_cast<FOOTPRINT*>( aItem );
  366. if( ( footprint->GetFlags() & MALFORMED_COURTYARDS ) != 0 )
  367. {
  368. if( aCtx->HasErrorCallback() )
  369. aCtx->ReportError( _( "Footprint's courtyard is not a single, closed shape." ) );
  370. return false;
  371. }
  372. if( ( aArea->GetLayerSet() & LSET::FrontMask() ).any() )
  373. {
  374. const SHAPE_POLY_SET& courtyard = footprint->GetCourtyard( F_CrtYd );
  375. if( courtyard.OutlineCount() == 0 )
  376. {
  377. if( aCtx->HasErrorCallback() )
  378. aCtx->ReportError( _( "Footprint has no front courtyard." ) );
  379. }
  380. else if( areaOutline.Collide( &courtyard.Outline( 0 ) ) )
  381. {
  382. return true;
  383. }
  384. }
  385. if( ( aArea->GetLayerSet() & LSET::BackMask() ).any() )
  386. {
  387. const SHAPE_POLY_SET& courtyard = footprint->GetCourtyard( B_CrtYd );
  388. if( courtyard.OutlineCount() == 0 )
  389. {
  390. if( aCtx->HasErrorCallback() )
  391. aCtx->ReportError( _( "Footprint has no back courtyard." ) );
  392. }
  393. else if( areaOutline.Collide( &courtyard.Outline( 0 ) ) )
  394. {
  395. return true;
  396. }
  397. }
  398. return false;
  399. }
  400. if( aItem->Type() == PCB_ZONE_T )
  401. {
  402. ZONE* zone = static_cast<ZONE*>( aItem );
  403. if( !zone->IsFilled() )
  404. return false;
  405. DRC_RTREE* zoneRTree = board->m_CopperZoneRTreeCache[ zone ].get();
  406. if( zoneRTree )
  407. {
  408. for( PCB_LAYER_ID layer : aArea->GetLayerSet().Seq() )
  409. {
  410. if( aCtx->GetLayer() == layer || aCtx->GetLayer() == UNDEFINED_LAYER )
  411. {
  412. if( zoneRTree->QueryColliding( areaBBox, &areaOutline, layer ) )
  413. return true;
  414. }
  415. }
  416. }
  417. return false;
  418. }
  419. else
  420. {
  421. PCB_LAYER_ID layer = aCtx->GetLayer();
  422. if( layer != UNDEFINED_LAYER && !( aArea->GetLayerSet().Contains( layer ) ) )
  423. return false;
  424. if( !shape )
  425. shape = aItem->GetEffectiveShape( layer );
  426. return areaOutline.Collide( shape.get() );
  427. }
  428. }
  429. bool searchAreas( BOARD* aBoard, const wxString& aArg, PCBEXPR_CONTEXT* aCtx,
  430. const std::function<bool( ZONE* )>& aFunc )
  431. {
  432. if( aArg == wxT( "A" ) )
  433. {
  434. return aFunc( dynamic_cast<ZONE*>( aCtx->GetItem( 0 ) ) );
  435. }
  436. else if( aArg == wxT( "B" ) )
  437. {
  438. return aFunc( dynamic_cast<ZONE*>( aCtx->GetItem( 1 ) ) );
  439. }
  440. else if( KIID::SniffTest( aArg ) )
  441. {
  442. KIID target( aArg );
  443. for( ZONE* area : aBoard->Zones() )
  444. {
  445. // Only a single zone can match the UUID; exit once we find a match whether
  446. // "inside" or not
  447. if( area->m_Uuid == target )
  448. return aFunc( area );
  449. }
  450. for( FOOTPRINT* footprint : aBoard->Footprints() )
  451. {
  452. for( ZONE* area : footprint->Zones() )
  453. {
  454. // Only a single zone can match the UUID; exit once we find a match
  455. // whether "inside" or not
  456. if( area->m_Uuid == target )
  457. return aFunc( area );
  458. }
  459. }
  460. return false;
  461. }
  462. else // Match on zone name
  463. {
  464. for( ZONE* area : aBoard->Zones() )
  465. {
  466. if( area->GetZoneName().Matches( aArg ) )
  467. {
  468. // Many zones can match the name; exit only when we find an "inside"
  469. if( aFunc( area ) )
  470. return true;
  471. }
  472. }
  473. for( FOOTPRINT* footprint : aBoard->Footprints() )
  474. {
  475. for( ZONE* area : footprint->Zones() )
  476. {
  477. // Many zones can match the name; exit only when we find an "inside"
  478. if( area->GetZoneName().Matches( aArg ) )
  479. {
  480. if( aFunc( area ) )
  481. return true;
  482. }
  483. }
  484. }
  485. return false;
  486. }
  487. }
  488. #define MISSING_AREA_ARG( f ) \
  489. wxString::Format( _( "Missing rule-area argument (A, B, or rule-area name) to %s." ), f )
  490. static void intersectsAreaFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  491. {
  492. PCBEXPR_CONTEXT* context = static_cast<PCBEXPR_CONTEXT*>( aCtx );
  493. LIBEVAL::VALUE* arg = aCtx->Pop();
  494. LIBEVAL::VALUE* result = aCtx->AllocValue();
  495. result->Set( 0.0 );
  496. aCtx->Push( result );
  497. if( !arg || arg->AsString().IsEmpty() )
  498. {
  499. if( aCtx->HasErrorCallback() )
  500. aCtx->ReportError( MISSING_AREA_ARG( wxT( "intersectsArea()" ) ) );
  501. return;
  502. }
  503. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  504. BOARD_ITEM* item = vref ? vref->GetObject( context ) : nullptr;
  505. if( !item )
  506. return;
  507. result->SetDeferredEval(
  508. [item, arg, context]() -> double
  509. {
  510. BOARD* board = item->GetBoard();
  511. PCB_LAYER_ID aLayer = context->GetLayer();
  512. BOX2I itemBBox = item->GetBoundingBox();
  513. if( searchAreas( board, arg->AsString(), context,
  514. [&]( ZONE* aArea )
  515. {
  516. if( !aArea || aArea == item || aArea->GetParent() == item )
  517. return false;
  518. LSET commonLayers = aArea->GetLayerSet() & item->GetLayerSet();
  519. if( !commonLayers.any() )
  520. return false;
  521. if( !aArea->GetBoundingBox().Intersects( itemBBox ) )
  522. return false;
  523. LSET testLayers;
  524. if( aLayer != UNDEFINED_LAYER )
  525. testLayers.set( aLayer );
  526. else
  527. testLayers = commonLayers;
  528. for( PCB_LAYER_ID layer : testLayers.UIOrder() )
  529. {
  530. PTR_PTR_LAYER_CACHE_KEY key = { aArea, item, layer };
  531. if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
  532. {
  533. std::shared_lock<std::shared_mutex> readLock( board->m_CachesMutex );
  534. auto i = board->m_IntersectsAreaCache.find( key );
  535. if( i != board->m_IntersectsAreaCache.end() && i->second )
  536. return true;
  537. }
  538. bool collides = collidesWithArea( item, context, aArea );
  539. if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
  540. {
  541. std::unique_lock<std::shared_mutex> writeLock( board->m_CachesMutex );
  542. board->m_IntersectsAreaCache[ key ] = collides;
  543. }
  544. if( collides )
  545. return true;
  546. }
  547. return false;
  548. } ) )
  549. {
  550. return 1.0;
  551. }
  552. return 0.0;
  553. } );
  554. }
  555. static void enclosedByAreaFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  556. {
  557. PCBEXPR_CONTEXT* context = static_cast<PCBEXPR_CONTEXT*>( aCtx );
  558. LIBEVAL::VALUE* arg = aCtx->Pop();
  559. LIBEVAL::VALUE* result = aCtx->AllocValue();
  560. result->Set( 0.0 );
  561. aCtx->Push( result );
  562. if( !arg || arg->AsString().IsEmpty() )
  563. {
  564. if( aCtx->HasErrorCallback() )
  565. aCtx->ReportError( MISSING_AREA_ARG( wxT( "enclosedByArea()" ) ) );
  566. return;
  567. }
  568. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  569. BOARD_ITEM* item = vref ? vref->GetObject( context ) : nullptr;
  570. if( !item )
  571. return;
  572. result->SetDeferredEval(
  573. [item, arg, context]() -> double
  574. {
  575. BOARD* board = item->GetBoard();
  576. int maxError = board->GetDesignSettings().m_MaxError;
  577. PCB_LAYER_ID layer = context->GetLayer();
  578. BOX2I itemBBox = item->GetBoundingBox();
  579. if( searchAreas( board, arg->AsString(), context,
  580. [&]( ZONE* aArea )
  581. {
  582. if( !aArea || aArea == item || aArea->GetParent() == item )
  583. return false;
  584. if( !( aArea->GetLayerSet() & item->GetLayerSet() ).any() )
  585. return false;
  586. if( !aArea->GetBoundingBox().Intersects( itemBBox ) )
  587. return false;
  588. PTR_PTR_LAYER_CACHE_KEY key = { aArea, item, layer };
  589. if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
  590. {
  591. std::shared_lock<std::shared_mutex> readLock( board->m_CachesMutex );
  592. auto i = board->m_EnclosedByAreaCache.find( key );
  593. if( i != board->m_EnclosedByAreaCache.end() )
  594. return i->second;
  595. }
  596. SHAPE_POLY_SET itemShape;
  597. bool enclosedByArea;
  598. item->TransformShapeToPolygon( itemShape, layer, 0, maxError,
  599. ERROR_OUTSIDE );
  600. if( itemShape.IsEmpty() )
  601. {
  602. // If it's already empty then our test will have no meaning.
  603. enclosedByArea = false;
  604. }
  605. else
  606. {
  607. itemShape.BooleanSubtract( *aArea->Outline(),
  608. SHAPE_POLY_SET::PM_FAST );
  609. enclosedByArea = itemShape.IsEmpty();
  610. }
  611. if( ( item->GetFlags() & ROUTER_TRANSIENT ) == 0 )
  612. {
  613. std::unique_lock<std::shared_mutex> writeLock( board->m_CachesMutex );
  614. board->m_EnclosedByAreaCache[ key ] = enclosedByArea;
  615. }
  616. return enclosedByArea;
  617. } ) )
  618. {
  619. return 1.0;
  620. }
  621. return 0.0;
  622. } );
  623. }
  624. #define MISSING_GROUP_ARG( f ) \
  625. wxString::Format( _( "Missing group name argument to %s." ), f )
  626. static void memberOfGroupFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  627. {
  628. LIBEVAL::VALUE* arg = aCtx->Pop();
  629. LIBEVAL::VALUE* result = aCtx->AllocValue();
  630. result->Set( 0.0 );
  631. aCtx->Push( result );
  632. if( !arg || arg->AsString().IsEmpty() )
  633. {
  634. if( aCtx->HasErrorCallback() )
  635. aCtx->ReportError( MISSING_GROUP_ARG( wxT( "memberOfGroup()" ) ) );
  636. return;
  637. }
  638. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  639. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  640. if( !item )
  641. return;
  642. result->SetDeferredEval(
  643. [item, arg]() -> double
  644. {
  645. PCB_GROUP* group = item->GetParentGroup();
  646. if( !group && item->GetParent() && item->GetParent()->Type() == PCB_FOOTPRINT_T )
  647. group = item->GetParent()->GetParentGroup();
  648. while( group )
  649. {
  650. if( group->GetName().Matches( arg->AsString() ) )
  651. return 1.0;
  652. group = group->GetParentGroup();
  653. }
  654. return 0.0;
  655. } );
  656. }
  657. #define MISSING_SHEET_ARG( f ) \
  658. wxString::Format( _( "Missing sheet name argument to %s." ), f )
  659. static void memberOfSheetFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  660. {
  661. LIBEVAL::VALUE* arg = aCtx->Pop();
  662. LIBEVAL::VALUE* result = aCtx->AllocValue();
  663. result->Set( 0.0 );
  664. aCtx->Push( result );
  665. if( !arg || arg->AsString().IsEmpty() )
  666. {
  667. if( aCtx->HasErrorCallback() )
  668. aCtx->ReportError( MISSING_SHEET_ARG( wxT( "memberOfSheet()" ) ) );
  669. return;
  670. }
  671. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  672. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  673. if( !item )
  674. return;
  675. result->SetDeferredEval(
  676. [item, arg]() -> double
  677. {
  678. FOOTPRINT* fp = item->GetParentFootprint();
  679. if( !fp )
  680. return 0.0;
  681. if( fp->GetSheetname().Matches( arg->AsString() ) )
  682. return 1.0;
  683. if( ( arg->AsString().Matches( wxT( "/" ) ) || arg->AsString().IsEmpty() )
  684. && fp->GetSheetname().IsEmpty() )
  685. {
  686. return 1.0;
  687. }
  688. return 0.0;
  689. } );
  690. }
  691. #define MISSING_REF_ARG( f ) \
  692. wxString::Format( _( "Missing footprint argument (reference designator) to %s." ), f )
  693. static void memberOfFootprintFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  694. {
  695. LIBEVAL::VALUE* arg = aCtx->Pop();
  696. LIBEVAL::VALUE* result = aCtx->AllocValue();
  697. result->Set( 0.0 );
  698. aCtx->Push( result );
  699. if( !arg || arg->AsString().IsEmpty() )
  700. {
  701. if( aCtx->HasErrorCallback() )
  702. aCtx->ReportError( MISSING_REF_ARG( wxT( "memberOfFootprint()" ) ) );
  703. return;
  704. }
  705. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  706. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  707. if( !item )
  708. return;
  709. result->SetDeferredEval(
  710. [item, arg]() -> double
  711. {
  712. ;
  713. if( FOOTPRINT* parentFP = item->GetParentFootprint() )
  714. {
  715. if( parentFP->GetReference().Matches( arg->AsString() ) )
  716. return 1.0;
  717. if( arg->AsString().Contains( ':' )
  718. && parentFP->GetFPIDAsString().Matches( arg->AsString() ) )
  719. {
  720. return 1.0;
  721. }
  722. }
  723. return 0.0;
  724. } );
  725. }
  726. static void isMicroVia( LIBEVAL::CONTEXT* aCtx, void* self )
  727. {
  728. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  729. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  730. LIBEVAL::VALUE* result = aCtx->AllocValue();
  731. result->Set( 0.0 );
  732. aCtx->Push( result );
  733. if( item && item->Type() == PCB_VIA_T
  734. && static_cast<PCB_VIA*>( item )->GetViaType() == VIATYPE::MICROVIA )
  735. {
  736. result->Set ( 1.0 );
  737. }
  738. }
  739. static void isBlindBuriedViaFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  740. {
  741. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  742. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  743. LIBEVAL::VALUE* result = aCtx->AllocValue();
  744. result->Set( 0.0 );
  745. aCtx->Push( result );
  746. if( item && item->Type() == PCB_VIA_T
  747. && static_cast<PCB_VIA*>( item )->GetViaType() == VIATYPE::BLIND_BURIED )
  748. {
  749. result->Set ( 1.0 );
  750. }
  751. }
  752. static void isCoupledDiffPairFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  753. {
  754. PCBEXPR_CONTEXT* context = static_cast<PCBEXPR_CONTEXT*>( aCtx );
  755. BOARD_CONNECTED_ITEM* a = dynamic_cast<BOARD_CONNECTED_ITEM*>( context->GetItem( 0 ) );
  756. BOARD_CONNECTED_ITEM* b = dynamic_cast<BOARD_CONNECTED_ITEM*>( context->GetItem( 1 ) );
  757. LIBEVAL::VALUE* result = aCtx->AllocValue();
  758. result->Set( 0.0 );
  759. aCtx->Push( result );
  760. result->SetDeferredEval(
  761. [a, b, context]() -> double
  762. {
  763. NETINFO_ITEM* netinfo = a ? a->GetNet() : nullptr;
  764. if( !netinfo )
  765. return 0.0;
  766. wxString coupledNet;
  767. wxString dummy;
  768. if( !DRC_ENGINE::MatchDpSuffix( netinfo->GetNetname(), coupledNet, dummy ) )
  769. return 0.0;
  770. if( context->GetConstraint() == DRC_CONSTRAINT_T::LENGTH_CONSTRAINT
  771. || context->GetConstraint() == DRC_CONSTRAINT_T::SKEW_CONSTRAINT )
  772. {
  773. // DRC engine evaluates these singly, so we won't have a B item
  774. return 1.0;
  775. }
  776. return b && b->GetNetname() == coupledNet;
  777. } );
  778. }
  779. #define MISSING_DP_ARG( f ) \
  780. wxString::Format( _( "Missing diff-pair name argument to %s." ), f )
  781. static void inDiffPairFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  782. {
  783. LIBEVAL::VALUE* argv = aCtx->Pop();
  784. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  785. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  786. LIBEVAL::VALUE* result = aCtx->AllocValue();
  787. result->Set( 0.0 );
  788. aCtx->Push( result );
  789. if( !argv || argv->AsString().IsEmpty() )
  790. {
  791. if( aCtx->HasErrorCallback() )
  792. aCtx->ReportError( MISSING_DP_ARG( wxT( "inDiffPair()" ) ) );
  793. return;
  794. }
  795. if( !item || !item->GetBoard() )
  796. return;
  797. result->SetDeferredEval(
  798. [item, argv]() -> double
  799. {
  800. if( item && item->IsConnected() )
  801. {
  802. NETINFO_ITEM* netinfo = static_cast<BOARD_CONNECTED_ITEM*>( item )->GetNet();
  803. if( !netinfo )
  804. return 0.0;
  805. wxString refName = netinfo->GetNetname();
  806. wxString arg = argv->AsString();
  807. wxString baseName, coupledNet;
  808. int polarity = DRC_ENGINE::MatchDpSuffix( refName, coupledNet, baseName );
  809. if( polarity != 0 && item->GetBoard()->FindNet( coupledNet ) )
  810. {
  811. if( baseName.Matches( arg ) )
  812. return 1.0;
  813. if( baseName.EndsWith( "_" ) && baseName.BeforeLast( '_' ).Matches( arg ) )
  814. return 1.0;
  815. }
  816. }
  817. return 0.0;
  818. } );
  819. }
  820. static void getFieldFunc( LIBEVAL::CONTEXT* aCtx, void* self )
  821. {
  822. LIBEVAL::VALUE* arg = aCtx->Pop();
  823. PCBEXPR_VAR_REF* vref = static_cast<PCBEXPR_VAR_REF*>( self );
  824. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  825. LIBEVAL::VALUE* result = aCtx->AllocValue();
  826. result->Set( "" );
  827. aCtx->Push( result );
  828. if( !arg )
  829. {
  830. if( aCtx->HasErrorCallback() )
  831. {
  832. aCtx->ReportError( wxString::Format( _( "Missing field name argument to %s." ),
  833. wxT( "getField()" ) ) );
  834. }
  835. return;
  836. }
  837. if( !item || !item->GetBoard() )
  838. return;
  839. result->SetDeferredEval(
  840. [item, arg]() -> wxString
  841. {
  842. if( item && item->Type() == PCB_FOOTPRINT_T )
  843. {
  844. FOOTPRINT* fp = static_cast<FOOTPRINT*>( item );
  845. PCB_FIELD* field = fp->GetFieldByName( arg->AsString() );
  846. if( field )
  847. return field->GetText();
  848. }
  849. return "";
  850. } );
  851. }
  852. PCBEXPR_BUILTIN_FUNCTIONS::PCBEXPR_BUILTIN_FUNCTIONS()
  853. {
  854. RegisterAllFunctions();
  855. }
  856. void PCBEXPR_BUILTIN_FUNCTIONS::RegisterAllFunctions()
  857. {
  858. m_funcs.clear();
  859. RegisterFunc( wxT( "existsOnLayer('x')" ), existsOnLayerFunc );
  860. RegisterFunc( wxT( "isPlated()" ), isPlatedFunc );
  861. RegisterFunc( wxT( "insideCourtyard('x') DEPRECATED" ), intersectsCourtyardFunc );
  862. RegisterFunc( wxT( "insideFrontCourtyard('x') DEPRECATED" ), intersectsFrontCourtyardFunc );
  863. RegisterFunc( wxT( "insideBackCourtyard('x') DEPRECATED" ), intersectsBackCourtyardFunc );
  864. RegisterFunc( wxT( "intersectsCourtyard('x')" ), intersectsCourtyardFunc );
  865. RegisterFunc( wxT( "intersectsFrontCourtyard('x')" ), intersectsFrontCourtyardFunc );
  866. RegisterFunc( wxT( "intersectsBackCourtyard('x')" ), intersectsBackCourtyardFunc );
  867. RegisterFunc( wxT( "insideArea('x') DEPRECATED" ), intersectsAreaFunc );
  868. RegisterFunc( wxT( "intersectsArea('x')" ), intersectsAreaFunc );
  869. RegisterFunc( wxT( "enclosedByArea('x')" ), enclosedByAreaFunc );
  870. RegisterFunc( wxT( "isMicroVia()" ), isMicroVia );
  871. RegisterFunc( wxT( "isBlindBuriedVia()" ), isBlindBuriedViaFunc );
  872. RegisterFunc( wxT( "memberOf('x') DEPRECATED" ), memberOfGroupFunc );
  873. RegisterFunc( wxT( "memberOfGroup('x')" ), memberOfGroupFunc );
  874. RegisterFunc( wxT( "memberOfFootprint('x')" ), memberOfFootprintFunc );
  875. RegisterFunc( wxT( "memberOfSheet('x')" ), memberOfSheetFunc );
  876. RegisterFunc( wxT( "fromTo('x','y')" ), fromToFunc );
  877. RegisterFunc( wxT( "isCoupledDiffPair()" ), isCoupledDiffPairFunc );
  878. RegisterFunc( wxT( "inDiffPair('x')" ), inDiffPairFunc );
  879. RegisterFunc( wxT( "getField('x')" ), getFieldFunc );
  880. }