KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_io_fabmaster.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2020 BeagleBoard Foundation
5 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-3.0.html
20 * or you may search the http://www.gnu.org website for the version 3 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
28
29#include "pcb_io_fabmaster.h"
30#include <board.h>
31#include <progress_reporter.h>
32#include <common.h>
33#include <macros.h>
34
35#include <algorithm>
36#include <fstream>
37#include <string>
38
39
41{
42}
43
44
48
49
50bool PCB_IO_FABMASTER::CanReadBoard( const wxString& aFileName ) const
51{
52 if( !PCB_IO::CanReadBoard( aFileName ) )
53 return false;
54
55 std::ifstream file( aFileName.fn_str() );
56
57 if( !file.is_open() )
58 return false;
59
60 // Fabmaster files are !-delimited ASCII with known column headers.
61 // Scan the first 100 lines for rows containing at least two ! delimiters
62 // and a recognized Fabmaster column name.
63 static const char* keywords[] = {
64 "REFDES", "COMPCLASS", "NETNAME", "SUBCLASS", "GRAPHICDATANAME",
65 "SYMNAME", "PINNAME", "VIAX", "PADSHAPENAME", "PADNAME", "LAYERSORT"
66 };
67
68 std::string line;
69 int linesRead = 0;
70
71 while( std::getline( file, line ) && linesRead < 100 )
72 {
73 linesRead++;
74
75 int delimCount = 0;
76
77 for( char ch : line )
78 {
79 if( ch == '!' )
80 delimCount++;
81 }
82
83 if( delimCount < 2 )
84 continue;
85
86 // Uppercase the line for case-insensitive matching
87 std::string upper = line;
88 std::transform( upper.begin(), upper.end(), upper.begin(), ::toupper );
89
90 for( const char* kw : keywords )
91 {
92 if( upper.find( kw ) != std::string::npos )
93 return true;
94 }
95 }
96
97 return false;
98}
99
100
101BOARD* PCB_IO_FABMASTER::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
102 const std::map<std::string, UTF8>* aProperties, PROJECT* aProject )
103{
104 m_props = aProperties;
105
106 m_board = aAppendToMe ? aAppendToMe : new BOARD();
107
108 // Give the filename to the board if it's new
109 if( !aAppendToMe )
110 m_board->SetFileName( aFileName );
111
113 {
114 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
115
116 if( !m_progressReporter->KeepRefreshing() )
117 THROW_IO_ERROR( _( "File import canceled by user." ) );
118 }
119
120 if( !m_fabmaster.Read( aFileName.ToStdString() ) )
121 {
122 std::string readerr;
123
124 readerr = _( "Could not read file " ) + aFileName.ToStdString();
125 THROW_IO_ERROR( readerr );
126 }
127
128 m_fabmaster.Process();
130
131 return m_board;
132}
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition io_base.h:240
PCB_IO_FABMASTER()
Pcbnew PLUGIN for FABMASTER ASCII *.txt / *.fab format.
BOARD * LoadBoard(const wxString &aFileName, BOARD *aAppendToMe, const std::map< std::string, UTF8 > *aProperties=nullptr, PROJECT *aProject=nullptr) override
Load information from some input file format that this PCB_IO implementation knows about into either ...
bool CanReadBoard(const wxString &aFileName) const override
Checks if this PCB_IO can read the specified board file.
BOARD * m_board
The board BOARD being worked on, no ownership here.
Definition pcb_io.h:344
virtual bool CanReadBoard(const wxString &aFileName) const
Checks if this PCB_IO can read the specified board file.
Definition pcb_io.cpp:42
PCB_IO(const wxString &aName)
Definition pcb_io.h:337
const std::map< std::string, UTF8 > * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
Definition pcb_io.h:347
Container for project specific data.
Definition project.h:65
The common library.
#define _(s)
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
This file contains miscellaneous commonly used macros and functions.