File: component_class.cpp

package info (click to toggle)
kicad 0.0.20060829-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 56,544 kB
  • ctags: 9,194
  • sloc: cpp: 88,990; ansic: 4,790; makefile: 88; sh: 39
file content (560 lines) | stat: -rw-r--r-- 15,654 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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/***********************************************************************/
/* Methodes de base de gestion des classes des elements de schematique */
/***********************************************************************/

#include "fctsys.h"
#include "gr_basic.h"
#include "macros.h"

#include "common.h"
#include "program.h"
#include "libcmp.h"
#include "general.h"
#include "id.h"

#include "protos.h"



	/***************************/
	/* class DrawPartStruct	*/
	/* class EDA_SchComponentStruct */
	/***************************/

/***********************************************************************************/
DrawPartStruct::DrawPartStruct( DrawStructureType struct_type, const wxPoint & pos):
				EDA_BaseStruct(struct_type)
/***********************************************************************************/
{
	m_Pos = pos;
	m_TimeStamp = 0;
}

/************************************/
DrawPartStruct::~DrawPartStruct(void)
/************************************/
{
}

/****************************************************************/
wxString ReturnDefaultFieldName(int FieldNumber)
/****************************************************************/
/* Return the defult ield name from its number (REFERENCE, VALUE ..)
	FieldDefaultNameList is not static, because we want the text translation
	for I18n
*/
{
wxString FieldDefaultNameList[] = {
	_("Ref"),		/* Reference of part, i.e. "IC21" */
	_("Value"),		/* Value of part, i.e. "3.3K" */
	_("Footprint"),	/* Footprint, used by cvpcb or pcbnew, i.e. "16DIP300" */
	_("Sheet"),		/* for components which are a schematic file, schematic file name, i.e. "cnt16.sch" */
	_("Field")		/* User fields (1 to n) have an editable name*/
 };
int ii = FieldNumber;
	if ( ii > FIELD1 ) ii = FIELD1;
wxString FieldName = FieldDefaultNameList[ii];
	
	if (FieldNumber >= FIELD1 ) FieldName << (FieldNumber - FIELD1 + 1);
	return FieldName;
}


/****************************************************************/
wxString EDA_SchComponentStruct::ReturnFieldName(int FieldNumber)
/****************************************************************/
/* Return the Field name from its number (REFERENCE, VALUE ..)
*/
{
wxString FieldName = m_Field[FieldNumber].m_Name;
	
	if ( (FieldNumber < FIELD1) || FieldName.IsEmpty() )
		FieldName = ReturnDefaultFieldName(FieldNumber);
	return FieldName;
}


/*******************************************************************/
EDA_SchComponentStruct::EDA_SchComponentStruct(const wxPoint & pos):
					DrawPartStruct(DRAW_LIB_ITEM_STRUCT_TYPE, pos)
/*******************************************************************/
{
int ii;
	m_Multi = 0;	/* In multi unit chip - which unit to draw. */
	m_FlagControlMulti = 0;
	m_Convert = 0;	/* Gestion des mutiples representations (conversion De Morgan) */
	/* The rotation/mirror transformation matrix. pos normal*/
	m_Transform[0][0] = 1;
	m_Transform[0][1] = 0;
	m_Transform[1][0] = 0;
	m_Transform[1][1] = -1;

	/* initialisation des Fields */
	for(ii = 0; ii < NUMBER_OF_FIELDS; ii++)
		{
		m_Field[ii].m_Pos = m_Pos;
		m_Field[ii].m_Layer = LAYER_FIELDS;
		m_Field[ii].m_FieldId = REFERENCE + ii;
		m_Field[ii].m_Parent = this;
		}

	m_Field[VALUE].m_Layer = LAYER_VALUEPART;
	m_Field[REFERENCE].m_Layer = LAYER_REFERENCEPART;

	m_PinIsDangling = NULL;

}


