File: wholepoly.c

package info (click to toggle)
pcb-rnd 3.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 29,624 kB
  • sloc: ansic: 197,571; yacc: 6,153; sh: 5,808; awk: 2,708; makefile: 2,139; lex: 1,107; python: 519; xml: 261; lisp: 169; tcl: 67; perl: 34; javascript: 6; ruby: 5
file content (292 lines) | stat: -rw-r--r-- 7,788 bytes parent folder | download | duplicates (4)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 *                            COPYRIGHT
 *
 *  pcb-rnd, interactive printed circuit board design
 *
 *  Extended object for a wholepoly; edit: first polygon; explicit polygon object for each island
 *  pcb-rnd Copyright (C) 2020 Tibor 'Igor2' Palinkas
 *
 *  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, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *  Contact:
 *    Project page: http://repo.hu/projects/pcb-rnd
 *    lead developer: http://repo.hu/projects/pcb-rnd/contact.html
 *    mailing list: pcb-rnd (at) list.repo.hu (send "subscribe")
 */

#define LID_WHP 0

typedef struct {
	RND_DAD_DECL_NOINIT(dlg)
	int gui_active;
} wholepoly;

static void pcb_wholepoly_del_pre(pcb_subc_t *subc)
{
	wholepoly *whp = subc->extobj_data;
	rnd_trace("WhP del_pre\n");

	if ((whp != NULL) && (whp->gui_active))
		RND_DAD_FREE(whp->dlg);

	free(whp);
	subc->extobj_data = NULL;
}

static void wholepoly_unpack(pcb_subc_t *obj)
{
#if 0
/* no settings yet */
	wholepoly *whp;

	if (obj->extobj_data == NULL)
		obj->extobj_data = calloc(sizeof(wholepoly), 1);
	whp = obj->extobj_data;
#endif
}

/* remove all existing padstacks from the subc */
static void wholepoly_clear(pcb_subc_t *subc)
{
	pcb_poly_t *p, *back = NULL;
	pcb_layer_t *ly = &subc->data->Layer[LID_WHP];

	for(p = polylist_first(&ly->Polygon); p != NULL; p = polylist_next(p)) {
		if (PCB_FLAG_TEST(PCB_FLAG_FLOATER, p)) {
			/* do not free the floater, but remember as the last kept floater */
			back = p;
		}
		else {
			pcb_poly_free(p);
			p = back;
			if (p == NULL)
				p = polylist_first(&ly->Polygon);
		}
	}
}

static void wholepoly_gen_poly(pcb_board_t *pcb, pcb_subc_t *subc, pcb_poly_t *poly)
{
	pcb_poly_it_t it;
	rnd_polyarea_t *pa;
	double best = 0;
	rnd_pline_t *best_pl;

	/* figure the biggest area island - that one shouldn't be traced */
	for(pa = pcb_poly_island_first(poly, &it); pa != NULL; pa = pcb_poly_island_next(&it)) {
		rnd_pline_t *pl = pcb_poly_contour(&it);
		if (pl != NULL) {
			if (pl->area > best) {
				best = pl->area;
				best_pl = pl;
			}
		}
	}

	/* trace each missing island with a new poly */
	for(pa = pcb_poly_island_first(poly, &it); pa != NULL; pa = pcb_poly_island_next(&it)) {
		int go;
		rnd_coord_t x, y;
		rnd_pline_t *pl = pcb_poly_contour(&it);
		pcb_poly_t *np;

		if ((pl == NULL) || (pl == best_pl))
			continue;

		np = pcb_poly_new(poly->parent.layer, poly->Clearance, poly->Flags);
		PCB_FLAG_CLEAR(PCB_FLAG_FLOATER, np);
		for(go = pcb_poly_vect_first(&it, &x, &y); go; go = pcb_poly_vect_next(&it, &x, &y))
			pcb_poly_point_new(np, x, y);
		pcb_add_poly_on_layer(poly->parent.layer, np);
	}
}

static int wholepoly_gen(pcb_subc_t *subc, pcb_any_obj_t *edit_obj)
{
	pcb_poly_t *poly;
	pcb_board_t *pcb = pcb_data_get_top(subc->data);
	pcb_layer_t *ly = &subc->data->Layer[LID_WHP];
	wholepoly *whp;

	if (subc->extobj_data == NULL)
		wholepoly_unpack(subc);

	whp = subc->extobj_data;

	pcb_exto_regen_begin(subc);
	for(poly = polylist_first(&ly->Polygon); poly != NULL; poly = polylist_next(poly))
		wholepoly_gen_poly(pcb, subc, poly);

	return pcb_exto_regen_end(subc);
}

static void pcb_wholepoly_draw_mark(pcb_draw_info_t *info, pcb_subc_t *subc)
{
	pcb_poly_t *poly;
	pcb_layer_t *ly = &subc->data->Layer[LID_WHP];

	if (subc->extobj_data == NULL)
		wholepoly_unpack(subc);

	printf("mark!!!\n");

	pcb_exto_draw_mark(info, subc);
}


static void pcb_wholepoly_float_pre(pcb_subc_t *subc, pcb_any_obj_t *edit_obj)
{
	rnd_trace("WhP: edit pre %ld %ld\n", subc->ID, edit_obj->ID);
	wholepoly_clear(subc);
}

