commit 2fcc6bc105b596b3864cd5e1dc5427bc98f9f8e3
Author: Maxim Kulkin <maxim.kulkin@gmail.com>
Date:   Tue Sep 17 13:48:48 2019 -0700

    Add UI to set negative plot margin
    
    NEW: Added field to PCBNEW plot dialog to allow specifying
    margin for negative plots.

diff --git a/common/pcb_plot_params.keywords b/common/pcb_plot_params.keywords
index 059eb8468..ac77dfbf5 100644
--- a/common/pcb_plot_params.keywords
+++ b/common/pcb_plot_params.keywords
@@ -22,6 +22,7 @@ plotvalue
 psa4output
 pscolor
 psnegative
+psnegativemargin
 scaleselection
 subtractmaskfromsilk
 true
diff --git a/pcbnew/dialogs/dialog_plot.cpp b/pcbnew/dialogs/dialog_plot.cpp
index 5f1c97fa7..a23142756 100644
--- a/pcbnew/dialogs/dialog_plot.cpp
+++ b/pcbnew/dialogs/dialog_plot.cpp
@@ -45,7 +45,8 @@ DIALOG_PLOT::DIALOG_PLOT( PCB_EDIT_FRAME* aParent ) :
     DIALOG_PLOT_BASE( aParent ), m_parent( aParent ),
     m_defaultLineWidth( aParent, m_lineWidthLabel, m_lineWidthCtrl, m_lineWidthUnits, true ),
     m_defaultPenSize( aParent, m_hpglPenLabel, m_hpglPenCtrl, m_hpglPenUnits, true ),
-    m_trackWidthCorrection( aParent, m_widthAdjustLabel, m_widthAdjustCtrl, m_widthAdjustUnits, true )
+    m_trackWidthCorrection( aParent, m_widthAdjustLabel, m_widthAdjustCtrl, m_widthAdjustUnits, true ),
+    m_negativeMargin( aParent, m_negativeMarginLabel, m_negativeMarginCtrl, m_negativeMarginUnits, true )
 {
     SetName( DLG_WINDOW_NAME );
     m_config = Kiface().KifaceSettings();
@@ -121,6 +122,8 @@ void DIALOG_PLOT::init_Dialog()
     m_trackWidthCorrection.SetValue( m_PSWidthAdjust );
 
     m_plotPSNegativeOpt->SetValue( m_plotOpts.GetNegative() );
+    m_negativeMargin.Enable( m_plotOpts.GetNegative() );
+    m_negativeMargin.SetValue( m_plotOpts.GetNegativeMargin() );
     m_forcePSA4OutputOpt->SetValue( m_plotOpts.GetA4Output() );
 
     // Could devote a PlotOrder() function in place of UIOrder().
@@ -695,6 +698,13 @@ void DIALOG_PLOT::applyPlotSettings()
     tempOptions.SetLayerSelection( selectedLayers );
 
     tempOptions.SetNegative( m_plotPSNegativeOpt->GetValue() );
+
+    if ( !tempOptions.SetNegativeMargin( m_negativeMargin.GetValue() ) ) {
+        m_negativeMargin.SetValue( tempOptions.GetNegativeMargin() );
+        msg.Printf( _( "Negative margin constrained." ) );
+        reporter.Report( msg, REPORTER::RPT_INFO );
+    }
+
     tempOptions.SetA4Output( m_forcePSA4OutputOpt->GetValue() );
 
     // Set output directory and replace backslashes with forward ones
@@ -722,6 +732,12 @@ void DIALOG_PLOT::OnGerberX2Checked( wxCommandEvent& event )
 }
 
 
