Pcbnew: fix issues in PCAD import of P-CAD ASCII files. (fixes lp:1547822)

This commit is contained in:
Eldar Khayrullin 2016-02-24 13:03:56 -05:00 committed by Wayne Stambaugh
parent e9570fddd6
commit 7d0299f153
6 changed files with 179 additions and 58 deletions

View File

@ -37,6 +37,9 @@
namespace PCAD2KICAD { namespace PCAD2KICAD {
// PCAD stroke font average ratio of width to height
const double TEXT_WIDTH_TO_HEIGHT = 0.79;
wxString GetWord( wxString* aStr ) wxString GetWord( wxString* aStr )
{ {
wxString result = wxEmptyString; wxString result = wxEmptyString;
@ -270,6 +273,31 @@ void SetDoublePrecisionPosition( wxString aStr,
aActualConversion ); aActualConversion );
} }
TTEXT_JUSTIFY GetJustifyIdentificator( wxString aJustify )
{
TTEXT_JUSTIFY id;
if( aJustify == wxT( "LowerCenter" ) )
id = LowerCenter;
else if( aJustify == wxT( "LowerRight" ) )
id = LowerRight;
else if( aJustify == wxT( "UpperLeft" ) )
id = UpperLeft;
else if( aJustify == wxT( "UpperCenter" ) )
id = UpperCenter;
else if( aJustify == wxT( "UpperRight" ) )
id = UpperRight;
else if( aJustify == wxT( "Left" ) )
id = Left;
else if( aJustify == wxT( "Center" ) )
id = Center;
else if( aJustify == wxT( "Right" ) )
id = Right;
else
id = LowerLeft;
return id;
}
void SetTextParameters( XNODE* aNode, void SetTextParameters( XNODE* aNode,
TTEXTVALUE* aTextValue, TTEXTVALUE* aTextValue,
@ -304,6 +332,14 @@ void SetTextParameters( XNODE* aNode,
else if( str == wxT( "False" ) ) else if( str == wxT( "False" ) )
aTextValue->textIsVisible = 0; aTextValue->textIsVisible = 0;
str = FindNodeGetContent( aNode, wxT( "justify" ) );
aTextValue->justify = GetJustifyIdentificator( str );
str = FindNodeGetContent( aNode, wxT( "isFlipped" ) );
if( str == wxT( "True" ) )
aTextValue->mirror = 1;
tNode = FindNode( aNode, wxT( "textStyleRef" ) ); tNode = FindNode( aNode, wxT( "textStyleRef" ) );
if( tNode ) if( tNode )
@ -367,48 +403,102 @@ void SetFontProperty( XNODE* aNode,
} }
} }
int CalculateTextLengthSize( TTEXTVALUE* aText )
void CorrectTextPosition( TTEXTVALUE* aValue, int aRotation )
{ {
aValue->correctedPositionX = aValue->textPositionX; return KiROUND( (double) aText->text.Len() *
aValue->correctedPositionY = aValue->textPositionY; (double) aText->textHeight * TEXT_WIDTH_TO_HEIGHT );
aValue->correctedPositionY = aValue->correctedPositionY - KiROUND( }
(double) aValue->textHeight / 3.0 );
aValue->correctedPositionX = aValue->correctedPositionX +
KiROUND( ( (double) aValue->text.Len() /
1.4 ) * ( (double) aValue->textHeight / 1.8 ) );
if( aRotation == 900 ) void CorrectTextPosition( TTEXTVALUE* aValue )
{ {
aValue->correctedPositionX = -aValue->textPositionY; int cm = aValue->mirror ? -1 : 1;
aValue->correctedPositionY = aValue->textPositionX; // sizes of justify correction
aValue->correctedPositionX = aValue->correctedPositionX + KiROUND( int cl = KiROUND( (double) CalculateTextLengthSize( aValue ) / 2.0 );
(double) aValue->textHeight / 3.0 ); int ch = KiROUND( (double) aValue->textHeight / 2.0 );
aValue->correctedPositionY = aValue->correctedPositionY +
KiROUND( ( (double) aValue->text.Len() /
1.4 ) * ( (double) aValue->textHeight / 1.8 ) );
}
if( aRotation == 1800 ) aValue->correctedPositionX = aValue->textPositionX;
{ aValue->correctedPositionY = aValue->textPositionY;
aValue->correctedPositionX = -aValue->textPositionX;
aValue->correctedPositionY = -aValue->textPositionY;
aValue->correctedPositionY = aValue->correctedPositionY +
KiROUND( (double) aValue->textHeight / 3.0 );
aValue->correctedPositionX = aValue->correctedPositionX -
KiROUND( ( (double) aValue->text.Len() /
1.4 ) * ( (double) aValue->textHeight / 1.8 ) );
}
if( aRotation == 2700 ) switch( aValue->textRotation )
{ {
aValue->correctedPositionX = aValue->textPositionY; case 0:
aValue->correctedPositionY = -aValue->textPositionX; if( aValue->justify == LowerLeft ||
aValue->correctedPositionX = aValue->correctedPositionX + aValue->justify == Left ||
KiROUND( (double) aValue->textHeight / 1.0 ); aValue->justify == UpperLeft )
aValue->correctedPositionY = aValue->correctedPositionY - aValue->correctedPositionX += cl * cm;
KiROUND( ( (double) aValue->text.Len() / else if( aValue->justify == LowerRight ||
3.4 ) * ( (double) aValue->textHeight / 1.8 ) ); aValue->justify == Right ||
aValue->justify == UpperRight )
aValue->correctedPositionX -= cl * cm;
if( aValue->justify == LowerLeft ||
aValue->justify == LowerCenter ||
aValue->justify == LowerRight )
aValue->correctedPositionY -= ch;
else if( aValue->justify == UpperLeft ||
aValue->justify == UpperCenter ||
aValue->justify == UpperRight )
aValue->correctedPositionY += ch;
break;
case 900:
if( aValue->justify == LowerLeft ||
aValue->justify == LowerCenter ||
aValue->justify == LowerRight )
aValue->correctedPositionX -= ch * cm;
else if( aValue->justify == UpperLeft ||
aValue->justify == UpperCenter ||
aValue->justify == UpperRight )
aValue->correctedPositionX += ch * cm;
if( aValue->justify == LowerLeft ||
aValue->justify == Left ||
aValue->justify == UpperLeft )
aValue->correctedPositionY -= cl;
else if( aValue->justify == LowerRight ||
aValue->justify == Right ||
aValue->justify == UpperRight )
aValue->correctedPositionY += cl;
break;
case 1800:
if( aValue->justify == LowerLeft ||
aValue->justify == Left ||
aValue->justify == UpperLeft )
aValue->correctedPositionX -= cl * cm;
else if( aValue->justify == LowerRight ||
aValue->justify == Right ||
aValue->justify == UpperRight )
aValue->correctedPositionX += cl * cm;
if( aValue->justify == LowerLeft ||
aValue->justify == LowerCenter ||
aValue->justify == LowerRight )
aValue->correctedPositionY += ch;
else if( aValue->justify == UpperLeft ||
aValue->justify == UpperCenter ||
aValue->justify == UpperRight )
aValue->correctedPositionY -= ch;
break;
case 2700:
if( aValue->justify == LowerLeft ||
aValue->justify == LowerCenter ||
aValue->justify == LowerRight )
aValue->correctedPositionX += ch * cm;
else if( aValue->justify == UpperLeft ||
aValue->justify == UpperCenter ||
aValue->justify == UpperRight )
aValue->correctedPositionX -= ch * cm;
if( aValue->justify == LowerLeft ||
aValue->justify == Left ||
aValue->justify == UpperLeft )
aValue->correctedPositionY += cl;
else if( aValue->justify == LowerRight ||
aValue->justify == Right ||
aValue->justify == UpperRight )
aValue->correctedPositionY -= cl;
break;
default:
break;
} }
} }
@ -457,6 +547,7 @@ void InitTTextValue( TTEXTVALUE* aTextValue )
aTextValue->textUnit = 0; aTextValue->textUnit = 0;
aTextValue->correctedPositionX = 0; aTextValue->correctedPositionX = 0;
aTextValue->correctedPositionY = 0; aTextValue->correctedPositionY = 0;
aTextValue->justify = LowerLeft;
} }
} // namespace PCAD2KICAD } // namespace PCAD2KICAD

