operations_on_items_lists.cpp 8.01 KB
Newer Older
1
/***************************************************
2
 *  operations_on_item_lists.cpp
3
 * functions used in block commands, on lists of schematic items:
4
 * move, mirror, delete and copy
5 6 7 8 9 10
 ****************************************************/

#include "fctsys.h"
#include "appl_wxstruct.h"
#include "common.h"
#include "class_drawpanel.h"
11

12 13 14 15 16
#include "program.h"
#include "general.h"
#include "class_marker_sch.h"
#include "protos.h"

17 18 19

void MoveItemsInList( PICKED_ITEMS_LIST& aItemsList, const wxPoint aMoveVector );
void MirrorListOfItems( PICKED_ITEMS_LIST& aItemsList, wxPoint& aMirrorPoint );
20
void DeleteItemsInList( WinEDA_DrawPanel*  panel,
21 22 23
                        PICKED_ITEMS_LIST& aItemsList );
void DuplicateItemsInList( SCH_SCREEN* screen, PICKED_ITEMS_LIST& aItemsList,
                           const wxPoint aMoveVector  );
24 25 26 27 28 29


void MirrorListOfItems( PICKED_ITEMS_LIST& aItemsList, wxPoint& aMirrorPoint )
{
    for( unsigned ii = 0; ii < aItemsList.GetCount(); ii++ )
    {
30
        SCH_ITEM* item = (SCH_ITEM*) aItemsList.GetPickedItem( ii );
31 32 33 34 35 36 37
        item->Mirror_Y( aMirrorPoint.x );      // Place it in its new position.
        item->m_Flags = 0;
    }
}


/** Function MoveItemsInList
38
 *  Move a list of items to a given move vector
39 40 41
 * @param aItemsList = list of picked items
 * @param aMoveVector = the move vector value
 */
42 43 44 45
void MoveItemsInList( PICKED_ITEMS_LIST& aItemsList, const wxPoint aMoveVector )
{
    for( unsigned ii = 0; ii < aItemsList.GetCount(); ii++ )
    {
46
        SCH_ITEM* item = (SCH_ITEM*) aItemsList.GetPickedItem( ii );
47 48 49 50 51 52 53 54 55 56 57 58
        item->Move( aMoveVector );
    }
}


/** function DeleteItemsInList
 * delete schematic items in aItemsList
 * deleted items are put in undo list
 */
void DeleteItemsInList( WinEDA_DrawPanel* panel, PICKED_ITEMS_LIST& aItemsList )
{
    SCH_SCREEN*            screen = (SCH_SCREEN*) panel->GetScreen();
59
    WinEDA_SchematicFrame* frame  = (WinEDA_SchematicFrame*) panel->GetParent();
60 61 62 63 64
    PICKED_ITEMS_LIST      itemsList;
    ITEM_PICKER            itemWrapper;

    for( unsigned ii = 0; ii < aItemsList.GetCount(); ii++ )
    {
65
        SCH_ITEM* item = (SCH_ITEM*) aItemsList.GetPickedItem( ii );
66
        itemWrapper.m_PickedItem     = item;
67 68 69 70
        itemWrapper.m_UndoRedoStatus = UR_DELETED;
        if( item->Type() == DRAW_HIERARCHICAL_PIN_SHEET_STRUCT_TYPE )
        {
            /* this item is depending on a sheet, and is not in global list */
71 72
            wxMessageBox( wxT( "DeleteItemsInList() err: unexpected \
DRAW_HIERARCHICAL_PIN_SHEET_STRUCT_TYPE" ) );
73
#if 0
74
            SCH_SHEET_PIN* pinlabel = (SCH_SHEET_PIN*) item;
75
            frame->DeleteSheetLabel( false, pinlabel->m_Parent );
76
            itemWrapper.m_PickedItem     = pinlabel->m_Parent;
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
            itemWrapper.m_UndoRedoStatus = UR_CHANGED;
            itemsList.PushItem( itemWrapper );
#endif
        }
        else
        {
            screen->RemoveFromDrawList( item );

            /* Unlink the structure */
            item->SetNext( 0 );
            item->SetBack( 0 );
            itemsList.PushItem( itemWrapper );
        }
    }

    frame->SaveCopyInUndoList( itemsList, UR_DELETED );
}


/* Routine to delete an object from global drawing object list.
 *  Object is put in Undo list
 */
99
void DeleteStruct( WinEDA_DrawPanel* panel, wxDC* DC, SCH_ITEM* DrawStruct )
100 101
{
    SCH_SCREEN*            screen = (SCH_SCREEN*) panel->GetScreen();
102
    WinEDA_SchematicFrame* frame  = (WinEDA_SchematicFrame*) panel->GetParent();
103 104 105 106 107 108

    if( !DrawStruct )
        return;

    if( DrawStruct->Type() == DRAW_HIERARCHICAL_PIN_SHEET_STRUCT_TYPE )
    {
109 110 111 112 113
        /* This structure is attached to a node, and is not accessible by
         * the global list directly. */
        frame->SaveCopyInUndoList(
            (SCH_ITEM*) ( (SCH_SHEET_PIN*) DrawStruct )-> GetParent(),
            UR_CHANGED );
114
        frame->DeleteSheetLabel( DC ? true : false,
115
                                 (SCH_SHEET_PIN*) DrawStruct );
116 117
        return;
    }
118
    else
119 120 121 122 123 124 125 126 127 128 129 130 131 132
    {
        screen->RemoveFromDrawList( DrawStruct );

        panel->PostDirtyRect( DrawStruct->GetBoundingBox() );

        /* Unlink the structure */
        DrawStruct->SetNext( 0 );
        DrawStruct->SetBack( 0 );  // Only one struct -> no link

        frame->SaveCopyInUndoList( DrawStruct, UR_DELETED );
    }
}