+void DIALOG_PLOT::OnPSNegativeChecked( wxCommandEvent& event )
+{
+    m_negativeMargin.Enable( m_plotPSNegativeOpt->GetValue() );
+}
+
+
 void DIALOG_PLOT::Plot( wxCommandEvent& event )
 {
     BOARD* board = m_parent->GetBoard();
diff --git a/pcbnew/dialogs/dialog_plot.h b/pcbnew/dialogs/dialog_plot.h
index 13678a69b..6feb76bd0 100644
--- a/pcbnew/dialogs/dialog_plot.h
+++ b/pcbnew/dialogs/dialog_plot.h
@@ -60,6 +60,7 @@ private:
     UNIT_BINDER         m_defaultLineWidth;
     UNIT_BINDER         m_defaultPenSize;
     UNIT_BINDER         m_trackWidthCorrection;
+    UNIT_BINDER         m_negativeMargin;
 
     PCB_PLOT_PARAMS     m_plotOpts;
 
@@ -73,6 +74,7 @@ private:
     void        OnSetScaleOpt( wxCommandEvent& event ) override;
     void        CreateDrillFile( wxCommandEvent& event ) override;
     void        OnGerberX2Checked( wxCommandEvent& event ) override;
+    void        OnPSNegativeChecked( wxCommandEvent& event ) override;
     void        onRunDRC( wxCommandEvent& event ) override;
 
     // other functions
diff --git a/pcbnew/dialogs/dialog_plot_base.cpp b/pcbnew/dialogs/dialog_plot_base.cpp
index 7e14300cc..e6d41f17b 100644
--- a/pcbnew/dialogs/dialog_plot_base.cpp
+++ b/pcbnew/dialogs/dialog_plot_base.cpp
@@ -164,6 +164,21 @@ DIALOG_PLOT_BASE::DIALOG_PLOT_BASE( wxWindow* parent, wxWindowID id, const wxStr
 	m_plotPSNegativeOpt = new wxCheckBox( sbOptionsSizer->GetStaticBox(), wxID_ANY, _("Negative plot"), wxDefaultPosition, wxDefaultSize, 0 );
 	gbSizer1->Add( m_plotPSNegativeOpt, wxGBPosition( 5, 1 ), wxGBSpan( 1, 2 ), wxALIGN_CENTER_VERTICAL|wxLEFT, 30 );
 
+	m_negativeMarginLabel = new wxStaticText( sbOptionsSizer->GetStaticBox(), wxID_ANY, _("Negative margin:"), wxDefaultPosition, wxDefaultSize, 0 );
+	m_negativeMarginLabel->Wrap( -1 );
+	m_negativeMarginLabel->SetToolTip( _("When plotting in negative color, add margin of this size around negative image of a board.") );
+
+	gbSizer1->Add( m_negativeMarginLabel, wxGBPosition( 6, 1 ), wxGBSpan( 1, 1 ), wxALIGN_CENTER_VERTICAL|wxLEFT, 30 );
+
+	m_negativeMarginCtrl = new wxTextCtrl( sbOptionsSizer->GetStaticBox(), wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
+	m_negativeMarginCtrl->SetToolTip( _("Size of a margin around negative color plots.") );
+	m_negativeMarginCtrl->SetMinSize( wxSize( 120,-1 ) );
+
+	gbSizer1->Add( m_negativeMarginCtrl, wxGBPosition( 6, 2 ), wxGBSpan( 1, 1 ), wxLEFT, 5 );
+	m_negativeMarginUnits = new wxStaticText( sbOptionsSizer->GetStaticBox(), wxID_ANY, _("mm"), wxDefaultPosition, wxDefaultSize, 0 );
+	m_negativeMarginUnits->Wrap( -1 );
+	gbSizer1->Add( m_negativeMarginUnits, wxGBPosition( 6, 3 ), wxGBSpan( 1, 1 ), wxALIGN_CENTER_VERTICAL|wxLEFT, 5 );
+
 	m_zoneFillCheck = new wxCheckBox( sbOptionsSizer->GetStaticBox(), wxID_ANY, _("Check zone fills before plotting"), wxDefaultPosition, wxDefaultSize, 0 );
 	gbSizer1->Add( m_zoneFillCheck, wxGBPosition( 7, 1 ), wxGBSpan( 1, 2 ), wxALIGN_CENTER_VERTICAL|wxLEFT, 30 );
 
@@ -411,6 +426,7 @@ DIALOG_PLOT_BASE::DIALOG_PLOT_BASE( wxWindow* parent, wxWindowID id, const wxStr
 	m_scaleOpt->Connect( wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler( DIALOG_PLOT_BASE::OnSetScaleOpt ), NULL, this );
 	m_useGerberX2Format->Connect( wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler( DIALOG_PLOT_BASE::OnGerberX2Checked ), NULL, this );
 	m_DXF_plotModeOpt->Connect( wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler( DIALOG_PLOT_BASE::OnChangeDXFPlotMode ), NULL, this );
+	m_plotPSNegativeOpt->Connect( wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler( DIALOG_PLOT_BASE::OnPSNegativeChecked ), NULL, this );
 	m_buttonDRC->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_PLOT_BASE::onRunDRC ), NULL, this );
 	m_sdbSizer1Apply->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_PLOT_BASE::CreateDrillFile ), NULL, this );
 	m_sdbSizer1OK->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_PLOT_BASE::Plot ), NULL, this );