/**********************************************************************/
EDA_Rect EDA_SchComponentStruct::GetBoundaryBox( void )
/**********************************************************************/
{
EDA_LibComponentStruct * Entry = FindLibPart(m_ChipName.GetData(), wxEmptyString, FIND_ROOT);
EDA_Rect BoundaryBox;
int x0, xm, y0, ym;
	/* Get the basic Boundary box */
	if ( Entry )
	{
		BoundaryBox = Entry->GetBoundaryBox( m_Multi, m_Convert);
		x0 = BoundaryBox.GetX(); xm = BoundaryBox.GetRight();
		// We must reverse Y values, because matrix orientation
		// suppose Y axis normal for the library items coordinates,
		// m_Transform reverse Y values, but BoundaryBox ais already reversed!
		y0 = - BoundaryBox.GetY();
		ym = - BoundaryBox.GetBottom();
	}
	else	/* if lib Entry not found, give a reasonable size */
	{
		x0 = y0 = -50;
		xm = ym = 50;
	}

	/* Compute the real Boundary box (rotated, mirrored ...)*/
	int x1 = m_Transform[0][0] * x0 + m_Transform[0][1] * y0;

	int y1 = m_Transform[1][0] * x0 + m_Transform[1][1] * y0;

	int x2 = m_Transform[0][0] * xm + m_Transform[0][1] * ym;

	int y2 = m_Transform[1][0] * xm + m_Transform[1][1] * ym;

	// H and W must be > 0 for wxRect:
	if ( x2 < x1 ) EXCHG( x2, x1 );
	if ( y2 < y1 ) EXCHG( y2, y1 );
	BoundaryBox.SetX(x1); BoundaryBox.SetY(y1);
	BoundaryBox.SetWidth(x2-x1);
	BoundaryBox.SetHeight(y2-y1);

	BoundaryBox.Offset(m_Pos);
	return BoundaryBox;
}

/************************************************/
void EDA_SchComponentStruct::ClearAnnotation(void)
/************************************************/
/* Suppress annotation ( i.i IC23 changed to IC? and part reset to 1)
*/
{
	while ( isdigit(m_Field[REFERENCE].m_Text.Last() ) )
		m_Field[REFERENCE].m_Text.RemoveLast();
	if ( m_Field[REFERENCE].m_Text.Last() != '?' )
		m_Field[REFERENCE].m_Text.Append('?');

EDA_LibComponentStruct *Entry;
	Entry = FindLibPart(m_ChipName.GetData(),wxEmptyString,FIND_ROOT);

	if ( !Entry || ! Entry->m_UnitSelectionLocked )
		m_Multi = 1;
}

/**************************************************************/
EDA_SchComponentStruct * EDA_SchComponentStruct::GenCopy(void)
/**************************************************************/
{
EDA_SchComponentStruct * new_item = new EDA_SchComponentStruct( m_Pos );
int ii;

	new_item->m_Multi = m_Multi;
	new_item->m_ChipName = m_ChipName;
	new_item->m_FlagControlMulti = m_FlagControlMulti;
	new_item->m_Convert = m_Convert;
	new_item->m_Transform[0][0] = m_Transform[0][0];
	new_item->m_Transform[0][1] = m_Transform[0][1];
	new_item->m_Transform[1][0] = m_Transform[1][0];
	new_item->m_Transform[1][1] = m_Transform[1][1];
	new_item->m_TimeStamp = m_TimeStamp;


	/* initialisation des Fields */
	for(ii = 0; ii < NUMBER_OF_FIELDS; ii++)
	{
		m_Field[ii].PartTextCopy(& new_item->m_Field[ii]);
	}

	return new_item;
}


