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, see <https://www.gnu.org/licenses/>.
19 */
20
24
25#include "pcb_io_fabmaster.h"
26#include <board.h>
27#include <progress_reporter.h>
28#include <common.h>
29#include <macros.h>
30
31#include <algorithm>
32#include <fstream>
33#include <string>
34
35
37{
38}
39
40
44
45
46bool PCB_IO_FABMASTER::CanReadBoard( const wxString& aFileName ) const
47{
48 if( !PCB_IO::CanReadBoard( aFileName ) )
49 return false;
50
51 std::ifstream file( aFileName.fn_str() );
52
53 if( !file.is_open() )
54 return false;
55
56 // Fabmaster files are !-delimited ASCII with known column headers.
57 // Scan the first 100 lines for rows containing at least two ! delimiters
58 // and a recognized Fabmaster column name.
59 static const char* keywords[] = {
60 "REFDES", "COMPCLASS", "NETNAME", "SUBCLASS", "GRAPHICDATANAME",
61 "SYMNAME", "PINNAME", "VIAX", "PADSHAPENAME", "PADNAME", "LAYERSORT"
62 };
63
64 std::string line;
65 int linesRead = 0;
66
67 while( std::getline( file, line ) && linesRead < 100 )
68 {
69 linesRead++;
70
71 int delimCount = 0;
72
73 for( char ch : line )
74 {
75 if( ch == '!' )
76 delimCount++;
77 }
78
79 if( delimCount < 2 )
80 continue;
81
82 // Uppercase the line for case-insensitive matching
83 std::string upper = line;
84 std::transform( upper.begin(), upper.end(), upper.begin(), ::toupper );
85
86 for( const char* kw : keywords )
87 {
88 if( upper.find( kw ) != std::string::npos )
89 return true;
90 }
91 }
92
93 return false;
94}
95
96
97BOARD* PCB_IO_FABMASTER::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
98 const std::map<std::string, UTF8>* aProperties, PROJECT* aProject )
99{
100 m_props = aProperties;
101
102 m_board = aAppendToMe ? aAppendToMe : new BOARD();
103
104 // Give the filename to the board if it's new
105 if( !aAppendToMe )
106 m_board->SetFileName( aFileName );
107
109 {
110 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
111
112 if( !m_progressReporter->KeepRefreshing() )
113 THROW_IO_ERROR( _( "File import canceled by user." ) );
114 }
115
116 if( !m_fabmaster.Read( aFileName.ToStdString() ) )
117 {
118 std::string readerr;
119
120 readerr = _( "Could not read file " ) + aFileName.ToStdString();
121 THROW_IO_ERROR( readerr );
122 }
123
124 m_fabmaster.Process();
126
127 return m_board;
128}
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:372
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:349
virtual bool CanReadBoard(const wxString &aFileName) const
Checks if this PCB_IO can read the specified board file.
Definition pcb_io.cpp:38
PCB_IO(const wxString &aName)
Definition pcb_io.h:342
const std::map< std::string, UTF8 > * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
Definition pcb_io.h:352
Container for project specific data.
Definition project.h:62
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.