symbedit.cpp 16.3 KB
Newer Older
1 2 3 4 5 6
/*************************************************/
/* Functions to Load  from file and save to file */
/* the graphic shapes  used to draw a component  */
/* When using the import/export symbol options	 */
/* files are the *.sym files 					 */
/*************************************************/
plyatov's avatar
plyatov committed
7

8
/* fichier symbedit.cpp */
plyatov's avatar
plyatov committed
9 10 11 12 13 14 15 16 17 18 19 20 21

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

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

#include "protos.h"


/* Routines locales */
22 23
static bool CompareSymbols( LibEDA_BaseStruct* DEntryRef,
                            LibEDA_BaseStruct* DEntryCompare );
plyatov's avatar
plyatov committed
24 25 26 27 28

/* Variables locales */


/***************************************************/
29
void WinEDA_LibeditFrame::LoadOneSymbol( wxDC* DC )
plyatov's avatar
plyatov committed
30
/***************************************************/
31

32
/* Read a component shape file and add data (graphic items) to the current
33 34
 *  component.
 */
plyatov's avatar
plyatov committed
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
    int                     NumOfParts;
    PriorQue*               Entries;
    EDA_LibComponentStruct* LibEntry = NULL;
    LibEDA_BaseStruct*      DrawEntry;
    wxString                FullFileName, mask;
    FILE*                   ImportFile;
    wxString                msg;

    if( CurrentDrawItem )
        return;
    if( CurrentLibEntry == NULL )
        return;

    DrawPanel->m_IgnoreMouseEvents = TRUE;

    mask = wxT( "*" ) + g_SymbolExtBuffer;
    FullFileName = EDA_FileSelector( _( "Import symbol drawings:" ),
                                     g_RealLibDirBuffer,    /* Chemin par defaut */
                                     wxEmptyString,         /* nom fichier par defaut */
                                     g_SymbolExtBuffer,     /* extension par defaut */
                                     mask,                  /* Masque d'affichage */
                                     this,
                                     0,
                                     TRUE
                                     );

    GetScreen()->m_Curseur = wxPoint( 0, 0 );
    DrawPanel->MouseToCursorSchema();
    DrawPanel->m_IgnoreMouseEvents = FALSE;

    if( FullFileName.IsEmpty() )
        return;


    /* Load data */
    ImportFile = wxFopen( FullFileName, wxT( "rt" ) );
    if( ImportFile == NULL )
    {
        msg.Printf( _( "Failed to open Symbol File <%s>" ), FullFileName.GetData() );
        DisplayError( this, msg, 20 );
        return;
    }


    Entries = LoadLibraryAux( this, NULL, ImportFile, &NumOfParts );
    fclose( ImportFile );

    if( Entries == NULL )
        return;

    if( NumOfParts > 1 )
        DisplayError( this, _( "Warning: more than 1 part in Symbol File" ), 20 );

    LibEntry = (EDA_LibComponentStruct*) PQFirst( &Entries, FALSE );

    if( LibEntry == NULL )
        DisplayError( this, _( "Symbol File is void" ), 20 );

    else /* add data to the current symbol */
    {
        DrawEntry = LibEntry->m_Drawings;
        while( DrawEntry )
        {
            if( DrawEntry->m_Unit )
                DrawEntry->m_Unit = CurrentUnit;
            if( DrawEntry->m_Convert )
                DrawEntry->m_Convert = CurrentConvert;
            DrawEntry->m_Flags    = IS_NEW;
            DrawEntry->m_Selected = IS_SELECTED;

            if( DrawEntry->Pnext == NULL )
            {   /* Fin de liste trouvee */
                DrawEntry->Pnext = CurrentLibEntry->m_Drawings;
                CurrentLibEntry->m_Drawings = LibEntry->m_Drawings;
                LibEntry->m_Drawings = NULL;
                break;
            }
            DrawEntry = DrawEntry->Next();
        }

        SuppressDuplicateDrawItem( CurrentLibEntry );
117
		GetScreen()->SetModify();
118 119 120 121 122 123 124 125

        // Move (and place ) the new draw items:
        HandleBlockBegin( DC, -1, GetScreen()->m_Curseur );
        HandleBlockEnd( DC );
        RedrawActiveWindow( DC, TRUE );
    }

    PQFreeFunc( Entries, ( void( * ) ( void* ) )FreeLibraryEntry );
