You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
7.0 KiB

  1. /**
  2. * @file operations_on_items_lists.cpp
  3. * @brief Functions used in block commands, or undo/redo, to move, mirror, delete, copy ...
  4. * lists of schematic items.
  5. */
  6. #include <fctsys.h>
  7. #include <appl_wxstruct.h>
  8. #include <class_drawpanel.h>
  9. #include <wxEeschemaStruct.h>
  10. #include <general.h>
  11. #include <protos.h>
  12. #include <sch_bus_entry.h>
  13. #include <sch_marker.h>
  14. #include <sch_line.h>
  15. #include <sch_no_connect.h>
  16. #include <sch_sheet.h>
  17. #include <sch_component.h>
  18. #include <sch_junction.h>
  19. void SetSchItemParent( SCH_ITEM* Struct, SCH_SCREEN* Screen )
  20. {
  21. switch( Struct->Type() )
  22. {
  23. case SCH_JUNCTION_T:
  24. case SCH_TEXT_T:
  25. case SCH_LABEL_T:
  26. case SCH_GLOBAL_LABEL_T:
  27. case SCH_HIERARCHICAL_LABEL_T:
  28. case SCH_COMPONENT_T:
  29. case SCH_LINE_T:
  30. case SCH_BUS_BUS_ENTRY_T:
  31. case SCH_BUS_WIRE_ENTRY_T:
  32. case SCH_SHEET_T:
  33. case SCH_MARKER_T:
  34. case SCH_NO_CONNECT_T:
  35. Struct->SetParent( Screen );
  36. break;
  37. case SCH_SHEET_PIN_T:
  38. break;
  39. default:
  40. break;
  41. }
  42. }
  43. void RotateListOfItems( PICKED_ITEMS_LIST& aItemsList, wxPoint& rotationPoint )
  44. {
  45. for( unsigned ii = 0; ii < aItemsList.GetCount(); ii++ )
  46. {
  47. SCH_ITEM* item = (SCH_ITEM*) aItemsList.GetPickedItem( ii );
  48. item->Rotate( rotationPoint ); // Place it in its new position.
  49. item->ClearFlags();
  50. }
  51. }
  52. void DeleteItemsInList( EDA_DRAW_PANEL* panel, PICKED_ITEMS_LIST& aItemsList );
  53. void DuplicateItemsInList( SCH_SCREEN* screen, PICKED_ITEMS_LIST& aItemsList,
  54. const wxPoint aMoveVector );
  55. void MirrorY( PICKED_ITEMS_LIST& aItemsList, wxPoint& aMirrorPoint )
  56. {
  57. for( unsigned ii = 0; ii < aItemsList.GetCount(); ii++ )
  58. {
  59. SCH_ITEM* item = (SCH_ITEM*) aItemsList.GetPickedItem( ii );
  60. item->MirrorY( aMirrorPoint.x ); // Place it in its new position.
  61. item->ClearFlags();
  62. }
  63. }
  64. void MirrorX( PICKED_ITEMS_LIST& aItemsList, wxPoint& aMirrorPoint )
  65. {
  66. for( unsigned ii = 0; ii < aItemsList.GetCount(); ii++ )
  67. {
  68. SCH_ITEM* item = (SCH_ITEM*) aItemsList.GetPickedItem( ii );
  69. item->MirrorX( aMirrorPoint.y ); // Place it in its new position.
  70. item->ClearFlags();
  71. }
  72. }
  73. /**
  74. * Function MoveItemsInList
  75. * Move a list of items to a given move vector
  76. * @param aItemsList = list of picked items
  77. * @param aMoveVector = the move vector value
  78. */
  79. void MoveItemsInList( PICKED_ITEMS_LIST& aItemsList, const wxPoint aMoveVector )
  80. {
  81. for( unsigned ii = 0; ii < aItemsList.GetCount(); ii++ )
  82. {
  83. SCH_ITEM* item = (SCH_ITEM*) aItemsList.GetPickedItem( ii );
  84. item->Move( aMoveVector );
  85. }
  86. }
  87. /**
  88. * Function DeleteItemsInList
  89. * delete schematic items in aItemsList
  90. * deleted items are put in undo list
  91. */
  92. void DeleteItemsInList( EDA_DRAW_PANEL* panel, PICKED_ITEMS_LIST& aItemsList )
  93. {
  94. SCH_SCREEN* screen = (SCH_SCREEN*) panel->GetScreen();
  95. SCH_EDIT_FRAME* frame = (SCH_EDIT_FRAME*) panel->GetParent();
  96. PICKED_ITEMS_LIST itemsList;
  97. for( unsigned ii = 0; ii < aItemsList.GetCount(); ii++ )
  98. {
  99. SCH_ITEM* item = (SCH_ITEM*) aItemsList.GetPickedItem( ii );
  100. ITEM_PICKER itemWrapper( item, UR_DELETED );
  101. if( item->Type() == SCH_SHEET_PIN_T )
  102. {
  103. /* this item is depending on a sheet, and is not in global list */
  104. wxMessageBox( wxT( "DeleteItemsInList() err: unexpected SCH_SHEET_PIN_T" ) );
  105. }
  106. else
  107. {
  108. screen->Remove( item );
  109. /* Unlink the structure */
  110. itemsList.PushItem( itemWrapper );
  111. }
  112. }
  113. frame->SaveCopyInUndoList( itemsList, UR_DELETED );
  114. }
  115. void SCH_EDIT_FRAME::DeleteItem( SCH_ITEM* aItem )
  116. {
  117. wxCHECK_RET( aItem != NULL, wxT( "Cannot delete invalid item." ) );
  118. if( aItem == NULL )
  119. return;
  120. SCH_SCREEN* screen = GetScreen();
  121. if( aItem->Type() == SCH_SHEET_PIN_T )
  122. {
  123. // This iten is attached to a node, and is not accessible by the global list directly.
  124. SCH_SHEET* sheet = (SCH_SHEET*) aItem->GetParent();
  125. wxCHECK_RET( (sheet != NULL) && (sheet->Type() == SCH_SHEET_T),
  126. wxT( "Sheet label has invalid parent item." ) );
  127. SaveCopyInUndoList( (SCH_ITEM*) sheet, UR_CHANGED );
  128. sheet->RemovePin( (SCH_SHEET_PIN*) aItem );
  129. m_canvas->RefreshDrawingRect( sheet->GetBoundingBox() );
  130. }
  131. else
  132. {
  133. screen->Remove( aItem );
  134. SaveCopyInUndoList( aItem, UR_DELETED );
  135. m_canvas->RefreshDrawingRect( aItem->GetBoundingBox() );
  136. }
  137. }
  138. /* Routine to copy a new entity of an object for each object in list and
  139. * reposition it.
  140. * Return the new created object list in aItemsList
  141. */
  142. void DuplicateItemsInList( SCH_SCREEN* screen, PICKED_ITEMS_LIST& aItemsList,
  143. const wxPoint aMoveVector )
  144. {
  145. SCH_ITEM* newitem;
  146. if( aItemsList.GetCount() == 0 )
  147. return;
  148. for( unsigned ii = 0; ii < aItemsList.GetCount(); ii++ )
  149. {
  150. newitem = DuplicateStruct( (SCH_ITEM*) aItemsList.GetPickedItem( ii ) );
  151. aItemsList.SetPickedItem( newitem, ii );
  152. aItemsList.SetPickedItemStatus( UR_NEW, ii );
  153. {
  154. switch( newitem->Type() )
  155. {
  156. case SCH_JUNCTION_T:
  157. case SCH_LINE_T:
  158. case SCH_BUS_BUS_ENTRY_T:
  159. case SCH_BUS_WIRE_ENTRY_T:
  160. case SCH_TEXT_T:
  161. case SCH_LABEL_T:
  162. case SCH_GLOBAL_LABEL_T:
  163. case SCH_HIERARCHICAL_LABEL_T:
  164. case SCH_SHEET_PIN_T:
  165. case SCH_MARKER_T:
  166. case SCH_NO_CONNECT_T:
  167. default:
  168. break;
  169. case SCH_SHEET_T:
  170. {
  171. SCH_SHEET* sheet = (SCH_SHEET*) newitem;
  172. sheet->SetTimeStamp( GetNewTimeStamp() );
  173. sheet->SetSon( NULL );
  174. break;
  175. }
  176. case SCH_COMPONENT_T:
  177. ( (SCH_COMPONENT*) newitem )->SetTimeStamp( GetNewTimeStamp() );
  178. ( (SCH_COMPONENT*) newitem )->ClearAnnotation( NULL );
  179. break;
  180. }
  181. SetSchItemParent( newitem, screen );
  182. screen->Append( newitem );
  183. }
  184. }
  185. MoveItemsInList( aItemsList, aMoveVector );
  186. }
  187. /**
  188. * Function DuplicateStruct
  189. * Routine to create a new copy of given struct.
  190. * The new object is not put in draw list (not linked)
  191. * @param aDrawStruct = the SCH_ITEM to duplicate
  192. * @param aClone (default = false)
  193. * if true duplicate also some parameters that must be unique
  194. * (timestamp and sheet name)
  195. * aClone must be false. use true only is undo/redo duplications
  196. */
  197. SCH_ITEM* DuplicateStruct( SCH_ITEM* aDrawStruct, bool aClone )
  198. {
  199. wxCHECK_MSG( aDrawStruct != NULL, NULL,
  200. wxT( "Cannot duplicate NULL schematic item! Bad programmer." ) );
  201. SCH_ITEM* NewDrawStruct = (SCH_ITEM*) aDrawStruct->Clone();
  202. if( aClone )
  203. NewDrawStruct->SetTimeStamp( aDrawStruct->GetTimeStamp() );
  204. NewDrawStruct->SetImage( aDrawStruct );
  205. return NewDrawStruct;
  206. }