/*****************************************************************/
void EDA_SchComponentStruct::SetRotationMiroir( int type_rotate )
/******************************************************************/
/* Compute the new matrix transform for a schematic component
	in order to have the requested transform (type_rotate = rot, mirror..)
	which is applied to the initial transform.
*/
{
int TempMat[2][2];
bool Transform = FALSE;

	switch (type_rotate)
	{
		case CMP_ORIENT_0:
		case CMP_NORMAL:		/* Position Initiale */
			m_Transform[0][0] = 1;
			m_Transform[1][1] = -1;
			m_Transform[1][0] = m_Transform[0][1] = 0;
			break;

		case CMP_ROTATE_CLOCKWISE:		  /* Rotate + */
			TempMat[0][0] = TempMat[1][1] = 0;
			TempMat[0][1] = 1;
			TempMat[1][0] = -1;
			Transform = TRUE;
			break;

		case CMP_ROTATE_COUNTERCLOCKWISE:		  /* Rotate - */
			TempMat[0][0] = TempMat[1][1] = 0;
			TempMat[0][1] = -1;
			TempMat[1][0] = 1;
			Transform = TRUE;
			break;

		case CMP_MIROIR_Y:		/* MirrorY */
			TempMat[0][0] = -1;
			TempMat[1][1] = 1;
			TempMat[0][1] = TempMat[1][0] = 0;
			Transform = TRUE;
			break;

		case CMP_MIROIR_X:		  /* MirrorX */
			TempMat[0][0] = 1;
			TempMat[1][1] = -1;
			TempMat[0][1] = TempMat[1][0] = 0;
			Transform = TRUE;
			break;

		case CMP_ORIENT_90:
			SetRotationMiroir( CMP_ORIENT_0 );
			SetRotationMiroir( CMP_ROTATE_COUNTERCLOCKWISE );
			break;

		case CMP_ORIENT_180:
			SetRotationMiroir( CMP_ORIENT_0 );
			SetRotationMiroir( CMP_ROTATE_COUNTERCLOCKWISE );
 			SetRotationMiroir( CMP_ROTATE_COUNTERCLOCKWISE );
			break;

		case CMP_ORIENT_270:
			SetRotationMiroir( CMP_ORIENT_0 );
			SetRotationMiroir( CMP_ROTATE_CLOCKWISE );
			break;

		case (CMP_ORIENT_0+CMP_MIROIR_X):
			SetRotationMiroir( CMP_ORIENT_0 );
			SetRotationMiroir( CMP_MIROIR_X );
			break;

		case (CMP_ORIENT_0+CMP_MIROIR_Y):
			SetRotationMiroir( CMP_ORIENT_0 );
			SetRotationMiroir( CMP_MIROIR_Y );
			break;

		case (CMP_ORIENT_90+CMP_MIROIR_X):
			SetRotationMiroir( CMP_ORIENT_90 );
			SetRotationMiroir( CMP_MIROIR_X );
			break;

		case (CMP_ORIENT_90+CMP_MIROIR_Y):
			SetRotationMiroir( CMP_ORIENT_90 );
			SetRotationMiroir( CMP_MIROIR_Y );
			break;

		case (CMP_ORIENT_180+CMP_MIROIR_X):
			SetRotationMiroir( CMP_ORIENT_180 );
			SetRotationMiroir( CMP_MIROIR_X );
			break;

		case (CMP_ORIENT_180+CMP_MIROIR_Y):
			SetRotationMiroir( CMP_ORIENT_180 );
			SetRotationMiroir( CMP_MIROIR_Y );
			break;

		case (CMP_ORIENT_270+CMP_MIROIR_X):
			SetRotationMiroir( CMP_ORIENT_270 );
			SetRotationMiroir( CMP_MIROIR_X );
			break;

		case (CMP_ORIENT_270+CMP_MIROIR_Y):
			SetRotationMiroir( CMP_ORIENT_270 );
			SetRotationMiroir( CMP_MIROIR_Y );
			break;

		default:
			Transform = FALSE;
			DisplayError(NULL, wxT("SetRotateMiroir() error: ill value") );
			break;
	}
		
	if ( Transform )
	{/* The new matrix transform is the old matrix transform modified by the
		requested transformation, which is the TempMat transform (rot, mirror ..)
		in order to have (in term of matrix transform):
			transform coord = new_m_Transform * coord
		where transform coord is the coord modified by new_m_Transform from the initial
		value coord.
		new_m_Transform is computed (from old_m_Transform and TempMat) to have:
			transform coord = old_m_Transform * coord * TempMat
		*/
	int NewMatrix[2][2];

		NewMatrix[0][0] = m_Transform[0][0] * TempMat[0][0] +
							m_Transform[1][0] * TempMat[0][1];

		NewMatrix[0][1] = m_Transform[0][1] * TempMat[0][0] +
								m_Transform[1][1] * TempMat[0][1];

		NewMatrix[1][0] = m_Transform[0][0] * TempMat[1][0] +
						m_Transform[1][0] * TempMat[1][1];

		NewMatrix[1][1] = m_Transform[0][1] * TempMat[1][0] +
								m_Transform[1][1] * TempMat[1][1];

		m_Transform[0][0] = NewMatrix[0][0];
		m_Transform[0][1] = NewMatrix[0][1];
		m_Transform[1][0] = NewMatrix[1][0];
		m_Transform[1][1] = NewMatrix[1][1];
	}
}