View File

@ -39,6 +39,19 @@ namespace PCAD2KICAD
#define PCAD2KICAD_SCALE_SCH_TO_INCH_GRID #define PCAD2KICAD_SCALE_SCH_TO_INCH_GRID
enum TTEXT_JUSTIFY
{
LowerLeft,
LowerCenter,
LowerRight,
UpperLeft,
UpperCenter,
UpperRight,
Left,
Center,
Right
};
typedef struct _TTEXTVALUE typedef struct _TTEXTVALUE
{ {
wxString text; wxString text;
@ -46,6 +59,7 @@ typedef struct _TTEXTVALUE
textRotation, textHeight, textstrokeWidth; textRotation, textHeight, textstrokeWidth;
int textIsVisible, mirror, textUnit; int textIsVisible, mirror, textUnit;
int correctedPositionX, correctedPositionY; int correctedPositionX, correctedPositionY;
TTEXT_JUSTIFY justify;
} TTEXTVALUE; } TTEXTVALUE;
extern wxString GetWord( wxString* aStr ); extern wxString GetWord( wxString* aStr );
@ -69,6 +83,7 @@ extern void SetDoublePrecisionPosition( wxString aStr,
double* aX, double* aX,
double* aY, double* aY,
wxString aActualConversion ); wxString aActualConversion );
extern TTEXT_JUSTIFY GetJustifyIdentificator( wxString aJustify );
extern void SetTextParameters( XNODE* aNode, extern void SetTextParameters( XNODE* aNode,
TTEXTVALUE* aTextValue, TTEXTVALUE* aTextValue,
wxString aDefaultMeasurementUnit, wxString aDefaultMeasurementUnit,
@ -77,7 +92,8 @@ extern void SetFontProperty( XNODE* aNode,
TTEXTVALUE* aTextValue, TTEXTVALUE* aTextValue,
wxString aDefaultMeasurementUnit, wxString aDefaultMeasurementUnit,
wxString aActualConversion ); wxString aActualConversion );
extern void CorrectTextPosition( TTEXTVALUE* aValue, int aRotation ); extern int CalculateTextLengthSize( TTEXTVALUE* aText );
extern void CorrectTextPosition( TTEXTVALUE* aValue );
extern XNODE* FindNode( XNODE* aChild, wxString aTag ); extern XNODE* FindNode( XNODE* aChild, wxString aTag );
extern wxString FindNodeGetContent( XNODE* aChild, wxString aTag ); extern wxString FindNodeGetContent( XNODE* aChild, wxString aTag );

View File

@ -166,7 +166,7 @@ void PCB::SetTextProperty( XNODE* aNode, TTEXTVALUE* aTextValue,
wxString aActualConversion ) wxString aActualConversion )
{ {
XNODE* tNode, * t1Node; XNODE* tNode, * t1Node;
wxString n, pn, propValue, str; wxString n, nnew, pn, propValue, str;
// aNode is pattern now // aNode is pattern now
tNode = aNode; tNode = aNode;
@ -199,7 +199,8 @@ void PCB::SetTextProperty( XNODE* aNode, TTEXTVALUE* aTextValue,
str = aTextValue->text; str = aTextValue->text;
str.Trim( false ); str.Trim( false );
str.Trim( true ); str.Trim( true );
n = n + wxT( ' ' ) + str; // changed in new file version..... nnew = n; // new file version
n = n + wxT( ' ' ) + str; // old file version
tNode = NULL; tNode = NULL;
} }
} }
@ -219,7 +220,7 @@ void PCB::SetTextProperty( XNODE* aNode, TTEXTVALUE* aTextValue,
propValue.Trim( false ); propValue.Trim( false );
propValue.Trim( true ); propValue.Trim( true );
if( propValue == n ) if( propValue == n || propValue == nnew )
break; break;
tNode = tNode->GetNext(); tNode = tNode->GetNext();

View File

@ -56,11 +56,11 @@ public:
int m_positionX; int m_positionX;
int m_positionY; int m_positionY;
int m_rotation; int m_rotation;
TTEXTVALUE m_name; // name has also privete positions, rotations nand so on.... TTEXTVALUE m_name; // name has also private positions, rotations and so on....
wxString m_net; wxString m_net;
int m_netCode; int m_netCode;
wxString m_compRef; // internal ussage for XL parsing wxString m_compRef; // internal usage for XL parsing
wxString m_patGraphRefName; // internal ussage for XL parsing wxString m_patGraphRefName; // internal usage for XL parsing
PCB_COMPONENT( PCB_CALLBACKS* aCallbacks, BOARD* aBoard ); PCB_COMPONENT( PCB_CALLBACKS* aCallbacks, BOARD* aBoard );
~PCB_COMPONENT(); ~PCB_COMPONENT();

View File

@ -43,6 +43,8 @@
#include <pcb_text.h> #include <pcb_text.h>
#include <pcb_via.h> #include <pcb_via.h>
#include <trigo.h>
namespace PCAD2KICAD { namespace PCAD2KICAD {
PCB_MODULE::PCB_MODULE( PCB_CALLBACKS* aCallbacks, BOARD* aBoard ) : PCB_COMPONENT( aCallbacks, PCB_MODULE::PCB_MODULE( PCB_CALLBACKS* aCallbacks, BOARD* aBoard ) : PCB_COMPONENT( aCallbacks,
@ -282,7 +284,7 @@ void PCB_MODULE::DoLayerContentsObjects( XNODE* aNode,
propValue.Trim( false ); propValue.Trim( false );
propValue.Trim( true ); propValue.Trim( true );
if( propValue == wxT( "Type" ) ) if( propValue == wxT( "RefDes" ) )
{ {
tNode = FindNode( lNode, wxT( "textStyleRef" ) ); tNode = FindNode( lNode, wxT( "textStyleRef" ) );
@ -500,10 +502,16 @@ wxString PCB_MODULE::ModuleLayer( int aMirror )
void PCB_MODULE::AddToBoard() void PCB_MODULE::AddToBoard()
{ {
int i; int i;
int r;
// transform text positions // transform text positions
CorrectTextPosition( &m_name, m_rotation ); CorrectTextPosition( &m_name );
CorrectTextPosition( &m_value, m_rotation ); RotatePoint( &m_name.correctedPositionX, &m_name.correctedPositionY,
(double) -m_rotation );
CorrectTextPosition( &m_value );
RotatePoint( &m_value.correctedPositionX, &m_value.correctedPositionY,
(double) -m_rotation );
MODULE* module = new MODULE( m_board ); MODULE* module = new MODULE( m_board );
m_board->Add( module, ADD_APPEND ); m_board->Add( module, ADD_APPEND );
@ -528,7 +536,9 @@ void PCB_MODULE::AddToBoard()
ref_text->SetSize( wxSize( KiROUND( m_name.textHeight / 2 ), ref_text->SetSize( wxSize( KiROUND( m_name.textHeight / 2 ),
KiROUND( m_name.textHeight / 1.5 ) ) ); KiROUND( m_name.textHeight / 1.5 ) ) );
ref_text->SetOrientation( m_name.textRotation ); r = m_name.textRotation - m_rotation;
ref_text->SetOrientation( r );
ref_text->SetThickness( m_name.textstrokeWidth ); ref_text->SetThickness( m_name.textstrokeWidth );
ref_text->SetMirrored( m_name.mirror ); ref_text->SetMirrored( m_name.mirror );
@ -549,7 +559,9 @@ void PCB_MODULE::AddToBoard()
val_text->SetSize( wxSize( KiROUND( m_value.textHeight / 2 ), val_text->SetSize( wxSize( KiROUND( m_value.textHeight / 2 ),
KiROUND( m_value.textHeight / 1.5 ) ) ); KiROUND( m_value.textHeight / 1.5 ) ) );
val_text->SetOrientation( m_value.textRotation ); r = m_value.textRotation - m_rotation;
val_text->SetOrientation( r );
val_text->SetThickness( m_value.textstrokeWidth ); val_text->SetThickness( m_value.textstrokeWidth );
val_text->SetMirrored( m_value.mirror ); val_text->SetMirrored( m_value.mirror );
@ -611,10 +623,6 @@ void PCB_MODULE::Flip()
// Flipped // Flipped
m_KiCadLayer = FlipLayer( m_KiCadLayer ); m_KiCadLayer = FlipLayer( m_KiCadLayer );
m_rotation = -m_rotation; m_rotation = -m_rotation;
m_name.textPositionX = -m_name.textPositionX;
m_name.mirror = m_mirror;
m_value.textPositionX = -m_value.textPositionX;
m_value.mirror = m_mirror;
for( i = 0; i < (int) m_moduleObjects.GetCount(); i++ ) for( i = 0; i < (int) m_moduleObjects.GetCount(); i++ )
{ {

View File

@ -78,6 +78,9 @@ void PCB_TEXT::Parse( XNODE* aNode,
aNode->GetAttribute( wxT( "Name" ), &m_name.text ); aNode->GetAttribute( wxT( "Name" ), &m_name.text );
str = FindNodeGetContent( aNode, wxT( "justify" ) );
m_name.justify = GetJustifyIdentificator( str );
str = FindNodeGetContent( aNode, wxT( "isFlipped" ) ); str = FindNodeGetContent( aNode, wxT( "isFlipped" ) );
if( str == wxT( "True" ) ) if( str == wxT( "True" ) )
@ -98,9 +101,10 @@ void PCB_TEXT::AddToModule( MODULE* aModule )
void PCB_TEXT::AddToBoard() void PCB_TEXT::AddToBoard()
{ {
// Simple, not the best, but acceptable text positioning. // Simple, not the best, but acceptable text positioning.
m_name.textPositionX = m_positionX; m_name.textPositionX = m_positionX;
m_name.textPositionY = m_positionY; m_name.textPositionY = m_positionY;
CorrectTextPosition( &m_name, m_rotation ); m_name.textRotation = m_rotation;
CorrectTextPosition( &m_name );
TEXTE_PCB* pcbtxt = new TEXTE_PCB( m_board ); TEXTE_PCB* pcbtxt = new TEXTE_PCB( m_board );
m_board->Add( pcbtxt, ADD_APPEND ); m_board->Add( pcbtxt, ADD_APPEND );
@ -111,9 +115,10 @@ void PCB_TEXT::AddToBoard()
KiROUND( m_name.textHeight / 1.1 ) ) ); KiROUND( m_name.textHeight / 1.1 ) ) );
pcbtxt->SetThickness( m_name.textstrokeWidth ); pcbtxt->SetThickness( m_name.textstrokeWidth );
pcbtxt->SetOrientation( m_rotation ); pcbtxt->SetOrientation( m_name.textRotation );
pcbtxt->SetTextPosition( wxPoint( m_name.correctedPositionX, m_name.correctedPositionY ) ); pcbtxt->SetTextPosition( wxPoint( m_name.correctedPositionX,
m_name.correctedPositionY ) );
pcbtxt->SetMirrored( m_name.mirror ); pcbtxt->SetMirrored( m_name.mirror );
pcbtxt->SetTimeStamp( 0 ); pcbtxt->SetTimeStamp( 0 );