pcad2kicad_common.cpp 13.6 KB
Newer Older
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
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2007, 2008 Lubo Racko <developer@lura.sk>
 * Copyright (C) 2008, 2012 Alexander Lunev <al.lunev@yahoo.com>
 * Copyright (C) 2012 KiCad Developers, see CHANGELOG.TXT for contributors.
 *
 * 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, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

/**
 * @file pcad2kicad_common.cpp
 */

#include <wx/wx.h>
#include <wx/config.h>

#include <common.h>

#include <pcad2kicad_common.h>

namespace PCAD2KICAD {

wxString GetWord( wxString* aStr )
{
    wxString result = wxEmptyString;

    *aStr = aStr->Trim( false );

    if( aStr->Len() == 0 )
        return result;

    if( (*aStr)[0] == wxT( '"' ) )
    {
        result  += (*aStr)[0];
        *aStr   = aStr->Mid( 1 ); // remove Frot apostrofe

        while( aStr->Len() > 0 && (*aStr)[0] != wxT( '"' ) )
        {
            result  += (*aStr)[0];
            *aStr   = aStr->Mid( 1 );
        }

        if( aStr->Len() > 0 && (*aStr)[0] == wxT( '"' ) )
        {
            result  += (*aStr)[0];
            *aStr   = aStr->Mid( 1 ); // remove ending apostrophe
        }
    }
    else
    {
        while( aStr->Len() > 0
               && !( (*aStr)[0] == wxT( ' ' )
                     || (*aStr)[0] == wxT( '(' )
                     || (*aStr)[0] == wxT( ')' ) ) )
        {
            result  += (*aStr)[0];
            *aStr   = aStr->Mid( 1 );
        }
    }

    result.Trim( true );
    result.Trim( false );

    return result;
}


XNODE* FindPinMap( XNODE* aNode )
{
    XNODE* result, * lNode;

    result  = NULL;
    lNode   = FindNode( aNode, wxT( "attachedPattern" ) );

    if( lNode )
        result = FindNode( lNode, wxT( "padPinMap" ) );

    return result;
}


double StrToDoublePrecisionUnits( wxString aStr, char aAxe, wxString aActualConversion )
{
    wxString    ls;
    double      i;
    char        u;

    ls = aStr;
    ls.Trim( true );
    ls.Trim( false );

    if( ls.Len() > 0 )
    {
        u = ls[ls.Len() - 1];

        while( ls.Len() > 0
               && !( ls[ls.Len() - 1] == wxT( '.' )
                     || ls[ls.Len() - 1] == wxT( ',' )
                     || (ls[ls.Len() - 1] >= wxT( '0' ) && ls[ls.Len() - 1] <= wxT( '9' ) ) ) )
        {
            ls = ls.Left( ls.Len() - 1 );
        }

        while( ls.Len() > 0
               && !( ls[0] == wxT( '-' )
                     || ls[0] == wxT( '+' )
                     || ls[0] == wxT( '.' )
                     || ls[0] == wxT( ',' )
                     || (ls[0] >= wxT( '0' ) && ls[0] <= wxT( '9' ) ) ) )
        {
            ls = ls.Mid( 1 );
        }

        if( u == wxT( 'm' ) )
        {
            ls.ToDouble( &i );
#ifdef PCAD2KICAD_SCALE_SCH_TO_INCH_GRID
134 135
            if( aActualConversion == wxT( "SCH" )
                || aActualConversion == wxT( "SCHLIB" ) )
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
                i = i * (0.0254 / 0.025);
#endif
            i = Millimeter2iu( i );
        }
        else
        {
            ls.ToDouble( &i );
            i = Mils2iu( i );
        }
    }
    else
        i = 0.0;

    if( ( aActualConversion == wxT( "PCB" ) || aActualConversion == wxT( "SCH" ) )
        && aAxe == wxT( 'Y' ) )
        return -i;
    else
        return i; // Y axe is mirrored compared to P-Cad
}


int StrToIntUnits( wxString aStr, char aAxe, wxString aActualConversion )
{
    return KiROUND( StrToDoublePrecisionUnits( aStr, aAxe, aActualConversion ) );
}


wxString GetAndCutWordWithMeasureUnits( wxString* aStr, wxString aDefaultMeasurementUnit )
{
    wxString s1, s2, result;

    aStr->Trim( false );
    result = wxEmptyString;

    // value
    while( aStr->Len() > 0 && (*aStr)[0] != wxT( ' ' ) )
    {
        result  += (*aStr)[0];
        *aStr   = aStr->Mid( 1 );
    }

    aStr->Trim( false );

    // if there is also measurement unit
    while( aStr->Len() > 0
           && ( ( (*aStr)[0] >= wxT( 'a' ) && (*aStr)[0] <= wxT( 'z' ) )
                || ( (*aStr)[0] >= wxT( 'A' ) && (*aStr)[0] <= wxT( 'Z' ) ) ) )
    {
        result  += (*aStr)[0];
        *aStr   = aStr->Mid( 1 );
    }

    // and if not, add default....
    if( result.Len() > 0
        && ( result[result.Len() - 1] == wxT( '.' )
             || result[result.Len() - 1] == wxT( ',' )
             || (result[result.Len() - 1] >= wxT( '0' )
                 && result[result.Len() - 1] <= wxT( '9' ) ) ) )
    {
        result += aDefaultMeasurementUnit;
    }

    return result;
}


int StrToInt1Units( wxString aStr )
{
    double num, precision = 10;

    // TODO: Is the following commented string necessary?
    // if (pos(',',s)>0) then DecimalSeparator:=',' else DecimalSeparator:='.';
    aStr.ToDouble( &num );
    return KiROUND( num * precision );
}


wxString ValidateName( wxString aName )
{
    aName.Replace( wxT( " " ), wxT( "_" ) );

    return aName;
}


void SetWidth( wxString aStr,
               wxString aDefaultMeasurementUnit,
               int*     aWidth,
               wxString aActualConversion )
{
    *aWidth = StrToIntUnits( GetAndCutWordWithMeasureUnits( &aStr,
                                                            aDefaultMeasurementUnit ), wxT( ' ' ),
                             aActualConversion );
}


void SetHeight( wxString    aStr,
                wxString    aDefaultMeasurementUnit,
                int*        aHeight,
                wxString    aActualConversion )
{
    *aHeight = StrToIntUnits( GetAndCutWordWithMeasureUnits( &aStr,
                                                             aDefaultMeasurementUnit ), wxT( ' ' ),
                              aActualConversion );
}


void SetPosition( wxString  aStr,
                  wxString  aDefaultMeasurementUnit,
                  int*      aX,
                  int*      aY,
                  wxString  aActualConversion )
{
    *aX = StrToIntUnits( GetAndCutWordWithMeasureUnits( &aStr,
                                                        aDefaultMeasurementUnit ), wxT( 'X' ),
                         aActualConversion );
    *aY = StrToIntUnits( GetAndCutWordWithMeasureUnits( &aStr,
                                                        aDefaultMeasurementUnit ), wxT( 'Y' ),
                         aActualConversion );
}


void SetDoublePrecisionPosition( wxString   aStr,
                                 wxString   aDefaultMeasurementUnit,
                                 double*    aX,
                                 double*    aY,
                                 wxString   aActualConversion )
{
    *aX = StrToDoublePrecisionUnits( GetAndCutWordWithMeasureUnits( &aStr,
                                                                    aDefaultMeasurementUnit ), wxT( 'X' ),
                                     aActualConversion );
    *aY = StrToDoublePrecisionUnits( GetAndCutWordWithMeasureUnits( &aStr,
                                                                    aDefaultMeasurementUnit ), wxT( 'Y' ),
                                     aActualConversion );
}


void SetTextParameters( XNODE*      aNode,
                        TTEXTVALUE* aTextValue,
                        wxString    aDefaultMeasurementUnit,
                        wxString    aActualConversion )
{
    XNODE*      tNode;
    wxString    str;

    tNode = FindNode( aNode, wxT( "pt" ) );

    if( tNode )
        SetPosition( tNode->GetNodeContent(),
                     aDefaultMeasurementUnit,
                     &aTextValue->textPositionX,
                     &aTextValue->textPositionY,
                     aActualConversion );

    tNode = FindNode( aNode, wxT( "rotation" ) );

    if( tNode )
    {
        str = tNode->GetNodeContent();
        str.Trim( false );
        aTextValue->textRotation = StrToInt1Units( str );
    }

    str = FindNodeGetContent( aNode, wxT( "isVisible" ) );

    if( str == wxT( "True" ) )
        aTextValue->textIsVisible = 1;
    else if( str == wxT( "False" ) )
        aTextValue->textIsVisible = 0;

    tNode = FindNode( aNode, wxT( "textStyleRef" ) );

    if( tNode )
        SetFontProperty( tNode, aTextValue, aDefaultMeasurementUnit, aActualConversion );
}


void SetFontProperty( XNODE*        aNode,
                      TTEXTVALUE*   aTextValue,
                      wxString      aDefaultMeasurementUnit,
                      wxString      aActualConversion )
{
    wxString n, propValue;

    aNode->GetAttribute( wxT( "Name" ), &n );

    while( aNode->GetName() != wxT( "www.lura.sk" ) )
        aNode = aNode->GetParent();

    aNode = FindNode( aNode, wxT( "library" ) );

    if( aNode )
        aNode = FindNode( aNode, wxT( "textStyleDef" ) );

    if( aNode )
    {
        while( true )
        {
            aNode->GetAttribute( wxT( "Name" ), &propValue );
            propValue.Trim( false );
            propValue.Trim( true );

            if( propValue == n )
                break;

            aNode = aNode->GetNext();
        }

        if( aNode )
        {
            aNode = FindNode( aNode, wxT( "font" ) );

            if( aNode )
            {
                if( FindNode( aNode, wxT( "fontHeight" ) ) )
                    // // SetWidth(iNode.ChildNodes.FindNode('fontHeight').Text,
                    // //          DefaultMeasurementUnit,tv.TextHeight);
                    // Fixed By Lubo, 02/2008
                    SetHeight( FindNode( aNode, wxT(
                                             "fontHeight" ) )->GetNodeContent(),
                               aDefaultMeasurementUnit, &aTextValue->textHeight,
                               aActualConversion );

                if( FindNode( aNode, wxT( "strokeWidth" ) ) )
                    SetWidth( FindNode( aNode, wxT(
                                            "strokeWidth" ) )->GetNodeContent(),
                              aDefaultMeasurementUnit, &aTextValue->textstrokeWidth,
                              aActualConversion );
            }
        }
    }
}


void CorrectTextPosition( TTEXTVALUE* aValue, int aRotation )
{
    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 == 900 )
    {
        aValue->correctedPositionX  = -aValue->textPositionY;
        aValue->correctedPositionY  = aValue->textPositionX;
        aValue->correctedPositionX  = aValue->correctedPositionX + KiROUND(
            (double) aValue->textHeight / 3.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->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 )
    {
        aValue->correctedPositionX  = aValue->textPositionY;
        aValue->correctedPositionY  = -aValue->textPositionX;
        aValue->correctedPositionX  = aValue->correctedPositionX +
                                      KiROUND( (double) aValue->textHeight / 1.0 );
        aValue->correctedPositionY = aValue->correctedPositionY -
                                     KiROUND( ( (double) aValue->text.Len() /
                                                3.4 ) * ( (double) aValue->textHeight / 1.8 ) );
    }
}


XNODE* FindNode( XNODE* aChild, wxString aTag )
{
    aChild = aChild->GetChildren();

    while( aChild )
    {
        if( aChild->GetName() == aTag )
            return aChild;

        aChild = aChild->GetNext();
    }

    return NULL;
}

wxString FindNodeGetContent( XNODE* aChild, wxString aTag )
{
    wxString str = wxEmptyString;

    aChild = FindNode( aChild, aTag );

    if( aChild )
    {
        str = aChild->GetNodeContent();
        str.Trim( false );
        str.Trim( true );
    }

    return str;
}

void InitTTextValue( TTEXTVALUE* aTextValue )
{
    aTextValue->text = wxEmptyString;
    aTextValue->textPositionX   = 0;
    aTextValue->textPositionY   = 0;
    aTextValue->textRotation    = 0;
    aTextValue->textHeight      = 0;
    aTextValue->textstrokeWidth = 0;
    aTextValue->textIsVisible   = 0;
    aTextValue->mirror      = 0;
    aTextValue->textUnit    = 0;
    aTextValue->correctedPositionX  = 0;
    aTextValue->correctedPositionY  = 0;
}

} // namespace PCAD2KICAD