File: tool_insert.c

package info (click to toggle)
pcb-rnd 3.1.7b-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,108 kB
  • sloc: ansic: 213,400; yacc: 6,241; sh: 4,698; awk: 3,016; makefile: 2,254; lex: 1,166; python: 519; xml: 261; lisp: 154; tcl: 67; perl: 34; javascript: 6; ruby: 5
file content (209 lines) | stat: -rw-r--r-- 6,734 bytes parent folder | download | duplicates (2)
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
/*
 *                            COPYRIGHT
 *
 *  pcb-rnd, interactive printed circuit board design
 *  Copyright (C) 1994,1995,1996 Thomas Nau
 *  Copyright (C) 1997, 1998, 1999, 2000, 2001 Harry Eaton
 *  Copyright (C) 2017,2019,2020,2023 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 St, 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")
 *
 *
 *  Old contact info:
 *  Harry Eaton, 6697 Buttonhole Ct, Columbia, MD 21044, USA
 *  haceaton@aplcomm.jhuapl.edu
 *
 */

#include "config.h"

#include "board.h"
#include "crosshair.h"
#include "insert.h"
#include "polygon.h"
#include "search.h"
#include "tool_logic.h"
#include <librnd/hid/tool.h>


static struct {
	pcb_poly_t *poly;
	pcb_line_t line;
} fake;

static rnd_point_t InsertedPoint;
static rnd_cardinal_t polyIndex = 0;


void pcb_tool_insert_uninit(void)
{
	rnd_hid_notify_crosshair_change(&PCB->hidlib, rnd_false);
	pcb_crosshair_attached_clean(&PCB->hidlib);
	pcb_crosshair.AttachedObject.Type = PCB_OBJ_VOID;
	pcb_crosshair.AttachedObject.State = PCB_CH_STATE_FIRST;
	pcb_crosshair.extobj_edit = NULL;
	rnd_hid_notify_crosshair_change(&PCB->hidlib, rnd_true);
}

void pcb_tool_insert_notify_mode(rnd_design_t *hl)
{
	pcb_board_t *pcb = (pcb_board_t *)hl;
	switch (pcb_crosshair.AttachedObject.State) {
		/* first notify, lookup object */
	case PCB_CH_STATE_FIRST:
		pcb_crosshair_attached_clean(hl);
		pcb_crosshair.AttachedObject.Type =
			pcb_search_screen_maybe_selector(hl->tool_x, hl->tool_y, PCB_INSERT_TYPES,
									 &pcb_crosshair.AttachedObject.Ptr1, &pcb_crosshair.AttachedObject.Ptr2, &pcb_crosshair.AttachedObject.Ptr3);

		if (pcb_crosshair.AttachedObject.Type != PCB_OBJ_VOID) {
			pcb_any_obj_t *obj = (pcb_any_obj_t *)pcb_crosshair.AttachedObject.Ptr2;
			if (obj->type == PCB_OBJ_RAT) {
				rnd_message(RND_MSG_WARNING, "Can not insert in a rat line\n");
				goto cancel;
			}
			else if (PCB_FLAG_TEST(PCB_FLAG_LOCK, obj)) {
				rnd_message(RND_MSG_WARNING, "Sorry, %s object is locked\n", pcb_obj_type_name(obj->type));
				cancel:;
				pcb_crosshair_attached_clean(hl);
				pcb_crosshair.AttachedObject.Type = PCB_OBJ_VOID;
				pcb_crosshair.extobj_edit = NULL;
				break;
			}
			else {
				/* get starting point of nearest segment */
				if (pcb_crosshair.AttachedObject.Type == PCB_OBJ_POLY) {
					fake.poly = (pcb_poly_t *) pcb_crosshair.AttachedObject.Ptr2;
					polyIndex = pcb_poly_get_lowest_distance_point(fake.poly, hl->tool_x, hl->tool_y);
					fake.line.Point1 = fake.poly->Points[polyIndex];
					fake.line.Point2 = fake.poly->Points[pcb_poly_contour_prev_point(fake.poly, polyIndex)];
					pcb_crosshair.AttachedObject.Ptr2 = &fake.line;

				}
				{
					InsertedPoint = *pcb_adjust_insert_point();
					pcb_crosshair_set_local_ref(InsertedPoint.X, InsertedPoint.Y, rnd_true);
					if (pcb_crosshair.AttachedObject.Type == PCB_OBJ_POLY)
						pcb_insert_point_in_object(PCB_OBJ_POLY,
																	pcb_crosshair.AttachedObject.Ptr1, fake.poly,
																	&polyIndex, InsertedPoint.X, InsertedPoint.Y, rnd_false, rnd_false);
					else
						pcb_insert_point_in_object(pcb_crosshair.AttachedObject.Type,
																	pcb_crosshair.AttachedObject.Ptr1,
																	pcb_crosshair.AttachedObject.Ptr2, &polyIndex, InsertedPoint.X, InsertedPoint.Y, rnd_false, rnd_false);
					pcb_board_set_changed_flag(pcb, rnd_true);

					pcb_crosshair_attached_clean(hl);
					pcb_crosshair.AttachedObject.Type = PCB_OBJ_VOID;
					pcb_crosshair.AttachedObject.State = PCB_CH_STATE_FIRST;
					pcb_crosshair.extobj_edit = NULL;
				}
			}
		}
		break;

		/* second notify, insert new point into object */
	case PCB_CH_STATE_SECOND:
		pcb_crosshair_attached_clean(hl);
		if (pcb_crosshair.AttachedObject.Type == PCB_OBJ_POLY)
			pcb_insert_point_in_object(PCB_OBJ_POLY,
														pcb_crosshair.AttachedObject.Ptr1, fake.poly,
														&polyIndex, InsertedPoint.X, InsertedPoint.Y, rnd_false, rnd_false);
		else
			pcb_insert_point_in_object(pcb_crosshair.AttachedObject.Type,
														pcb_crosshair.AttachedObject.Ptr1,
														pcb_crosshair.AttachedObject.Ptr2, &polyIndex, InsertedPoint.X, InsertedPoint.Y, rnd_false, rnd_false);
		pcb_board_set_changed_flag(pcb, rnd_true);

		/* reset identifiers */
		pcb_crosshair_attached_clean(hl);
		pcb_crosshair.AttachedObject.Type = PCB_OBJ_VOID;
		pcb_crosshair.AttachedObject.State = PCB_CH_STATE_FIRST;
		pcb_crosshair.extobj_edit = NULL;
		break;
	}
}

