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.

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