2020-07-03 16:36:33 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2020-06-13 23:22:08 +00:00
|
|
|
#include <cstdio>
|
2020-07-19 21:22:49 +00:00
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
2020-07-21 15:49:55 +00:00
|
|
|
#include <memory>
|
2020-06-13 23:22:08 +00:00
|
|
|
#include "class_board.h"
|
|
|
|
#include "pcb_expr_evaluator.h"
|
|
|
|
|
2020-06-16 19:49:09 +00:00
|
|
|
|
|
|
|
|
2020-07-23 20:47:38 +00:00
|
|
|
static void onLayer( LIBEVAL::CONTEXT* aCtx, void *self )
|
2020-07-22 20:27:38 +00:00
|
|
|
{
|
|
|
|
LIBEVAL::VALUE* arg = aCtx->Pop();
|
|
|
|
LIBEVAL::VALUE* result = aCtx->AllocValue();
|
2020-06-16 19:49:09 +00:00
|
|
|
|
2020-07-22 20:27:38 +00:00
|
|
|
result->Set( 0.0 );
|
|
|
|
aCtx->Push( result );
|
2020-06-16 19:49:09 +00:00
|
|
|
|
2020-07-22 20:27:38 +00:00
|
|
|
if( !arg )
|
2020-07-19 21:22:49 +00:00
|
|
|
{
|
2020-07-22 20:27:38 +00:00
|
|
|
aCtx->ReportError( _( "Missing argument to 'onLayer()'" ) );
|
|
|
|
return;
|
2020-07-19 21:22:49 +00:00
|
|
|
}
|
2020-06-16 19:49:09 +00:00
|
|
|
|
2020-07-22 20:27:38 +00:00
|
|
|
wxString layerName = arg->AsString();
|
|
|
|
PCB_LAYER_ID layer = ENUM_MAP<PCB_LAYER_ID>::Instance().ToEnum( layerName );
|
2020-06-16 19:49:09 +00:00
|
|
|
|
2020-07-22 20:27:38 +00:00
|
|
|
if( layer == UNDEFINED_LAYER )
|
2020-07-19 21:22:49 +00:00
|
|
|
{
|
2020-07-22 20:27:38 +00:00
|
|
|
aCtx->ReportError( wxString::Format( _( "Unrecognized layer '%s' " ), layerName ) );
|
|
|
|
return;
|
|
|
|
}
|
2020-07-22 13:08:57 +00:00
|
|
|
|
2020-07-22 20:27:38 +00:00
|
|
|
PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
|
2020-07-23 20:47:38 +00:00
|
|
|
BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
|
2020-07-22 13:08:57 +00:00
|
|
|
|
2020-07-22 20:27:38 +00:00
|
|
|
if( item && item->IsOnLayer( layer ) )
|
|
|
|
result->Set( 1.0 );
|
|
|
|
}
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-06-16 19:49:09 +00:00
|
|
|
|
2020-07-23 20:47:38 +00:00
|
|
|
static void isPlated( LIBEVAL::CONTEXT* aCtx, void *self )
|
2020-07-22 20:27:38 +00:00
|
|
|
{
|
|
|
|
LIBEVAL::VALUE* result = aCtx->AllocValue();
|
2020-06-19 21:33:29 +00:00
|
|
|
|
2020-07-22 20:27:38 +00:00
|
|
|
result->Set( 0.0 );
|
|
|
|
aCtx->Push( result );
|
2020-07-22 13:08:57 +00:00
|
|
|
|
2020-07-22 20:27:38 +00:00
|
|
|
PCB_EXPR_VAR_REF* vref = static_cast<PCB_EXPR_VAR_REF*>( self );
|
2020-07-23 20:47:38 +00:00
|
|
|
BOARD_ITEM* item = vref ? vref->GetObject( aCtx ) : nullptr;
|
2020-07-22 20:27:38 +00:00
|
|
|
D_PAD* pad = dynamic_cast<D_PAD*>( item );
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-07-22 20:27:38 +00:00
|
|
|
if( pad && pad->GetAttribute() == PAD_ATTRIB_STANDARD )
|
|
|
|
result->Set( 1.0 );
|
|
|
|
}
|
2020-06-16 19:49:09 +00:00
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-06-16 19:49:09 +00:00
|
|
|
PCB_EXPR_BUILTIN_FUNCTIONS::PCB_EXPR_BUILTIN_FUNCTIONS()
|
2020-06-13 23:22:08 +00:00
|
|
|
{
|
2020-07-22 20:27:38 +00:00
|
|
|
auto registerFunc = [&]( const wxString& funcSignature, FPTR funcPtr )
|
|
|
|
{
|
|
|
|
wxString funcName = funcSignature.BeforeFirst( '(' );
|
|
|
|
m_funcs[ std::string( funcName.Lower() ) ] = std::move( funcPtr );
|
|
|
|
m_funcSigs.Add( funcSignature );
|
|
|
|
};
|
|
|
|
|
|
|
|
registerFunc( "onLayer('x')", onLayer );
|
|
|
|
registerFunc( "isPlated()", isPlated );
|
2020-06-16 19:49:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-07-23 20:47:38 +00:00
|
|
|
BOARD_ITEM* PCB_EXPR_VAR_REF::GetObject( LIBEVAL::CONTEXT* aCtx ) const
|
2020-07-22 07:02:30 +00:00
|
|
|
{
|
2020-07-23 20:47:38 +00:00
|
|
|
const PCB_EXPR_CONTEXT* ctx = static_cast<const PCB_EXPR_CONTEXT*>( aCtx );
|
|
|
|
BOARD_ITEM* item = ctx->GetItem( m_itemIndex );
|
2020-06-16 19:49:09 +00:00
|
|
|
return item;
|
|
|
|
}
|
2020-06-13 23:22:08 +00:00
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-07-23 20:47:38 +00:00
|
|
|
LIBEVAL::VALUE PCB_EXPR_VAR_REF::GetValue( LIBEVAL::CONTEXT* aCtx )
|
2020-06-16 19:49:09 +00:00
|
|
|
{
|
2020-07-23 20:47:38 +00:00
|
|
|
BOARD_ITEM* item = const_cast<BOARD_ITEM*>( GetObject( aCtx ) );
|
2020-07-19 21:22:49 +00:00
|
|
|
auto it = m_matchingTypes.find( TYPE_HASH( *item ) );
|
2020-06-13 23:22:08 +00:00
|
|
|
|
|
|
|
if( it == m_matchingTypes.end() )
|
|
|
|
{
|
2020-07-23 10:35:05 +00:00
|
|
|
// 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( "UNDEFINED" );
|
2020-06-13 23:22:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( m_type == LIBEVAL::VT_NUMERIC )
|
|
|
|
return LIBEVAL::VALUE( (double) item->Get<int>( it->second ) );
|
|
|
|
else
|
|
|
|
{
|
2020-06-16 19:49:09 +00:00
|
|
|
wxString str;
|
2020-07-23 10:35:05 +00:00
|
|
|
|
2020-06-16 19:49:09 +00:00
|
|
|
if( !m_isEnum )
|
|
|
|
{
|
|
|
|
//printf("item %p Get string '%s'\n", item, (const char*) it->second->Name().c_str() );
|
|
|
|
str = item->Get<wxString>( it->second );
|
2020-07-19 21:22:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-23 10:35:05 +00:00
|
|
|
const wxAny& any = item->Get( it->second );
|
2020-06-16 19:49:09 +00:00
|
|
|
any.GetAs<wxString>( &str );
|
|
|
|
//printf("item %p get enum: '%s'\n", item , (const char*) str.c_str() );
|
|
|
|
}
|
2020-07-23 10:35:05 +00:00
|
|
|
|
2020-06-13 23:22:08 +00:00
|
|
|
return LIBEVAL::VALUE( (const char*) str.c_str() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
|
|
|
|
LIBEVAL::UCODE::FUNC_PTR PCB_EXPR_UCODE::createFuncCall( LIBEVAL::COMPILER* aCompiler,
|
|
|
|
const std::string& name )
|
2020-06-16 19:49:09 +00:00
|
|
|
{
|
2020-07-19 21:22:49 +00:00
|
|
|
PCB_EXPR_BUILTIN_FUNCTIONS& registry = PCB_EXPR_BUILTIN_FUNCTIONS::Instance();
|
|
|
|
|
|
|
|
auto f = registry.Get( boost::to_lower_copy( name ) );
|
2020-07-03 16:36:33 +00:00
|
|
|
|
2020-06-16 19:49:09 +00:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-06-13 23:22:08 +00:00
|
|
|
LIBEVAL::VAR_REF* PCB_EXPR_UCODE::createVarRef( LIBEVAL::COMPILER *aCompiler,
|
2020-07-21 22:42:40 +00:00
|
|
|
const std::string& aVar,
|
|
|
|
const std::string& aField )
|
2020-06-13 23:22:08 +00:00
|
|
|
{
|
|
|
|
PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
|
2020-07-19 21:22:49 +00:00
|
|
|
PCB_EXPR_VAR_REF* vref = nullptr;
|
2020-06-13 23:22:08 +00:00
|
|
|
|
2020-07-21 22:42:40 +00:00
|
|
|
if( aVar == "A" )
|
2020-07-19 21:22:49 +00:00
|
|
|
{
|
|
|
|
vref = new PCB_EXPR_VAR_REF( 0 );
|
|
|
|
}
|
2020-07-21 22:42:40 +00:00
|
|
|
else if( aVar == "B" )
|
2020-07-19 21:22:49 +00:00
|
|
|
{
|
|
|
|
vref = new PCB_EXPR_VAR_REF( 1 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aCompiler->ReportError( "var" );
|
|
|
|
return vref;
|
|
|
|
}
|
2020-06-13 23:22:08 +00:00
|
|
|
|
2020-07-21 22:42:40 +00:00
|
|
|
if( aField.empty() ) // return reference to base object
|
2020-06-16 19:49:09 +00:00
|
|
|
return vref;
|
|
|
|
|
2020-07-21 22:42:40 +00:00
|
|
|
std::string field( aField );
|
|
|
|
std::replace( field.begin(), field.end(), '_', ' ');
|
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
for( const PROPERTY_MANAGER::CLASS_INFO& cls : propMgr.GetAllClasses() )
|
2020-06-13 23:22:08 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2020-06-13 23:22:08 +00:00
|
|
|
if( prop )
|
|
|
|
{
|
2020-06-16 19:49:09 +00:00
|
|
|
//printf("Field '%s' class %s ptr %p haschoices %d typeid %s\n", field.c_str(), (const char *) cls.name.c_str(), prop, !!prop->HasChoices(), typeid(*prop).name() );
|
2020-06-13 23:22:08 +00:00
|
|
|
vref->AddAllowedClass( cls.type, prop );
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-06-13 23:22:08 +00:00
|
|
|
if( prop->TypeHash() == TYPE_HASH( int ) )
|
2020-07-19 21:22:49 +00:00
|
|
|
{
|
2020-06-13 23:22:08 +00:00
|
|
|
vref->SetType( LIBEVAL::VT_NUMERIC );
|
2020-07-19 21:22:49 +00:00
|
|
|
}
|
2020-06-13 23:22:08 +00:00
|
|
|
else if( prop->TypeHash() == TYPE_HASH( wxString ) )
|
2020-07-19 21:22:49 +00:00
|
|
|
{
|
2020-06-13 23:22:08 +00:00
|
|
|
vref->SetType( LIBEVAL::VT_STRING );
|
2020-07-19 21:22:49 +00:00
|
|
|
}
|
2020-06-16 19:49:09 +00:00
|
|
|
else if ( prop->HasChoices() )
|
|
|
|
{ // it's an enum, we treat it as string
|
|
|
|
vref->SetType( LIBEVAL::VT_STRING );
|
|
|
|
vref->SetIsEnum ( true );
|
|
|
|
}
|
2020-06-13 23:22:08 +00:00
|
|
|
else
|
|
|
|
{
|
2020-06-16 19:49:09 +00:00
|
|
|
(void) 0; // should we do anything here?
|
|
|
|
//msg.Printf("Unrecognized type for property '%s'", field.c_str() );
|
|
|
|
//aCompiler->ReportError( (const char*) msg.c_str() );
|
2020-06-13 23:22:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
if( vref->GetType() == LIBEVAL::VT_UNDEFINED )
|
|
|
|
aCompiler->ReportError( "field" );
|
|
|
|
|
2020-06-13 23:22:08 +00:00
|
|
|
return vref;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class PCB_UNIT_RESOLVER : public LIBEVAL::UNIT_RESOLVER
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~PCB_UNIT_RESOLVER()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const std::vector<std::string>& GetSupportedUnits() const override
|
|
|
|
{
|
|
|
|
static const std::vector<std::string> pcbUnits = { "mil", "mm", "in" };
|
|
|
|
|
|
|
|
return pcbUnits;
|
|
|
|
}
|
|
|
|
|
2020-07-19 21:22:49 +00:00
|
|
|
virtual double Convert( const std::string& aString, int unitId ) const override
|
2020-06-13 23:22:08 +00:00
|
|
|
{
|
|
|
|
double v = atof( aString.c_str() );
|
2020-07-19 21:22:49 +00:00
|
|
|
|
2020-06-13 23:22:08 +00:00
|
|
|
switch( unitId )
|
|
|
|
{
|
2020-07-19 21:22:49 +00:00
|
|
|
case 0: return Mils2iu( v );
|
|
|
|
case 1: return Millimeter2iu( v );
|
|
|
|
case 2: return Mils2iu( v * 1000.0 );
|
|
|
|
default: return v;
|
2020-06-13 23:22:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
PCB_EXPR_COMPILER::PCB_EXPR_COMPILER()
|
|
|
|
{
|
2020-07-21 15:49:55 +00:00
|
|
|
m_unitResolver = std::make_unique<PCB_UNIT_RESOLVER>();
|
2020-06-13 23:22:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PCB_EXPR_EVALUATOR::PCB_EXPR_EVALUATOR()
|
|
|
|
{
|
2020-07-22 07:02:30 +00:00
|
|
|
m_result = 0;
|
2020-06-13 23:22:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PCB_EXPR_EVALUATOR::~PCB_EXPR_EVALUATOR()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PCB_EXPR_EVALUATOR::Evaluate( const wxString& aExpr )
|
|
|
|
{
|
2020-07-23 20:47:38 +00:00
|
|
|
PCB_EXPR_UCODE ucode;
|
|
|
|
LIBEVAL::CONTEXT preflightContext;
|
2020-07-21 15:49:55 +00:00
|
|
|
|
2020-07-23 20:47:38 +00:00
|
|
|
if( !m_compiler.Compile( (const char*) aExpr.c_str(), &ucode, &preflightContext ) )
|
2020-07-21 15:49:55 +00:00
|
|
|
{
|
|
|
|
m_errorStatus = m_compiler.GetErrorStatus();
|
2020-06-13 23:22:08 +00:00
|
|
|
return false;
|
2020-07-21 15:49:55 +00:00
|
|
|
}
|
2020-06-13 23:22:08 +00:00
|
|
|
|
2020-07-21 15:49:55 +00:00
|
|
|
// fixme: handle error conditions
|
2020-07-23 20:47:38 +00:00
|
|
|
LIBEVAL::CONTEXT ctx;
|
|
|
|
LIBEVAL::VALUE* result = ucode.Run( &ctx );
|
2020-06-13 23:22:08 +00:00
|
|
|
|
|
|
|
if( result->GetType() == LIBEVAL::VT_NUMERIC )
|
|
|
|
m_result = KiROUND( result->AsDouble() );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|