@@ -432,6 +448,7 @@ DIALOG_PLOT_BASE::~DIALOG_PLOT_BASE()
 	m_scaleOpt->Disconnect( wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler( DIALOG_PLOT_BASE::OnSetScaleOpt ), NULL, this );
 	m_useGerberX2Format->Disconnect( wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler( DIALOG_PLOT_BASE::OnGerberX2Checked ), NULL, this );
 	m_DXF_plotModeOpt->Disconnect( wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler( DIALOG_PLOT_BASE::OnChangeDXFPlotMode ), NULL, this );
+	m_plotPSNegativeOpt->Disconnect( wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler( DIALOG_PLOT_BASE::OnPSNegativeChecked ), NULL, this );
 	m_buttonDRC->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_PLOT_BASE::onRunDRC ), NULL, this );
 	m_sdbSizer1Apply->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_PLOT_BASE::CreateDrillFile ), NULL, this );
 	m_sdbSizer1OK->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_PLOT_BASE::Plot ), NULL, this );
diff --git a/pcbnew/dialogs/dialog_plot_base.fbp b/pcbnew/dialogs/dialog_plot_base.fbp
index cbbe0e962..670b583c5 100644
--- a/pcbnew/dialogs/dialog_plot_base.fbp
+++ b/pcbnew/dialogs/dialog_plot_base.fbp
@@ -1787,6 +1787,201 @@
                                                         <property name="window_style"></property>
                                                     </object>
                                                 </object>
