kicad/pcbnew/class_text_mod.cpp

571 lines
15 KiB
C++
Raw Normal View History

/****************************************************/
/* class_module.cpp : fonctions de la classe MODULE */
/****************************************************/
#include "fctsys.h"
#include "gr_basic.h"
#include "wxstruct.h"
#include "common.h"
#include "pcbnew.h"
#include "trigo.h"
#include "class_drawpanel.h"
#include "drawtxt.h"
#include "kicad_string.h"
#include "pcbcommon.h"
2009-10-28 11:48:47 +00:00
#include "class_board_design_settings.h"
2009-10-28 11:48:47 +00:00
//#include "autorout.h"
//#include "drag.h"
//#include "protos.h"
/************************************************************************/
/* Class TEXTE_MODULE classe de base des elements type Texte sur module */
/************************************************************************/
/* Constructeur de TEXTE_MODULE */
TEXTE_MODULE::TEXTE_MODULE( MODULE* parent, int text_type ) :
BOARD_ITEM( parent, TYPE_TEXTE_MODULE ), EDA_TextStruct ()
{
MODULE* Module = (MODULE*) m_Parent;
m_Type = text_type; /* Reference */
if( (m_Type != TEXT_is_REFERENCE) && (m_Type != TEXT_is_VALUE) )
m_Type = TEXT_is_DIVERS;
m_NoShow = false;
m_Size.x = m_Size.y = 400; m_Width = 120; /* dimensions raisonnables par defaut */
2008-03-04 04:22:27 +00:00
2007-08-23 04:28:46 +00:00
SetLayer( SILKSCREEN_N_CMP );
if( Module && (Module->Type() == TYPE_MODULE) )
{
m_Pos = Module->m_Pos;
2007-08-23 04:28:46 +00:00
int moduleLayer = Module->GetLayer();
2008-03-04 04:22:27 +00:00
if( moduleLayer == COPPER_LAYER_N )
2007-08-23 04:28:46 +00:00
SetLayer( SILKSCREEN_N_CU );
else if( moduleLayer == CMP_N )
SetLayer( SILKSCREEN_N_CMP );
2008-03-04 04:22:27 +00:00
else
2007-08-23 04:28:46 +00:00
SetLayer( moduleLayer );
2008-03-04 04:22:27 +00:00
2007-08-23 04:28:46 +00:00
if( moduleLayer == SILKSCREEN_N_CU
|| moduleLayer == ADHESIVE_N_CU
|| moduleLayer == COPPER_LAYER_N )
2007-08-23 04:28:46 +00:00
{
m_Mirror = true;
2007-08-23 04:28:46 +00:00
}
}
}
2007-09-01 12:00:30 +00:00
TEXTE_MODULE::~TEXTE_MODULE()
{
}
2008-07-13 10:04:00 +00:00
/*******************************************/
2007-10-30 21:30:58 +00:00
bool TEXTE_MODULE::Save( FILE* aFile ) const
2008-07-13 10:04:00 +00:00
/*******************************************/
/**
* Function Save
* writes the data structures for this object out to a FILE in "*.brd" format.
* @param aFile The FILE to write to.
* @return bool - true if success writing else false.
*/
2007-10-30 21:30:58 +00:00
{
MODULE* parent = (MODULE*) GetParent();
int orient = m_Orient;
2008-03-04 04:22:27 +00:00
// Due to the pcbnew history, m_Orient is saved in screen value
// but it is handled as relative to its parent footprint
2007-10-30 21:30:58 +00:00
if( parent )
orient += parent->m_Orient;
2008-03-04 04:22:27 +00:00
int ret = fprintf( aFile, "T%d %d %d %d %d %d %d %c %c %d %c\"%s\"\n",
m_Type,
m_Pos0.x, m_Pos0.y,
m_Size.y, m_Size.x,
orient,
m_Width,
m_Mirror ? 'M' : 'N', m_NoShow ? 'I' : 'V',
GetLayer(),
m_Italic ? 'I' : 'N',
CONV_TO_UTF8( m_Text ) );
return ret > 20;
}
/*********************************************************************/
int TEXTE_MODULE::ReadDescr( char* aLine, FILE* aFile, int* aLineNum )
/*********************************************************************/
/**
* Function ReadLineDescr
* Read description from a given line in "*.brd" format.
* @param aLine The current line which contains the first line of description.
* @param aLine The FILE to read next lines (currently not used).
* @param LineNum a point to the line count (currently not used).
* @return int - > 0 if success reading else 0.
*/
{
int success = true;
int type;
int layer;
char BufCar1[128], BufCar2[128], BufCar3[128], BufLine[256];
layer = SILKSCREEN_N_CMP;
BufCar1[0] = 0;
BufCar2[0] = 0;
BufCar3[0] = 0;
if ( sscanf( aLine + 1, "%d %d %d %d %d %d %d %s %s %d %s",
&type,
&m_Pos0.x, &m_Pos0.y,
&m_Size.y, &m_Size.x,
&m_Orient, &m_Width,
BufCar1, BufCar2, &layer, BufCar3 ) >= 10 )
success = true;
if( (type != TEXT_is_REFERENCE) && (type != TEXT_is_VALUE) )
type = TEXT_is_DIVERS;
m_Type = type;
// Due to the pcbnew history, .m_Orient is saved in screen value
// but it is handled as relative to its parent footprint
m_Orient -= ((MODULE * )m_Parent)->m_Orient;
if( BufCar1[0] == 'M' )
m_Mirror = true;
else
m_Mirror = false;
if( BufCar2[0] == 'I' )
m_NoShow = true;
else
m_NoShow = false;
if( BufCar3[0] == 'I' )
m_Italic = true;
else
m_Italic = false;
// Test for a reasonnable layer:
if( layer < 0 )
layer = 0;
if( layer > LAST_NO_COPPER_LAYER )
layer = LAST_NO_COPPER_LAYER;
if( layer == COPPER_LAYER_N )
layer = SILKSCREEN_N_CU;
else if( layer == CMP_N )
layer = SILKSCREEN_N_CMP;
SetLayer( layer );
/* calcul de la position vraie */
SetDrawCoord();
/* Lecture de la chaine "text" */
ReadDelimitedText( BufLine, aLine, sizeof(BufLine) );
m_Text = CONV_FROM_UTF8( BufLine );
// Test for a reasonnable size:
if( m_Size.x < TEXTS_MIN_SIZE )
m_Size.x = TEXTS_MIN_SIZE;
if( m_Size.y < TEXTS_MIN_SIZE )
m_Size.y = TEXTS_MIN_SIZE;
// Set a reasonnable width:
if( m_Width < 1 )
m_Width = 1;
m_Width = Clamp_Text_PenSize( m_Width, m_Size );
return success;
2007-10-30 21:30:58 +00:00
}
/**********************************************/
void TEXTE_MODULE::Copy( TEXTE_MODULE* source )
/**********************************************/
// copy structure
{
if( source == NULL )
return;
m_Pos = source->m_Pos;
2007-08-23 04:28:46 +00:00
SetLayer( source->GetLayer() );
m_Mirror = source->m_Mirror; // Show normal / mirror
m_NoShow = source->m_NoShow; // 0: visible 1: invisible
m_Type = source->m_Type; // 0: ref,1: val, others = 2..255
m_Orient = source->m_Orient; // orientation in 1/10 deg
m_Pos0 = source->m_Pos0; // text coordinates relatives to the footprint ancre, orient 0
2008-04-01 05:21:50 +00:00
// Text coordinate ref point is the text centre
m_Size = source->m_Size;
m_Width = source->m_Width;
m_Italic = source->m_Italic;
m_Bold = source->m_Bold;
m_Text = source->m_Text;
}
/******************************************/
2007-09-01 12:00:30 +00:00
int TEXTE_MODULE:: GetLength()
/******************************************/
{
return m_Text.Len();
}
/******************************************/
void TEXTE_MODULE:: SetWidth( int new_width )
/******************************************/
{
m_Width = new_width;
}
// Update draw ccordinates
2007-09-01 12:00:30 +00:00
void TEXTE_MODULE:: SetDrawCoord()
{
MODULE* Module = (MODULE*) m_Parent;
m_Pos = m_Pos0;
if( Module == NULL )
return;
int angle = Module->m_Orient;
NORMALIZE_ANGLE_POS( angle );
RotatePoint( &m_Pos.x, &m_Pos.y, angle );
2009-08-11 10:27:21 +00:00
m_Pos += Module->m_Pos;
}
// Update "local" cooedinates (coordinates relatives to the footprint anchor point)
2007-09-01 12:00:30 +00:00
void TEXTE_MODULE:: SetLocalCoord()
{
MODULE* Module = (MODULE*) m_Parent;
if( Module == NULL )
2009-08-11 10:27:21 +00:00
{
m_Pos0 = m_Pos;
return;
2009-08-11 10:27:21 +00:00
}
2009-08-11 10:27:21 +00:00
m_Pos0 = m_Pos - Module->m_Pos;
int angle = Module->m_Orient;
NORMALIZE_ANGLE_POS( angle );
RotatePoint( &m_Pos0.x, &m_Pos0.y, -angle );
}
/* locate functions */
/** Function GetTextRect
* @return an EDA_Rect which gives the position and size of the text area (for the O orient footprint)
*/
EDA_Rect TEXTE_MODULE::GetTextRect( void )
{
2008-04-01 05:21:50 +00:00
EDA_Rect area;
int dx, dy;
dx = ( m_Size.x * GetLength() ) / 2;
dx = (dx * 10) / 9; /* letter size = 10/9 */
dx += m_Width / 2;
dy = ( m_Size.y + m_Width ) / 2;
2008-04-01 05:21:50 +00:00
wxPoint Org = m_Pos; // This is the position of the centre of the area
2008-04-01 05:21:50 +00:00
Org.x -= dx;
Org.y -= dy;
area.SetOrigin( Org );
area.SetHeight( 2 * dy );
area.SetWidth( 2 * dx );
2008-04-01 05:21:50 +00:00
area.Normalize();
return area;
}
/**
* Function HitTest
* tests if the given wxPoint is within the bounds of this object.
* @param refPos A wxPoint to test
* @return bool - true if a hit, else false
*/
bool TEXTE_MODULE::HitTest( const wxPoint& refPos )
{
wxPoint rel_pos;
2008-04-01 05:21:50 +00:00
EDA_Rect area = GetTextRect();
/* Rotate refPos to - angle
* to test if refPos is within area (which is relative to an horizontal text)
*/
rel_pos = refPos;
RotatePoint( &rel_pos, m_Pos, -GetDrawRotation() );
2008-03-04 04:22:27 +00:00
if( area.Inside( rel_pos ) )
return true;
return false;
}
/**
* Function GetBoundingBox
* returns the bounding box of this Text (according to text and footprint orientation)
*/
EDA_Rect TEXTE_MODULE::GetBoundingBox()
{
2008-04-01 05:21:50 +00:00
// Calculate area without text fielsd:
EDA_Rect text_area;
int angle = GetDrawRotation();
wxPoint textstart, textend;
2008-04-01 05:21:50 +00:00
text_area = GetTextRect();
textstart = text_area.GetOrigin();
textend = text_area.GetEnd();
RotatePoint( &textstart, m_Pos, angle );
RotatePoint( &textend, m_Pos, angle );
2008-04-01 05:21:50 +00:00
text_area.SetOrigin( textstart );
text_area.SetEnd( textend );
2008-04-01 05:21:50 +00:00
text_area.Normalize();
return text_area;
}
/******************************************************************************************/
void TEXTE_MODULE::Draw( WinEDA_DrawPanel* panel, wxDC* DC, int draw_mode, const wxPoint& offset )
/******************************************************************************************/
/** Function Draw
* Draw the text accordint to the footprint pos and orient
* @param panel = draw panel, Used to know the clip box
* @param DC = Current Device Context
* @param offset = draw offset (usually wxPoint(0,0)
* @param draw_mode = GR_OR, GR_XOR..
*/
{
int width, color, orient;
wxSize size;
wxPoint pos; // Centre du texte
PCB_SCREEN* screen;
WinEDA_BasePcbFrame* frame;
MODULE* Module = (MODULE*) m_Parent;
if( panel == NULL )
return;
screen = (PCB_SCREEN*) panel->GetScreen();
frame = (WinEDA_BasePcbFrame*) panel->m_Parent;
pos.x = m_Pos.x - offset.x;
pos.y = m_Pos.y - offset.y;
size = m_Size;
orient = GetDrawRotation();
width = m_Width;
2008-03-04 04:22:27 +00:00
if( (frame->m_DisplayModText == FILAIRE)
|| ( screen->Scale( width ) < L_MIN_DESSIN ) )
width = 0;
else if( frame->m_DisplayModText == SKETCH )
width = -width;
GRSetDrawMode( DC, draw_mode );
/* trace du centre du texte */
2009-10-10 01:25:53 +00:00
if( g_DesignSettings.IsElementVisible( ANCHOR_VISIBLE ))
{
int anchor_size = screen->Unscale( 2 );
GRLine( &panel->m_ClipBox, DC,
pos.x - anchor_size, pos.y,
pos.x + anchor_size, pos.y, 0, g_AnchorColor );
GRLine( &panel->m_ClipBox, DC,
pos.x, pos.y - anchor_size,
pos.x, pos.y + anchor_size, 0, g_AnchorColor );
}
2009-10-10 01:25:53 +00:00
//@@@@IMB: BIG BIG BUG Here???? May Module be NULL?
2007-08-23 04:28:46 +00:00
color = g_DesignSettings.m_LayerColor[Module->GetLayer()];
2009-10-10 01:25:53 +00:00
//@@IMB: Why the next ifs are testing for Module?
if( Module && Module->GetLayer() == COPPER_LAYER_N )
2009-10-10 01:25:53 +00:00
{
if( g_DesignSettings.IsElementVisible( MODULE_TEXT_CU_VISIBLE ) == false )
return;
color = g_ModuleTextCUColor;
2009-10-10 01:25:53 +00:00
}
2007-08-23 04:28:46 +00:00
else if( Module && Module->GetLayer() == CMP_N )
2009-10-10 01:25:53 +00:00
{
if( g_DesignSettings.IsElementVisible( MODULE_TEXT_CMP_VISIBLE ) == false )
return;
color = g_ModuleTextCMPColor;
2009-10-10 01:25:53 +00:00
}
if( m_NoShow )
2009-10-10 01:25:53 +00:00
{
if( g_DesignSettings.IsElementVisible( MODULE_TEXT_NOV_VISIBLE ) == false )
return;
color = g_ModuleTextNOVColor;
2009-10-10 01:25:53 +00:00
}
/* If the text is mirrored : negate size.x (mirror / Y axis) */
if( m_Mirror )
size.x = -size.x;
/* Trace du texte */
DrawGraphicText( panel, DC, pos, (enum EDA_Colors) color, m_Text,
2009-05-28 17:39:40 +00:00
orient, size, m_HJustify, m_VJustify, width, m_Italic, m_Bold);
}
/******************************************/
2007-09-01 12:00:30 +00:00
int TEXTE_MODULE::GetDrawRotation()
/******************************************/
/* Return text rotation for drawings and plotting
*/
{
int rotation;
MODULE* Module = (MODULE*) m_Parent;
rotation = m_Orient;
if( Module )
rotation += Module->m_Orient;
NORMALIZE_ANGLE_POS( rotation );
// if( (rotation > 900 ) && (rotation < 2700 ) ) rotation -= 1800; // For angle = 0 .. 180 deg
while( rotation > 900 )
rotation -= 1800;
// For angle = -90 .. 90 deg
return rotation;
}
2008-03-04 04:22:27 +00:00
// see class_text_mod.h
void TEXTE_MODULE::DisplayInfo( WinEDA_DrawFrame* frame )
{
MODULE* module = (MODULE*) m_Parent;
2009-08-11 10:27:21 +00:00
if( module == NULL ) // Happens in modedit, and for new texts
return;
2009-08-11 10:27:21 +00:00
wxString msg, Line;
int ii;
2008-03-04 04:22:27 +00:00
static const wxString text_type_msg[3] = {
_( "Ref." ), _( "Value" ), _( "Text" )
};
2008-03-04 04:22:27 +00:00
frame->ClearMsgPanel();
Line = module->m_Reference->m_Text;
frame->AppendMsgPanel( _( "Module" ), Line, DARKCYAN );
Line = m_Text;
frame->AppendMsgPanel( _( "Text" ), Line, BROWN );
2008-03-04 04:22:27 +00:00
ii = m_Type;
if( ii > 2 )
ii = 2;
frame->AppendMsgPanel( _( "Type" ), text_type_msg[ii], DARKGREEN );
if( m_NoShow )
msg = _( "No" );
else
msg = _( "Yes" );
frame->AppendMsgPanel( _( "Display" ), msg, DARKGREEN );
2009-08-11 10:27:21 +00:00
// Display text layer (use layer name if possible)
BOARD* board = NULL;
board = (BOARD*) module->GetParent();
if( m_Layer < NB_LAYERS && board )
msg = board->GetLayerName( m_Layer );
else
2009-08-11 10:27:21 +00:00
msg.Printf( wxT( "%d" ), m_Layer );
frame->AppendMsgPanel( _( "Layer" ), msg, DARKGREEN );
msg = _( " No" );
if( m_Mirror )
msg = _( " Yes" );
frame->AppendMsgPanel( _( "Mirror" ), msg, DARKGREEN );
msg.Printf( wxT( "%.1f" ), (float) m_Orient / 10 );
frame->AppendMsgPanel( _( "Orient" ), msg, DARKGREEN );
valeur_param( m_Width, msg );
frame->AppendMsgPanel( _( "Width" ), msg, DARKGREEN );
valeur_param( m_Size.x, msg );
frame->AppendMsgPanel( _( "H Size" ), msg, RED );
valeur_param( m_Size.y, msg );
frame->AppendMsgPanel( _( "V Size" ), msg, RED );
}
// see class_text_mod.h
bool TEXTE_MODULE::IsOnLayer( int aLayer ) const
{
if( m_Layer == aLayer )
return true;
/* test the parent, which is a MODULE */
if( aLayer == GetParent()->GetLayer() )
return true;
2008-03-04 04:22:27 +00:00
if( aLayer == COPPER_LAYER_N )
{
if( m_Layer==ADHESIVE_N_CU || m_Layer==SILKSCREEN_N_CU )
return true;
}
else if( aLayer == CMP_N )
{
if( m_Layer==ADHESIVE_N_CMP || m_Layer==SILKSCREEN_N_CMP )
return true;
}
2008-03-04 04:22:27 +00:00
return false;
}
2008-03-04 04:22:27 +00:00
/* see class_text_mod.h
* bool TEXTE_MODULE::IsOnOneOfTheseLayers( int aLayerMask ) const
* {
*
* }
*/
2008-03-04 04:22:27 +00:00
#if defined (DEBUG)
/**
* Function Show
* is used to output the object tree, currently for debugging only.
2008-03-04 04:22:27 +00:00
* @param nestLevel An aid to prettier tree indenting, and is the level
* of nesting of this object within the overall tree.
* @param os The ostream& to output to.
*/
void TEXTE_MODULE::Show( int nestLevel, std::ostream& os )
{
2007-08-08 03:50:44 +00:00
// for now, make it look like XML:
NestedSpace( nestLevel, os ) << '<' << GetClass().Lower().mb_str() <<
" string=\"" << m_Text.mb_str() << "\"/>\n";
2008-03-04 04:22:27 +00:00
2007-08-08 03:50:44 +00:00
// NestedSpace( nestLevel, os ) << "</" << GetClass().Lower().mb_str() << ">\n";
}
#endif