-
Notifications
You must be signed in to change notification settings - Fork 7
/
board.cpp
166 lines (140 loc) · 4.76 KB
/
board.cpp
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
/*
* This file is part of pcb2gcode.
*
* Copyright (C) 2009, 2010 Patrick Birnzain <[email protected]>
*
* pcb2gcode 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 3 of the License, or
* (at your option) any later version.
*
* pcb2gcode 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 pcb2gcode. If not, see <http://www.gnu.org/licenses/>.
*/
#include "board.hpp"
typedef pair<string, shared_ptr<Layer> > layer_t;
Board::Board( int _dpi, bool _fill_outline, double _outline_width)
{
margin = 0.0;
dpi = _dpi;
fill_outline = _fill_outline;
outline_width = _outline_width;
}
double
Board::get_width()
{
return layers.begin()->second->surface->get_width_in();
}
double
Board::get_height()
{
return layers.begin()->second->surface->get_height_in();
}
uint
Board::get_dpi()
{
return dpi;
}
void
Board::prepareLayer( string layername, shared_ptr<LayerImporter> importer, shared_ptr<RoutingMill> manufacturer, bool mirror, bool mirror_absolute )
{
// see comment for prep_t in board.hpp
prepared_layers.insert( std::make_pair( layername, make_tuple(importer, manufacturer, mirror, mirror_absolute) ) );
}
void
Board::createLayers()
{
if( !prepared_layers.size() )
throw std::logic_error("No layers prepared.");
// start calculating the minimal board size
min_x = INFINITY;
max_x = -INFINITY;
min_y = INFINITY;
max_y = -INFINITY;
// calculate room needed by the PCB traces
for( map< string, prep_t >::iterator it = prepared_layers.begin(); it != prepared_layers.end(); it++ ) {
shared_ptr<LayerImporter> importer = it->second.get<0>();
float t;
t = importer->get_min_x();
if(min_x > t) min_x = t;
t = importer->get_max_x();
if(max_x < t) max_x = t;
t = importer->get_min_y();
if(min_y > t) min_y = t;
t = importer->get_max_y();
if(max_y < t) max_y = t;
}
// if there's no pcb outline, add the specified margins
try {
shared_ptr<RoutingMill> outline_mill = prepared_layers.at("outline").get<1>();
ivalue_t radius = outline_mill->tool_diameter / 2;
min_x -= radius;
max_x += radius;
min_y -= radius;
max_y += radius;
}
catch( std::logic_error& e ) {
min_x -= margin;
max_x += margin;
min_y -= margin;
max_y += margin;
}
// board size calculated. create layers
for( map<string, prep_t>::iterator it = prepared_layers.begin(); it != prepared_layers.end(); it++ ) {
// prepare the surface
shared_ptr<Surface> surface( new Surface(dpi, min_x, max_x, min_y, max_y) );
shared_ptr<LayerImporter> importer = it->second.get<0>();
surface->render(importer);
shared_ptr<Layer> layer( new Layer(it->first, surface, it->second.get<1>(), it->second.get<2>(), it->second.get<3>() ) ); // see comment for prep_t in board.hpp
layers.insert( std::make_pair( layer->get_name(), layer ) );
}
// DEBUG output
BOOST_FOREACH( layer_t layer, layers ) {
layer.second->surface->save_debug_image(string("original_")+layer.second->get_name());
}
// mask layers with outline
if( prepared_layers.find("outline") != prepared_layers.end()) {
shared_ptr<Layer> outline_layer = layers.at("outline");
if(fill_outline) {
outline_layer->surface->fill_outline(outline_width);
}
for (map<string, shared_ptr<Layer> >::iterator it = layers.begin(); it != layers.end(); it++ ) {
if(it->second != outline_layer) {
it->second->add_mask(outline_layer);
it->second->surface->save_debug_image("masked");
}
}
}
}
vector< shared_ptr<icoords> >
Board::get_toolpath( string layername )
{
vector< shared_ptr<icoords> > toolpath;
try {
return layers[layername]->get_toolpaths();
} catch ( std::logic_error& e ) {
std::stringstream msg;
msg << "class Board: get_toolpath(): layer not available: ";
msg << layername << std::endl;
throw std::logic_error( msg.str() );
}
}
vector< string >
Board::list_layers()
{
vector<string> layerlist;
BOOST_FOREACH( layer_t layer, layers ) {
layerlist.push_back( layer.first );
}
return layerlist;
}
shared_ptr<Layer>
Board::get_layer( string layername )
{
return layers.at(layername);
}