autosel.cpp 6.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 1992-2012 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
 */

24 25 26
/**
 * @file autosel.cpp
 */
27

28
// Routines for automatic selection of modules.
29

30 31
#include <fctsys.h>
#include <common.h>
32
#include <kiface_i.h>
33
#include <project.h>
34 35
#include <confirm.h>
#include <gestfich.h>
36
#include <pgm_base.h>
37 38 39 40 41 42
#include <kicad_string.h>
#include <macros.h>

#include <cvpcb.h>
#include <cvpcb_mainframe.h>
#include <cvstruct.h>
43

44 45 46 47
#define QUOTE   '\''

#define FMT_TITLE_LIB_LOAD_ERROR    _( "Library Load Error" )

48

49
class FOOTPRINT_ALIAS
50 51
{
public:
52 53
    int         m_Type;
    wxString    m_Name;
54
    wxString    m_FootprintName;
55

56
    FOOTPRINT_ALIAS() { m_Type = 0; }
57
};
58

59 60
typedef boost::ptr_vector< FOOTPRINT_ALIAS > FOOTPRINT_ALIAS_LIST;

61

62 63 64 65 66
/*
 * read the string between quotes and put it in aTarget
 * put text in aTarget
 * return a pointer to the last read char (the second quote if Ok)
 */
67
wxString GetQuotedText( wxString & text )
68
{
69
    int i = text.Find( QUOTE );
70 71 72 73

    if( wxNOT_FOUND == i )
        return wxT( "" );

74 75
    wxString shrt = text.Mid( i + 1 );
    i = shrt.Find( QUOTE );
76 77 78 79

    if( wxNOT_FOUND == i )
        return wxT( "" );

80 81
    text = shrt.Mid( i + 1 );
    return shrt.Mid( 0, i );
82
}
83

84

85
void CVPCB_MAINFRAME::AssocieModule( wxCommandEvent& event )
86
{
87 88
    FOOTPRINT_ALIAS_LIST aliases;
    FOOTPRINT_ALIAS*     alias;
89
    COMPONENT*           component;
90 91 92 93 94
    wxFileName           fn;
    wxString             msg, tmp;
    char                 Line[1024];
    FILE*                file;
    size_t               ii;
95 96

    SEARCH_STACK&        search = Kiface().KifaceSearch();
97

98
    if( m_netlist.IsEmpty() )
99 100
        return;

101
    // Find equivalents in all available files.
102
    for( ii = 0; ii < m_AliasLibNames.GetCount(); ii++ )
103
    {
104
        fn = m_AliasLibNames[ii];
105

106 107
        if( !fn.HasExt() )
        {
108
            fn.SetExt( FootprintAliasFileExtension );
109
            // above fails if filename has more than one point
110 111 112 113 114
        }
        else
        {
            fn.SetExt( fn.GetExt() + wxT( "." ) + FootprintAliasFileExtension );
        }
115

116
        tmp = search.FindValidPath( fn.GetFullPath() );
117 118 119

        if( !tmp )
        {
120
            msg.Printf( _( "Footprint alias library file '%s' could not be found in the "
121
                           "default search paths." ),
122
                        GetChars( fn.GetFullName() ) );
123
            wxMessageBox( msg, FMT_TITLE_LIB_LOAD_ERROR, wxOK | wxICON_ERROR );
124 125 126 127 128 129 130
            continue;
        }

        file = wxFopen( tmp, wxT( "rt" ) );

        if( file == NULL )
        {
131 132
            msg.Printf( _( "Error opening alias library '%s'." ), GetChars( tmp ) );
            wxMessageBox( msg, FMT_TITLE_LIB_LOAD_ERROR, wxOK | wxICON_ERROR );
133 134 135
            continue;
        }

136
        while( GetLine( file, Line, NULL, sizeof(Line) ) != NULL )
137
        {
138
            char* text = Line;
139
            wxString value, footprint, wtext = FROM_UTF8( Line );
140

141
            value = GetQuotedText( wtext );
142

143
            if( text == NULL || ( *text == 0 ) || value.IsEmpty() )
144
                continue;
145

146
            footprint = GetQuotedText( wtext );
147

148 149
            if( footprint.IsEmpty() )
                continue;
150

151 152
            value.Replace( wxT( " " ), wxT( "_" ) );

153 154 155 156
            alias = new FOOTPRINT_ALIAS();
            alias->m_Name = value;
            alias->m_FootprintName = footprint;
            aliases.push_back( alias );
157 158 159 160 161
        }

        fclose( file );
    }

162
    // Display the number of footprint aliases.
163
    msg.Printf( _( "%d footprint aliases found." ), aliases.size() );
164 165
    SetStatusText( msg, 0 );

166
    m_skipComponentSelect = true;
167
    ii = 0;
168 169

    for( unsigned kk = 0;  kk < m_netlist.GetCount();  kk++ )
170
    {
171 172
        component = m_netlist.GetComponent( kk );

173
        bool found = false;
174
        m_compListBox->SetSelection( ii++, true );
175

176
        if( !component->GetFPID().empty() )
177
            continue;
178

179
        BOOST_FOREACH( FOOTPRINT_ALIAS& alias, aliases )
180
        {
181

182
            if( alias.m_Name.CmpNoCase( component->GetValue() ) != 0 )
183
                continue;
184

185 186
            // filter alias so one can use multiple aliases (for polar and
            // nonpolar caps for example)
187
            const FOOTPRINT_INFO *module = m_footprints.GetModuleInfo( alias.m_FootprintName );
188 189 190

            if( module )
            {
191
                size_t filtercount = component->GetFootprintFilters().GetCount();
192
                found = ( 0 == filtercount ); // if no entries, do not filter
193

194
                for( size_t jj = 0; jj < filtercount && !found; jj++ )
195
                {
196
                    found = module->GetFootprintName().Matches( component->GetFootprintFilters()[jj] );
197 198 199
                }
            }
            else
200
            {
201 202 203
                msg.Printf( _( "Component %s: footprint %s not found in any of the project "
                               "footprint libraries." ),
                            GetChars( component->GetReference() ),
204
                            GetChars( alias.m_FootprintName ) );
205
                wxMessageBox( msg, _( "CvPcb Error" ), wxOK | wxICON_ERROR, this );
206
            }
207

208 209 210 211 212 213
            if( found )
            {
                SetNewPkg( alias.m_FootprintName );
                break;
            }

214
        }
215

216
        // obviously the last chance: there's only one filter matching one footprint
217 218
        if( !found && 1 == component->GetFootprintFilters().GetCount() )
        {
219 220
            // we do not need to analyse wildcards: single footprint do not
            // contain them and if there are wildcards it just will not match any
221
            const FOOTPRINT_INFO* module = m_footprints.GetModuleInfo( component->GetFootprintFilters()[0] );
222 223 224 225

            if( module )
            {
                SetNewPkg( component->GetFootprintFilters()[0] );
226 227
            }
        }
228
    }
229

230
    m_skipComponentSelect = false;
231
}