plyatov's avatar
plyatov committed
126 127 128 129
}


/********************************************/
130
void WinEDA_LibeditFrame::SaveOneSymbol()
plyatov's avatar
plyatov committed
131
/********************************************/
132

133
/* Save in file the current symbol
134 135 136
 *  file format is like the standard libraries, but there is only one symbol
 *  Invisible pins are not saved
 */
plyatov's avatar
plyatov committed
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
    EDA_LibComponentStruct* LibEntry = CurrentLibEntry;
    int Unit = CurrentUnit, convert = CurrentConvert;
    int SymbUnit, SymbConvert;
    LibEDA_BaseStruct*      DrawEntry;
    wxString FullFileName, mask;
    wxString msg;
    FILE*    ExportFile;

    if( LibEntry->m_Drawings == NULL )
        return;

    /* Creation du fichier symbole */
    mask = wxT( "*" ) + g_SymbolExtBuffer;
    FullFileName = EDA_FileSelector( _( "Export symbol drawings:" ),
                                     g_RealLibDirBuffer,    /* Chemin par defaut */
                                     wxEmptyString,         /* nom fichier par defaut */
                                     g_SymbolExtBuffer,     /* extension par defaut */
                                     mask,                  /* Masque d'affichage */
                                     this,
                                     wxFD_SAVE,
                                     TRUE
                                     );
    if( FullFileName.IsEmpty() )
        return;

    ExportFile = wxFopen( FullFileName, wxT( "wt" ) );
    if( ExportFile == NULL )
    {
        msg.Printf( _( "Unable to create <%s>" ), FullFileName.GetData() );
        DisplayError( this, msg );
        return;
    }

    msg.Printf( _( "Save Symbol in [%s]" ), FullFileName.GetData() );
    Affiche_Message( msg );

    /* Creation de l'entete de la librairie */
    char Line[256];
    fprintf( ExportFile, "%s %d.%d  %s  Date: %s\n", LIBFILE_IDENT,
            LIB_VERSION_MAJOR, LIB_VERSION_MINOR,
            "SYMBOL", DateAndTime( Line ) );

    /* Creation du commentaire donnant le nom du composant */
    fprintf( ExportFile, "# SYMBOL %s\n#\n",
            (const char*) LibEntry->m_Name.m_Text.GetData() );

    /* Generation des lignes utiles */
    fprintf( ExportFile, "DEF %s", (const char*) LibEntry->m_Name.m_Text.GetData() );
    if( !LibEntry->m_Prefix.m_Text.IsEmpty() )
        fprintf( ExportFile, " %s", (const char*) LibEntry->m_Prefix.m_Text.GetData() );
    else
        fprintf( ExportFile, " ~" );

    fprintf( ExportFile, " %d %d %c %c %d %d %c\n",
             0, /* unused */
             LibEntry->m_TextInside,
             LibEntry->m_DrawPinNum ? 'Y' : 'N',
             LibEntry->m_DrawPinName ? 'Y' : 'N',
             1, 0 /* unused */, 'N' );

    /* Position / orientation / visibilite des champs */
