File: copy.c

package info (click to toggle)
pcb 20080202-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 22,684 kB
  • ctags: 10,254
  • sloc: ansic: 81,609; sh: 5,803; yacc: 5,032; pascal: 3,999; makefile: 1,322; lex: 410; perl: 308; awk: 158; tcl: 63; xml: 20
file content (407 lines) | stat: -rw-r--r-- 10,701 bytes parent folder | download
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/* $Id: copy.c,v 1.19 2007/04/21 21:21:55 djdelorie Exp $ */

/*
 *                            COPYRIGHT
 *
 *  PCB, interactive printed circuit board design
 *  Copyright (C) 1994,1995,1996 Thomas Nau
 *
 *  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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Contact addresses for paper mail and Email:
 *  Thomas Nau, Schlehenweg 15, 88471 Baustetten, Germany
 *  Thomas.Nau@rz.uni-ulm.de
 *
 */

/* functions used to copy pins, elements ...
 * it's necessary to copy data by calling create... since the base pointer
 * may change cause of dynamic memory allocation
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>

#include "global.h"

#include "copy.h"
#include "create.h"
#include "data.h"
#include "draw.h"
#include "mymem.h"
#include "mirror.h"
#include "misc.h"
#include "move.h"
#include "polygon.h"
#include "rats.h"
#include "rtree.h"
#include "select.h"
#include "undo.h"

#ifdef HAVE_LIBDMALLOC
#include <dmalloc.h>
#endif

RCSID ("$Id: copy.c,v 1.19 2007/04/21 21:21:55 djdelorie Exp $");

/* ---------------------------------------------------------------------------
 * some local prototypes
 */
static void *CopyVia (PinTypePtr);
static void *CopyLine (LayerTypePtr, LineTypePtr);
static void *CopyArc (LayerTypePtr, ArcTypePtr);
static void *CopyText (LayerTypePtr, TextTypePtr);
static void *CopyPolygon (LayerTypePtr, PolygonTypePtr);
static void *CopyElement (ElementTypePtr);

/* ---------------------------------------------------------------------------
 * some local identifiers
 */
static LocationType DeltaX, DeltaY;	/* movement vector */
static ObjectFunctionType CopyFunctions = {
  CopyLine,
  CopyText,
  CopyPolygon,
  CopyVia,
  CopyElement,
  NULL,
  NULL,
  NULL,
  NULL,
  NULL,
  CopyArc,
  NULL
};

/* ---------------------------------------------------------------------------
 * copies data from one polygon to another
 * 'Dest' has to exist
 */
PolygonTypePtr
CopyPolygonLowLevel (PolygonTypePtr Dest, PolygonTypePtr Src)
{
  /* copy all data */
  POLYGONPOINT_LOOP (Src);
  {
    CreateNewPointInPolygon (Dest, point->X, point->Y);
  }
  END_LOOP;
  SetPolygonBoundingBox (Dest);
  Dest->Flags = Src->Flags;
  CLEAR_FLAG (FOUNDFLAG, Dest);
  return (Dest);
}

/* ---------------------------------------------------------------------------
 * copies data from one element to another and creates the destination 
 * if necessary
 */