/*********************************************/
int EDA_SchComponentStruct::GetRotationMiroir(void)
/*********************************************/
{
int type_rotate = CMP_ORIENT_0;
int TempMat[2][2], MatNormal[2][2];
int ii;
bool found = FALSE;

	memcpy(TempMat, m_Transform, sizeof(TempMat));
	SetRotationMiroir(CMP_ORIENT_0);
	memcpy(MatNormal, m_Transform, sizeof(MatNormal));

	for ( ii = 0; ii < 4; ii++ )
		{
		if ( memcmp(TempMat, m_Transform, sizeof(MatNormal)) == 0)
			{
			found = TRUE; break;
			}
		SetRotationMiroir(CMP_ROTATE_COUNTERCLOCKWISE);
		}

	if ( ! found )
		{
		type_rotate = CMP_MIROIR_X + CMP_ORIENT_0;
		SetRotationMiroir(CMP_NORMAL);
		SetRotationMiroir(CMP_MIROIR_X);
		for ( ii = 0; ii < 4; ii++ )
			{
			if ( memcmp(TempMat, m_Transform, sizeof(MatNormal)) == 0)
				{
				found = TRUE; break;
				}
			SetRotationMiroir(CMP_ROTATE_COUNTERCLOCKWISE);
			}
		}

	if ( ! found )
		{
		type_rotate = CMP_MIROIR_Y + CMP_ORIENT_0;
		SetRotationMiroir(CMP_NORMAL);
		SetRotationMiroir(CMP_MIROIR_Y);
		for ( ii = 0; ii < 4; ii++ )
			{
			if ( memcmp(TempMat, m_Transform, sizeof(MatNormal)) == 0)
				{
				found = TRUE; break;
				}
			SetRotationMiroir(CMP_ROTATE_COUNTERCLOCKWISE);
			}
		}

	memcpy(m_Transform, TempMat, sizeof(m_Transform));

	if ( found )
		{
		return (type_rotate + ii);
		}
	else
		{
		wxBell(); return CMP_NORMAL;
		}
}

/***************************************************************/
wxPoint EDA_SchComponentStruct::GetScreenCoord(const wxPoint & coord)
/***************************************************************/
/* Renvoie la coordonne du point coord, en fonction de l'orientation
du composant (rotation, miroir).
	Les coord sont toujours relatives  l'ancre (coord 0,0) du composant
*/
{
wxPoint screenpos;
	screenpos.x = m_Transform[0][0] * coord.x + m_Transform[0][1] * coord.y;
	screenpos.y = m_Transform[1][0] * coord.x + m_Transform[1][1] * coord.y;
	return screenpos;
}


/***************************************************************************/
PartTextStruct::PartTextStruct(const wxPoint & pos, const wxString & text):
			EDA_BaseStruct(DRAW_PART_TEXT_STRUCT_TYPE),
			EDA_TextStruct(text)
/***************************************************************************/
{
	m_Pos = pos;
	m_FieldId = 0;
}

/************************************/
PartTextStruct::~PartTextStruct(void)
/************************************/
{
}

