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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
/*
* COPYRIGHT
*
* pcb-rnd, interactive printed circuit board design
* Copyright (C) 2018 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")
*/
#include "config.h"
#include "pstk_help.h"
#include "obj_pstk_inlines.h"
#include <librnd/core/compat_misc.h>
#include "search.h"
#include "remove.h"
#include "find.h"
pcb_pstk_t *pcb_pstk_new_hole(pcb_data_t *data, rnd_coord_t x, rnd_coord_t y, rnd_coord_t drill_dia, rnd_bool plated)
{
pcb_pstk_proto_t proto;
rnd_cardinal_t pid;
memset(&proto, 0, sizeof(proto));
proto.hdia = drill_dia;
proto.hplated = plated;
pcb_pstk_proto_update(&proto);
pid = pcb_pstk_proto_insert_dup(data, &proto, 1, 0);
return pcb_pstk_new(data, -1, pid, x, y, 0, pcb_flag_make(PCB_FLAG_CLEARLINE));
}
pcb_layer_type_t lyts[] = {
/* primary for thru-hole */
PCB_LYT_TOP | PCB_LYT_COPPER,
PCB_LYT_BOTTOM | PCB_LYT_COPPER,
/* secondary */
PCB_LYT_INTERN | PCB_LYT_COPPER,
PCB_LYT_TOP | PCB_LYT_PASTE,
PCB_LYT_TOP | PCB_LYT_SILK,
PCB_LYT_TOP | PCB_LYT_MASK,
PCB_LYT_BOTTOM | PCB_LYT_MASK,
PCB_LYT_BOTTOM | PCB_LYT_SILK,
PCB_LYT_BOTTOM | PCB_LYT_PASTE
};
#define NUM_LYTS (sizeof(lyts) / sizeof(lyts[0]))
static int vect2pstk_conv_cand(pcb_data_t *data, vtp0_t *objs, rnd_bool_t quiet, pcb_any_obj_t **cand, int *num_cand, rnd_coord_t cx, rnd_coord_t cy, int plated, pcb_any_obj_t *extrao)
{
pcb_pstk_proto_t proto;
int l, ci, res = -1;
vtp0_t tmp;
char *term = NULL;
vtp0_init(&tmp);
if (extrao != NULL)
vtp0_append(&tmp, extrao);
for(l = 0; l < NUM_LYTS; l++) {
if (num_cand[l] == 1) {
vtp0_append(&tmp, cand[l]);
if (term == NULL)
term = pcb_attribute_get(&cand[l]->Attributes, "term");
}
}
rnd_trace("Converting %d objects into a pstk\n", tmp.used);
memset(&proto, 0, sizeof(proto));
if (pcb_pstk_proto_conv(data, &proto, quiet, &tmp, cx, cy) == 0) {
rnd_cardinal_t pid;
if (plated != -1)
proto.hplated = plated;
pid = pcb_pstk_proto_insert_or_free(data, &proto, quiet, 0);
if (pid != PCB_PADSTACK_INVALID) {
pcb_pstk_t *ps = pcb_pstk_new(data, -1, pid, cx, cy, 0, pcb_flag_make(PCB_FLAG_CLEARLINE | PCB_FLAG_FOUND));
vtp0_append(objs, ps);
if (term != NULL)
pcb_attribute_put(&ps->Attributes, "term", term);
/* got our new proto - remove the objects we used */
for(ci = 0; ci < tmp.used; ci++) {
int i;
pcb_any_obj_t *o = tmp.array[ci];
for(i = 0; i < objs->used; i++) {
if (objs->array[i] == o) {
vtp0_remove(objs, i, 1);
pcb_destroy_object(data, o->type, o->parent.any, o, o);
break;
}
}
}
}
res = 0;
}
vtp0_uninit(&tmp);
return res;
}
static void clear_found(vtp0_t *objs)
{
long n;
for(n = 0; n < objs->used; n++)
PCB_FLAG_CLEAR(PCB_FLAG_FOUND, (pcb_any_obj_t *)(objs->array[n]));
}
int pcb_pstk_vect2pstk_thr(pcb_data_t *data, vtp0_t *objs, rnd_bool_t quiet)
{
int l, n, plated, done = 0, ci;
pcb_pstk_proto_t *p;
rnd_coord_t cx, cy, d, r, valid;
pcb_any_obj_t *cand[NUM_LYTS];
int num_cand[NUM_LYTS];
/* output padstacks are going to be marked with the FOUND flag */
clear_found(objs);
for(n = 0; n < objs->used; n++) {
pcb_any_obj_t *h = objs->array[n];
/* find holes */
switch(h->type) {
case PCB_OBJ_PSTK:
p = pcb_pstk_get_proto((pcb_pstk_t *)h);
if ((p == NULL) || (p->hdia <= 0))
continue;
cx = ((pcb_pstk_t *)h)->x;
cy = ((pcb_pstk_t *)h)->y;
d = p->hdia;
plated = p->hplated;
break;
default:
continue;
}
r = d/2;
/* we have a hole center and dia - look for objects that could be pads for this hole */
memset(cand, 0, sizeof(cand));
memset(num_cand, 0, sizeof(num_cand));
for(ci = 0; ci < objs->used; ci++) {
pcb_layer_type_t lyt;
pcb_any_obj_t *c = objs->array[ci]; /* candidate */
if (PCB_FLAG_TEST(PCB_FLAG_FOUND, c)) continue; /* already used */
switch(c->type) {
case PCB_OBJ_LINE:
if (!pcb_is_point_in_line(cx, cy, r, (pcb_any_line_t *)c)) continue;
break;
case PCB_OBJ_POLY:
{
pcb_poly_t *poly = (pcb_poly_t *)c;
if (poly->Clipped == NULL) /* clip inhibit may have kept this for later - but we need it now for determining if the point is inside */
pcb_poly_init_clip(poly->parent.layer->parent.data, poly->parent.layer, poly);
if (!pcb_poly_is_point_in_p(cx, cy, r, poly)) continue;
}
break;
default: continue; /* this type can not be used */
}
/* candidate is around the hole - figure the indexing from lyt */
assert(c->parent_type == PCB_PARENT_LAYER);
lyt = pcb_layer_flags_(c->parent.layer);
for(l = 0; l < NUM_LYTS; l++) {
if (lyts[l] == lyt) {
cand[l] = c;
num_cand[l]++;
break;
}
}
}
/* all candidates are in cand[] and num_cand[]; check if there's any
usable candidate (layer type with only one candidate); allow multiple
intern coppers for now */
valid = 0;
for(l = 0; l < NUM_LYTS; l++) {
if ((num_cand[l] == 1) || ((num_cand[l] > 1) && (lyts[l] & PCB_LYT_INTERN))) {
num_cand[l] = 1;
valid = 1;
break;
}
}
if (!valid) continue; /* this hole can not be converted */
if (vect2pstk_conv_cand(data, objs, quiet, cand, num_cand, cx, cy, plated, h) == 0) {
/* we have deleted from objs, need to start over the main loop */
n = 0;
}
}
/* output padstacks shouldn't be FOUND */
clear_found(objs);
return done;
}
int pcb_pstk_vect2pstk_smd(pcb_data_t *data, vtp0_t *objs, rnd_bool_t quiet)
{
int l, n, done = 0, ci;
rnd_coord_t cx, cy;
pcb_any_obj_t *cand[NUM_LYTS];
int num_cand[NUM_LYTS];
/* output padstacks are going to be marked with the FOUND flag */
clear_found(objs);
for(n = 0; n < objs->used; n++) {
pcb_any_obj_t *o = objs->array[n];
if ((o->type == PCB_OBJ_LINE) || (o->type == PCB_OBJ_ARC) || (o->type == PCB_OBJ_POLY)) {
pcb_layer_type_t olyt;
assert(o->parent_type == PCB_PARENT_LAYER);
olyt = pcb_layer_flags_(o->parent.layer);
if ((!(olyt & PCB_LYT_COPPER)) || (olyt & PCB_LYT_INTERN))
continue; /* deal with outer copper objects only */
/* assume padstack origin is middle of the object, which is approximated here */
cx = (o->BoundingBox.X1 + o->BoundingBox.X2) / 2;
cy = (o->BoundingBox.Y1 + o->BoundingBox.Y2) / 2;
memset(cand, 0, sizeof(cand));
memset(num_cand, 0, sizeof(num_cand));
for(l = 0; l < NUM_LYTS; l++) {
if (lyts[l] == olyt) {
cand[l] = o;
num_cand[l]++;
break;
}
}
for(ci = 0; ci < objs->used; ci++) {
pcb_layer_type_t lyt;
pcb_any_obj_t *c = objs->array[ci]; /* candidate */
lyt = pcb_layer_flags_(c->parent.layer);
if ((lyt & PCB_LYT_ANYWHERE) != (olyt & PCB_LYT_ANYWHERE))
continue; /* care for the same side only */
if (!((lyt & (PCB_LYT_MASK | PCB_LYT_PASTE))))
continue; /* care for mask and paste objects */
if (!pcb_intersect_obj_obj(pcb_find0, o, c))
continue; /* only if intersects with the original copper pad */
for(l = 0; l < NUM_LYTS; l++) {
if (lyts[l] == lyt) {
cand[l] = c;
num_cand[l]++;
break;
}
}
}
if (vect2pstk_conv_cand(data, objs, quiet, cand, num_cand, cx, cy, -1, NULL) == 0) {
/* we have deleted from objs, need to start over the main loop */
n = 0;
}
}
}
/* output padstacks shouldn't be FOUND */
clear_found(objs);
return done;
}
int pcb_pstk_vect2pstk(pcb_data_t *data, vtp0_t *objs, rnd_bool_t quiet)
{
int t = pcb_pstk_vect2pstk_thr(data, objs, quiet);
int s = pcb_pstk_vect2pstk_smd(data, objs, quiet);
if ((t < 0) && (s < 0))
return -1;
if (t < 0) t = 0;
if (s < 0) s = 0;
return t+s;
}
pcb_pstk_t *pcb_pstk_new_from_shape(pcb_data_t *data, rnd_coord_t x, rnd_coord_t y, rnd_coord_t drill_dia, rnd_bool plated, rnd_coord_t glob_clearance, pcb_pstk_shape_t *shape)
{
pcb_pstk_proto_t proto;
rnd_cardinal_t pid;
pcb_pstk_tshape_t tshp;
pcb_pstk_shape_t *s;
memset(&proto, 0, sizeof(proto));
memset(&tshp, 0, sizeof(tshp));
tshp.len = 0;
for(s = shape; s->layer_mask != 0; s++,tshp.len++) ;
tshp.shape = shape;
proto.tr.alloced = proto.tr.used = 1; /* has the canonical form only */
proto.tr.array = &tshp;
proto.hdia = drill_dia;
proto.hplated = plated;
pcb_pstk_proto_update(&proto);
pid = pcb_pstk_proto_insert_dup(data, &proto, 1, 0);
if (pid == PCB_PADSTACK_INVALID)
return NULL;
return pcb_pstk_new(data, -1, pid, x, y, glob_clearance, pcb_flag_make(PCB_FLAG_CLEARLINE));
}
void pcb_shape_rect(pcb_pstk_shape_t *shape, rnd_coord_t width, rnd_coord_t height)
{
pcb_pstk_poly_t *dst = &shape->data.poly;
pcb_pstk_shape_alloc_poly(dst, 4);
shape->shape = PCB_PSSH_POLY;
width/=2;
height/=2;
dst->x[0] = -width; dst->y[0] = -height;
dst->x[1] = +width; dst->y[1] = -height;
dst->x[2] = +width; dst->y[2] = +height;
dst->x[3] = -width; dst->y[3] = +height;
}
void pcb_shape_rect_trdelta(pcb_pstk_shape_t *shape, rnd_coord_t width, rnd_coord_t height, rnd_coord_t dx, rnd_coord_t dy)
{
pcb_pstk_poly_t *dst = &shape->data.poly;
pcb_pstk_shape_alloc_poly(dst, 4);
shape->shape = PCB_PSSH_POLY;
width/=2;
height/=2;
dx/=2;
dy/=2;
dst->x[0] = -width+dx; dst->y[0] = -height-dy;
dst->x[1] = +width-dx; dst->y[1] = -height+dy;
dst->x[2] = +width+dx; dst->y[2] = +height-dy;
dst->x[3] = -width-dx; dst->y[3] = +height+dy;
}
void pcb_shape_oval(pcb_pstk_shape_t *shape, rnd_coord_t width, rnd_coord_t height)
{
shape->shape = PCB_PSSH_LINE;
if (width == height) {
shape->shape = PCB_PSSH_CIRC;
shape->data.circ.x = shape->data.circ.y = 0;
shape->data.circ.dia = height;
}
else if (width > height) {
shape->data.line.x1 = -width/2 + height/2; shape->data.line.y1 = 0;
shape->data.line.x2 = +width/2 - height/2; shape->data.line.y2 = 0;
shape->data.line.thickness = height;
}
else {
shape->data.line.x1 = 0; shape->data.line.y1 = -height/2 + width/2;
shape->data.line.x2 = 0; shape->data.line.y2 = +height/2 - width/2;
shape->data.line.thickness = width;
}
}
|