133 134
/* Routine to copy a new entity of an object for each object in list and
 * reposition it.
135 136
 * Return the new created object list in aItemsList
 */
137 138
void DuplicateItemsInList( SCH_SCREEN* screen, PICKED_ITEMS_LIST& aItemsList,
                           const wxPoint aMoveVector )
139 140 141 142 143 144 145 146
{
    SCH_ITEM* newitem;

    if( aItemsList.GetCount() == 0 )
        return;

    for( unsigned ii = 0; ii < aItemsList.GetCount(); ii++ )
    {
147 148 149
        newitem = DuplicateStruct( (SCH_ITEM*) aItemsList.GetPickedItem( ii ) );
        aItemsList.SetPickedItem( newitem, ii );
        aItemsList.SetPickedItemStatus( UR_NEW, ii );
150 151 152 153 154 155 156 157 158 159 160 161
        {
            switch( newitem->Type() )
            {
            case DRAW_POLYLINE_STRUCT_TYPE:
            case DRAW_JUNCTION_STRUCT_TYPE:
            case DRAW_SEGMENT_STRUCT_TYPE:
            case DRAW_BUSENTRY_STRUCT_TYPE:
            case TYPE_SCH_TEXT:
            case TYPE_SCH_LABEL:
            case TYPE_SCH_GLOBALLABEL:
            case TYPE_SCH_HIERLABEL:
            case DRAW_HIERARCHICAL_PIN_SHEET_STRUCT_TYPE:
162
            case TYPE_SCH_MARKER:
163 164 165 166 167 168
            case DRAW_NOCONNECT_STRUCT_TYPE:
            default:
                break;

            case DRAW_SHEET_STRUCT_TYPE:
            {
169
                SCH_SHEET* sheet = (SCH_SHEET*) newitem;
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
                sheet->m_TimeStamp = GetTimeStamp();
                sheet->SetSon( NULL );
                break;
            }

            case TYPE_SCH_COMPONENT:
                ( (SCH_COMPONENT*) newitem )->m_TimeStamp = GetTimeStamp();
                ( (SCH_COMPONENT*) newitem )->ClearAnnotation( NULL );
                break;
            }

            SetaParent( newitem, screen );
            newitem->SetNext( screen->EEDrawList );
            screen->EEDrawList = newitem;
        }
    }

    MoveItemsInList( aItemsList, aMoveVector );
}


/* Routine to create a new copy of given struct.
 *  The new object is not put in draw list (not linked)
 */
194
SCH_ITEM* DuplicateStruct( SCH_ITEM* DrawStruct )
195 196 197 198 199 200 201 202 203 204 205 206
{
    SCH_ITEM* NewDrawStruct = NULL;

    if( DrawStruct == NULL )
    {
        wxMessageBox( wxT( "DuplicateStruct error: NULL struct" ) );
        return NULL;
    }

    switch( DrawStruct->Type() )
    {
    case DRAW_POLYLINE_STRUCT_TYPE:
207
        NewDrawStruct = ( (SCH_POLYLINE*) DrawStruct )->GenCopy();
208 209 210
        break;

    case DRAW_SEGMENT_STRUCT_TYPE:
211
        NewDrawStruct = ( (SCH_LINE*) DrawStruct )->GenCopy();
212 213 214
        break;

    case DRAW_BUSENTRY_STRUCT_TYPE:
215
        NewDrawStruct = ( (SCH_BUS_ENTRY*) DrawStruct )->GenCopy();
216 217 218
        break;

    case DRAW_JUNCTION_STRUCT_TYPE:
219
        NewDrawStruct = ( (SCH_JUNCTION*) DrawStruct )->GenCopy();
220 221
        break;

222 223
    case TYPE_SCH_MARKER:
        NewDrawStruct = ( (SCH_MARKER*) DrawStruct )->GenCopy();
224 225 226
        break;

    case DRAW_NOCONNECT_STRUCT_TYPE:
227
        NewDrawStruct = ( (SCH_NO_CONNECT*) DrawStruct )->GenCopy();
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
        break;

    case TYPE_SCH_TEXT:
        NewDrawStruct = ( (SCH_TEXT*) DrawStruct )->GenCopy();
        break;

    case TYPE_SCH_LABEL:
        NewDrawStruct = ( (SCH_LABEL*) DrawStruct )->GenCopy();
        break;

    case TYPE_SCH_HIERLABEL:
        NewDrawStruct = ( (SCH_HIERLABEL*) DrawStruct )->GenCopy();
        break;

    case TYPE_SCH_GLOBALLABEL:
        NewDrawStruct = ( (SCH_GLOBALLABEL*) DrawStruct )->GenCopy();
        break;

    case TYPE_SCH_COMPONENT:
        NewDrawStruct = ( (SCH_COMPONENT*) DrawStruct )->GenCopy();
        break;

    case DRAW_SHEET_STRUCT_TYPE:
251
        NewDrawStruct = ( (SCH_SHEET*) DrawStruct )->GenCopy();
252 253 254 255 256
        break;

    default:
    {
        wxString msg;
257 258
        msg << wxT( "DuplicateStruct error: unexpected StructType " )
            << DrawStruct->Type() << wxT( " " ) << DrawStruct->GetClass();
259 260 261 262 263 264 265 266
        wxMessageBox( msg );
    }
    break;
    }

    NewDrawStruct->m_Image = DrawStruct;
    return NewDrawStruct;
}