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.

515 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-2022 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 <erc_settings.h>
  29. #include <sch_reference_list.h>
  30. #include <symbol_library.h>
  31. #include <tools/ee_selection.h>
  32. #include <tools/ee_selection_tool.h>
  33. #include <tool/tool_manager.h>
  34. #include <dialog_erc.h>
  35. void SCH_EDIT_FRAME::mapExistingAnnotation( std::map<wxString, wxString>& aMap )
  36. {
  37. SCH_REFERENCE_LIST references;
  38. Schematic().GetSheets().GetSymbols( references );
  39. for( size_t i = 0; i < references.GetCount(); i++ )
  40. {
  41. SCH_SYMBOL* symbol = references[ i ].GetSymbol();
  42. SCH_SHEET_PATH* curr_sheetpath = &references[ i ].GetSheetPath();
  43. KIID_PATH curr_full_uuid = curr_sheetpath->Path();
  44. curr_full_uuid.push_back( symbol->m_Uuid );
  45. wxString ref = symbol->GetRef( curr_sheetpath, true );
  46. if( symbol->IsAnnotated( curr_sheetpath ) )
  47. aMap[ curr_full_uuid.AsString() ] = ref;
  48. }
  49. }
  50. void SCH_EDIT_FRAME::DeleteAnnotation( ANNOTATE_SCOPE_T aAnnotateScope, bool aRecursive,
  51. bool* aAppendUndo )
  52. {
  53. auto clearSymbolAnnotation =
  54. [&]( EDA_ITEM* aItem, SCH_SCREEN* aScreen, SCH_SHEET_PATH* aSheet, bool aResetPrefixes )
  55. {
  56. SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( aItem );
  57. SaveCopyInUndoList( aScreen, symbol, UNDO_REDO::CHANGED, *aAppendUndo );
  58. *aAppendUndo = true;
  59. symbol->ClearAnnotation( aSheet, aResetPrefixes );
  60. };
  61. auto clearSheetAnnotation =
  62. [&]( SCH_SCREEN* aScreen, SCH_SHEET_PATH* aSheet, bool aResetPrefixes )
  63. {
  64. for( SCH_ITEM* item : aScreen->Items().OfType( SCH_SYMBOL_T ) )
  65. clearSymbolAnnotation( item, aScreen, aSheet, aResetPrefixes );
  66. };
  67. SCH_SHEET_LIST sheets = Schematic().GetSheets();
  68. SCH_SCREEN* screen = GetScreen();
  69. SCH_SHEET_PATH currentSheet = GetCurrentSheet();
  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. SyncView();
  124. GetCanvas()->Refresh();
  125. OnModify();
  126. // Must go after OnModify() so the connectivity graph has been updated
  127. UpdateNetHighlightStatus();
  128. }
  129. std::unordered_set<SCH_SYMBOL*> getInferredSymbols( const EE_SELECTION& aSelection )
  130. {
  131. std::unordered_set<SCH_SYMBOL*> symbols;
  132. for( EDA_ITEM* item : aSelection )
  133. {
  134. switch( item->Type() )
  135. {
  136. case SCH_FIELD_T:
  137. {
  138. SCH_FIELD* field = static_cast<SCH_FIELD*>( item );
  139. if( field->GetId() == REFERENCE_FIELD && field->GetParent()->Type() == SCH_SYMBOL_T )
  140. symbols.insert( static_cast<SCH_SYMBOL*>( field->GetParent() ) );
  141. break;
  142. }
  143. case SCH_SYMBOL_T:
  144. symbols.insert( static_cast<SCH_SYMBOL*>( item ) );
  145. break;
  146. default:
  147. break;
  148. }
  149. }
  150. return symbols;
  151. }
  152. void SCH_EDIT_FRAME::AnnotateSymbols( ANNOTATE_SCOPE_T aAnnotateScope,
  153. ANNOTATE_ORDER_T aSortOption,
  154. ANNOTATE_ALGO_T aAlgoOption,
  155. bool aRecursive,
  156. int aStartNumber,
  157. bool aResetAnnotation,
  158. bool aRepairTimestamps,
  159. REPORTER& aReporter,
  160. bool appendUndo )
  161. {
  162. EE_SELECTION_TOOL* selTool = m_toolManager->GetTool<EE_SELECTION_TOOL>();
  163. EE_SELECTION& selection = selTool->GetSelection();
  164. SCH_REFERENCE_LIST references;
  165. SCH_SCREENS screens( Schematic().Root() );
  166. SCH_SHEET_LIST sheets = Schematic().GetSheets();
  167. SCH_SHEET_PATH currentSheet = GetCurrentSheet();
  168. // Store the selected sheets relative to the full hierarchy so we get the correct sheet numbers
  169. SCH_SHEET_LIST selectedSheets;
  170. for( EDA_ITEM* item : selection )
  171. {
  172. if( item->Type() == SCH_SHEET_T )
  173. {
  174. SCH_SHEET_PATH subSheetPath = currentSheet;
  175. subSheetPath.push_back( static_cast<SCH_SHEET*>( item ) );
  176. sheets.GetSheetsWithinPath( selectedSheets, subSheetPath );
  177. }
  178. }
  179. // Like above, store subsheets relative to full hierarchy for recursive annotation from current
  180. // sheet
  181. SCH_SHEET_LIST subSheets;
  182. std::vector<SCH_ITEM*> tempSubSheets;
  183. currentSheet.LastScreen()->GetSheets( &tempSubSheets );
  184. for( SCH_ITEM* item : tempSubSheets )
  185. {
  186. SCH_SHEET_PATH subSheetPath = currentSheet;
  187. subSheetPath.push_back( static_cast<SCH_SHEET*>( item ) );
  188. sheets.GetSheetsWithinPath( subSheets, subSheetPath );
  189. }
  190. std::unordered_set<SCH_SYMBOL*> selectedSymbols;
  191. if( aAnnotateScope == ANNOTATE_SELECTION )
  192. selectedSymbols = getInferredSymbols( selection );
  193. // Map of locked symbols
  194. SCH_MULTI_UNIT_REFERENCE_MAP lockedSymbols;
  195. // Map of previous annotation for building info messages
  196. std::map<wxString, wxString> previousAnnotation;
  197. // Test for and replace duplicate time stamps in symbols and sheets. Duplicate time stamps
  198. // can happen with old schematics, schematic conversions, or manual editing of files.
  199. if( aRepairTimestamps )
  200. {
  201. int count = screens.ReplaceDuplicateTimeStamps();
  202. if( count )
  203. {
  204. wxString msg;
  205. msg.Printf( _( "%d duplicate time stamps were found and replaced." ), count );
  206. aReporter.ReportTail( msg, RPT_SEVERITY_WARNING );
  207. }
  208. }
  209. // Collect all the sets that must be annotated together.
  210. switch( aAnnotateScope )
  211. {
  212. case ANNOTATE_ALL:
  213. sheets.GetMultiUnitSymbols( lockedSymbols );
  214. break;
  215. case ANNOTATE_CURRENT_SHEET:
  216. currentSheet.GetMultiUnitSymbols( lockedSymbols );
  217. break;
  218. case ANNOTATE_SELECTION:
  219. for( SCH_SYMBOL* symbol : selectedSymbols )
  220. currentSheet.AppendMultiUnitSymbol( lockedSymbols, symbol );
  221. break;
  222. }
  223. // Store previous annotations for building info messages
  224. mapExistingAnnotation( previousAnnotation );
  225. // Set sheet number and number of sheets.
  226. SetSheetNumberAndCount();
  227. // Build symbol list
  228. switch( aAnnotateScope )
  229. {
  230. case ANNOTATE_ALL:
  231. sheets.GetSymbols( references );
  232. break;
  233. case ANNOTATE_CURRENT_SHEET:
  234. currentSheet.GetSymbols( references );
  235. if( aRecursive )
  236. subSheets.GetSymbolsWithinPath( references, currentSheet, false, true );
  237. break;
  238. case ANNOTATE_SELECTION:
  239. for( SCH_SYMBOL* symbol : selectedSymbols )
  240. currentSheet.AppendSymbol( references, symbol, false, true );
  241. if( aRecursive )
  242. selectedSheets.GetSymbolsWithinPath( references, currentSheet, false, true );
  243. break;
  244. }
  245. // Remove annotation only updates the "new" flag to indicate to the algorithm
  246. // that these references must be reannotated, but keeps the original reference
  247. // so that we can reannotate multi-unit symbols together.
  248. if( aResetAnnotation )
  249. references.RemoveAnnotation();
  250. // Build additional list of references to be used during reannotation
  251. // to avoid duplicate designators (no additional references when annotating
  252. // the full schematic)
  253. SCH_REFERENCE_LIST additionalRefs;
  254. if( aAnnotateScope != ANNOTATE_ALL )
  255. {
  256. SCH_REFERENCE_LIST allRefs;
  257. sheets.GetSymbols( allRefs );
  258. for( size_t i = 0; i < allRefs.GetCount(); i++ )
  259. {
  260. if( !references.Contains( allRefs[i] ) )
  261. additionalRefs.AddItem( allRefs[i] );
  262. }
  263. }
  264. // Break full symbol reference into name (prefix) and number:
  265. // example: IC1 become IC, and 1
  266. references.SplitReferences();
  267. // Annotate all of the references we've collected by our options
  268. references.AnnotateByOptions( aSortOption, aAlgoOption, aStartNumber, lockedSymbols,
  269. additionalRefs, false );
  270. for( size_t i = 0; i < references.GetCount(); i++ )
  271. {
  272. SCH_REFERENCE& ref = references[i];
  273. SCH_SYMBOL* symbol = ref.GetSymbol();
  274. SCH_SHEET_PATH* sheet = &ref.GetSheetPath();
  275. SaveCopyInUndoList( sheet->LastScreen(), symbol, UNDO_REDO::CHANGED, appendUndo );
  276. appendUndo = true;
  277. ref.Annotate();
  278. KIID_PATH full_uuid = sheet->Path();
  279. full_uuid.push_back( symbol->m_Uuid );
  280. wxString prevRef = previousAnnotation[ full_uuid.AsString() ];
  281. wxString newRef = symbol->GetRef( sheet );
  282. if( symbol->GetUnitCount() > 1 )
  283. newRef << LIB_SYMBOL::SubReference( symbol->GetUnitSelection( sheet ) );
  284. wxString msg;
  285. if( prevRef.Length() )
  286. {
  287. if( newRef == prevRef )
  288. continue;
  289. if( symbol->GetUnitCount() > 1 )
  290. {
  291. msg.Printf( _( "Updated %s (unit %s) from %s to %s." ),
  292. symbol->GetValueFieldText( true ),
  293. LIB_SYMBOL::SubReference( symbol->GetUnit(), false ),
  294. prevRef,
  295. newRef );
  296. }
  297. else
  298. {
  299. msg.Printf( _( "Updated %s from %s to %s." ),
  300. symbol->GetValueFieldText( true ),
  301. prevRef,
  302. newRef );
  303. }
  304. }
  305. else
  306. {
  307. if( symbol->GetUnitCount() > 1 )
  308. {
  309. msg.Printf( _( "Annotated %s (unit %s) as %s." ),
  310. symbol->GetValueFieldText( true ),
  311. LIB_SYMBOL::SubReference( symbol->GetUnit(), false ),
  312. newRef );
  313. }
  314. else
  315. {
  316. msg.Printf( _( "Annotated %s as %s." ),
  317. symbol->GetValueFieldText( true ),
  318. newRef );
  319. }
  320. }
  321. aReporter.Report( msg, RPT_SEVERITY_ACTION );
  322. }
  323. // Final control (just in case ... ).
  324. if( !CheckAnnotate(
  325. [ &aReporter ]( ERCE_T , const wxString& aMsg, SCH_REFERENCE* , SCH_REFERENCE* )
  326. {
  327. aReporter.Report( aMsg, RPT_SEVERITY_ERROR );
  328. },
  329. aAnnotateScope, aRecursive ) )
  330. {
  331. aReporter.ReportTail( _( "Annotation complete." ), RPT_SEVERITY_ACTION );
  332. }
  333. // Update on screen references, that can be modified by previous calculations:
  334. GetCurrentSheet().UpdateAllScreenReferences();
  335. SetSheetNumberAndCount();
  336. wxWindow* erc_dlg = wxWindow::FindWindowByName( DIALOG_ERC_WINDOW_NAME );
  337. if( erc_dlg )
  338. static_cast<DIALOG_ERC*>( erc_dlg )->UpdateAnnotationWarning();
  339. SyncView();
  340. GetCanvas()->Refresh();
  341. OnModify();
  342. // Must go after OnModify() so the connectivity graph has been updated
  343. UpdateNetHighlightStatus();
  344. }
  345. int SCH_EDIT_FRAME::CheckAnnotate( ANNOTATION_ERROR_HANDLER aErrorHandler,
  346. ANNOTATE_SCOPE_T aAnnotateScope,
  347. bool aRecursive )
  348. {
  349. SCH_REFERENCE_LIST referenceList;
  350. constexpr bool includePowerSymbols = false;
  351. SCH_SHEET_LIST sheets = Schematic().GetSheets();
  352. SCH_SHEET_PATH currentSheet = GetCurrentSheet();
  353. // Build the list of symbols
  354. switch( aAnnotateScope )
  355. {
  356. case ANNOTATE_ALL:
  357. Schematic().GetSheets().GetSymbols( referenceList );
  358. break;
  359. case ANNOTATE_CURRENT_SHEET:
  360. GetCurrentSheet().GetSymbols( referenceList, includePowerSymbols );
  361. if( aRecursive )
  362. {
  363. SCH_SHEET_LIST subSheets;
  364. std::vector<SCH_ITEM*> tempSubSheets;
  365. currentSheet.LastScreen()->GetSheets( &tempSubSheets );
  366. for( SCH_ITEM* item : tempSubSheets )
  367. {
  368. SCH_SHEET_PATH subSheetPath = currentSheet;
  369. subSheetPath.push_back( static_cast<SCH_SHEET*>( item ) );
  370. sheets.GetSheetsWithinPath( subSheets, subSheetPath );
  371. }
  372. for( SCH_SHEET_PATH sheet : subSheets )
  373. sheet.GetSymbols( referenceList, includePowerSymbols );
  374. }
  375. break;
  376. case ANNOTATE_SELECTION:
  377. EE_SELECTION_TOOL* selTool = m_toolManager->GetTool<EE_SELECTION_TOOL>();
  378. EE_SELECTION& selection = selTool->RequestSelection();
  379. for( SCH_SYMBOL* symbol : getInferredSymbols( selection ) )
  380. GetCurrentSheet().AppendSymbol( referenceList, symbol, false, true );
  381. if( aRecursive )
  382. {
  383. SCH_SHEET_LIST selectedSheets;
  384. for( EDA_ITEM* item : selection.Items() )
  385. {
  386. if( item->Type() == SCH_SHEET_T )
  387. {
  388. SCH_SHEET_PATH subSheetPath = currentSheet;
  389. subSheetPath.push_back( static_cast<SCH_SHEET*>( item ) );
  390. sheets.GetSheetsWithinPath( selectedSheets, subSheetPath );
  391. }
  392. }
  393. for( SCH_SHEET_PATH sheet : selectedSheets )
  394. sheet.GetSymbols( referenceList, includePowerSymbols );
  395. }
  396. break;
  397. }
  398. // Empty schematic does not need annotation
  399. if( referenceList.GetCount() == 0 )
  400. return 0;
  401. return referenceList.CheckAnnotation( aErrorHandler );
  402. }