+                                                <object class="gbsizeritem" expanded="0">
+                                                    <property name="border">5</property>
+                                                    <property name="colspan">1</property>
+                                                    <property name="column">1</property>
+                                                    <property name="flag">wxALIGN_CENTER_VERTICAL|wxLEFT</property>
+                                                    <property name="row">6</property>
+                                                    <property name="rowspan">1</property>
+                                                    <object class="wxStaticText" expanded="0">
+                                                        <property name="BottomDockable">1</property>
+                                                        <property name="LeftDockable">1</property>
+                                                        <property name="RightDockable">1</property>
+                                                        <property name="TopDockable">1</property>
+                                                        <property name="aui_layer"></property>
+                                                        <property name="aui_name"></property>
+                                                        <property name="aui_position"></property>
+                                                        <property name="aui_row"></property>
+                                                        <property name="best_size"></property>
+                                                        <property name="bg"></property>
+                                                        <property name="caption"></property>
+                                                        <property name="caption_visible">1</property>
+                                                        <property name="center_pane">0</property>
+                                                        <property name="close_button">1</property>
+                                                        <property name="context_help"></property>
+                                                        <property name="context_menu">1</property>
+                                                        <property name="default_pane">0</property>
+                                                        <property name="dock">Dock</property>
+                                                        <property name="dock_fixed">0</property>
+                                                        <property name="docking">Left</property>
+                                                        <property name="enabled">1</property>
+                                                        <property name="fg"></property>
+                                                        <property name="floatable">1</property>
+                                                        <property name="font"></property>
+                                                        <property name="gripper">0</property>
+                                                        <property name="hidden">0</property>
+                                                        <property name="id">wxID_ANY</property>
+                                                        <property name="label">Negative margin:</property>
+                                                        <property name="markup">0</property>
+                                                        <property name="max_size"></property>
+                                                        <property name="maximize_button">0</property>
+                                                        <property name="maximum_size"></property>
+                                                        <property name="min_size"></property>
+                                                        <property name="minimize_button">0</property>
+                                                        <property name="minimum_size"></property>
+                                                        <property name="moveable">1</property>
+                                                        <property name="name">m_negativeMarginLabel</property>
+                                                        <property name="pane_border">1</property>
+                                                        <property name="pane_position"></property>
+                                                        <property name="pane_size"></property>
+                                                        <property name="permission">protected</property>
+                                                        <property name="pin_button">1</property>
+                                                        <property name="pos"></property>
+                                                        <property name="resize">Resizable</property>
+                                                        <property name="show">1</property>
+                                                        <property name="size"></property>
+                                                        <property name="style"></property>
+                                                        <property name="subclass"></property>
+                                                        <property name="toolbar_pane">0</property>
+                                                        <property name="tooltip">When plotting in negative color, add margin of this size around negative image of a board.</property>
+                                                        <property name="window_extra_style"></property>
+                                                        <property name="window_name"></property>
+                                                        <property name="window_style"></property>
+                                                        <property name="wrap">-1</property>
+                                                    </object>
+                                                </object>
+                                                <object class="gbsizeritem" expanded="0">
+                                                    <property name="border">5</property>
+                                                    <property name="colspan">1</property>
+                                                    <property name="column">2</property>
+                                                    <property name="flag">wxLEFT</property>
+                                                    <property name="row">6</property>
+                                                    <property name="rowspan">1</property>
+                                                    <object class="wxTextCtrl" expanded="0">
+                                                        <property name="BottomDockable">1</property>
+                                                        <property name="LeftDockable">1</property>
+                                                        <property name="RightDockable">1</property>
+                                                        <property name="TopDockable">1</property>
+                                                        <property name="aui_layer"></property>
+                                                        <property name="aui_name"></property>
+                                                        <property name="aui_position"></property>
+                                                        <property name="aui_row"></property>
+                                                        <property name="best_size"></property>
+                                                        <property name="bg"></property>
+                                                        <property name="caption"></property>
+                                                        <property name="caption_visible">1</property>
+                                                        <property name="center_pane">0</property>
+                                                        <property name="close_button">1</property>
+                                                        <property name="context_help"></property>
+                                                        <property name="context_menu">1</property>
+                                                        <property name="default_pane">0</property>
+                                                        <property name="dock">Dock</property>
+                                                        <property name="dock_fixed">0</property>
+                                                        <property name="docking">Left</property>
+                                                        <property name="enabled">1</property>
+                                                        <property name="fg"></property>
+                                                        <property name="floatable">1</property>
+                                                        <property name="font"></property>
+                                                        <property name="gripper">0</property>
+                                                        <property name="hidden">0</property>
+                                                        <property name="id">wxID_ANY</property>
+                                                        <property name="max_size"></property>
+                                                        <property name="maximize_button">0</property>
+                                                        <property name="maximum_size"></property>
+                                                        <property name="maxlength">0</property>
+                                                        <property name="min_size"></property>
+                                                        <property name="minimize_button">0</property>
+                                                        <property name="minimum_size">120,-1</property>
+                                                        <property name="moveable">1</property>
+                                                        <property name="name">m_negativeMarginCtrl</property>
+                                                        <property name="pane_border">1</property>
+                                                        <property name="pane_position"></property>
+                                                        <property name="pane_size"></property>
+                                                        <property name="permission">protected</property>
+                                                        <property name="pin_button">1</property>
+                                                        <property name="pos"></property>
+                                                        <property name="resize">Resizable</property>
+                                                        <property name="show">1</property>
+                                                        <property name="size"></property>
+                                                        <property name="style"></property>
+                                                        <property name="subclass"></property>
+                                                        <property name="toolbar_pane">0</property>
+                                                        <property name="tooltip">Size of a margin around negative color plots.</property>
+                                                        <property name="validator_data_type"></property>
+                                                        <property name="validator_style">wxFILTER_NONE</property>
+                                                        <property name="validator_type">wxDefaultValidator</property>
+                                                        <property name="validator_variable"></property>
+                                                        <property name="value"></property>
+                                                        <property name="window_extra_style"></property>
+                                                        <property name="window_name"></property>
+                                                        <property name="window_style"></property>
+                                                    </object>
+                                                </object>
+                                                <object class="gbsizeritem" expanded="0">
+                                                    <property name="border">5</property>
+                                                    <property name="colspan">1</property>
+                                                    <property name="column">3</property>
+                                                    <property name="flag">wxALIGN_CENTER_VERTICAL|wxLEFT</property>
+                                                    <property name="row">6</property>
+                                                    <property name="rowspan">1</property>
+                                                    <object class="wxStaticText" expanded="0">
+                                                        <property name="BottomDockable">1</property>
+                                                        <property name="LeftDockable">1</property>
+                                                        <property name="RightDockable">1</property>
+                                                        <property name="TopDockable">1</property>
+                                                        <property name="aui_layer"></property>
+                                                        <property name="aui_name"></property>
+                                                        <property name="aui_position"></property>
+                                                        <property name="aui_row"></property>
+                                                        <property name="best_size"></property>
+                                                        <property name="bg"></property>
+                                                        <property name="caption"></property>
+                                                        <property name="caption_visible">1</property>
+                                                        <property name="center_pane">0</property>
+                                                        <property name="close_button">1</property>
+                                                        <property name="context_help"></property>
+                                                        <property name="context_menu">1</property>
+                                                        <property name="default_pane">0</property>
+                                                        <property name="dock">Dock</property>
+                                                        <property name="dock_fixed">0</property>
+                                                        <property name="docking">Left</property>
+                                                        <property name="enabled">1</property>
+                                                        <property name="fg"></property>
+                                                        <property name="floatable">1</property>
+                                                        <property name="font"></property>
+                                                        <property name="gripper">0</property>
+                                                        <property name="hidden">0</property>
+                                                        <property name="id">wxID_ANY</property>
+                                                        <property name="label">mm</property>
+                                                        <property name="markup">0</property>
+                                                        <property name="max_size"></property>
+                                                        <property name="maximize_button">0</property>
+                                                        <property name="maximum_size"></property>
+                                                        <property name="min_size"></property>
+                                                        <property name="minimize_button">0</property>
+                                                        <property name="minimum_size"></property>
+                                                        <property name="moveable">1</property>
+                                                        <property name="name">m_negativeMarginUnits</property>
+                                                        <property name="pane_border">1</property>
+                                                        <property name="pane_position"></property>
+                                                        <property name="pane_size"></property>
+                                                        <property name="permission">protected</property>
+                                                        <property name="pin_button">1</property>
+                                                        <property name="pos"></property>
+                                                        <property name="resize">Resizable</property>
+                                                        <property name="show">1</property>
+                                                        <property name="size"></property>
+                                                        <property name="style"></property>
+                                                        <property name="subclass">; forward_declare</property>
+                                                        <property name="toolbar_pane">0</property>
+                                                        <property name="tooltip"></property>
+                                                        <property name="window_extra_style"></property>
+                                                        <property name="window_name"></property>
+                                                        <property name="window_style"></property>
+                                                        <property name="wrap">-1</property>
+                                                    </object>
+                                                </object>
                                                 <object class="gbsizeritem" expanded="0">
                                                     <property name="border">30</property>
                                                     <property name="colspan">2</property>
