File: layer.c

package info (click to toggle)
fped 0.1%2B201210-1.1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 952 kB
  • ctags: 1,663
  • sloc: ansic: 12,650; yacc: 1,112; sh: 974; lex: 204; makefile: 140; awk: 75
file content (196 lines) | stat: -rw-r--r-- 4,441 bytes parent folder | download | duplicates (3)
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
/*
 * layer.c - PCB layers on a pad
 *
 * Written 2009-2012 by Werner Almesberger
 * Copyright 2009-2012 by Werner Almesberger
 *
 * 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.
 */

/*
 * We don't reject solder paste pads that don't cover anything yet.
 * That way, things can be constructed step by step without getting blue
 * screens all the time.
 */


#include <stdlib.h>

#include "error.h"
#include "overlap.h"
#include "inst.h"
#include "obj.h"
#include "layer.h"


/*
 * Shorthands for the layers we use in a general sense.
 */

#define LAYER_COPPER_TOP	(1 << layer_top)
#define LAYER_PASTE_TOP		(1 << layer_paste_top)
#define LAYER_MASK_TOP		(1 << layer_mask_top)
#define LAYER_COPPER_BOTTOM	(1 << layer_bottom)
#define LAYER_PASTE_BOTTOM	(1 << layer_paste_bottom)
#define LAYER_MASK_BOTTOM	(1 << layer_mask_bottom)


/* ----- Conversion between pad types and layer sets ----------------------- */


layer_type pad_type_to_layers(enum pad_type type)
{
	layer_type layers = 0;

	switch (type) {
	case pt_normal:
		layers = LAYER_PASTE_TOP;
		/* fall through */
	case pt_bare:
		layers |= LAYER_COPPER_TOP | LAYER_MASK_TOP;
		break;
	case pt_trace:
		layers = LAYER_COPPER_TOP;
		break;
	case pt_paste:
		layers = LAYER_PASTE_TOP;
		break;
	case pt_mask:
		layers = LAYER_MASK_TOP;
		break;
	default:
		abort();
	}
	return layers;
}


enum pad_type layers_to_pad_type(layer_type layers)
{
	if (layers & LAYER_COPPER_TOP) {
		if (layers & LAYER_PASTE_TOP)
			return pt_normal;
		if (layers & LAYER_MASK_TOP)
			return pt_bare;
		return pt_trace;
	} else {
		if (layers & LAYER_PASTE_TOP)
			return pt_paste;
		if (layers & LAYER_MASK_TOP)
			return pt_mask;
		abort();
	}
}


const char *pad_type_name(enum pad_type type)
{
	switch (type) {
	case pt_normal:
		return "normal";
	case pt_bare:
		return "bare";
	case pt_trace:
		return "trace";
	case pt_paste:
		return "paste";
	case pt_mask:
		return "mask";
	default:
		abort();
	}
}


/* ----- layers in mechanical holes ---------------------------------------- */


layer_type mech_hole_layers(void)
{
	return LAYER_MASK_TOP | LAYER_MASK_BOTTOM;
}


/* ----- Refine layers after instantiation --------------------------------- */


static int refine_overlapping(struct inst *copper, struct inst *other)
{
	if (other->u.pad.layers & LAYER_PASTE_TOP) {
		copper->u.pad.layers &= ~LAYER_PASTE_TOP;
		if (!inside(other, copper)) {
			fail("solder paste without copper underneath "
			    "(\"%s\" line %d, \"%s\" line %d)",
			    copper->u.pad.name, copper->obj->lineno,
			    other->u.pad.name, other->obj->lineno);
			instantiation_error = other->obj;
			return 0;
		}
	}
	if (other->u.pad.layers & LAYER_MASK_TOP)
		copper->u.pad.layers &= ~LAYER_MASK_TOP;
	return 1;
}


static int refine_copper(const struct pkg *pkg_copper, struct inst *copper,
    enum allow_overlap allow)
{
	const struct pkg *pkg;
	struct inst *other;

	for (pkg = pkgs; pkg; pkg = pkg->next) {
		/*
		 * Pads in distinct packages can happily coexist.
		 */
		if (pkg != pkgs && pkg_copper != pkgs && pkg_copper != pkg)
			continue;
		for (other = pkg->insts[ip_pad_copper]; other;
		    other = other->next)
			if (copper != other && overlap(copper, other, allow)) {
				fail("overlapping copper pads "
				    "(\"%s\" line %d, \"%s\" line %d)",
				    copper->u.pad.name, copper->obj->lineno,
				    other->u.pad.name, other->obj->lineno);
				instantiation_error = copper->obj;
				return 0;
			}
		for (other = pkg->insts[ip_pad_special]; other;
		    other = other->next)
			if (overlap(copper, other, ao_none))
				if (!refine_overlapping(copper, other))
					return 0;
	}
	return 1;
}


static void mirror_layers(layer_type *layers)
{
	if (*layers & LAYER_COPPER_TOP)
		*layers |= LAYER_COPPER_BOTTOM;
	if (*layers & LAYER_PASTE_TOP)
		*layers |= LAYER_PASTE_BOTTOM;
	if (*layers & LAYER_MASK_TOP)
		*layers |= LAYER_MASK_BOTTOM;
}


int refine_layers(enum allow_overlap allow)
{
	const struct pkg *pkg;
	struct inst *copper;

	for (pkg = pkgs; pkg; pkg = pkg->next)
		for (copper = pkg->insts[ip_pad_copper]; copper;
		    copper = copper->next) {
			if (!refine_copper(pkg, copper, allow))
				return 0;
			if (copper->u.pad.hole)
				mirror_layers(&copper->u.pad.layers);
		}
	return 1;
}