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.

436 lines
14 KiB

5 months ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2009 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <sch_draw_panel.h>
  25. #include <trigo.h>
  26. #include <widgets/msgpanel.h>
  27. #include <bitmaps.h>
  28. #include <base_units.h>
  29. #include <eda_draw_frame.h>
  30. #include <erc/erc_settings.h>
  31. #include <sch_marker.h>
  32. #include <schematic.h>
  33. #include <widgets/ui_common.h>
  34. #include <pgm_base.h>
  35. #include <settings/settings_manager.h>
  36. #include <settings/color_settings.h>
  37. #include <erc/erc_item.h>
  38. #include <sch_screen.h>
  39. /// Factor to convert the maker unit shape to internal units:
  40. #define SCALING_FACTOR schIUScale.mmToIU( 0.15 )
  41. SCH_MARKER::SCH_MARKER( std::shared_ptr<ERC_ITEM> aItem, const VECTOR2I& aPos ) :
  42. SCH_ITEM( nullptr, SCH_MARKER_T ),
  43. MARKER_BASE( SCALING_FACTOR, aItem, MARKER_BASE::MARKER_ERC )
  44. {
  45. if( m_rcItem )
  46. m_rcItem->SetParent( this );
  47. m_Pos = aPos;
  48. m_isLegacyMarker = false;
  49. }
  50. SCH_MARKER::~SCH_MARKER()
  51. {
  52. if( m_rcItem )
  53. m_rcItem->SetParent( nullptr );
  54. }
  55. EDA_ITEM* SCH_MARKER::Clone() const
  56. {
  57. return new SCH_MARKER( *this );
  58. }
  59. void SCH_MARKER::swapData( SCH_ITEM* aItem )
  60. {
  61. SCH_MARKER* item = static_cast<SCH_MARKER*>( aItem );
  62. std::swap( m_isLegacyMarker, item->m_isLegacyMarker );
  63. std::swap( m_Pos, item->m_Pos );
  64. std::swap( m_markerType, item->m_markerType );
  65. std::swap( m_excluded, item->m_excluded );
  66. std::swap( m_rcItem, item->m_rcItem );
  67. std::swap( m_scalingFactor, item->m_scalingFactor );
  68. std::swap( m_shapeBoundingBox, item->m_shapeBoundingBox );
  69. // TODO: isn't this going to swap all the stuff above a second time?
  70. std::swap( *((SCH_MARKER*) this), *((SCH_MARKER*) aItem ) );
  71. }
  72. wxString SCH_MARKER::SerializeToString() const
  73. {
  74. std::shared_ptr<ERC_ITEM> erc = std::static_pointer_cast<ERC_ITEM>( m_rcItem );
  75. wxString sheetSpecificPath, mainItemPath, auxItemPath;
  76. if( erc->IsSheetSpecific() )
  77. sheetSpecificPath = erc->GetSpecificSheetPath().Path().AsString();
  78. if( erc->MainItemHasSheetPath() )
  79. mainItemPath = erc->GetMainItemSheetPath().Path().AsString();
  80. if( erc->AuxItemHasSheetPath() )
  81. auxItemPath = erc->GetAuxItemSheetPath().Path().AsString();
  82. if( m_rcItem->GetErrorCode() == ERCE_GENERIC_WARNING
  83. || m_rcItem->GetErrorCode() == ERCE_GENERIC_ERROR
  84. || m_rcItem->GetErrorCode() == ERCE_UNRESOLVED_VARIABLE )
  85. {
  86. SCH_ITEM* sch_item = Schematic()->ResolveItem( erc->GetMainItemID() );
  87. SCH_ITEM* parent = static_cast<SCH_ITEM*>( sch_item->GetParent() );
  88. EDA_TEXT* text_item = dynamic_cast<EDA_TEXT*>( sch_item );
  89. // SCH_FIELDs and SCH_ITEMs inside LIB_SYMBOLs don't have persistent KIIDs. So the
  90. // exclusion must refer to the parent's KIID, and include the text of the original text
  91. // item for later look-up.
  92. if( parent && parent->IsType( { SCH_SYMBOL_T, SCH_LABEL_T, SCH_SHEET_T } ) )
  93. {
  94. if( text_item ) // should always be true, but Coverity doesn't know that
  95. {
  96. return wxString::Format( wxT( "%s|%d|%d|%s|%s|%s|%s|%s" ),
  97. m_rcItem->GetSettingsKey(),
  98. m_Pos.x,
  99. m_Pos.y,
  100. parent->m_Uuid.AsString(),
  101. text_item->GetText(),
  102. sheetSpecificPath,
  103. mainItemPath,
  104. wxEmptyString );
  105. }
  106. }
  107. }
  108. return wxString::Format( wxT( "%s|%d|%d|%s|%s|%s|%s|%s" ),
  109. m_rcItem->GetSettingsKey(),
  110. m_Pos.x,
  111. m_Pos.y,
  112. m_rcItem->GetMainItemID().AsString(),
  113. m_rcItem->GetAuxItemID().AsString(),
  114. sheetSpecificPath,
  115. mainItemPath,
  116. auxItemPath );
  117. }
  118. SCH_MARKER* SCH_MARKER::DeserializeFromString( const SCH_SHEET_LIST& aSheetList,
  119. const wxString& data )
  120. {
  121. wxArrayString props = wxSplit( data, '|' );
  122. VECTOR2I markerPos( (int) strtol( props[1].c_str(), nullptr, 10 ),
  123. (int) strtol( props[2].c_str(), nullptr, 10 ) );
  124. std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( props[0] );
  125. if( !ercItem )
  126. return nullptr;
  127. if( ercItem->GetErrorCode() == ERCE_GENERIC_WARNING
  128. || ercItem->GetErrorCode() == ERCE_GENERIC_ERROR
  129. || ercItem->GetErrorCode() == ERCE_UNRESOLVED_VARIABLE )
  130. {
  131. // SCH_FIELDs and SCH_ITEMs inside LIB_SYMBOLs don't have persistent KIIDs. So the
  132. // exclusion will contain the parent's KIID in prop[3], and the text of the original
  133. // text item in prop[4].
  134. if( !props[4].IsEmpty() )
  135. {
  136. KIID uuid = niluuid;
  137. SCH_ITEM* parent = aSheetList.ResolveItem( KIID( props[3] ) );
  138. // Check fields and pins for a match
  139. parent->RunOnChildren(
  140. [&]( SCH_ITEM* child )
  141. {
  142. if( EDA_TEXT* text_item = dynamic_cast<EDA_TEXT*>( child ) )
  143. {
  144. if( text_item->GetText() == props[4] )
  145. uuid = child->m_Uuid;
  146. }
  147. },
  148. RECURSE_MODE::NO_RECURSE );
  149. // If it's a symbol, we must also check non-overridden LIB_SYMBOL text children
  150. if( uuid == niluuid && parent->Type() == SCH_SYMBOL_T )
  151. {
  152. static_cast<SCH_SYMBOL*>( parent )->GetLibSymbolRef()->RunOnChildren(
  153. [&]( SCH_ITEM* child )
  154. {
  155. if( child->Type() == SCH_FIELD_T )
  156. {
  157. // Match only on SCH_SYMBOL fields, not LIB_SYMBOL fields.
  158. }
  159. else if( EDA_TEXT* text_item = dynamic_cast<EDA_TEXT*>( child ) )
  160. {
  161. if( text_item->GetText() == props[4] )
  162. uuid = child->m_Uuid;
  163. }
  164. },
  165. RECURSE_MODE::NO_RECURSE );
  166. }
  167. if( uuid != niluuid )
  168. ercItem->SetItems( uuid );
  169. else
  170. return nullptr;
  171. }
  172. else
  173. {
  174. ercItem->SetItems( KIID( props[3] ) );
  175. }
  176. }
  177. else
  178. {
  179. ercItem->SetItems( KIID( props[3] ), KIID( props[4] ) );
  180. }
  181. bool isLegacyMarker = true;
  182. // Deserialize sheet / item specific paths - we are not able to use the file version to
  183. // determine if markers are legacy as there could be a file opened with a prior version
  184. // but which has new markers - this code is called not just during schematic load, but
  185. // also to match new ERC exceptions to exclusions.
  186. if( props.size() == 8 )
  187. {
  188. isLegacyMarker = false;
  189. if( !props[5].IsEmpty() )
  190. {
  191. KIID_PATH sheetSpecificKiidPath( props[5] );
  192. std::optional<SCH_SHEET_PATH> sheetSpecificPath =
  193. aSheetList.GetSheetPathByKIIDPath( sheetSpecificKiidPath, true );
  194. if( sheetSpecificPath.has_value() )
  195. ercItem->SetSheetSpecificPath( sheetSpecificPath.value() );
  196. }
  197. if( !props[6].IsEmpty() )
  198. {
  199. KIID_PATH mainItemKiidPath( props[6] );
  200. std::optional<SCH_SHEET_PATH> mainItemPath =
  201. aSheetList.GetSheetPathByKIIDPath( mainItemKiidPath, true );
  202. if( mainItemPath.has_value() )
  203. {
  204. if( props[7].IsEmpty() )
  205. {
  206. ercItem->SetItemsSheetPaths( mainItemPath.value() );
  207. }
  208. else
  209. {
  210. KIID_PATH auxItemKiidPath( props[7] );
  211. std::optional<SCH_SHEET_PATH> auxItemPath =
  212. aSheetList.GetSheetPathByKIIDPath( auxItemKiidPath, true );
  213. if( auxItemPath.has_value() )
  214. ercItem->SetItemsSheetPaths( mainItemPath.value(), auxItemPath.value() );
  215. }
  216. }
  217. }
  218. }
  219. SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), markerPos );
  220. marker->SetIsLegacyMarker( isLegacyMarker );
  221. return marker;
  222. }
  223. #if defined(DEBUG)
  224. void SCH_MARKER::Show( int nestLevel, std::ostream& os ) const
  225. {
  226. // for now, make it look like XML:
  227. NestedSpace( nestLevel, os ) << '<' << GetClass().Lower().mb_str() << GetPos() << "/>\n";
  228. }
  229. #endif
  230. std::vector<int> SCH_MARKER::ViewGetLayers() const
  231. {
  232. wxCHECK2_MSG( Schematic(), return {}, "No SCHEMATIC set for SCH_MARKER!" );
  233. // Don't display sheet-specific markers when SCH_SHEET_PATHs do not match
  234. std::shared_ptr<ERC_ITEM> ercItem = std::static_pointer_cast<ERC_ITEM>( GetRCItem() );
  235. if( ercItem->IsSheetSpecific()
  236. && ( ercItem->GetSpecificSheetPath() != Schematic()->CurrentSheet() ) )
  237. {
  238. return {};
  239. }
  240. std::vector<int> layers( 2 );
  241. if( IsExcluded() )
  242. {
  243. layers[0] = LAYER_ERC_EXCLUSION;
  244. }
  245. else
  246. {
  247. switch( Schematic()->ErcSettings().GetSeverity( m_rcItem->GetErrorCode() ) )
  248. {
  249. default:
  250. case SEVERITY::RPT_SEVERITY_ERROR: layers[0] = LAYER_ERC_ERR; break;
  251. case SEVERITY::RPT_SEVERITY_WARNING: layers[0] = LAYER_ERC_WARN; break;
  252. }
  253. }
  254. layers[1] = LAYER_SELECTION_SHADOWS;
  255. return layers;
  256. }
  257. SCH_LAYER_ID SCH_MARKER::GetColorLayer() const
  258. {
  259. if( IsExcluded() )
  260. return LAYER_ERC_EXCLUSION;
  261. wxCHECK_MSG( Schematic(), LAYER_ERC_ERR, "No SCHEMATIC set for SCH_MARKER!" );
  262. switch( Schematic()->ErcSettings().GetSeverity( m_rcItem->GetErrorCode() ) )
  263. {
  264. default:
  265. case SEVERITY::RPT_SEVERITY_ERROR: return LAYER_ERC_ERR;
  266. case SEVERITY::RPT_SEVERITY_WARNING: return LAYER_ERC_WARN;
  267. }
  268. }
  269. KIGFX::COLOR4D SCH_MARKER::getColor() const
  270. {
  271. return ::GetColorSettings( DEFAULT_THEME )->GetColor( GetColorLayer() );
  272. }
  273. SEVERITY SCH_MARKER::GetSeverity() const
  274. {
  275. if( IsExcluded() )
  276. return RPT_SEVERITY_EXCLUSION;
  277. ERC_ITEM* item = static_cast<ERC_ITEM*>( m_rcItem.get() );
  278. return Schematic()->ErcSettings().GetSeverity( item->GetErrorCode() );
  279. }
  280. bool SCH_MARKER::Matches( const EDA_SEARCH_DATA& aSearchData, void* aAuxData ) const
  281. {
  282. return SCH_ITEM::Matches( m_rcItem->GetErrorMessage(), aSearchData );
  283. }
  284. const BOX2I SCH_MARKER::GetBoundingBox() const
  285. {
  286. return GetBoundingBoxMarker();
  287. }
  288. void SCH_MARKER::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  289. {
  290. aList.emplace_back( _( "Type" ), _( "Marker" ) );
  291. aList.emplace_back( _( "Violation" ), m_rcItem->GetErrorMessage() );
  292. switch( GetSeverity() )
  293. {
  294. case RPT_SEVERITY_IGNORE:
  295. aList.emplace_back( _( "Severity" ), _( "Ignore" ) );
  296. break;
  297. case RPT_SEVERITY_WARNING:
  298. aList.emplace_back( _( "Severity" ), _( "Warning" ) );
  299. break;
  300. case RPT_SEVERITY_ERROR:
  301. aList.emplace_back( _( "Severity" ), _( "Error" ) );
  302. break;
  303. default:
  304. break;
  305. }
  306. if( GetMarkerType() == MARKER_DRAWING_SHEET )
  307. {
  308. aList.emplace_back( _( "Drawing Sheet" ), wxEmptyString );
  309. }
  310. else
  311. {
  312. wxString mainText;
  313. wxString auxText;
  314. EDA_ITEM* mainItem = nullptr;
  315. EDA_ITEM* auxItem = nullptr;
  316. if( m_rcItem->GetMainItemID() != niluuid )
  317. mainItem = aFrame->ResolveItem( m_rcItem->GetMainItemID() );
  318. if( m_rcItem->GetAuxItemID() != niluuid )
  319. auxItem = aFrame->ResolveItem( m_rcItem->GetAuxItemID() );
  320. if( mainItem )
  321. mainText = mainItem->GetItemDescription( aFrame, true );
  322. if( auxItem )
  323. auxText = auxItem->GetItemDescription( aFrame, true );
  324. aList.emplace_back( mainText, auxText );
  325. }
  326. if( IsExcluded() )
  327. aList.emplace_back( _( "Excluded" ), m_comment );
  328. }
  329. BITMAPS SCH_MARKER::GetMenuImage() const
  330. {
  331. return BITMAPS::erc;
  332. }
  333. void SCH_MARKER::Rotate( const VECTOR2I& aCenter, bool aRotateCCW )
  334. {
  335. // Marker geometry isn't user-editable
  336. }
  337. void SCH_MARKER::MirrorVertically( int aCenter )
  338. {
  339. // Marker geometry isn't user-editable
  340. }
  341. void SCH_MARKER::MirrorHorizontally( int aCenter )
  342. {
  343. // Marker geometry isn't user-editable
  344. }
  345. bool SCH_MARKER::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
  346. {
  347. return HitTestMarker( aPosition, aAccuracy );
  348. }