kicad/pcbnew/pcb_expr_evaluator.cpp

712 lines
20 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2019-2020 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <cstdio>
#include <memory>
#include <board.h>
#include <track.h>
#include <geometry/shape_segment.h>
#include <pcb_expr_evaluator.h>
#include <connectivity/connectivity_data.h>
#include <connectivity/connectivity_algo.h>
#include <connectivity/from_to_cache.h>
#include <drc/drc_engine.h>
2020-10-22 12:36:28 +00:00
#include <geometry/shape_circle.h>
bool exprFromTo( LIBEVAL::CONTEXT* aCtx, void* self )
{
PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
LIBEVAL::VALUE* result = aCtx->AllocValue();
LIBEVAL::VALUE* argTo = aCtx->Pop();
LIBEVAL::VALUE* argFrom = aCtx->Pop();
result->Set(0.0);
aCtx->Push( result );
if(!item)
return false;
auto ftCache = item->GetBoard()->GetConnectivity()->GetFromToCache();
if( !ftCache )
{
wxLogWarning( "Attempting to call fromTo() with non-existent from-to cache, aborting...");
return true;
}
if( ftCache->IsOnFromToPath( static_cast<BOARD_CONNECTED_ITEM*>( item ),
argFrom->AsString(), argTo->AsString() ) )
{
result->Set(1.0);
}
return true;
}
static void existsOnLayer( LIBEVAL::CONTEXT* aCtx, void *self )
{
PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
LIBEVAL::VALUE* arg = aCtx->Pop();
LIBEVAL::VALUE* result = aCtx->AllocValue();
result->Set( 0.0 );
aCtx->Push( result );
if( !item )
return;
if( !arg )
{
aCtx->ReportError( wxString::Format( _( "Missing argument to '%s'" ),
wxT( "existsOnLayer()" ) ) );
return;
}
wxString layerName = arg->AsString();
wxPGChoices& layerMap = ENUM_MAP<PCB_LAYER_ID>::Instance().Choices();
bool anyMatch = false;
for( unsigned ii = 0; ii < layerMap.GetCount(); ++ii )
{
wxPGChoiceEntry& entry = layerMap[ii];
if( entry.GetText().Matches( layerName ) )
{
anyMatch = true;
if( item->IsOnLayer( ToLAYER_ID( entry.GetValue() ) ) )
{
result->Set( 1.0 );
return;
}
}
}
if( !anyMatch )
2020-09-14 08:02:07 +00:00
aCtx->ReportError( wxString::Format( _( "Unrecognized layer '%s'" ), layerName ) );
}
static void isPlated( LIBEVAL::CONTEXT* aCtx, void* self )
{
LIBEVAL::VALUE* result = aCtx->AllocValue();
result->Set( 0.0 );
aCtx->Push( result );
PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
2020-11-12 22:30:02 +00:00
PAD* pad = dynamic_cast<PAD*>( item );
2020-09-30 15:38:35 +00:00
if( pad && pad->GetAttribute() == PAD_ATTRIB_PTH )
result->Set( 1.0 );
}
static void insideCourtyard( LIBEVAL::CONTEXT* aCtx, void* self )
{
PCB_EXPR_CONTEXT* context = static_cast<PCB_EXPR_CONTEXT*>( aCtx );
LIBEVAL::VALUE* arg = aCtx->Pop();
LIBEVAL::VALUE* result = aCtx->AllocValue();
result->Set( 0.0 );
aCtx->Push( result );
if( !arg )
{
aCtx->ReportError( wxString::Format( _( "Missing argument to '%s'" ),
wxT( "insideCourtyard()" ) ) );
return;
}
PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
2020-11-13 15:15:52 +00:00
FOOTPRINT* footprint = nullptr;
if( !item )
return;
if( arg->AsString() == "A" )
{
2020-11-13 15:15:52 +00:00
footprint = dynamic_cast<FOOTPRINT*>( context->GetItem( 0 ) );
}
else if( arg->AsString() == "B" )
{
2020-11-13 15:15:52 +00:00
footprint = dynamic_cast<FOOTPRINT*>( context->GetItem( 1 ) );
}
else
{
2020-11-13 15:15:52 +00:00
for( FOOTPRINT* candidate : item->GetBoard()->Footprints() )
{
if( candidate->GetReference().Matches( arg->AsString() ) )
{
footprint = candidate;
break;
}
}
}
if( footprint )
{
SHAPE_POLY_SET footprintCourtyard;
if( footprint->IsFlipped() )
footprintCourtyard = footprint->GetPolyCourtyardBack();
else
footprintCourtyard = footprint->GetPolyCourtyardFront();
SHAPE_POLY_SET testPoly;
item->TransformShapeWithClearanceToPolygon( testPoly, context->GetLayer(), 0,
ARC_LOW_DEF, ERROR_INSIDE );
testPoly.BooleanIntersection( footprintCourtyard, SHAPE_POLY_SET::PM_FAST );
if( testPoly.OutlineCount() )
result->Set( 1.0 );
}
}
static void insideArea( LIBEVAL::CONTEXT* aCtx, void* self )
{
PCB_EXPR_CONTEXT* context = static_cast<PCB_EXPR_CONTEXT*>( aCtx );
LIBEVAL::VALUE* arg = aCtx->Pop();
LIBEVAL::VALUE* result = aCtx->AllocValue();
result->Set( 0.0 );
aCtx->Push( result );
if( !arg )
{
aCtx->ReportError( wxString::Format( _( "Missing argument to '%s'" ),
wxT( "insideArea()" ) ) );
return;
}
PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
if( !item )
return;
auto insideZone =
[&]( ZONE* zone ) -> bool
{
if( !zone )
return false;
if( !zone->GetCachedBoundingBox().Intersects( item->GetBoundingBox() ) )
return false;
if( item->GetFlags() & HOLE_PROXY )
{
if( item->Type() == PCB_PAD_T )
{
2020-11-12 22:30:02 +00:00
PAD* pad = static_cast<PAD*>( item );
const SHAPE_SEGMENT* holeShape = pad->GetEffectiveHoleShape();
return zone->Outline()->Collide( holeShape );
}
else if( item->Type() == PCB_VIA_T )
{
VIA* via = static_cast<VIA*>( item );
const SHAPE_CIRCLE holeShape( via->GetPosition(), via->GetDrillValue() );
return zone->Outline()->Collide( &holeShape );
}
return false;
}
2020-11-13 12:21:02 +00:00
if( item->Type() == PCB_FOOTPRINT_T )
{
2020-11-13 15:15:52 +00:00
FOOTPRINT* footprint = static_cast<FOOTPRINT*>( item );
if( ( footprint->GetFlags() & MALFORMED_COURTYARDS ) != 0 )
{
aCtx->ReportError( _( "Footprint's courtyard is not a single, closed shape." ) );
return false;
}
if( ( zone->GetLayerSet() & LSET::FrontMask() ).any() )
{
SHAPE_POLY_SET courtyard = footprint->GetPolyCourtyardFront();
if( courtyard.OutlineCount() == 0 )
{
aCtx->ReportError( _( "Footprint has no front courtyard." ) );
return false;
}
else
{
return zone->Outline()->Collide( &courtyard.Outline( 0 ) );
}
}
if( ( zone->GetLayerSet() & LSET::BackMask() ).any() )
{
SHAPE_POLY_SET courtyard = footprint->GetPolyCourtyardBack();
if( courtyard.OutlineCount() == 0 )
{
aCtx->ReportError( _( "Footprint has no back courtyard." ) );
return false;
}
else
{
return zone->Outline()->Collide( &courtyard.Outline( 0 ) );
}
}
return false;
}
std::shared_ptr<SHAPE> shape = item->GetEffectiveShape( context->GetLayer() );
return zone->Outline()->Collide( shape.get() );
};
if( arg->AsString() == "A" )
{
if( insideZone( dynamic_cast<ZONE*>( context->GetItem( 0 ) ) ) )
result->Set( 1.0 );
}
else if( arg->AsString() == "B" )
{
if( insideZone( dynamic_cast<ZONE*>( context->GetItem( 1 ) ) ) )
result->Set( 1.0 );
}
else if( KIID::SniffTest( arg->AsString() ) )
{
KIID target( arg->AsString() );
for( ZONE* candidate : item->GetBoard()->Zones() )
{
// Only a single zone can match the UUID; exit once we find a match whether
// "inside" or not
if( candidate->m_Uuid == target )
{
if( insideZone( candidate ) )
result->Set( 1.0 );
return;
}
}
2020-11-13 15:15:52 +00:00
for( FOOTPRINT* footprint : item->GetBoard()->Footprints() )
{
2020-11-13 02:57:11 +00:00
for( ZONE* candidate : footprint->Zones() )
{
// Only a single zone can match the UUID; exit once we find a match whether
// "inside" or not
if( candidate->m_Uuid == target )
{
if( insideZone( candidate ) )
result->Set( 1.0 );
return;
}
}
}
}
else // Match on zone name
{
for( ZONE* candidate : item->GetBoard()->Zones() )
{
if( candidate->GetZoneName().Matches( arg->AsString() ) )
{
// Many zones can match the name; exit only when we find an "inside"
if( insideZone( candidate ) )
{
result->Set( 1.0 );
return;
}
}
}
2020-11-13 15:15:52 +00:00
for( FOOTPRINT* footprint : item->GetBoard()->Footprints() )
{
2020-11-13 02:57:11 +00:00
for( ZONE* candidate : footprint->Zones() )
{
// Many zones can match the name; exit only when we find an "inside"
if( candidate->GetZoneName().Matches( arg->AsString() ) )
{
if( insideZone( candidate ) )
{
result->Set( 1.0 );
return;
}
}
}
}
}
}
static void memberOf( LIBEVAL::CONTEXT* aCtx, void* self )
{
LIBEVAL::VALUE* arg = aCtx->Pop();
LIBEVAL::VALUE* result = aCtx->AllocValue();
result->Set( 0.0 );
aCtx->Push( result );
if( !arg )
{
aCtx->ReportError( wxString::Format( _( "Missing argument to '%s'" ),
wxT( "memberOf()" ) ) );
return;
}
PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
if( !item )
return;
PCB_GROUP* group = item->GetParentGroup();
2020-11-13 12:21:02 +00:00
if( !group && item->GetParent() && item->GetParent()->Type() == PCB_FOOTPRINT_T )
group = item->GetParent()->GetParentGroup();
while( group )
{
if( group->GetName().Matches( arg->AsString() ) )
{
result->Set( 1.0 );
return;
}
group = group->GetParentGroup();
}
}
static void isMicroVia( LIBEVAL::CONTEXT* aCtx, void* self )
{
PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
LIBEVAL::VALUE* result = aCtx->AllocValue();
result->Set( 0.0 );
aCtx->Push( result );
auto via = dyn_cast<VIA*>( item );
if( via && via->GetViaType() == VIATYPE::MICROVIA )
{
result->Set ( 1.0 );
}
}
static void isBlindBuriedVia( LIBEVAL::CONTEXT* aCtx, void* self )
{
PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
LIBEVAL::VALUE* result = aCtx->AllocValue();
result->Set( 0.0 );
aCtx->Push( result );
auto via = dyn_cast<VIA*>( item );
if( via && via->GetViaType() == VIATYPE::BLIND_BURIED )
{
result->Set ( 1.0 );
}
}
static void isDiffPair( LIBEVAL::CONTEXT* aCtx, void* self )
{
PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
LIBEVAL::VALUE* result = aCtx->AllocValue();
result->Set( 0.0 );
aCtx->Push( result );
if( item && item->IsConnected() )
{
NETINFO_ITEM* netinfo = static_cast<BOARD_CONNECTED_ITEM*>( item )->GetNet();
int dummy_p, dummy_n;
if( netinfo && DRC_ENGINE::IsNetADiffPair( item->GetBoard(), netinfo, dummy_p, dummy_n ) )
result->Set( 1.0 );
}
}
PCB_EXPR_BUILTIN_FUNCTIONS::PCB_EXPR_BUILTIN_FUNCTIONS()
{
RegisterAllFunctions();
}
void PCB_EXPR_BUILTIN_FUNCTIONS::RegisterAllFunctions()
{
m_funcs.clear();
RegisterFunc( "existsOnLayer('x')", existsOnLayer );
RegisterFunc( "isPlated()", isPlated );
RegisterFunc( "insideCourtyard('x')", insideCourtyard );
RegisterFunc( "insideArea('x')", insideArea );
RegisterFunc( "isMicroVia()", isMicroVia );
RegisterFunc( "isBlindBuriedVia()", isBlindBuriedVia );
RegisterFunc( "memberOf('x')", memberOf );
RegisterFunc( "fromTo('x','y')", exprFromTo );
RegisterFunc( "isDiffPair()", isDiffPair );
}
2020-10-27 11:03:35 +00:00
BOARD_ITEM* PCB_EXPR_VAR_REF::GetObject( const LIBEVAL::CONTEXT* aCtx ) const
2020-07-22 07:02:30 +00:00
{
2020-10-27 11:03:35 +00:00
wxASSERT( dynamic_cast<const PCB_EXPR_CONTEXT*>( aCtx ) );
2020-08-12 15:26:10 +00:00
const PCB_EXPR_CONTEXT* ctx = static_cast<const PCB_EXPR_CONTEXT*>( aCtx );
BOARD_ITEM* item = ctx->GetItem( m_itemIndex );
return item;
}
class PCB_LAYER_VALUE : public LIBEVAL::VALUE
{
public:
PCB_LAYER_VALUE( PCB_LAYER_ID aLayer ) :
LIBEVAL::VALUE( double( aLayer ) )
{};
virtual bool EqualTo( const VALUE* b ) const override
{
// For boards with user-defined layer names there will be 2 entries for each layer
// in the ENUM_MAP: one for the canonical layer name and one for the user layer name.
// We need to check against both.
wxPGChoices& layerMap = ENUM_MAP<PCB_LAYER_ID>::Instance().Choices();
PCB_LAYER_ID layerId = ToLAYER_ID( (int) AsDouble() );
for( unsigned ii = 0; ii < layerMap.GetCount(); ++ii )
{
wxPGChoiceEntry& entry = layerMap[ii];
if( entry.GetValue() == layerId && entry.GetText().Matches( b->AsString() ) )
return true;
}
return false;
}
};
LIBEVAL::VALUE PCB_EXPR_VAR_REF::GetValue( LIBEVAL::CONTEXT* aCtx )
{
if( m_itemIndex == 2 )
{
PCB_EXPR_CONTEXT* context = static_cast<PCB_EXPR_CONTEXT*>( aCtx );
return PCB_LAYER_VALUE( context->GetLayer() );
}
BOARD_ITEM* item = const_cast<BOARD_ITEM*>( GetObject( aCtx ) );
auto it = m_matchingTypes.find( TYPE_HASH( *item ) );
if( it == m_matchingTypes.end() )
{
// Don't force user to type "A.Type == 'via' && A.Via_Type == 'buried'" when the
// simplier "A.Via_Type == 'buried'" is perfectly clear. Instead, return an undefined
// value when the property doesn't appear on a particular object.
return LIBEVAL::VALUE();
}
else
{
if( m_type == LIBEVAL::VT_NUMERIC )
return LIBEVAL::VALUE( (double) item->Get<int>( it->second ) );
else
{
wxString str;
if( !m_isEnum )
{
str = item->Get<wxString>( it->second );
}
else
{
const wxAny& any = item->Get( it->second );
any.GetAs<wxString>( &str );
}
if( str == "UNDEFINED" )
return LIBEVAL::VALUE();
else
return LIBEVAL::VALUE( str );
}
}
}
2020-08-11 13:33:16 +00:00
LIBEVAL::FUNC_CALL_REF PCB_EXPR_UCODE::CreateFuncCall( const wxString& aName )
{
PCB_EXPR_BUILTIN_FUNCTIONS& registry = PCB_EXPR_BUILTIN_FUNCTIONS::Instance();
return registry.Get( aName.Lower() );
}
std::unique_ptr<LIBEVAL::VAR_REF> PCB_EXPR_UCODE::CreateVarRef( const wxString& aVar,
const wxString& aField )
{
PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
std::unique_ptr<PCB_EXPR_VAR_REF> vref;
2020-08-11 13:33:16 +00:00
if( aVar == "A" )
vref = std::make_unique<PCB_EXPR_VAR_REF>( 0 );
2020-08-11 13:33:16 +00:00
else if( aVar == "B" )
vref = std::make_unique<PCB_EXPR_VAR_REF>( 1 );
else if( aVar == "L" )
vref = std::make_unique<PCB_EXPR_VAR_REF>( 2 );
else
2020-08-11 13:33:16 +00:00
return nullptr;
2020-08-11 13:33:16 +00:00
if( aField.length() == 0 ) // return reference to base object
{
return std::move( vref );
}
2020-08-11 13:33:16 +00:00
wxString field( aField );
field.Replace( "_", " " );
for( const PROPERTY_MANAGER::CLASS_INFO& cls : propMgr.GetAllClasses() )
{
if( propMgr.IsOfType( cls.type, TYPE_HASH( BOARD_ITEM ) ) )
{
PROPERTY_BASE* prop = propMgr.GetProperty( cls.type, field );
2020-07-22 07:02:30 +00:00
if( prop )
{
vref->AddAllowedClass( cls.type, prop );
if( prop->TypeHash() == TYPE_HASH( int ) )
{
vref->SetType( LIBEVAL::VT_NUMERIC );
}
else if( prop->TypeHash() == TYPE_HASH( wxString ) )
{
vref->SetType( LIBEVAL::VT_STRING );
}
else if ( prop->HasChoices() )
{ // it's an enum, we treat it as string
vref->SetType( LIBEVAL::VT_STRING );
vref->SetIsEnum ( true );
}
else
{
wxFAIL_MSG( "PCB_EXPR_UCODE::createVarRef: Unknown property type." );
}
}
}
}
if( vref->GetType() == LIBEVAL::VT_UNDEFINED )
vref->SetType( LIBEVAL::VT_PARSE_ERROR );
2020-08-11 13:33:16 +00:00
return std::move( vref );
}
class PCB_UNIT_RESOLVER : public LIBEVAL::UNIT_RESOLVER
{
public:
virtual ~PCB_UNIT_RESOLVER()
{
}
2020-08-11 13:33:16 +00:00
virtual const std::vector<wxString>& GetSupportedUnits() const override
{
2020-08-11 13:33:16 +00:00
static const std::vector<wxString> pcbUnits = { "mil", "mm", "in" };
return pcbUnits;
}
virtual wxString GetSupportedUnitsMessage() const override
{
return _( "must be mm, in, or mil" );
}
2020-08-11 13:33:16 +00:00
virtual double Convert( const wxString& aString, int unitId ) const override
{
2020-08-11 13:33:16 +00:00
double v = wxAtof( aString );
switch( unitId )
{
case 0: return DoubleValueFromString( EDA_UNITS::MILS, aString );
case 1: return DoubleValueFromString( EDA_UNITS::MILLIMETRES, aString );
case 2: return DoubleValueFromString( EDA_UNITS::INCHES, aString );
default: return v;
}
};
};
2020-08-11 13:33:16 +00:00
PCB_EXPR_COMPILER::PCB_EXPR_COMPILER()
{
m_unitResolver = std::make_unique<PCB_UNIT_RESOLVER>();
}
2020-08-11 13:33:16 +00:00
PCB_EXPR_EVALUATOR::PCB_EXPR_EVALUATOR()
{
2020-07-22 07:02:30 +00:00
m_result = 0;
}
PCB_EXPR_EVALUATOR::~PCB_EXPR_EVALUATOR()
{
}
bool PCB_EXPR_EVALUATOR::Evaluate( const wxString& aExpr )
{
PCB_EXPR_UCODE ucode;
2020-08-12 15:26:10 +00:00
PCB_EXPR_CONTEXT preflightContext( F_Cu );
if( !m_compiler.Compile( aExpr.ToUTF8().data(), &ucode, &preflightContext ) )
return false;
2020-08-12 15:26:10 +00:00
PCB_EXPR_CONTEXT evaluationContext( F_Cu );
LIBEVAL::VALUE* result = ucode.Run( &evaluationContext );
if( result->GetType() == LIBEVAL::VT_NUMERIC )
m_result = KiROUND( result->AsDouble() );
return true;
}