static void pcb_wholepoly_float_geo(pcb_subc_t *subc, pcb_any_obj_t *edit_obj)
{
	rnd_trace("WhP: edit geo %ld %ld\n", subc->ID, edit_obj == NULL ? -1 : edit_obj->ID);
	wholepoly_gen(subc, edit_obj);
}

static pcb_extobj_new_t pcb_wholepoly_float_new(pcb_subc_t *subc, pcb_any_obj_t *floater)
{
	rnd_trace("WhP: float new %ld %ld\n", subc->ID, floater->ID);
	return PCB_EXTONEW_FLOATER;
}

static pcb_extobj_del_t pcb_wholepoly_float_del(pcb_subc_t *subc, pcb_any_obj_t *floater)
{
	pcb_layer_t *ly = &subc->data->Layer[LID_WHP];
	long len = polylist_length(&ly->Polygon);

	rnd_trace("WhP: float del %ld %ld edit-objs=%ld\n", subc->ID, floater->ID, len);

	return len == 1 ? PCB_EXTODEL_SUBC : PCB_EXTODEL_FLOATER; /* removing the last floater should remove the subc */
}


static void pcb_wholepoly_chg_attr(pcb_subc_t *subc, const char *key, const char *value)
{
	rnd_trace("WhP chg_attr\n");
	if (strncmp(key, "extobj::", 8) == 0) {
		wholepoly_clear(subc);
		wholepoly_unpack(subc);
		wholepoly_gen(subc, NULL);
	}
}


static pcb_subc_t *pcb_wholepoly_conv_objs(pcb_data_t *dst, vtp0_t *objs, pcb_subc_t *copy_from)
{
	long n;
	rnd_coord_t cx, cy;
	pcb_subc_t *subc;
	pcb_poly_t *p;
	pcb_layer_t *ly;
	pcb_dflgmap_t layers[] = {
		{"edit", PCB_LYT_DOC, "extobj", 0, 0},
		{NULL, 0, NULL, 0, 0}
	};

	rnd_trace("WhP: conv_objs\n");

	/* refuse anything that's not a polygon */
	for(n = 0; n < objs->used; n++) {
		p = objs->array[n];
		if (p->type != PCB_OBJ_POLY)
			return NULL;
	}

	/* use the layer of the first object */
	p = objs->array[0];
	layers[0].lyt = pcb_layer_flags_(p->parent.layer);
	pcb_layer_purpose_(p->parent.layer, &layers[0].purpose);

	cx = cy = 0;
	for(n = 0; n < p->PointN; n++) {
		cx += p->Points[n].X;
		cy += p->Points[n].Y;
	}
	cx /= (double)p->PointN;
	cy /= (double)p->PointN;
rnd_printf("Center: %mm;%mm\n", cx, cy);
	subc = pcb_exto_create(dst, "wholepoly", layers, cx, cy, 0, copy_from);

#if 0
	if (copy_from == NULL)
		pcb_attribute_put(&subc->Attributes, "extobj::pitch", "4mm");
#endif

	/* create edit-objects */
	ly = &subc->data->Layer[LID_WHP];
	for(n = 0; n < objs->used; n++) {
		p = pcb_poly_dup(ly, objs->array[n]);
		PCB_FLAG_SET(PCB_FLAG_FLOATER, p);
		PCB_FLAG_CLEAR(PCB_FLAG_SELECTED, p);
		pcb_attribute_put(&p->Attributes, "extobj::role", "edit");
	}

	wholepoly_unpack(subc);
	wholepoly_gen(subc, NULL);
	return subc;
}


static void pcb_wholepoly_close_cb(void *caller_data, rnd_hid_attr_ev_t ev)
{
	pcb_subc_t *subc = caller_data;
	wholepoly *whp = subc->extobj_data;

	RND_DAD_FREE(whp->dlg);
	whp->gui_active = 0;
}

static void pcb_wholepoly_gui_propedit(pcb_subc_t *subc)
{
	rnd_hid_dad_buttons_t clbtn[] = {{"Close", 0}, {NULL, 0}};
	wholepoly *whp;

	rnd_trace("WhP: gui propedit\n");

	if (subc->extobj_data == NULL)
		wholepoly_unpack(subc);
	whp = subc->extobj_data;

	rnd_trace("WhP: active=%d\n", whp->gui_active);
	if (whp->gui_active)
		return; /* do not open another */

	RND_DAD_BEGIN_VBOX(whp->dlg);
		RND_DAD_COMPFLAG(whp->dlg, RND_HATF_EXPFILL);
		RND_DAD_LABEL(whp->dlg, "The whole poly extended object\ndoes not have settings");
		RND_DAD_BUTTON_CLOSES(whp->dlg, clbtn);
	RND_DAD_END(whp->dlg);

	/* set up the context */
	whp->gui_active = 1;

	RND_DAD_NEW("wholepoly", whp->dlg, "Whole polygons", subc, rnd_false, pcb_wholepoly_close_cb);
}

static pcb_extobj_t pcb_wholepoly = {
	"wholepoly",
	pcb_wholepoly_draw_mark,
	pcb_wholepoly_float_pre,
	pcb_wholepoly_float_geo,
	pcb_wholepoly_float_new,
	pcb_wholepoly_float_del,
	pcb_wholepoly_chg_attr,
	pcb_wholepoly_del_pre,
	pcb_wholepoly_conv_objs,
	pcb_wholepoly_gui_propedit
};