diff --git a/pcbnew/dialogs/dialog_plot_base.h b/pcbnew/dialogs/dialog_plot_base.h
index 063d26352..b157775d1 100644
--- a/pcbnew/dialogs/dialog_plot_base.h
+++ b/pcbnew/dialogs/dialog_plot_base.h
@@ -85,6 +85,9 @@ class DIALOG_PLOT_BASE : public DIALOG_SHIM
 		wxStaticText* m_lineWidthUnits;
 		wxCheckBox* m_plotMirrorOpt;
 		wxCheckBox* m_plotPSNegativeOpt;
+		wxStaticText* m_negativeMarginLabel;
+		wxTextCtrl* m_negativeMarginCtrl;
+		wxStaticText* m_negativeMarginUnits;
 		wxCheckBox* m_zoneFillCheck;
 		wxStaticBoxSizer* m_GerberOptionsSizer;
 		wxCheckBox* m_useGerberExtensions;
@@ -128,6 +131,7 @@ class DIALOG_PLOT_BASE : public DIALOG_SHIM
 		virtual void OnOutputDirectoryBrowseClicked( wxCommandEvent& event ) { event.Skip(); }
 		virtual void OnSetScaleOpt( wxCommandEvent& event ) { event.Skip(); }
 		virtual void OnGerberX2Checked( wxCommandEvent& event ) { event.Skip(); }