ElementTypePtr
CopyElementLowLevel (DataTypePtr Data, ElementTypePtr Dest,
		     ElementTypePtr Src, Boolean uniqueName, LocationType dx,
		     LocationType dy)
{
  int i;
  /* release old memory if necessary */
  if (Dest)
    FreeElementMemory (Dest);

  /* both coordinates and flags are the same */
  Dest = CreateNewElement (Data, Dest, &PCB->Font,
			   MaskFlags (Src->Flags, FOUNDFLAG),
			   DESCRIPTION_NAME (Src), NAMEONPCB_NAME (Src),
			   VALUE_NAME (Src), DESCRIPTION_TEXT (Src).X + dx,
			   DESCRIPTION_TEXT (Src).Y + dy,
			   DESCRIPTION_TEXT (Src).Direction,
			   DESCRIPTION_TEXT (Src).Scale,
			   MaskFlags (DESCRIPTION_TEXT (Src).Flags,
				      FOUNDFLAG), uniqueName);

  /* abort on error */
  if (!Dest)
    return (Dest);

  ELEMENTLINE_LOOP (Src);
  {
    CreateNewLineInElement (Dest, line->Point1.X + dx,
			    line->Point1.Y + dy, line->Point2.X + dx,
			    line->Point2.Y + dy, line->Thickness);
  }
  END_LOOP;
  PIN_LOOP (Src);
  {
    CreateNewPin (Dest, pin->X + dx, pin->Y + dy, pin->Thickness,
		  pin->Clearance, pin->Mask, pin->DrillingHole,
		  pin->Name, pin->Number, MaskFlags (pin->Flags, FOUNDFLAG));
  }
  END_LOOP;
  PAD_LOOP (Src);
  {
    CreateNewPad (Dest, pad->Point1.X + dx, pad->Point1.Y + dy,
		  pad->Point2.X + dx, pad->Point2.Y + dy, pad->Thickness,
		  pad->Clearance, pad->Mask, pad->Name, pad->Number,
		  MaskFlags (pad->Flags, FOUNDFLAG));
  }
  END_LOOP;
  ARC_LOOP (Src);
  {
    CreateNewArcInElement (Dest, arc->X + dx, arc->Y + dy, arc->Width,
			   arc->Height, arc->StartAngle, arc->Delta,
			   arc->Thickness);
  }
  END_LOOP;

  for (i=0; i<Src->Attributes.Number; i++)
    CreateNewAttribute (& Dest->Attributes,
			Src->Attributes.List[i].name,
			Src->Attributes.List[i].value);

  Dest->MarkX = Src->MarkX + dx;
  Dest->MarkY = Src->MarkY + dy;

  SetElementBoundingBox (Data, Dest, &PCB->Font);
  return (Dest);
}

/* ---------------------------------------------------------------------------
 * copies a via 
 */
static void *
CopyVia (PinTypePtr Via)
{
  PinTypePtr via;

  via = CreateNewVia (PCB->Data, Via->X + DeltaX, Via->Y + DeltaY,
		      Via->Thickness, Via->Clearance, Via->Mask,
		      Via->DrillingHole, Via->Name,
		      MaskFlags (Via->Flags, FOUNDFLAG));
  if (!via)
    return (via);
  DrawVia (via, 0);
  AddObjectToCreateUndoList (VIA_TYPE, via, via, via);
  return (via);
}

/* ---------------------------------------------------------------------------
 * copies a line 
 */
static void *
CopyLine (LayerTypePtr Layer, LineTypePtr Line)
{
  LineTypePtr line;

  line = CreateDrawnLineOnLayer (Layer, Line->Point1.X + DeltaX,
				 Line->Point1.Y + DeltaY,
				 Line->Point2.X + DeltaX,
				 Line->Point2.Y + DeltaY, Line->Thickness,
				 Line->Clearance,
				 MaskFlags (Line->Flags, FOUNDFLAG));
  if (!line)
    return (line);
  if (Line->Number)
    line->Number = MyStrdup (Line->Number, "CopyLine");
  DrawLine (Layer, line, 0);
  AddObjectToCreateUndoList (LINE_TYPE, Layer, line, line);
  return (line);
}

/* ---------------------------------------------------------------------------
 * copies an arc
 */
static void *
CopyArc (LayerTypePtr Layer, ArcTypePtr Arc)
{
  ArcTypePtr arc;

  arc = CreateNewArcOnLayer (Layer, Arc->X + DeltaX,
			     Arc->Y + DeltaY, Arc->Width, Arc->Height, Arc->StartAngle,
			     Arc->Delta, Arc->Thickness, Arc->Clearance,
			     MaskFlags (Arc->Flags, FOUNDFLAG));
  if (!arc)
    return (arc);
  DrawArc (Layer, arc, 0);
  AddObjectToCreateUndoList (ARC_TYPE, Layer, arc, arc);
  return (arc);
}

/* ---------------------------------------------------------------------------
 * copies a text 
 */
static void *
CopyText (LayerTypePtr Layer, TextTypePtr Text)
{
  TextTypePtr text;

  text = CreateNewText (Layer, &PCB->Font, Text->X + DeltaX,
			Text->Y + DeltaY, Text->Direction,
			Text->Scale, Text->TextString,
			MaskFlags (Text->Flags, FOUNDFLAG));
  DrawText (Layer, text, 0);
  AddObjectToCreateUndoList (TEXT_TYPE, Layer, text, text);
  return (text);
}

/* ---------------------------------------------------------------------------
 * copies a polygon 
 */
