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.

576 lines
15 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019-2020 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 <reporter.h>
  26. #include <class_board.h>
  27. #include <class_track.h>
  28. #include <pcb_expr_evaluator.h>
  29. #include <connectivity/connectivity_data.h>
  30. #include <connectivity/connectivity_algo.h>
  31. #include <connectivity/from_to_cache.h>
  32. bool exprFromTo( LIBEVAL::CONTEXT* aCtx, void* self )
  33. {
  34. PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
  35. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  36. LIBEVAL::VALUE* result = aCtx->AllocValue();
  37. LIBEVAL::VALUE* argTo = aCtx->Pop();
  38. LIBEVAL::VALUE* argFrom = aCtx->Pop();
  39. result->Set(0.0);
  40. aCtx->Push( result );
  41. if(!item)
  42. return false;
  43. auto ftCache = item->GetBoard()->GetConnectivity()->GetFromToCache();
  44. if( !ftCache )
  45. {
  46. wxLogWarning( "Attempting to call fromTo() with non-existent from-to cache, aborting...");
  47. return true;
  48. }
  49. int r =0 ;
  50. if( ftCache->IsOnFromToPath( static_cast<BOARD_CONNECTED_ITEM*>( item ),
  51. argFrom->AsString(), argTo->AsString() ) )
  52. {
  53. result->Set(1.0);
  54. r = 1;
  55. }
  56. /*printf("isonfromto %p %s %s -> %d\n", static_cast<BOARD_CONNECTED_ITEM*>( item ),
  57. (const char *)argFrom->AsString(),
  58. (const char *)argTo->AsString(), r );*/
  59. return true;
  60. }
  61. static void existsOnLayer( LIBEVAL::CONTEXT* aCtx, void *self )
  62. {
  63. PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
  64. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  65. LIBEVAL::VALUE* arg = aCtx->Pop();
  66. LIBEVAL::VALUE* result = aCtx->AllocValue();
  67. result->Set( 0.0 );
  68. aCtx->Push( result );
  69. if( !item )
  70. return;
  71. if( !arg )
  72. {
  73. aCtx->ReportError( wxString::Format( _( "Missing argument to '%s'" ),
  74. wxT( "existsOnLayer()" ) ) );
  75. return;
  76. }
  77. wxString layerName = arg->AsString();
  78. wxPGChoices& layerMap = ENUM_MAP<PCB_LAYER_ID>::Instance().Choices();
  79. bool anyMatch = false;
  80. for( unsigned ii = 0; ii < layerMap.GetCount(); ++ii )
  81. {
  82. wxPGChoiceEntry& entry = layerMap[ii];
  83. if( entry.GetText().Matches( layerName ) )
  84. {
  85. anyMatch = true;
  86. if( item->IsOnLayer( ToLAYER_ID( entry.GetValue() ) ) )
  87. {
  88. result->Set( 1.0 );
  89. return;
  90. }
  91. }
  92. }
  93. if( !anyMatch )
  94. aCtx->ReportError( wxString::Format( _( "Unrecognized layer '%s'" ), layerName ) );
  95. }
  96. static void isPlated( LIBEVAL::CONTEXT* aCtx, void* self )
  97. {
  98. LIBEVAL::VALUE* result = aCtx->AllocValue();
  99. result->Set( 0.0 );
  100. aCtx->Push( result );
  101. PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
  102. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  103. D_PAD* pad = dynamic_cast<D_PAD*>( item );
  104. if( pad && pad->GetAttribute() == PAD_ATTRIB_PTH )
  105. result->Set( 1.0 );
  106. }
  107. static void insideCourtyard( LIBEVAL::CONTEXT* aCtx, void* self )
  108. {
  109. PCB_EXPR_CONTEXT* context = static_cast<PCB_EXPR_CONTEXT*>( aCtx );
  110. LIBEVAL::VALUE* arg = aCtx->Pop();
  111. LIBEVAL::VALUE* result = aCtx->AllocValue();
  112. result->Set( 0.0 );
  113. aCtx->Push( result );
  114. if( !arg )
  115. {
  116. aCtx->ReportError( wxString::Format( _( "Missing argument to '%s'" ),
  117. wxT( "insideCourtyard()" ) ) );
  118. return;
  119. }
  120. PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
  121. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  122. MODULE* footprint = nullptr;
  123. if( !item )
  124. return;
  125. if( arg->AsString() == "A" )
  126. {
  127. footprint = dynamic_cast<MODULE*>( context->GetItem( 0 ) );
  128. }
  129. else if( arg->AsString() == "B" )
  130. {
  131. footprint = dynamic_cast<MODULE*>( context->GetItem( 1 ) );
  132. }
  133. else
  134. {
  135. for( MODULE* candidate : item->GetBoard()->Modules() )
  136. {
  137. if( candidate->GetReference().Matches( arg->AsString() ) )
  138. {
  139. footprint = candidate;
  140. break;
  141. }
  142. }
  143. }
  144. if( footprint )
  145. {
  146. SHAPE_POLY_SET footprintCourtyard;
  147. if( footprint->IsFlipped() )
  148. footprintCourtyard = footprint->GetPolyCourtyardBack();
  149. else
  150. footprintCourtyard = footprint->GetPolyCourtyardFront();
  151. SHAPE_POLY_SET testPoly;
  152. item->TransformShapeWithClearanceToPolygon( testPoly, context->GetLayer(), 0 );
  153. testPoly.BooleanIntersection( footprintCourtyard, SHAPE_POLY_SET::PM_FAST );
  154. if( testPoly.OutlineCount() )
  155. result->Set( 1.0 );
  156. }
  157. }
  158. static void insideArea( LIBEVAL::CONTEXT* aCtx, void* self )
  159. {
  160. PCB_EXPR_CONTEXT* context = static_cast<PCB_EXPR_CONTEXT*>( aCtx );
  161. LIBEVAL::VALUE* arg = aCtx->Pop();
  162. LIBEVAL::VALUE* result = aCtx->AllocValue();
  163. result->Set( 0.0 );
  164. aCtx->Push( result );
  165. if( !arg )
  166. {
  167. aCtx->ReportError( wxString::Format( _( "Missing argument to '%s'" ),
  168. wxT( "insideArea()" ) ) );
  169. return;
  170. }
  171. PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
  172. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  173. ZONE_CONTAINER* zone = nullptr;
  174. if( !item )
  175. return;
  176. if( arg->AsString() == "A" )
  177. {
  178. zone = dynamic_cast<ZONE_CONTAINER*>( context->GetItem( 0 ) );
  179. }
  180. else if( arg->AsString() == "B" )
  181. {
  182. zone = dynamic_cast<ZONE_CONTAINER*>( context->GetItem( 1 ) );
  183. }
  184. else
  185. {
  186. for( ZONE_CONTAINER* candidate : item->GetBoard()->Zones() )
  187. {
  188. if( candidate->GetZoneName().Matches( arg->AsString() ) )
  189. {
  190. zone = candidate;
  191. break;
  192. }
  193. }
  194. }
  195. if( zone )
  196. {
  197. SHAPE_POLY_SET testPoly;
  198. item->TransformShapeWithClearanceToPolygon( testPoly, context->GetLayer(), 0 );
  199. testPoly.BooleanIntersection( *zone->Outline(), SHAPE_POLY_SET::PM_FAST );
  200. if( testPoly.OutlineCount() )
  201. result->Set( 1.0 );
  202. }
  203. }
  204. static void memberOf( LIBEVAL::CONTEXT* aCtx, void* self )
  205. {
  206. LIBEVAL::VALUE* arg = aCtx->Pop();
  207. LIBEVAL::VALUE* result = aCtx->AllocValue();
  208. result->Set( 0.0 );
  209. aCtx->Push( result );
  210. if( !arg )
  211. {
  212. aCtx->ReportError( wxString::Format( _( "Missing argument to '%s'" ),
  213. wxT( "memberOf()" ) ) );
  214. return;
  215. }
  216. PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
  217. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  218. if( !item )
  219. return;
  220. PCB_GROUP* group = item->GetParentGroup();
  221. if( !group && item->GetParent() && item->GetParent()->Type() == PCB_MODULE_T )
  222. group = item->GetParent()->GetParentGroup();
  223. while( group )
  224. {
  225. if( group->GetName().Matches( arg->AsString() ) )
  226. {
  227. result->Set( 1.0 );
  228. return;
  229. }
  230. group = group->GetParentGroup();
  231. }
  232. }
  233. static void isMicroVia( LIBEVAL::CONTEXT* aCtx, void* self )
  234. {
  235. PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
  236. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  237. LIBEVAL::VALUE* result = aCtx->AllocValue();
  238. result->Set( 0.0 );
  239. aCtx->Push( result );
  240. auto via = dyn_cast<VIA*>( item );
  241. if( via && via->GetViaType() == VIATYPE::MICROVIA )
  242. {
  243. result->Set ( 1.0 );
  244. }
  245. }
  246. static void isBlindBuriedVia( LIBEVAL::CONTEXT* aCtx, void* self )
  247. {
  248. PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
  249. BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
  250. LIBEVAL::VALUE* result = aCtx->AllocValue();
  251. result->Set( 0.0 );
  252. aCtx->Push( result );
  253. auto via = dyn_cast<VIA*>( item );
  254. if( via && via->GetViaType() == VIATYPE::BLIND_BURIED )
  255. {
  256. result->Set ( 1.0 );
  257. }
  258. }
  259. PCB_EXPR_BUILTIN_FUNCTIONS::PCB_EXPR_BUILTIN_FUNCTIONS()
  260. {
  261. RegisterAllFunctions();
  262. }
  263. void PCB_EXPR_BUILTIN_FUNCTIONS::RegisterAllFunctions()
  264. {
  265. m_funcs.clear();
  266. RegisterFunc( "existsOnLayer('x')", existsOnLayer );
  267. RegisterFunc( "isPlated()", isPlated );
  268. RegisterFunc( "insideCourtyard('x')", insideCourtyard );
  269. RegisterFunc( "insideArea('x')", insideArea );
  270. RegisterFunc( "isMicroVia()", isMicroVia );
  271. RegisterFunc( "isBlindBuriedVia()", isBlindBuriedVia );
  272. RegisterFunc( "memberOf('x')", memberOf );
  273. RegisterFunc( "fromTo('x','y')", exprFromTo );
  274. //RegisterFunc( "isDiffPair()", exprFromTo );
  275. }
  276. BOARD_ITEM* PCB_EXPR_VAR_REF::GetObject( LIBEVAL::CONTEXT* aCtx ) const
  277. {
  278. wxASSERT( dynamic_cast<PCB_EXPR_CONTEXT*>( aCtx ) );
  279. const PCB_EXPR_CONTEXT* ctx = static_cast<const PCB_EXPR_CONTEXT*>( aCtx );
  280. BOARD_ITEM* item = ctx->GetItem( m_itemIndex );
  281. return item;
  282. }
  283. class PCB_LAYER_VALUE : public LIBEVAL::VALUE
  284. {
  285. public:
  286. PCB_LAYER_VALUE( PCB_LAYER_ID aLayer ) :
  287. LIBEVAL::VALUE( double( aLayer ) )
  288. {};
  289. virtual bool EqualTo( const VALUE* b ) const override
  290. {
  291. // For boards with user-defined layer names there will be 2 entries for each layer
  292. // in the ENUM_MAP: one for the canonical layer name and one for the user layer name.
  293. // We need to check against both.
  294. wxPGChoices& layerMap = ENUM_MAP<PCB_LAYER_ID>::Instance().Choices();
  295. PCB_LAYER_ID layerId = ToLAYER_ID( (int) AsDouble() );
  296. for( unsigned ii = 0; ii < layerMap.GetCount(); ++ii )
  297. {
  298. wxPGChoiceEntry& entry = layerMap[ii];
  299. if( entry.GetValue() == layerId && entry.GetText().Matches( b->AsString() ) )
  300. return true;
  301. }
  302. return false;
  303. }
  304. };
  305. LIBEVAL::VALUE PCB_EXPR_VAR_REF::GetValue( LIBEVAL::CONTEXT* aCtx )
  306. {
  307. if( m_itemIndex == 2 )
  308. {
  309. PCB_EXPR_CONTEXT* context = static_cast<PCB_EXPR_CONTEXT*>( aCtx );
  310. return PCB_LAYER_VALUE( context->GetLayer() );
  311. }
  312. BOARD_ITEM* item = const_cast<BOARD_ITEM*>( GetObject( aCtx ) );
  313. auto it = m_matchingTypes.find( TYPE_HASH( *item ) );
  314. if( it == m_matchingTypes.end() )
  315. {
  316. // Don't force user to type "A.Type == 'via' && A.Via_Type == 'buried'" when the
  317. // simplier "A.Via_Type == 'buried'" is perfectly clear. Instead, return an undefined
  318. // value when the property doesn't appear on a particular object.
  319. return LIBEVAL::VALUE( "UNDEFINED" );
  320. }
  321. else
  322. {
  323. if( m_type == LIBEVAL::VT_NUMERIC )
  324. return LIBEVAL::VALUE( (double) item->Get<int>( it->second ) );
  325. else
  326. {
  327. wxString str;
  328. if( !m_isEnum )
  329. {
  330. str = item->Get<wxString>( it->second );
  331. }
  332. else
  333. {
  334. const wxAny& any = item->Get( it->second );
  335. any.GetAs<wxString>( &str );
  336. }
  337. return LIBEVAL::VALUE( str );
  338. }
  339. }
  340. }
  341. LIBEVAL::FUNC_CALL_REF PCB_EXPR_UCODE::CreateFuncCall( const wxString& aName )
  342. {
  343. PCB_EXPR_BUILTIN_FUNCTIONS& registry = PCB_EXPR_BUILTIN_FUNCTIONS::Instance();
  344. return registry.Get( aName.Lower() );
  345. }
  346. std::unique_ptr<LIBEVAL::VAR_REF> PCB_EXPR_UCODE::CreateVarRef( const wxString& aVar,
  347. const wxString& aField )
  348. {
  349. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  350. std::unique_ptr<PCB_EXPR_VAR_REF> vref;
  351. if( aVar == "A" )
  352. {
  353. vref.reset( new PCB_EXPR_VAR_REF( 0 ) );
  354. }
  355. else if( aVar == "B" )
  356. {
  357. vref.reset( new PCB_EXPR_VAR_REF( 1 ) );
  358. }
  359. else if( aVar == "L" )
  360. {
  361. vref.reset( new PCB_EXPR_VAR_REF( 2 ) );
  362. }
  363. else
  364. {
  365. return nullptr;
  366. }
  367. if( aField.length() == 0 ) // return reference to base object
  368. {
  369. return std::move( vref );
  370. }
  371. wxString field( aField );
  372. field.Replace( "_", " " );
  373. for( const PROPERTY_MANAGER::CLASS_INFO& cls : propMgr.GetAllClasses() )
  374. {
  375. if( propMgr.IsOfType( cls.type, TYPE_HASH( BOARD_ITEM ) ) )
  376. {
  377. PROPERTY_BASE* prop = propMgr.GetProperty( cls.type, field );
  378. if( prop )
  379. {
  380. vref->AddAllowedClass( cls.type, prop );
  381. if( prop->TypeHash() == TYPE_HASH( int ) )
  382. {
  383. vref->SetType( LIBEVAL::VT_NUMERIC );
  384. }
  385. else if( prop->TypeHash() == TYPE_HASH( wxString ) )
  386. {
  387. vref->SetType( LIBEVAL::VT_STRING );
  388. }
  389. else if ( prop->HasChoices() )
  390. { // it's an enum, we treat it as string
  391. vref->SetType( LIBEVAL::VT_STRING );
  392. vref->SetIsEnum ( true );
  393. }
  394. else
  395. {
  396. wxFAIL_MSG( "PCB_EXPR_UCODE::createVarRef: Unknown property type." );
  397. }
  398. }
  399. }
  400. }
  401. if( vref->GetType() == LIBEVAL::VT_UNDEFINED )
  402. vref->SetType( LIBEVAL::VT_PARSE_ERROR );
  403. return std::move( vref );
  404. }
  405. class PCB_UNIT_RESOLVER : public LIBEVAL::UNIT_RESOLVER
  406. {
  407. public:
  408. virtual ~PCB_UNIT_RESOLVER()
  409. {
  410. }
  411. virtual const std::vector<wxString>& GetSupportedUnits() const override
  412. {
  413. static const std::vector<wxString> pcbUnits = { "mil", "mm", "in" };
  414. return pcbUnits;
  415. }
  416. virtual double Convert( const wxString& aString, int unitId ) const override
  417. {
  418. double v = wxAtof( aString );
  419. switch( unitId )
  420. {
  421. case 0: return DoubleValueFromString( EDA_UNITS::MILS, aString );
  422. case 1: return DoubleValueFromString( EDA_UNITS::MILLIMETRES, aString );
  423. case 2: return DoubleValueFromString( EDA_UNITS::INCHES, aString );
  424. default: return v;
  425. }
  426. };
  427. };
  428. PCB_EXPR_COMPILER::PCB_EXPR_COMPILER()
  429. {
  430. m_unitResolver = std::make_unique<PCB_UNIT_RESOLVER>();
  431. }
  432. PCB_EXPR_EVALUATOR::PCB_EXPR_EVALUATOR()
  433. {
  434. m_result = 0;
  435. }
  436. PCB_EXPR_EVALUATOR::~PCB_EXPR_EVALUATOR()
  437. {
  438. }
  439. bool PCB_EXPR_EVALUATOR::Evaluate( const wxString& aExpr )
  440. {
  441. PCB_EXPR_UCODE ucode;
  442. PCB_EXPR_CONTEXT preflightContext( F_Cu );
  443. m_compiler.Compile( aExpr.ToUTF8().data(), &ucode, &preflightContext );
  444. PCB_EXPR_CONTEXT evaluationContext( F_Cu );
  445. LIBEVAL::VALUE* result = ucode.Run( &evaluationContext );
  446. if( result->GetType() == LIBEVAL::VT_NUMERIC )
  447. m_result = KiROUND( result->AsDouble() );
  448. return true;
  449. }