plyatov's avatar
plyatov committed
199 200 201
    LibEntry->m_Prefix.WriteDescr( ExportFile );
    LibEntry->m_Name.WriteDescr( ExportFile );

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
    DrawEntry = LibEntry->m_Drawings;
    if( DrawEntry )
    {
        fprintf( ExportFile, "DRAW\n" );
        for( ; DrawEntry != NULL; DrawEntry = DrawEntry->Next() )
        {
            /* Elimination des elements non relatifs a l'unite */
            if( Unit && DrawEntry->m_Unit && (DrawEntry->m_Unit != Unit) )
                continue;
            if( convert && DrawEntry->m_Convert && (DrawEntry->m_Convert != convert) )
                continue;

            /* .Unit , . Convert est laisse a 0 ou mis a 1 */
            SymbUnit    = DrawEntry->m_Unit; if( SymbUnit > 1 )
                SymbUnit = 1;
            SymbConvert = DrawEntry->m_Convert;
            if( SymbConvert > 1 )
                SymbConvert = 1;

221
            switch( DrawEntry->Type() )
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
            {
            case COMPONENT_ARC_DRAW_TYPE:
                #define DRAWSTRUCT ( (LibDrawArc*) DrawEntry )
                DRAWSTRUCT->WriteDescr( ExportFile );
                break;

            case COMPONENT_CIRCLE_DRAW_TYPE:
                #undef DRAWSTRUCT
                #define DRAWSTRUCT ( (LibDrawCircle*) DrawEntry )
                DRAWSTRUCT->WriteDescr( ExportFile );
                break;

            case COMPONENT_GRAPHIC_TEXT_DRAW_TYPE:
                #undef DRAWSTRUCT
                #define DRAWSTRUCT ( (LibDrawText*) DrawEntry )
                DRAWSTRUCT->WriteDescr( ExportFile );
                break;

            case COMPONENT_RECT_DRAW_TYPE:
                #undef DRAWSTRUCT
                #define DRAWSTRUCT ( (LibDrawSquare*) DrawEntry )
                DRAWSTRUCT->WriteDescr( ExportFile );
                break;

            case COMPONENT_PIN_DRAW_TYPE:
                #undef DRAWSTRUCT
                #define DRAWSTRUCT ( (LibDrawPin*) DrawEntry )
                if( DRAWSTRUCT->m_Attributs & PINNOTDRAW )
                    break;
                DRAWSTRUCT->WriteDescr( ExportFile );
                break;

            case COMPONENT_POLYLINE_DRAW_TYPE:
                #undef DRAWSTRUCT
                #define DRAWSTRUCT ( (LibDrawPolyline*) DrawEntry )
                DRAWSTRUCT->WriteDescr( ExportFile );
                break;

            default:
                ;
            }
        }
        fprintf( ExportFile, "ENDDRAW\n" );
    }
    fprintf( ExportFile, "ENDDEF\n" );
    fclose( ExportFile );
plyatov's avatar
plyatov committed
268 269 270 271
}


/*****************************************************************/
272
void SuppressDuplicateDrawItem( EDA_LibComponentStruct* LibEntry )
plyatov's avatar
plyatov committed
273
/*****************************************************************/
274

275
/* Delete redundant graphic items.
276 277 278
 *  Useful after loading asymbole from a file symbol, because some graphic items
 *  can be duplicated.
 */
plyatov's avatar
plyatov committed
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
    LibEDA_BaseStruct* DEntryRef, * DEntryCompare;
    bool deleted;
    wxDC* DC = NULL;

    DEntryRef = LibEntry->m_Drawings;
    while( DEntryRef )
    {
        if( DEntryRef->Pnext == NULL )
            return;
        DEntryCompare = DEntryRef->Next();
        if( DEntryCompare == NULL )
            return;
        deleted = 0;
        while( DEntryCompare )
        {
            if( CompareSymbols( DEntryRef, DEntryCompare ) == TRUE )
            {
                DeleteOneLibraryDrawStruct( NULL, DC, LibEntry, DEntryRef, 1 );
                deleted = TRUE;
                break;
            }
            DEntryCompare = DEntryCompare->Next();
        }

        if( !deleted )
            DEntryRef = DEntryRef->Next();
        else
            DEntryRef = LibEntry->m_Drawings;
    }
plyatov's avatar
plyatov committed
309 310 311 312
}


/********************************************************************/
313 314 315 316
static bool CompareSymbols( LibEDA_BaseStruct* DEntryRef,
                            LibEDA_BaseStruct* DEntryCompare )
/********************************************************************/

317
/* Compare 2 graphic items (arc, lines ...).
318 319 320
 *  return FALSE si different
 *          TRUE si they are identical, and therefore redundant
 */