+		virtual void OnPSNegativeChecked( wxCommandEvent& event ) { event.Skip(); }
 		virtual void OnChangeDXFPlotMode( wxCommandEvent& event ) { event.Skip(); }
 		virtual void onRunDRC( wxCommandEvent& event ) { event.Skip(); }
 		virtual void CreateDrillFile( wxCommandEvent& event ) { event.Skip(); }
diff --git a/pcbnew/pcb_plot_params.cpp b/pcbnew/pcb_plot_params.cpp
index 9ae9707ef..05b186ace 100644
--- a/pcbnew/pcb_plot_params.cpp
+++ b/pcbnew/pcb_plot_params.cpp
@@ -33,6 +33,7 @@
 #define PLOT_LINEWIDTH_MIN        ( 0.02 * IU_PER_MM )  // min value for default line thickness
 #define PLOT_LINEWIDTH_MAX        ( 2 * IU_PER_MM )     // max value for default line thickness
 #define PLOT_LINEWIDTH_DEFAULT    ( DEFAULT_TEXT_WIDTH * IU_PER_MM )
+#define PLOT_NEGATIVE_MARGIN_MAX  ( 100 * IU_PER_MM )
 #define HPGL_PEN_DIAMETER_MIN     0
 #define HPGL_PEN_DIAMETER_MAX     100.0     // Unit = mil
 #define HPGL_PEN_SPEED_MIN        1         // this param is always in cm/s
@@ -109,6 +110,7 @@ PCB_PLOT_PARAMS::PCB_PLOT_PARAMS()
     m_HPGLPenSpeed               = 20;        // this param is always in cm/s
     m_HPGLPenDiam                = 15;        // in mils
     m_negative                   = false;
+    m_negativeMargin             = 5 * IU_PER_MM;
     m_A4Output                   = false;
     m_plotReference              = true;
     m_plotValue                  = true;
@@ -202,6 +204,8 @@ void PCB_PLOT_PARAMS::Format( OUTPUTFORMATTER* aFormatter,
                        m_HPGLPenDiam );
     aFormatter->Print( aNestLevel+1, "(%s %s)\n", getTokenName( T_psnegative ),
                        m_negative ? trueStr : falseStr );
