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.

516 lines
16 KiB

18 years ago
18 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2004-2023 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include <algorithm>
  24. #include <confirm.h>
  25. #include <reporter.h>
  26. #include <sch_edit_frame.h>
  27. #include <schematic.h>
  28. #include <schematic_commit.h>
  29. #include <erc_settings.h>
  30. #include <sch_reference_list.h>
  31. #include <symbol_library.h>
  32. #include <tools/ee_selection.h>
  33. #include <tools/ee_selection_tool.h>
  34. #include <tool/tool_manager.h>
  35. #include <dialog_erc.h>
  36. void SCH_EDIT_FRAME::mapExistingAnnotation( std::map<wxString, wxString>& aMap )
  37. {
  38. SCH_REFERENCE_LIST references;
  39. Schematic().GetSheets().GetSymbols( references );
  40. for( size_t i = 0; i < references.GetCount(); i++ )
  41. {
  42. SCH_SYMBOL* symbol = references[ i ].GetSymbol();
  43. SCH_SHEET_PATH* curr_sheetpath = &references[ i ].GetSheetPath();
  44. KIID_PATH curr_full_uuid = curr_sheetpath->Path();
  45. curr_full_uuid.push_back( symbol->m_Uuid );
  46. wxString ref = symbol->GetRef( curr_sheetpath, true );
  47. if( symbol->IsAnnotated( curr_sheetpath ) )
  48. aMap[ curr_full_uuid.AsString() ] = ref;
  49. }
  50. }
  51. void SCH_EDIT_FRAME::DeleteAnnotation( ANNOTATE_SCOPE_T aAnnotateScope, bool aRecursive )
  52. {
  53. SCH_SHEET_LIST sheets = Schematic().GetSheets();
  54. SCH_SCREEN* screen = GetScreen();
  55. SCH_SHEET_PATH currentSheet = GetCurrentSheet();
  56. SCHEMATIC_COMMIT commit( this );
  57. auto clearSymbolAnnotation =
  58. [&]( EDA_ITEM* aItem, SCH_SCREEN* aScreen, SCH_SHEET_PATH* aSheet, bool aResetPrefixes )
  59. {
  60. SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( aItem );
  61. commit.Modify( aItem, aScreen );
  62. symbol->ClearAnnotation( aSheet, aResetPrefixes );
  63. };
  64. auto clearSheetAnnotation =
  65. [&]( SCH_SCREEN* aScreen, SCH_SHEET_PATH* aSheet, bool aResetPrefixes )
  66. {
  67. for( SCH_ITEM* item : aScreen->Items().OfType( SCH_SYMBOL_T ) )
  68. clearSymbolAnnotation( item, aScreen, aSheet, aResetPrefixes );
  69. };
  70. switch( aAnnotateScope )
  71. {
  72. case ANNOTATE_ALL:
  73. {
  74. for( const SCH_SHEET_PATH& sheet : sheets )
  75. clearSheetAnnotation( sheet.LastScreen(), nullptr, false );
  76. break;
  77. }
  78. case ANNOTATE_CURRENT_SHEET:
  79. {
  80. clearSheetAnnotation( screen, &currentSheet, false );
  81. if( aRecursive )
  82. {
  83. SCH_SHEET_LIST subSheets;
  84. std::vector<SCH_ITEM*> tempSubSheets;
  85. currentSheet.LastScreen()->GetSheets( &tempSubSheets );
  86. for( SCH_ITEM* item : tempSubSheets )
  87. {
  88. SCH_SHEET_PATH subSheetPath = currentSheet;
  89. subSheetPath.push_back( static_cast<SCH_SHEET*>( item ) );
  90. sheets.GetSheetsWithinPath( subSheets, subSheetPath );
  91. }
  92. for( SCH_SHEET_PATH sheet : subSheets )
  93. clearSheetAnnotation( sheet.LastScreen(), &sheet, false );
  94. }
  95. break;
  96. }
  97. case ANNOTATE_SELECTION:
  98. {
  99. EE_SELECTION_TOOL* selTool = m_toolManager->GetTool<EE_SELECTION_TOOL>();
  100. EE_SELECTION& selection = selTool->RequestSelection();
  101. SCH_SHEET_LIST selectedSheets;
  102. for( EDA_ITEM* item : selection.Items() )
  103. {
  104. if( item->Type() == SCH_SYMBOL_T )
  105. clearSymbolAnnotation( item, screen, &currentSheet, false );
  106. if( item->Type() == SCH_SHEET_T && aRecursive )
  107. {
  108. SCH_SHEET_PATH subSheetPath = currentSheet;
  109. subSheetPath.push_back( static_cast<SCH_SHEET*>( item ) );
  110. sheets.GetSheetsWithinPath( selectedSheets, subSheetPath );
  111. }
  112. }
  113. for( SCH_SHEET_PATH sheet : selectedSheets )
  114. clearSheetAnnotation( sheet.LastScreen(), &sheet, false );
  115. break;
  116. }
  117. }
  118. // Update the references for the sheet that is currently being displayed.
  119. GetCurrentSheet().UpdateAllScreenReferences();
  120. wxWindow* erc_dlg = wxWindow::FindWindowByName( DIALOG_ERC_WINDOW_NAME );
  121. if( erc_dlg )
  122. static_cast<DIALOG_ERC*>( erc_dlg )->UpdateAnnotationWarning();
  123. commit.Push( _( "Delete Annotation" ) );
  124. GetCanvas()->Refresh();
  125. // Must go after OnModify() so the connectivity graph has been updated
  126. UpdateNetHighlightStatus();
  127. }
  128. std::unordered_set<SCH_SYMBOL*> getInferredSymbols( const EE_SELECTION& aSelection )
  129. {
  130. std::unordered_set<SCH_SYMBOL*> symbols;
  131. for( EDA_ITEM* item : aSelection )
  132. {
  133. switch( item->Type() )
  134. {
  135. case SCH_FIELD_T:
  136. {
  137. SCH_FIELD* field = static_cast<SCH_FIELD*>( item );
  138. if( field->GetId() == REFERENCE_FIELD && field->GetParent()->Type() == SCH_SYMBOL_T )
  139. symbols.insert( static_cast<SCH_SYMBOL*>( field->GetParent() ) );
  140. break;
  141. }
  142. case SCH_SYMBOL_T:
  143. symbols.insert( static_cast<SCH_SYMBOL*>( item ) );
  144. break;
  145. default:
  146. break;
  147. }
  148. }
  149. return symbols;
  150. }
  151. void SCH_EDIT_FRAME::AnnotateSymbols( ANNOTATE_SCOPE_T aAnnotateScope,
  152. ANNOTATE_ORDER_T aSortOption,
  153. ANNOTATE_ALGO_T aAlgoOption,
  154. bool aRecursive,
  155. int aStartNumber,
  156. bool aResetAnnotation,
  157. bool aRepairTimestamps,
  158. REPORTER& aReporter,
  159. bool appendUndo )
  160. {
  161. EE_SELECTION_TOOL* selTool = m_toolManager->GetTool<EE_SELECTION_TOOL>();
  162. EE_SELECTION& selection = selTool->GetSelection();
  163. SCH_REFERENCE_LIST references;
  164. SCH_SCREENS screens( Schematic().Root() );
  165. SCH_SHEET_LIST sheets = Schematic().GetSheets();
  166. SCH_SHEET_PATH currentSheet = GetCurrentSheet();
  167. // Store the selected sheets relative to the full hierarchy so we get the correct sheet numbers
  168. SCH_SHEET_LIST selectedSheets;
  169. for( EDA_ITEM* item : selection )
  170. {
  171. if( item->Type() == SCH_SHEET_T )
  172. {
  173. SCH_SHEET_PATH subSheetPath = currentSheet;
  174. subSheetPath.push_back( static_cast<SCH_SHEET*>( item ) );
  175. sheets.GetSheetsWithinPath( selectedSheets, subSheetPath );
  176. }
  177. }
  178. // Like above, store subsheets relative to full hierarchy for recursive annotation from current
  179. // sheet
  180. SCH_SHEET_LIST subSheets;
  181. std::vector<SCH_ITEM*> tempSubSheets;
  182. currentSheet.LastScreen()->GetSheets( &tempSubSheets );
  183. for( SCH_ITEM* item : tempSubSheets )
  184. {
  185. SCH_SHEET_PATH subSheetPath = currentSheet;
  186. subSheetPath.push_back( static_cast<SCH_SHEET*>( item ) );
  187. sheets.GetSheetsWithinPath( subSheets, subSheetPath );
  188. }
  189. std::unordered_set<SCH_SYMBOL*> selectedSymbols;
  190. if( aAnnotateScope == ANNOTATE_SELECTION )
  191. selectedSymbols = getInferredSymbols( selection );
  192. // Map of locked symbols
  193. SCH_MULTI_UNIT_REFERENCE_MAP lockedSymbols;
  194. // Map of previous annotation for building info messages
  195. std::map<wxString, wxString> previousAnnotation;
  196. // Test for and replace duplicate time stamps in symbols and sheets. Duplicate time stamps
  197. // can happen with old schematics, schematic conversions, or manual editing of files.
  198. if( aRepairTimestamps )
  199. {
  200. int count = screens.ReplaceDuplicateTimeStamps();
  201. if( count )
  202. {
  203. wxString msg;
  204. msg.Printf( _( "%d duplicate time stamps were found and replaced." ), count );
  205. aReporter.ReportTail( msg, RPT_SEVERITY_WARNING );
  206. }
  207. }
  208. // Collect all the sets that must be annotated together.
  209. switch( aAnnotateScope )
  210. {
  211. case ANNOTATE_ALL:
  212. sheets.GetMultiUnitSymbols( lockedSymbols );
  213. break;
  214. case ANNOTATE_CURRENT_SHEET:
  215. currentSheet.GetMultiUnitSymbols( lockedSymbols );
  216. break;
  217. case ANNOTATE_SELECTION:
  218. for( SCH_SYMBOL* symbol : selectedSymbols )
  219. currentSheet.AppendMultiUnitSymbol( lockedSymbols, symbol );
  220. break;
  221. }
  222. // Store previous annotations for building info messages
  223. mapExistingAnnotation( previousAnnotation );
  224. // Set sheet number and number of sheets.
  225. SetSheetNumberAndCount();
  226. // Build symbol list
  227. switch( aAnnotateScope )
  228. {
  229. case ANNOTATE_ALL:
  230. sheets.GetSymbols( references );
  231. break;
  232. case ANNOTATE_CURRENT_SHEET:
  233. currentSheet.GetSymbols( references );
  234. if( aRecursive )
  235. subSheets.GetSymbolsWithinPath( references, currentSheet, false, true );
  236. break;
  237. case ANNOTATE_SELECTION:
  238. for( SCH_SYMBOL* symbol : selectedSymbols )
  239. currentSheet.AppendSymbol( references, symbol, false, true );
  240. if( aRecursive )
  241. selectedSheets.GetSymbolsWithinPath( references, currentSheet, false, true );
  242. break;
  243. }
  244. // Remove annotation only updates the "new" flag to indicate to the algorithm
  245. // that these references must be reannotated, but keeps the original reference
  246. // so that we can reannotate multi-unit symbols together.
  247. if( aResetAnnotation )
  248. references.RemoveAnnotation();
  249. // Build additional list of references to be used during reannotation
  250. // to avoid duplicate designators (no additional references when annotating
  251. // the full schematic)
  252. SCH_REFERENCE_LIST additionalRefs;
  253. if( aAnnotateScope != ANNOTATE_ALL )
  254. {
  255. SCH_REFERENCE_LIST allRefs;
  256. sheets.GetSymbols( allRefs );
  257. for( size_t i = 0; i < allRefs.GetCount(); i++ )
  258. {
  259. if( !references.Contains( allRefs[i] ) )
  260. additionalRefs.AddItem( allRefs[i] );
  261. }
  262. }
  263. // Break full symbol reference into name (prefix) and number:
  264. // example: IC1 become IC, and 1
  265. references.SplitReferences();
  266. // Annotate all of the references we've collected by our options
  267. references.AnnotateByOptions( aSortOption, aAlgoOption, aStartNumber, lockedSymbols,
  268. additionalRefs, false );
  269. for( size_t i = 0; i < references.GetCount(); i++ )
  270. {
  271. SCH_REFERENCE& ref = references[i];
  272. SCH_SYMBOL* symbol = ref.GetSymbol();
  273. SCH_SHEET_PATH* sheet = &ref.GetSheetPath();
  274. SaveCopyInUndoList( sheet->LastScreen(), symbol, UNDO_REDO::CHANGED, appendUndo );
  275. appendUndo = true;
  276. ref.Annotate();
  277. KIID_PATH full_uuid = sheet->Path();
  278. full_uuid.push_back( symbol->m_Uuid );
  279. wxString prevRef = previousAnnotation[ full_uuid.AsString() ];
  280. wxString newRef = symbol->GetRef( sheet );
  281. if( symbol->GetUnitCount() > 1 )
  282. newRef << LIB_SYMBOL::SubReference( symbol->GetUnitSelection( sheet ) );
  283. wxString msg;
  284. if( prevRef.Length() )
  285. {
  286. if( newRef == prevRef )
  287. continue;
  288. if( symbol->GetUnitCount() > 1 )
  289. {
  290. msg.Printf( _( "Updated %s (unit %s) from %s to %s." ),
  291. symbol->GetValueFieldText( true, sheet, false ),
  292. LIB_SYMBOL::SubReference( symbol->GetUnit(), false ),
  293. prevRef,
  294. newRef );
  295. }
  296. else
  297. {
  298. msg.Printf( _( "Updated %s from %s to %s." ),
  299. symbol->GetValueFieldText( true, sheet, false ),
  300. prevRef,
  301. newRef );
  302. }
  303. }
  304. else
  305. {
  306. if( symbol->GetUnitCount() > 1 )
  307. {
  308. msg.Printf( _( "Annotated %s (unit %s) as %s." ),
  309. symbol->GetValueFieldText( true, sheet, false ),
  310. LIB_SYMBOL::SubReference( symbol->GetUnit(), false ),
  311. newRef );
  312. }
  313. else
  314. {
  315. msg.Printf( _( "Annotated %s as %s." ),
  316. symbol->GetValueFieldText( true, sheet, false ),
  317. newRef );
  318. }
  319. }
  320. aReporter.Report( msg, RPT_SEVERITY_ACTION );
  321. }
  322. // Final control (just in case ... ).
  323. if( !CheckAnnotate(
  324. [ &aReporter ]( ERCE_T , const wxString& aMsg, SCH_REFERENCE* , SCH_REFERENCE* )
  325. {
  326. aReporter.Report( aMsg, RPT_SEVERITY_ERROR );
  327. },
  328. aAnnotateScope, aRecursive ) )
  329. {
  330. aReporter.ReportTail( _( "Annotation complete." ), RPT_SEVERITY_ACTION );
  331. }
  332. // Update on screen references, that can be modified by previous calculations:
  333. GetCurrentSheet().UpdateAllScreenReferences();
  334. SetSheetNumberAndCount();
  335. wxWindow* erc_dlg = wxWindow::FindWindowByName( DIALOG_ERC_WINDOW_NAME );
  336. if( erc_dlg )
  337. static_cast<DIALOG_ERC*>( erc_dlg )->UpdateAnnotationWarning();
  338. SyncView();
  339. GetCanvas()->Refresh();
  340. OnModify();
  341. // Must go after OnModify() so the connectivity graph has been updated
  342. UpdateNetHighlightStatus();
  343. }
  344. int SCH_EDIT_FRAME::CheckAnnotate( ANNOTATION_ERROR_HANDLER aErrorHandler,
  345. ANNOTATE_SCOPE_T aAnnotateScope,
  346. bool aRecursive )
  347. {
  348. SCH_REFERENCE_LIST referenceList;
  349. constexpr bool includePowerSymbols = false;
  350. SCH_SHEET_LIST sheets = Schematic().GetSheets();
  351. SCH_SHEET_PATH currentSheet = GetCurrentSheet();
  352. // Build the list of symbols
  353. switch( aAnnotateScope )
  354. {
  355. case ANNOTATE_ALL:
  356. Schematic().GetSheets().GetSymbols( referenceList );
  357. break;
  358. case ANNOTATE_CURRENT_SHEET:
  359. GetCurrentSheet().GetSymbols( referenceList, includePowerSymbols );
  360. if( aRecursive )
  361. {
  362. SCH_SHEET_LIST subSheets;
  363. std::vector<SCH_ITEM*> tempSubSheets;
  364. currentSheet.LastScreen()->GetSheets( &tempSubSheets );
  365. for( SCH_ITEM* item : tempSubSheets )
  366. {
  367. SCH_SHEET_PATH subSheetPath = currentSheet;
  368. subSheetPath.push_back( static_cast<SCH_SHEET*>( item ) );
  369. sheets.GetSheetsWithinPath( subSheets, subSheetPath );
  370. }
  371. for( SCH_SHEET_PATH sheet : subSheets )
  372. sheet.GetSymbols( referenceList, includePowerSymbols );
  373. }
  374. break;
  375. case ANNOTATE_SELECTION:
  376. EE_SELECTION_TOOL* selTool = m_toolManager->GetTool<EE_SELECTION_TOOL>();
  377. EE_SELECTION& selection = selTool->RequestSelection();
  378. for( SCH_SYMBOL* symbol : getInferredSymbols( selection ) )
  379. GetCurrentSheet().AppendSymbol( referenceList, symbol, false, true );
  380. if( aRecursive )
  381. {
  382. SCH_SHEET_LIST selectedSheets;
  383. for( EDA_ITEM* item : selection.Items() )
  384. {
  385. if( item->Type() == SCH_SHEET_T )
  386. {
  387. SCH_SHEET_PATH subSheetPath = currentSheet;
  388. subSheetPath.push_back( static_cast<SCH_SHEET*>( item ) );
  389. sheets.GetSheetsWithinPath( selectedSheets, subSheetPath );
  390. }
  391. }
  392. for( SCH_SHEET_PATH sheet : selectedSheets )
  393. sheet.GetSymbols( referenceList, includePowerSymbols );
  394. }
  395. break;
  396. }
  397. // Empty schematic does not need annotation
  398. if( referenceList.GetCount() == 0 )
  399. return 0;
  400. return referenceList.CheckAnnotation( aErrorHandler );
  401. }