plyatov's avatar
plyatov committed
321
{
322 323 324 325
    int ii;
    int* ptref, * ptcomp;

    /* Comparaison des proprietes generales */
326
    if( DEntryRef->Type() != DEntryCompare->Type() )
327 328 329 330 331 332
        return FALSE;
    if( DEntryRef->m_Unit != DEntryCompare->m_Unit )
        return FALSE;
    if( DEntryRef->m_Convert != DEntryCompare->m_Convert )
        return FALSE;

333
    switch( DEntryRef->Type() )
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
    {
    case COMPONENT_ARC_DRAW_TYPE:
        #undef REFSTRUCT
        #undef CMPSTRUCT
        #define REFSTRUCT ( (LibDrawArc*) DEntryRef )
        #define CMPSTRUCT ( (LibDrawArc*) DEntryCompare )
        if( REFSTRUCT->m_Pos.x != CMPSTRUCT->m_Pos.x )
            return FALSE;
        if( REFSTRUCT->m_Pos.y != CMPSTRUCT->m_Pos.y )
            return FALSE;
        if( REFSTRUCT->t1 != CMPSTRUCT->t1 )
            return FALSE;
        if( REFSTRUCT->t2 != CMPSTRUCT->t2 )
            return FALSE;
        break;

    case COMPONENT_CIRCLE_DRAW_TYPE:
        #undef REFSTRUCT
        #undef CMPSTRUCT
        #define REFSTRUCT ( (LibDrawCircle*) DEntryRef )
        #define CMPSTRUCT ( (LibDrawCircle*) DEntryCompare )
        if( REFSTRUCT->m_Pos.x != CMPSTRUCT->m_Pos.x )
            return FALSE;
        if( REFSTRUCT->m_Pos.y != CMPSTRUCT->m_Pos.y )
            return FALSE;
        if( REFSTRUCT->m_Rayon != CMPSTRUCT->m_Rayon )
            return FALSE;
        break;

    case COMPONENT_GRAPHIC_TEXT_DRAW_TYPE:
        #undef REFSTRUCT
        #undef CMPSTRUCT
        #define REFSTRUCT ( (LibDrawText*) DEntryRef )
        #define CMPSTRUCT ( (LibDrawText*) DEntryCompare )
        if( REFSTRUCT->m_Pos != CMPSTRUCT->m_Pos )
            return FALSE;
        if( REFSTRUCT->m_Size != CMPSTRUCT->m_Size )
            return FALSE;
        if( REFSTRUCT->m_Text != CMPSTRUCT->m_Text )
            return FALSE;
        break;

    case COMPONENT_RECT_DRAW_TYPE:
        #undef REFSTRUCT
        #undef CMPSTRUCT
        #define REFSTRUCT ( (LibDrawSquare*) DEntryRef )
        #define CMPSTRUCT ( (LibDrawSquare*) DEntryCompare )
        if( REFSTRUCT->m_Pos != CMPSTRUCT->m_Pos )
            return FALSE;
        if( REFSTRUCT->m_End != CMPSTRUCT->m_End )
            return FALSE;
        break;

    case COMPONENT_PIN_DRAW_TYPE:
        #undef REFSTRUCT
        #undef CMPSTRUCT
        #define REFSTRUCT ( (LibDrawPin*) DEntryRef )
        #define CMPSTRUCT ( (LibDrawPin*) DEntryCompare )
        if( REFSTRUCT->m_Pos != CMPSTRUCT->m_Pos )
            return FALSE;
        break;

    case COMPONENT_POLYLINE_DRAW_TYPE:
        #undef REFSTRUCT
        #undef CMPSTRUCT
        #define REFSTRUCT ( (LibDrawPolyline*) DEntryRef )
        #define CMPSTRUCT ( (LibDrawPolyline*) DEntryCompare )
        if( REFSTRUCT->n != CMPSTRUCT->n )
            return FALSE;
        ptref  = REFSTRUCT->PolyList;
        ptcomp = CMPSTRUCT->PolyList;
        for( ii = 2 * REFSTRUCT->n; ii > 0; ii-- )
        {
            if( *ptref != *ptcomp )
                return FALSE;
            ptref++; ptcomp++;
        }

        break;

    default:
        ;
    }

    return TRUE;
plyatov's avatar
plyatov committed
419 420 421 422 423 424 425 426 427 428 429
}