void pcb_tool_insert_adjust_attached_objects(rnd_design_t *hl)
{
	rnd_point_t *pnt;
	pnt = pcb_adjust_insert_point();
	if (pnt)
		InsertedPoint = *pnt;
}

void pcb_tool_insert_draw_attached(rnd_design_t *hl)
{
	pcb_xordraw_insert_pt_obj();
}

rnd_bool pcb_tool_insert_undo_act(rnd_design_t *hl)
{
	/* don't allow undo in the middle of an operation */
	if (pcb_crosshair.AttachedObject.State != PCB_CH_STATE_FIRST)
		return rnd_false;
	return rnd_true;
}

/* XPM */
static const char *ins_icon[] = {
/* columns rows colors chars-per-pixel */
"21 21 3 1",
"  c #000000",
". c #6EA5D7",
"o c None",
/* pixels */
"oooooo...oooooooooooo",
"ooooo.ooo.ooooooooooo",
"ooooo.o.o.ooooooooooo",
"oooooo....ooooooooooo",
"ooooooooooooooooooooo",
"oooo  ooooo  oooooooo",
"ooooooooooooooooooooo",
"oo...ooooooooooo...oo",
"o.oo..ooooooooo.ooo.o",
"o.o o...........o o.o",
"o.ooo.ooooooooo.ooo.o",
"oo...ooooooooooo...oo",
"ooooooooooooooooooooo",
"ooo   o ooo oo    ooo",
"oooo oo ooo o ooooooo",
"oooo oo  oo o ooooooo",
"oooo oo o o oo   oooo",
"oooo oo oo  ooooo ooo",
"oooo oo ooo ooooo ooo",
"oooo oo ooo ooooo ooo",
"ooo   o ooo o    oooo"
};

rnd_tool_t pcb_tool_insert = {
	"insert", NULL, NULL, 100, ins_icon, RND_TOOL_CURSOR_NAMED("dotbox"), 0,
	NULL,
	pcb_tool_insert_uninit,
	pcb_tool_insert_notify_mode,
	NULL,
	pcb_tool_insert_adjust_attached_objects,
	pcb_tool_insert_draw_attached,
	pcb_tool_insert_undo_act,
	NULL,
	NULL, /* escape */
	
	PCB_TLF_EDIT
};