From b14bc1beadd74b1bfd5c1b05566c4f0ea016086a Mon Sep 17 00:00:00 2001
From: Seth Hillbrand <hillbrand@ucdavis.edu>
Date: Sat, 26 Jan 2019 16:58:33 -0800
Subject: [PATCH] svg: Use grouping

Uses existing grouping in SVG output.  Sets schematic components as a
grouped element in SVG as well as pcbnew elements per layer.

Fixes: lp:1011754
* https://bugs.launchpad.net/kicad/+bug/1011754
---
 common/plotters/SVG_plotter.cpp | 18 ++++++++++++++++++
 eeschema/sch_component.cpp      |  3 +++
 include/plotter.h               | 16 ++++++++++++++++
 3 files changed, 37 insertions(+)

diff --git a/common/plotters/SVG_plotter.cpp b/common/plotters/SVG_plotter.cpp
index e6ecb6aa90..2b2dc8dcb5 100644
--- a/common/plotters/SVG_plotter.cpp
+++ b/common/plotters/SVG_plotter.cpp
@@ -288,6 +288,24 @@ void SVG_PLOTTER::SetCurrentLineWidth( int width, void* aData )
 }
 
 
+void SVG_PLOTTER::StartBlock( void* aData )
+{
+    std::string* idstr = reinterpret_cast<std::string*>( aData );
+
+    fputs( "<svg:g ", outputFile );
+    if( idstr )
+        fprintf( outputFile, "id=\"%s\"", idstr->c_str() );
+
+    fprintf( outputFile, ">\n" );
+}
+
+
+void SVG_PLOTTER::EndBlock( void* aData )
+{
+    fprintf( outputFile, "</g>\n" );
+}
+
+
 /* initialize m_red, m_green, m_blue ( 0 ... 255)
  * from reduced values r, g ,b ( 0.0 to 1.0 )
  */
diff --git a/eeschema/sch_component.cpp b/eeschema/sch_component.cpp
index c016fe0418..94345f38f7 100644
--- a/eeschema/sch_component.cpp
+++ b/eeschema/sch_component.cpp
@@ -1968,6 +1968,7 @@ void SCH_COMPONENT::Plot( PLOTTER* aPlotter )
     if( PART_SPTR part = m_part.lock() )
     {
         temp = GetTransform();
+        aPlotter->StartBlock( nullptr );
 
         part->Plot( aPlotter, GetUnit(), GetConvert(), m_Pos, temp );
 
@@ -1975,6 +1976,8 @@ void SCH_COMPONENT::Plot( PLOTTER* aPlotter )
         {
             m_Fields[i].Plot( aPlotter );
         }
+
+        aPlotter->EndBlock( nullptr );
     }
 }
 
diff --git a/include/plotter.h b/include/plotter.h
index 40e2248554..0321a1f7da 100644
--- a/include/plotter.h
+++ b/include/plotter.h
@@ -936,6 +936,22 @@ public:
                             double aScaleFactor ) override;
 
     virtual void PenTo( const wxPoint& pos, char plume ) override;
+
+
+    /**
+     * calling this function allows one to define the beginning of a group
+     * of drawing items (used in SVG format to separate components)
+     * @param aData should be a string for the SVG ID tage
+     */
+    virtual void StartBlock( void* aData ) override;
+
+    /**
+     * calling this function allows one to define the end of a group of drawing
+     * items the group is started by StartBlock()
+     * @param aData should be null
+     */
+    virtual void EndBlock( void* aData ) override;
+
     virtual void Text( const wxPoint&              aPos,
                        const COLOR4D               aColor,
                        const wxString&             aText,