/***********************************************************/
void PartTextStruct::PartTextCopy(PartTextStruct * target)
/***********************************************************/
{
	target->m_Text = m_Text;
	target->m_Layer = m_Layer;
	target->m_Pos = m_Pos;
	target->m_Size = m_Size;
	target->m_Attributs = m_Attributs;
	target->m_FieldId = m_FieldId;
	target->m_Orient = m_Orient;
	target->m_HJustify = m_HJustify;
	target->m_VJustify = m_VJustify;
	target->m_Flags = m_Flags;
}


/*********************************/
bool PartTextStruct::IsVoid(void)
/*********************************/
/* return True if The field is void, i.e.:
	contains wxEmptyString or "~"
*/
{
	if ( m_Text.IsEmpty() || m_Text == wxT("~") ) return TRUE;
	return FALSE;
}


/********************************************/
EDA_Rect PartTextStruct::GetBoundaryBox(void)
/********************************************/
/* return
	EDA_Rect contains the real (user coordinates) boundary box for a text field,
according to the component position, rotation, mirror ...

*/
{
EDA_Rect BoundaryBox;
int hjustify, vjustify;
int textlen;
int orient;
int dx, dy, x1, y1, x2, y2;

EDA_SchComponentStruct * DrawLibItem = (EDA_SchComponentStruct *) m_Parent;

	orient = m_Orient;
	wxPoint pos = DrawLibItem->m_Pos;
	x1 = m_Pos.x - pos.x;
	y1 = m_Pos.y - pos.y;

	textlen = GetLength();
	if ( m_FieldId == REFERENCE )	// Real Text can be U1 or U1A
	{
		EDA_LibComponentStruct *Entry =
			FindLibPart(DrawLibItem->m_ChipName.GetData(),wxEmptyString,FIND_ROOT);
		if ( Entry && (Entry->m_UnitCount > 1) )
			textlen ++;	// because U1 is show as U1A or U1B ...
	}
	dx = m_Size.x * textlen;

	// Real X Size is 10/9 char size because space between 2 chars is 1/10 X Size
	dx = (dx * 10) / 9;

	dy = m_Size.y;
	hjustify = m_HJustify;
	vjustify = m_VJustify;

	x2 = pos.x  + (DrawLibItem->m_Transform[0][0] * x1)
		+ (DrawLibItem->m_Transform[0][1] * y1);
	y2 = pos.y + (DrawLibItem->m_Transform[1][0] * x1)
		+ (DrawLibItem->m_Transform[1][1] * y1);

	/* If the component orientation is +/- 90 deg, the text orienation must be changed */
	if(DrawLibItem->m_Transform[0][1])
	{
		if ( orient == TEXT_ORIENT_HORIZ) orient = TEXT_ORIENT_VERT;
		else orient = TEXT_ORIENT_HORIZ;
		/* is it mirrored (for text justify)*/
		EXCHG(hjustify, vjustify);
		if (DrawLibItem->m_Transform[1][0] < 0 ) vjustify = - vjustify;
		if (DrawLibItem->m_Transform[0][1] > 0 ) hjustify = - hjustify;
	}
	else	/* component horizontal: is it mirrored (for text justify)*/
	{
		if (DrawLibItem->m_Transform[0][0] < 0 )
			hjustify = - hjustify;
		if (DrawLibItem->m_Transform[1][1] > 0 )
			vjustify = - vjustify;
	}
	
	if ( orient == TEXT_ORIENT_VERT ) EXCHG(dx, dy);

	switch ( hjustify )
	{
		case GR_TEXT_HJUSTIFY_CENTER:
			x1 = x2 - (dx/2);
			break;
		case GR_TEXT_HJUSTIFY_RIGHT:
			x1 = x2 - dx;
			break;
		default:
			x1 = x2;
			break;
	}
	switch ( vjustify )
	{
		case GR_TEXT_VJUSTIFY_CENTER:
			y1 = y2 - (dy/2);
			break;
		case GR_TEXT_VJUSTIFY_BOTTOM:
			y1 = y2 - dy;
			break;
		default:
			y1 = y2;
			break;
	}

	BoundaryBox.SetX(x1);
	BoundaryBox.SetY(y1);
	BoundaryBox.SetWidth(dx);
	BoundaryBox.SetHeight(dy);

	return BoundaryBox;
}