+    aFormatter->Print( aNestLevel+1, "(%s %d)\n", getTokenName( T_psnegativemargin ),
+                       m_negativeMargin );
     aFormatter->Print( aNestLevel+1, "(%s %s)\n", getTokenName( T_psa4output ),
                        m_A4Output ? trueStr : falseStr );
     aFormatter->Print( aNestLevel+1, "(%s %s)\n", getTokenName( T_plotreference ),
@@ -276,6 +280,8 @@ bool PCB_PLOT_PARAMS::IsSameAs( const PCB_PLOT_PARAMS &aPcbPlotParams, bool aCom
         return false;
     if( m_negative != aPcbPlotParams.m_negative )
         return false;
+    if( m_negativeMargin != aPcbPlotParams.m_negativeMargin )
+        return false;
     if( m_A4Output != aPcbPlotParams.m_A4Output )
         return false;
     if( m_plotReference != aPcbPlotParams.m_plotReference )
@@ -337,6 +343,12 @@ bool PCB_PLOT_PARAMS::SetLineWidth( int aValue )
     return setInt( &m_lineWidth, aValue, PLOT_LINEWIDTH_MIN, PLOT_LINEWIDTH_MAX );
 }
 
+
+bool PCB_PLOT_PARAMS::SetNegativeMargin( int aValue )
+{
+    return setInt( &m_negativeMargin, aValue, 0, PLOT_NEGATIVE_MARGIN_MAX );
+}
+
 // PCB_PLOT_PARAMS_PARSER
 
 PCB_PLOT_PARAMS_PARSER::PCB_PLOT_PARAMS_PARSER( LINE_READER* aReader ) :
@@ -484,6 +496,10 @@ void PCB_PLOT_PARAMS_PARSER::Parse( PCB_PLOT_PARAMS* aPcbPlotParams )
             aPcbPlotParams->m_negative = parseBool();
             break;
 
+        case T_psnegativemargin:
+            aPcbPlotParams->m_negativeMargin = parseInt( 0, PLOT_NEGATIVE_MARGIN_MAX );
+            break;
+
         case T_plotreference:
             aPcbPlotParams->m_plotReference = parseBool();
             break;
diff --git a/pcbnew/pcb_plot_params.h b/pcbnew/pcb_plot_params.h
index 14d028761..bc1627a74 100644
--- a/pcbnew/pcb_plot_params.h
+++ b/pcbnew/pcb_plot_params.h
@@ -89,6 +89,9 @@ private:
     /// Plot in negative color (supported only by some drivers)
     bool        m_negative;
 
+    /// Add margin around board when plotting in negative color
+    int         m_negativeMargin;
+
     /// True if vias are drawn on Mask layer (ie untented, *exposed* by mask)
     bool        m_plotViaOnMaskLayer;
 
@@ -243,6 +246,9 @@ public:
     void        SetNegative( bool aFlag ) { m_negative = aFlag; }
     bool        GetNegative() const { return m_negative; }
 
+    bool        SetNegativeMargin( int aVal );
+    int         GetNegativeMargin() const { return m_negativeMargin; }
+
     void        SetPlotViaOnMaskLayer( bool aFlag ) { m_plotViaOnMaskLayer = aFlag; }
     bool        GetPlotViaOnMaskLayer() const { return m_plotViaOnMaskLayer; }
 
diff --git a/pcbnew/plot_board_layers.cpp b/pcbnew/plot_board_layers.cpp
index ed46a72aa..eb01f762f 100644
--- a/pcbnew/plot_board_layers.cpp
+++ b/pcbnew/plot_board_layers.cpp
@@ -1000,12 +1000,9 @@ static void initializePlotter( PLOTTER *aPlotter, BOARD * aBoard,
  */
 static void FillNegativeKnockout( PLOTTER *aPlotter, const EDA_RECT &aBbbox )
 {
-    const int margin = 5 * IU_PER_MM;   // Add a 5 mm margin around the board
     aPlotter->SetNegative( true );
     aPlotter->SetColor( WHITE );        // Which will be plotted as black
-    EDA_RECT area = aBbbox;
-    area.Inflate( margin );
-    aPlotter->Rect( area.GetOrigin(), area.GetEnd(), FILLED_SHAPE );
+    aPlotter->Rect( aBbbox.GetOrigin(), aBbbox.GetEnd(), FILLED_SHAPE );
     aPlotter->SetColor( BLACK );
 }
 
@@ -1127,6 +1124,8 @@ PLOTTER* StartPlotBoard( BOARD *aBoard, PCB_PLOT_PARAMS *aPlotOpts, int aLayer,
         if( aPlotOpts->GetNegative() )
         {
             EDA_RECT bbox = aBoard->ComputeBoundingBox();
+            // Add margin around the board
+            bbox.Inflate( aPlotOpts->GetNegativeMargin() );
             FillNegativeKnockout( plotter, bbox );
         }
 
