summaryrefslogtreecommitdiff
path: root/gerbview/class_am_param.cpp
blob: 63306a42514c18f149d232ed732e3b187d67f121 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
 * @file class_am_param.cpp
 */

/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 1992-2010 Jean-Pierre Charras <jean-pierre.charras@gipsa-lab.inpg.fr>
 * Copyright (C) 2010 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
 * Copyright (C) 1992-2010 KiCad Developers, see change_log.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 <class_am_param.h>
#include <class_aperture_macro.h>

extern int    ReadInt( char*& text, bool aSkipSeparator = true );
extern double ReadDouble( char*& text, bool aSkipSeparator = true );

/* Class AM_PARAM
 * holds a parameter value for an "aperture macro" as defined within
 * standard RS274X.  The parameter can be a constant, i.e. "immediate" parameter,
 * or depend on some defered values, defined in a D_CODE, by the ADD command.
 * Note the actual value could need an evaluation from an arithmetical expression
 * items in the expression are stored in .
 * A simple definition is just a value stored in one item in m_paramStack
 */
AM_PARAM::AM_PARAM( )
{
    m_index = -1;
}

/**
 * Function IsImmediate
 * tests if this AM_PARAM holds an immediate parameter or has parameter
 * held by an owning D_CODE.
 */
bool AM_PARAM::IsImmediate() const
{
    bool isimmediate = true;
    for( unsigned ii = 0; ii < m_paramStack.size(); ii++ )
    {
        if( m_paramStack[ii].IsDefered() )
        {   // a defered value is found in operand list,
            // so the parameter is not immediate
            isimmediate = false;
            break;
        }
    }
    return isimmediate;
}

double AM_PARAM::GetValue( const D_CODE* aDcode ) const
{
    double paramvalue = 0.0;
    double curr_value = 0.0;
    parm_item_type state = POPVALUE;

    for( unsigned ii = 0; ii < m_paramStack.size(); ii++ )
    {
        AM_PARAM_ITEM item = m_paramStack[ii];
        switch( item.GetType() )
        {
            case ADD:
            case SUB:
            case MUL:
            case DIV:   // just an operator for next parameter value: store it
                state = item.GetType();
                break;

            case PUSHPARM:
                // get the parameter from the aDcode
                if( aDcode )    // should be always true here
                {
                    if( item.GetIndex() <= aDcode->GetParamCount() )
                        curr_value = aDcode->GetParam( item.GetIndex() );
                    else    // Get parameter from local param definition
                    {
                        const APERTURE_MACRO * am_parent = aDcode->GetMacro();
                        curr_value = am_parent->GetLocalParam( aDcode, item.GetIndex() );
                    }
                }
                else
                {
                    wxLogDebug( wxT( "AM_PARAM::GetValue(): NULL param aDcode\n" ) );
                }
                // Fall through
            case PUSHVALUE: // a value is on the stack:
                if( item.GetType() == PUSHVALUE )
                    curr_value = item.GetValue();
                switch( state )
                {
                    case POPVALUE:
                        paramvalue = curr_value;
                        break;

                    case ADD:
                        paramvalue += curr_value;
                        break;

                    case SUB:
                        paramvalue -= curr_value;
                        break;

                    case MUL:
                        paramvalue *= curr_value;
                        break;

                    case DIV:
                        paramvalue /= curr_value;
                        break;

                    default:
                        wxLogDebug( wxT( "AM_PARAM::GetValue() : unexpected operator\n" ) );
                        break;
                }
                break;

            default:
                wxLogDebug( wxT( "AM_PARAM::GetValue(): unexpected type\n" ) );
                break;
        }
    }
    return paramvalue;
}

/**
 * add an operator/operand to the current stack
 * aType = NOP, PUSHVALUE, PUSHPARM, ADD, SUB, MUL, DIV, EQUATE
 * aValue required only for PUSHVALUE (double) or PUSHPARM (int) aType.
 */
void AM_PARAM::PushOperator( parm_item_type aType, double aValue )
{
    AM_PARAM_ITEM item( aType, aValue);
    m_paramStack.push_back( item );
}

void AM_PARAM::PushOperator( parm_item_type aType, int aValue )
{
    AM_PARAM_ITEM item( aType, aValue);
    m_paramStack.push_back( item );
}

/**
 * Function ReadParam
 * Read one aperture macro parameter
 * a parameter can be:
 *      a number
 *      a reference to an aperture definition parameter value: $1 ot $3 ...
 * a parameter definition can be complex and have operators between numbers and/or other parameter
 * like $1+3 or $2x2..
 * Note minus sign is not always an operator. It can be the sign of a value.
 * Parameters are separated by a comma ( of finish by *)
 * @param aText = pointer to the parameter to read. Will be modified to point to the next field
 * @return true if a param is read, or false
 */
bool AM_PARAM::ReadParam( char*& aText  )
{
    bool found = false;
    int ivalue;
    double dvalue;
    bool end = false;

    while( !end )
    {
        switch( *aText )
        {
            case ',':
                aText++;
                // fall through
            case 0:     // EOL
            case '*':   // Terminator in a gerber command
                end = true;
                break;

            case ' ':
                aText++;
                break;

            case '$':
                // defered value defined later, in ADD command which define defered parameters
                ++aText;
                ivalue = ReadInt( aText, false );
                if( m_index < 1 )
                    SetIndex( ivalue );
                PushOperator( PUSHPARM, ivalue );
                found = true;
                break;

            case '/':
                PushOperator( DIV );
                aText++;
                break;

            case 'x':
            case 'X':
                PushOperator( MUL );
                aText++;
                break;

            case '-':
            case '+':
                // Test if this is an operator between 2 params, or the sign of a value
                if( m_paramStack.size() > 0 && !m_paramStack.back().IsOperator() )
                {   // Seems an operator
                    PushOperator( *aText == '+' ? ADD : SUB );
                    aText++;
                }
                else
                {   // seems the sign of a value
                    dvalue = ReadDouble( aText, false );
                    PushOperator( PUSHVALUE, dvalue );
                    found = true;
                }
                break;

            case '=':   // A local definition found like $4=$3/2
                // At this point, one defered parameter is expected to be read.
                // this parameter value (the index) is stored in m_index.
                // The list of items is cleared
                aText++;
                m_paramStack.clear();
                found = false;
                break;

            default:
                dvalue = ReadDouble( aText, false );
                PushOperator( PUSHVALUE, dvalue );
                found = true;
                break;
        }
    }

    return found;
}