/***************************************************************************/
/* Routine de placement du point d'ancrage ( reference des coordonnes pour */
/* le trace) du composant courant											  */
/*	Toutes les coord apparaissant dans les structures sont modifiees		  */
/*	pour repositionner le point repere par le curseur souris au point	  */
/*	d'ancrage ( coord 0,0 ).                                               */
/***************************************************************************/

430
void WinEDA_LibeditFrame::PlaceAncre()
plyatov's avatar
plyatov committed
431
{
432 433 434 435 436
    int ii, * ptsegm;
    int dx, dy;         /* Offsets de deplacement */
    EDA_LibComponentStruct* LibEntry;
    LibEDA_BaseStruct* DrawEntry;

437 438
	dx = -( GetScreen()->m_Curseur.x );
	dy = GetScreen()->m_Curseur.y;
439 440 441 442 443

    LibEntry = CurrentLibEntry;
    if( LibEntry == NULL )
        return;

444
	GetScreen()->SetModify();
445 446 447 448 449 450 451

    LibEntry->m_Name.m_Pos.x   += dx; LibEntry->m_Name.m_Pos.y += dy;
    LibEntry->m_Prefix.m_Pos.x += dx; LibEntry->m_Prefix.m_Pos.y += dy;

    DrawEntry = LibEntry->m_Drawings;
    while( DrawEntry )
    {
452
        switch( DrawEntry->Type() )
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
        {
        case COMPONENT_ARC_DRAW_TYPE:
            #undef STRUCT
            #define STRUCT ( (LibDrawArc*) DrawEntry )
            STRUCT->m_Pos.x      += dx;
            STRUCT->m_Pos.y      += dy;
            STRUCT->m_ArcStart.x += dx;
            STRUCT->m_ArcStart.y += dy;
            STRUCT->m_ArcEnd.x   += dx;
            STRUCT->m_ArcEnd.y   += dy;
            break;

        case COMPONENT_CIRCLE_DRAW_TYPE:
            #undef STRUCT
            #define STRUCT ( (LibDrawCircle*) DrawEntry )
            STRUCT->m_Pos.x += dx;
            STRUCT->m_Pos.y += dy;
            break;

        case COMPONENT_GRAPHIC_TEXT_DRAW_TYPE:
            #undef STRUCT
            #define STRUCT ( (LibDrawText*) DrawEntry )
            STRUCT->m_Pos.x += dx;
            STRUCT->m_Pos.y += dy;
            break;

        case COMPONENT_RECT_DRAW_TYPE:
            #undef STRUCT
            #define STRUCT ( (LibDrawSquare*) DrawEntry )
            STRUCT->m_Pos.x += dx;
            STRUCT->m_Pos.y += dy;
            STRUCT->m_End.x += dx;
            STRUCT->m_End.y += dy;
            break;

        case COMPONENT_PIN_DRAW_TYPE:
            #undef STRUCT
            #define STRUCT ( (LibDrawPin*) DrawEntry )
            STRUCT->m_Pos.x += dx;
            STRUCT->m_Pos.y += dy;
            break;

        case COMPONENT_POLYLINE_DRAW_TYPE:
            #undef STRUCT
            #define STRUCT ( (LibDrawPolyline*) DrawEntry )
            ptsegm = STRUCT->PolyList;
            for( ii = STRUCT->n; ii > 0; ii-- )
            {
                *ptsegm += dx; ptsegm++;
                *ptsegm += dy; ptsegm++;
            }
            break;
            
        default:
            ;
        }
        DrawEntry = DrawEntry->Next();
    }

    /* Redraw the symbol */
513
	GetScreen()->m_Curseur.x = GetScreen()->m_Curseur.y = 0;
514
    Recadre_Trace( TRUE );
515
	GetScreen()->SetRefreshReq();
plyatov's avatar
plyatov committed
516
}