KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_geometry_extractor.cpp
Go to the documentation of this file.
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 3
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/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 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
25#include "sch_diff_utils.h"
26
27#include <lib_symbol.h>
28#include <schematic.h>
29#include <sch_item.h>
30#include <sch_junction.h>
31#include <sch_line.h>
32#include <sch_screen.h>
33#include <sch_sheet.h>
34#include <sch_sheet_path.h>
35#include <sch_symbol.h>
36
37#include <algorithm>
38#include <set>
39
40
41namespace KICAD_DIFF
42{
43
44namespace
45{
46
47 void addBBoxAsPolygon( const BOX2I& aBBox, const KIGFX::COLOR4D& aColor, bool aFilled, DOCUMENT_GEOMETRY& aOut )
48 {
49 DOCUMENT_POLYGON poly = MakeBBoxOutline( aBBox, aColor );
50 poly.filled = aFilled;
51
52 if( !poly.outline.empty() )
53 aOut.polygons.push_back( std::move( poly ) );
54 }
55
56
57 void extractScreen( const SCHEMATIC* aSchematic, const SCH_SHEET_PATH& aPath, const SCH_SCREEN& aScreen,
58 const KIGFX::COLOR4D& aColor, const std::map<KIID, KIGFX::COLOR4D>& aOverrides,
59 bool aOnlyOverrides, DOCUMENT_GEOMETRY& aOut )
60 {
61 SHEET_SCOPE scope( aSchematic, &aPath );
62
63 auto colorFor = [&]( const SCH_ITEM* aItem ) -> KIGFX::COLOR4D
64 {
65 auto it = aOverrides.find( aItem->m_Uuid );
66 return it != aOverrides.end() ? it->second : aColor;
67 };
68
69 for( const SCH_ITEM* item : aScreen.Items() )
70 {
71 if( !item )
72 continue;
73
74 if( aOnlyOverrides && !aOverrides.count( item->m_Uuid ) )
75 continue;
76
77 const KIGFX::COLOR4D color = colorFor( item );
78
79 switch( item->Type() )
80 {
81 case SCH_LINE_T:
82 {
83 const SCH_LINE& line = static_cast<const SCH_LINE&>( *item );
84
86 seg.start = line.GetStartPoint();
87 seg.end = line.GetEndPoint();
88 seg.width = line.GetLineWidth();
89 seg.color = color;
90 aOut.segments.push_back( seg );
91 break;
92 }
93
94 case SCH_JUNCTION_T:
95 {
96 const SCH_JUNCTION& junc = static_cast<const SCH_JUNCTION&>( *item );
97
99 c.center = junc.GetPosition();
100 c.radius = std::max( 1, junc.GetDiameter() / 2 );
101 c.filled = true;
102 c.lineWidth = 0;
103 c.color = color;
104 aOut.circles.push_back( c );
105 break;
106 }
107
108 case SCH_SHEET_T:
109 case SCH_SYMBOL_T:
110 case SCH_NO_CONNECT_T:
111 case SCH_TEXT_T:
112 case SCH_TEXTBOX_T:
113 case SCH_TABLE_T:
114 case SCH_BITMAP_T:
115 case SCH_RULE_AREA_T:
116 case SCH_LABEL_T:
118 case SCH_HIER_LABEL_T:
120 case SCH_SHAPE_T:
123 {
124 const bool overridden = aOverrides.count( item->m_Uuid ) > 0;
125 addBBoxAsPolygon( item->GetBoundingBox(), color, overridden, aOut );
126 break;
127 }
128
129 default: break;
130 }
131 }
132 }
133
134} // namespace
135
136
138 const std::map<KIID, KIGFX::COLOR4D>& aOverrides, bool aOnlyOverrides )
139{
141
142 std::set<const SCH_SCREEN*> visited;
143
144 for( const SCH_SHEET_PATH& sheet : aSchematic.Hierarchy() )
145 {
146 const SCH_SCREEN* screen = sheet.LastScreen();
147
148 if( !screen || !visited.insert( screen ).second )
149 continue;
150
151 extractScreen( &aSchematic, sheet, *screen, aColor, aOverrides, aOnlyOverrides, out );
152 }
153
154 return out;
155}
156
157
158DOCUMENT_GEOMETRY ExtractSymbolGeometry( const LIB_SYMBOL& aSymbol, const KIGFX::COLOR4D& aColor, int aUnit,
159 int aBodyStyle )
160{
162
163 for( const SCH_ITEM& item : aSymbol.GetDrawItems() )
164 {
165 if( item.GetUnit() > 0 && aUnit > 0 && item.GetUnit() != aUnit )
166 continue;
167
168 if( item.GetBodyStyle() > 0 && aBodyStyle > 0 && item.GetBodyStyle() != aBodyStyle )
169 continue;
170
171 addBBoxAsPolygon( item.GetBoundingBox(), aColor, false, out );
172 }
173
174 if( out.Empty() )
175 addBBoxAsPolygon( aSymbol.GetUnitBoundingBox( aUnit, aBodyStyle ), aColor, false, out );
176
177 return out;
178}
179
180} // namespace KICAD_DIFF
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
RAII guard that temporarily swaps SCHEMATIC::CurrentSheet to a given path for the duration of a scope...
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
Define a library symbol object.
Definition lib_symbol.h:79
const BOX2I GetUnitBoundingBox(int aUnit, int aBodyStyle, bool aIgnoreHiddenFields=true, bool aIgnoreLabelsOnInvisiblePins=true) const
Get the bounding box for the symbol.
LIB_ITEMS_CONTAINER & GetDrawItems()
Return a reference to the draw item list.
Definition lib_symbol.h:709
Holds all the data relating to one schematic.
Definition schematic.h:90
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:162
int GetDiameter() const
VECTOR2I GetPosition() const override
VECTOR2I GetEndPoint() const
Definition sch_line.h:144
VECTOR2I GetStartPoint() const
Definition sch_line.h:135
int GetLineWidth() const
Definition sch_line.h:194
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:115
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
DOCUMENT_POLYGON MakeBBoxOutline(const BOX2I &aBBox, const KIGFX::COLOR4D &aColor, int aLineWidth)
Build a DOCUMENT_POLYGON outlining a bounding box.
DOCUMENT_GEOMETRY ExtractSymbolGeometry(const LIB_SYMBOL &aSymbol, const KIGFX::COLOR4D &aColor, int aUnit, int aBodyStyle)
Extract coarse drawable context from a library symbol for visual symbol diffs.
DOCUMENT_GEOMETRY ExtractSchematicGeometry(const SCHEMATIC &aSchematic, const KIGFX::COLOR4D &aColor, const std::map< KIID, KIGFX::COLOR4D > &aOverrides, bool aOnlyOverrides)
Extract a coarse outline of a SCHEMATIC into a DOCUMENT_GEOMETRY for use as background context in DIF...
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
Filled or stroked circle.
Definition diff_scene.h:146
Aggregate of background geometry extracted from one source document.
Definition diff_scene.h:163
Closed polygon outline from a source document.
Definition diff_scene.h:133
Stroked line segment from one of the source documents.
Definition diff_scene.h:117
@ SCH_TABLE_T
Definition typeinfo.h:162
@ SCH_LINE_T
Definition typeinfo.h:160
@ SCH_NO_CONNECT_T
Definition typeinfo.h:157
@ SCH_SYMBOL_T
Definition typeinfo.h:169
@ SCH_DIRECTIVE_LABEL_T
Definition typeinfo.h:168
@ SCH_LABEL_T
Definition typeinfo.h:164
@ SCH_SHEET_T
Definition typeinfo.h:172
@ SCH_SHAPE_T
Definition typeinfo.h:146
@ SCH_RULE_AREA_T
Definition typeinfo.h:167
@ SCH_HIER_LABEL_T
Definition typeinfo.h:166
@ SCH_BUS_BUS_ENTRY_T
Definition typeinfo.h:159
@ SCH_TEXT_T
Definition typeinfo.h:148
@ SCH_BUS_WIRE_ENTRY_T
Definition typeinfo.h:158
@ SCH_BITMAP_T
Definition typeinfo.h:161
@ SCH_TEXTBOX_T
Definition typeinfo.h:149
@ SCH_GLOBAL_LABEL_T
Definition typeinfo.h:165
@ SCH_JUNCTION_T
Definition typeinfo.h:156