static void *
CopyPolygon (LayerTypePtr Layer, PolygonTypePtr Polygon)
{
  PolygonTypePtr polygon;

  polygon = CreateNewPolygon (Layer, NoFlags ());
  CopyPolygonLowLevel (polygon, Polygon);
  MovePolygonLowLevel (polygon, DeltaX, DeltaY);
  if (!Layer->polygon_tree)
    Layer->polygon_tree = r_create_tree (NULL, 0, 0);
  r_insert_entry (Layer->polygon_tree, (BoxTypePtr) polygon, 0);
  InitClip (PCB->Data, Layer, polygon);
  DrawPolygon (Layer, polygon, 0);
  AddObjectToCreateUndoList (POLYGON_TYPE, Layer, polygon, polygon);
  return (polygon);
}

/* ---------------------------------------------------------------------------
 * copies a element 
 */
static void *
CopyElement (ElementTypePtr Element)
{
  Boolean didDraw = False;
  ElementTypePtr element = CopyElementLowLevel (PCB->Data,
						NULL, Element,
						TEST_FLAG (UNIQUENAMEFLAG,
							   PCB), DeltaX,
						DeltaY);

  /* this call clears the polygons */
  AddObjectToCreateUndoList (ELEMENT_TYPE, element, element, element);
  if (PCB->ElementOn && (FRONT (element) || PCB->InvisibleObjectsOn))
    {
      DrawElementName (element, 0);
      DrawElementPackage (element, 0);
      didDraw = True;
    }
  if (PCB->PinOn)
    {
      DrawElementPinsAndPads (element, 0);
      didDraw = True;
    }
  return (element);
}

/* ---------------------------------------------------------------------------
 * pastes the contents of the buffer to the layout. Only visible objects
 * are handled by the routine.
 */
Boolean
CopyPastebufferToLayout (LocationType X, LocationType Y)
{
  Cardinal i;
  Boolean changed = False;

  /* set movement vector */
  DeltaX = X - PASTEBUFFER->X, DeltaY = Y - PASTEBUFFER->Y;

  /* paste all layers */
  for (i = 0; i < max_layer + 2; i++)
    {
      LayerTypePtr sourcelayer = &PASTEBUFFER->Data->Layer[i],
	destlayer = LAYER_PTR (i);

      if (destlayer->On)
	{
	  changed = changed ||
	    (sourcelayer->LineN != 0) ||
	    (sourcelayer->ArcN != 0) ||
	    (sourcelayer->PolygonN != 0) || (sourcelayer->TextN != 0);
	  LINE_LOOP (sourcelayer);
	  {
	    CopyLine (destlayer, line);
	  }
	  END_LOOP;
	  ARC_LOOP (sourcelayer);
	  {
	    CopyArc (destlayer, arc);
	  }
	  END_LOOP;
	  TEXT_LOOP (sourcelayer);
	  {
	    CopyText (destlayer, text);
	  }
	  END_LOOP;
	  POLYGON_LOOP (sourcelayer);
	  {
	    CopyPolygon (destlayer, polygon);
	  }
	  END_LOOP;
	}
    }

  /* paste elements */
  if (PCB->PinOn && PCB->ElementOn)
    {
      ELEMENT_LOOP (PASTEBUFFER->Data);
      {
	if (FRONT (element) || PCB->InvisibleObjectsOn)
	  {
	    CopyElement (element);
	    changed = True;
	  }
      }
      END_LOOP;
    }

  /* finally the vias */
  if (PCB->ViaOn)
    {
      changed |= (PASTEBUFFER->Data->ViaN != 0);
      VIA_LOOP (PASTEBUFFER->Data);
      {
	CopyVia (via);
      }
      END_LOOP;
    }
  if (changed)
    {
      Draw ();
      IncrementUndoSerialNumber ();
    }
  return (changed);
}

/* ---------------------------------------------------------------------------
 * copies the object identified by its data pointers and the type
 * the new objects is moved by DX,DY
 * I assume that the appropriate layer ... is switched on
 */
void *
CopyObject (int Type, void *Ptr1, void *Ptr2, void *Ptr3,
	    LocationType DX, LocationType DY)
{
  void *ptr;

  /* setup movement vector */
  DeltaX = DX;
  DeltaY = DY;

  /* the subroutines add the objects to the undo-list */
  ptr = ObjectOperation (&CopyFunctions, Type, Ptr1, Ptr2, Ptr3);
  IncrementUndoSerialNumber ();
  return (ptr);
}