← Back to team overview

kicad-developers team mailing list archive

Re: [PATCH] Implement auto annotation on component/symbol placement.

 

Hi,
No problem, I just wanted to make sure I sent the message properly. Here's
a single squashed patch with all previous changes and these comments about
copying selection.

Thank you so much for your review.

On Mon, Oct 14, 2019 at 12:13 AM Ian McInerney <Ian.S.McInerney@xxxxxxxx>
wrote:

> Zifcani,
>
> Sorry for the delay in the response. Yes, you can just attach the new
> patches to an email and send it to the list. You can either squash them all
> into 1 patch, or create 2 patches (the first just the wxFormbuilder dialog
> class, so all the _base.fbp/cpp/h files, and the second the remaining
> files).
>
> In regard to the selection return type, it sounds like the sorting
> operation is invalidating an iterator held in another object for iterating
> over the selection, causing the hang/crash. I guess that for now it is fine
> to make these a full copy instead of a reference, but it would be good if
> you included a comment with them explaining why they are a copy instead of
> the reference. We can look into which iterators are being broken by the
> sort operation later (unless you want to do it now).
>
> -Ian
>
> On Fri, Oct 11, 2019 at 9:09 AM Zficani Zficani <zifcani@xxxxxxxxx> wrote:
>
>> Hi,
>> I see, I will send in new patches later today. Do I just send them in an
>> email like this?
>>
>> On Fri, 11 Oct 2019, 02:10 Seth Hillbrand, <seth@xxxxxxxxxxxxx> wrote:
>>
>>>
>>> On 2019-10-09 17:08, Zficani Zficani wrote:
>>>
>>> Hi,
>>> it's been about 10 days since my message so I just want to make sure I
>>> sent it properly and you noticed it because I'm not sure as I'd want to be
>>> able to actually use this feature soon because it's very useful for me.
>>>
>>> Thank you.
>>>
>>> On Sun, Sep 29, 2019 at 11:55 PM Zficani Zficani <zifcani@xxxxxxxxx>
>>> wrote:
>>>
>>>> Hi,
>>>> I reviewed your remarks and made appropriate changes but I have a few
>>>> questions.
>>>> Regarding your first remark, I had to make them non-reference because
>>>> the selection is sorted inplace which means that the original selection
>>>> will be modified which in turn make unhighlight hang/enter and infinite
>>>> loop when copying/duplicating or just unhighlighting multiple objects.
>>>> I'm not sure why exactly this is happening but it obviously has
>>>> something to do with the fact that selection is now sorted by
>>>> reference.
>>>>
>>>> I did change the code according to the second and third remark though
>>>> and will send in the patches later (it's currently available on
>>>>
>>>> https://github.com/Nufflee/kicad-source-mirror/tree/1335616-annotate-on-placement
>>>> if anyone wants to take a look).
>>>>
>>>>
>>>>
>>> Hi Zficani-
>>>
>>>
>>> I can't speak for others but I was waiting for either a patch with the
>>> revised code or a merge request on launchpad.  I like the idea of your
>>> feature and look forward to helping get it into merge shape.  I agree with
>>> Ian's comments and was hoping to see how you address them.
>>>
>>>
>>> Best-
>>>
>>> Seth
>>>
>>>
>>> Seth Hillbrand
>>>
>>> Chief Technologist
>>>
>>> KiCad Services Corporation
>>> Twitter [image: Twitter] <https://twitter.com/KiProEDA>   LinkedIn [image:
>>> LinkedIn] <https://www.linkedin.com/company/kicad/about>
>>> +1 530 302 5483 <+15303025483> | +1 212 603 9372 <+12126039372>
>>> www.kipro-pcb.com
>>> Davis, CA
>>>
>>
diff --git a/.gitignore b/.gitignore
index 619c7f963..f62e05e15 100644
--- a/.gitignore
+++ b/.gitignore
@@ -74,6 +74,7 @@ pcbnew/Info.plist
 .*.swp
 *~
 .idea
+.vscode
 .DS_Store
 *.png
 *.kiface
diff --git a/eeschema/CMakeLists.txt b/eeschema/CMakeLists.txt
index f22886414..86569a39d 100644
--- a/eeschema/CMakeLists.txt
+++ b/eeschema/CMakeLists.txt
@@ -97,6 +97,8 @@ set( EESCHEMA_DLGS
     dialogs/panel_eeschema_display_options_base.cpp
     dialogs/panel_eeschema_settings.cpp
     dialogs/panel_eeschema_settings_base.cpp
+    dialogs/panel_eeschema_annotation_options.cpp
+    dialogs/panel_eeschema_annotation_options_base.cpp
     dialogs/panel_libedit_settings.cpp
     dialogs/panel_libedit_settings_base.cpp
     dialogs/panel_sym_lib_table.cpp
diff --git a/eeschema/component_references_lister.cpp b/eeschema/component_references_lister.cpp
index 867fd18e3..503202ea3 100644
--- a/eeschema/component_references_lister.cpp
+++ b/eeschema/component_references_lister.cpp
@@ -112,21 +112,7 @@ bool SCH_REFERENCE_LIST::sortByRefAndValue( const SCH_REFERENCE& item1,
 bool SCH_REFERENCE_LIST::sortByReferenceOnly( const SCH_REFERENCE& item1,
                                               const SCH_REFERENCE& item2 )
 {
-    int             ii;
-
-    ii = UTIL::RefDesStringCompare( item1.GetRef(), item2.GetRef() );
-
-    if( ii == 0 )
-    {
-        ii = item1.m_RootCmp->GetField( VALUE )->GetText().CmpNoCase( item2.m_RootCmp->GetField( VALUE )->GetText() );
-    }
-
-    if( ii == 0 )
-    {
-        ii = item1.m_Unit - item2.m_Unit;
-    }
-
-    return ii < 0;
+    return item1.m_RootCmp->CompareRefDes(item2.m_RootCmp);
 }
 
 
diff --git a/eeschema/dialogs/panel_eeschema_annotation_options.cpp b/eeschema/dialogs/panel_eeschema_annotation_options.cpp
new file mode 100644
index 000000000..a2d0cf51c
--- /dev/null
+++ b/eeschema/dialogs/panel_eeschema_annotation_options.cpp
@@ -0,0 +1,118 @@
+/*
+ * This program source code file is part of KiCad, a free EDA CAD application.
+ *
+ * Copyright (C) 2019 Kicad Developers, see AUTHORS.txt for contributors.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, you may find one here:
+ * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
+ * or you may search the http://www.gnu.org website for the version 2 license,
+ * or you may write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ */
+
+#include <panel_eeschema_annotation_options.h>
+#include <sch_component.h>
+
+PANEL_EESCHEMA_ANNOTATION_OPTIONS::PANEL_EESCHEMA_ANNOTATION_OPTIONS( SCH_EDIT_FRAME* aFrame, wxWindow* aWindow )
+    : PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE( aWindow ),
+      m_frame( aFrame )
+{
+}
+
+bool PANEL_EESCHEMA_ANNOTATION_OPTIONS::TransferDataToWindow()
+{
+    bool enabled = m_frame->IsAutoAnnotationEnabled();
+
+    m_cbEnabled->SetValue( enabled );
+
+    if ( enabled )
+    {
+        ShowContent();
+    }
+    else
+    {
+        HideContent();
+    }
+
+    m_rbScope->SetSelection( m_frame->GetAutoAnnotationScopeOption() );
+
+    switch ( m_frame->GetAutoAnnotationAlgoOption() )
+    {
+        case INCREMENTAL_BY_REF:  m_rbFirstFree->SetValue( true );  break;
+        case SHEET_NUMBER_X_100:  m_rbSheetX100->SetValue( true );  break;
+        case SHEET_NUMBER_X_1000: m_rbSheetX1000->SetValue( true ); break;
+    }
+
+    m_textNumberAfter->SetValue( wxString::Format( "%d", m_frame->GetAutoAnnotationFirstFreeNumOption() ) );
+
+    return true;
+}
+
+bool PANEL_EESCHEMA_ANNOTATION_OPTIONS::TransferDataFromWindow()
+{
+    m_frame->SetIsAutoAnnotationEnabled( m_cbEnabled->GetValue() );
+
+    m_frame->SetAutoAnnotationScopeOption( m_rbScope->GetSelection() );
+
+    if ( m_rbFirstFree->GetValue() )
+    {
+        m_frame->SetAutoAnnotationAlgoOption( INCREMENTAL_BY_REF );
+    }
+    else if ( m_rbSheetX100->GetValue() )
+    {
+        m_frame->SetAutoAnnotationAlgoOption( SHEET_NUMBER_X_100 );
+    }
+    else if ( m_rbSheetX1000->GetValue() )
+    {
+        m_frame->SetAutoAnnotationAlgoOption( SHEET_NUMBER_X_1000 );
+    }
+
+    long numberAfter;
+
+    if ( m_textNumberAfter->GetValue().ToLong( &numberAfter ) )
+    {
+        m_frame->SetAutoAnnotationFirstFreeNumOption( numberAfter );
+    }
+
+    m_frame->SaveProjectSettings( false );
+
+    return true;
+}
+
+void PANEL_EESCHEMA_ANNOTATION_OPTIONS::OnEnableClick( wxCommandEvent& event )
+{
+    if( m_cbEnabled->IsChecked() )
+    {
+        ShowContent();
+    }
+    else
+    {
+        HideContent();
+    }
+}
+
+void PANEL_EESCHEMA_ANNOTATION_OPTIONS::ShowContent()
+{
+    m_separator->Show( true );
+    m_contentSizer->Show( true );
+
+    // Call Layout() to size all of the elements properly.
+    Layout();
+}
+
+void PANEL_EESCHEMA_ANNOTATION_OPTIONS::HideContent()
+{
+    m_separator->Show( false );
+    m_contentSizer->Show( false );
+}
\ No newline at end of file
diff --git a/eeschema/dialogs/panel_eeschema_annotation_options.h b/eeschema/dialogs/panel_eeschema_annotation_options.h
new file mode 100644
index 000000000..57d73b620
--- /dev/null
+++ b/eeschema/dialogs/panel_eeschema_annotation_options.h
@@ -0,0 +1,48 @@
+/*
+ * This program source code file is part of KiCad, a free EDA CAD application.
+ *
+ * Copyright (C) 2019 Kicad Developers, see AUTHORS.txt for contributors.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, you may find one here:
+ * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
+ * or you may search the http://www.gnu.org website for the version 2 license,
+ * or you may write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ */
+
+#ifndef KICAD_PANEL_EESCHEMA_ANNOTATION_OPTIONS_H
+#define KICAD_PANEL_EESCHEMA_ANNOTATION_OPTIONS_H
+
+#include "panel_eeschema_annotation_options_base.h"
+#include <sch_edit_frame.h>
+
+class PANEL_EESCHEMA_ANNOTATION_OPTIONS : public PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE
+{
+private:
+    SCH_EDIT_FRAME* m_frame;
+
+public:
+    PANEL_EESCHEMA_ANNOTATION_OPTIONS( SCH_EDIT_FRAME* aFrame, wxWindow* aWindow );
+
+    void OnEnableClick( wxCommandEvent& event ) override;
+
+private:
+    bool TransferDataFromWindow() override;
+    bool TransferDataToWindow() override;
+
+    void ShowContent();
+    void HideContent();
+};
+
+#endif //KICAD_PANEL_EESCHEMA_ANNOTATION_OPTIONS_H
\ No newline at end of file
diff --git a/eeschema/dialogs/panel_eeschema_annotation_options_base.cpp b/eeschema/dialogs/panel_eeschema_annotation_options_base.cpp
new file mode 100644
index 000000000..cd1363092
--- /dev/null
+++ b/eeschema/dialogs/panel_eeschema_annotation_options_base.cpp
@@ -0,0 +1,83 @@
+///////////////////////////////////////////////////////////////////////////
+// C++ code generated with wxFormBuilder (version Sep 10 2019)
+// http://www.wxformbuilder.org/
+//
+// PLEASE DO *NOT* EDIT THIS FILE!
+///////////////////////////////////////////////////////////////////////////
+
+#include "panel_eeschema_annotation_options_base.h"
+
+///////////////////////////////////////////////////////////////////////////
+
+PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE::PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxString& name ) : wxPanel( parent, id, pos, size, style, name )
+{
+	wxBoxSizer* bmainSizer;
+	bmainSizer = new wxBoxSizer( wxVERTICAL );
+
+	wxBoxSizer* bupperSizer;
+	bupperSizer = new wxBoxSizer( wxVERTICAL );
+
+	m_cbEnabled = new wxCheckBox( this, wxID_ANY, _("Annotate components on placement"), wxDefaultPosition, wxDefaultSize, 0 );
+	bupperSizer->Add( m_cbEnabled, 0, wxALL, 5 );
+
+	m_separator = new wxStaticLine( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL );
+	bupperSizer->Add( m_separator, 0, wxEXPAND | wxALL, 5 );
+
+	m_contentSizer = new wxFlexGridSizer( 1, 2, 0, 0 );
+	m_contentSizer->AddGrowableCol( 0 );
+	m_contentSizer->AddGrowableCol( 1 );
+	m_contentSizer->SetFlexibleDirection( wxBOTH );
+	m_contentSizer->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED );
+
+	wxString m_rbScopeChoices[] = { _("Use the current page only"), _("Use the entire schematic") };
+	int m_rbScopeNChoices = sizeof( m_rbScopeChoices ) / sizeof( wxString );
+	m_rbScope = new wxRadioBox( this, wxID_ANY, _("Scope:"), wxDefaultPosition, wxDefaultSize, m_rbScopeNChoices, m_rbScopeChoices, 1, wxRA_SPECIFY_COLS );
+	m_rbScope->SetSelection( 0 );
+	m_contentSizer->Add( m_rbScope, 0, wxALL|wxEXPAND, 5 );
+
+	wxStaticBoxSizer* sbSizer2;
+	sbSizer2 = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, _("Numbering:") ), wxVERTICAL );
+
+	wxGridBagSizer* gbSizer1;
+	gbSizer1 = new wxGridBagSizer( 0, 0 );
+	gbSizer1->SetFlexibleDirection( wxBOTH );
+	gbSizer1->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED );
+
+	m_rbFirstFree = new wxRadioButton( sbSizer2->GetStaticBox(), wxID_FIRST_FREE, _("Use first free number after:"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP );
+	gbSizer1->Add( m_rbFirstFree, wxGBPosition( 0, 0 ), wxGBSpan( 1, 1 ), wxTOP, 2 );
+
+	m_textNumberAfter = new wxTextCtrl( sbSizer2->GetStaticBox(), wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize( 60,-1 ), 0 );
+	gbSizer1->Add( m_textNumberAfter, wxGBPosition( 0, 1 ), wxGBSpan( 1, 1 ), wxLEFT, 2 );
+
+	m_rbSheetX100 = new wxRadioButton( sbSizer2->GetStaticBox(), wxID_SHEET_X_100, _("First free after sheet number X 100"), wxDefaultPosition, wxDefaultSize, 0 );
+	gbSizer1->Add( m_rbSheetX100, wxGBPosition( 1, 0 ), wxGBSpan( 1, 2 ), wxBOTTOM|wxTOP, 4 );
+
+	m_rbSheetX1000 = new wxRadioButton( sbSizer2->GetStaticBox(), wxID_SHEET_X_1000, _("First free after sheet number X 1000"), wxDefaultPosition, wxDefaultSize, 0 );
+	gbSizer1->Add( m_rbSheetX1000, wxGBPosition( 2, 0 ), wxGBSpan( 1, 2 ), wxBOTTOM|wxTOP, 2 );
+
+
+	sbSizer2->Add( gbSizer1, 1, wxEXPAND, 5 );
+
+
+	m_contentSizer->Add( sbSizer2, 1, wxALL|wxEXPAND, 5 );
+
+
+	bupperSizer->Add( m_contentSizer, 0, wxBOTTOM|wxEXPAND, 5 );
+
+
+	bmainSizer->Add( bupperSizer, 1, wxEXPAND|wxTOP|wxRIGHT|wxLEFT, 6 );
+
+
+	this->SetSizer( bmainSizer );
+	this->Layout();
+
+	// Connect Events
+	m_cbEnabled->Connect( wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler( PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE::OnEnableClick ), NULL, this );
+}
+
+PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE::~PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE()
+{
+	// Disconnect Events
+	m_cbEnabled->Disconnect( wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler( PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE::OnEnableClick ), NULL, this );
+
+}
diff --git a/eeschema/dialogs/panel_eeschema_annotation_options_base.fbp b/eeschema/dialogs/panel_eeschema_annotation_options_base.fbp
new file mode 100644
index 000000000..53c664cb8
--- /dev/null
+++ b/eeschema/dialogs/panel_eeschema_annotation_options_base.fbp
@@ -0,0 +1,579 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
+<wxFormBuilder_Project>
+    <FileVersion major="1" minor="15" />
+    <object class="Project" expanded="1">
+        <property name="class_decoration"></property>
+        <property name="code_generation">C++</property>
+        <property name="disconnect_events">1</property>
+        <property name="disconnect_mode">source_name</property>
+        <property name="disconnect_php_events">0</property>
+        <property name="disconnect_python_events">0</property>
+        <property name="embedded_files_path">res</property>
+        <property name="encoding">UTF-8</property>
+        <property name="event_generation">connect</property>
+        <property name="file">panel_eeschema_annotation_options_base</property>
+        <property name="first_id">1000</property>
+        <property name="help_provider">none</property>
+        <property name="image_path_wrapper_function_name"></property>
+        <property name="indent_with_spaces"></property>
+        <property name="internationalize">1</property>
+        <property name="name">PanelEeschemaAnnotationOptionsBase</property>
+        <property name="namespace"></property>
+        <property name="path">.</property>
+        <property name="precompiled_header"></property>
+        <property name="relative_path">1</property>
+        <property name="skip_lua_events">1</property>
+        <property name="skip_php_events">1</property>
+        <property name="skip_python_events">1</property>
+        <property name="ui_table">UI</property>
+        <property name="use_array_enum">0</property>
+        <property name="use_enum">0</property>
+        <property name="use_microsoft_bom">0</property>
+        <object class="Panel" expanded="1">
+            <property name="aui_managed">0</property>
+            <property name="aui_manager_style">wxAUI_MGR_DEFAULT</property>
+            <property name="bg"></property>
+            <property name="context_help"></property>
+            <property name="context_menu">1</property>
+            <property name="enabled">1</property>
+            <property name="event_handler">impl_virtual</property>
+            <property name="fg"></property>
+            <property name="font"></property>
+            <property name="hidden">0</property>
+            <property name="id">wxID_ANY</property>
+            <property name="maximum_size"></property>
+            <property name="minimum_size"></property>
+            <property name="name">PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE</property>
+            <property name="pos"></property>
+            <property name="size">500,156</property>
+            <property name="subclass">; ; forward_declare</property>
+            <property name="tooltip"></property>
+            <property name="window_extra_style"></property>
+            <property name="window_name"></property>
+            <property name="window_style">wxTAB_TRAVERSAL</property>
+            <object class="wxBoxSizer" expanded="0">
+                <property name="minimum_size"></property>
+                <property name="name">bmainSizer</property>
+                <property name="orient">wxVERTICAL</property>
+                <property name="permission">none</property>
+                <object class="sizeritem" expanded="0">
+                    <property name="border">6</property>
+                    <property name="flag">wxEXPAND|wxTOP|wxRIGHT|wxLEFT</property>
+                    <property name="proportion">1</property>
+                    <object class="wxBoxSizer" expanded="0">
+                        <property name="minimum_size"></property>
+                        <property name="name">bupperSizer</property>
+                        <property name="orient">wxVERTICAL</property>
+                        <property name="permission">none</property>
+                        <object class="sizeritem" expanded="0">
+                            <property name="border">5</property>
+                            <property name="flag">wxALL</property>
+                            <property name="proportion">0</property>
+                            <object class="wxCheckBox" 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="checked">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">Annotate components on placement</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_cbEnabled</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="validator_data_type"></property>
+                                <property name="validator_style">wxFILTER_NONE</property>
+                                <property name="validator_type">wxDefaultValidator</property>
+                                <property name="validator_variable"></property>
+                                <property name="window_extra_style"></property>
+                                <property name="window_name"></property>
+                                <property name="window_style"></property>
+                                <event name="OnCheckBox">OnEnableClick</event>
+                            </object>
+                        </object>
+                        <object class="sizeritem" expanded="0">
+                            <property name="border">5</property>
+                            <property name="flag">wxEXPAND | wxALL</property>
+                            <property name="proportion">0</property>
+                            <object class="wxStaticLine" 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="min_size"></property>
+                                <property name="minimize_button">0</property>
+                                <property name="minimum_size"></property>
+                                <property name="moveable">1</property>
+                                <property name="name">m_separator</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">wxLI_HORIZONTAL</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>
+                            </object>
+                        </object>
+                        <object class="sizeritem" expanded="0">
+                            <property name="border">5</property>
+                            <property name="flag">wxBOTTOM|wxEXPAND</property>
+                            <property name="proportion">0</property>
+                            <object class="wxFlexGridSizer" expanded="0">
+                                <property name="cols">2</property>
+                                <property name="flexible_direction">wxBOTH</property>
+                                <property name="growablecols">0,1</property>
+                                <property name="growablerows"></property>
+                                <property name="hgap">0</property>
+                                <property name="minimum_size"></property>
+                                <property name="name">m_contentSizer</property>
+                                <property name="non_flexible_grow_mode">wxFLEX_GROWMODE_SPECIFIED</property>
+                                <property name="permission">protected</property>
+                                <property name="rows">1</property>
+                                <property name="vgap">0</property>
+                                <object class="sizeritem" expanded="0">
+                                    <property name="border">5</property>
+                                    <property name="flag">wxALL|wxEXPAND</property>
+                                    <property name="proportion">0</property>
+                                    <object class="wxRadioBox" 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="choices">&quot;Use the entire schematic&quot; &quot;Use the current page only&quot;</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">Scope:</property>
+                                        <property name="majorDimension">1</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_rbScope</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="selection">0</property>
+                                        <property name="show">1</property>
+                                        <property name="size"></property>
+                                        <property name="style">wxRA_SPECIFY_COLS</property>
+                                        <property name="subclass">; forward_declare</property>
+                                        <property name="toolbar_pane">0</property>
+                                        <property name="tooltip"></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="window_extra_style"></property>
+                                        <property name="window_name"></property>
+                                        <property name="window_style"></property>
+                                    </object>
+                                </object>
+                                <object class="sizeritem" expanded="0">
+                                    <property name="border">5</property>
+                                    <property name="flag">wxALL|wxEXPAND</property>
+                                    <property name="proportion">1</property>
+                                    <object class="wxStaticBoxSizer" expanded="0">
+                                        <property name="id">wxID_ANY</property>
+                                        <property name="label">Numbering:</property>
+                                        <property name="minimum_size"></property>
+                                        <property name="name">sbSizer2</property>
+                                        <property name="orient">wxVERTICAL</property>
+                                        <property name="parent">1</property>
+                                        <property name="permission">none</property>
+                                        <object class="sizeritem" expanded="0">
+                                            <property name="border">5</property>
+                                            <property name="flag">wxEXPAND</property>
+                                            <property name="proportion">1</property>
+                                            <object class="wxGridBagSizer" expanded="0">
+                                                <property name="empty_cell_size"></property>
+                                                <property name="flexible_direction">wxBOTH</property>
+                                                <property name="growablecols"></property>
+                                                <property name="growablerows"></property>
+                                                <property name="hgap">0</property>
+                                                <property name="minimum_size"></property>
+                                                <property name="name">gbSizer1</property>
+                                                <property name="non_flexible_grow_mode">wxFLEX_GROWMODE_SPECIFIED</property>
+                                                <property name="permission">none</property>
+                                                <property name="vgap">0</property>
+                                                <object class="gbsizeritem" expanded="0">
+                                                    <property name="border">2</property>
+                                                    <property name="colspan">1</property>
+                                                    <property name="column">0</property>
+                                                    <property name="flag">wxTOP</property>
+                                                    <property name="row">0</property>
+                                                    <property name="rowspan">1</property>
+                                                    <object class="wxRadioButton" 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_FIRST_FREE</property>
+                                                        <property name="label">Use first free number after:</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_rbFirstFree</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">wxRB_GROUP</property>
+                                                        <property name="subclass">; forward_declare</property>
+                                                        <property name="toolbar_pane">0</property>
+                                                        <property name="tooltip"></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">0</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">2</property>
+                                                    <property name="colspan">1</property>
+                                                    <property name="column">1</property>
+                                                    <property name="flag">wxLEFT</property>
+                                                    <property name="row">0</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"></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_textNumberAfter</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">60,-1</property>
+                                                        <property name="style"></property>
+                                                        <property name="subclass">; forward_declare</property>
+                                                        <property name="toolbar_pane">0</property>
+                                                        <property name="tooltip"></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">4</property>
+                                                    <property name="colspan">2</property>
+                                                    <property name="column">0</property>
+                                                    <property name="flag">wxBOTTOM|wxTOP</property>
+                                                    <property name="row">1</property>
+                                                    <property name="rowspan">1</property>
+                                                    <object class="wxRadioButton" 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_SHEET_X_100</property>
+                                                        <property name="label">First free after sheet number X 100</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_rbSheetX100</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="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">0</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">2</property>
+                                                    <property name="colspan">2</property>
+                                                    <property name="column">0</property>
+                                                    <property name="flag">wxBOTTOM|wxTOP</property>
+                                                    <property name="row">2</property>
+                                                    <property name="rowspan">1</property>
+                                                    <object class="wxRadioButton" 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_SHEET_X_1000</property>
+                                                        <property name="label">First free after sheet number X 1000</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_rbSheetX1000</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="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">0</property>
+                                                        <property name="window_extra_style"></property>
+                                                        <property name="window_name"></property>
+                                                        <property name="window_style"></property>
+                                                    </object>
+                                                </object>
+                                            </object>
+                                        </object>
+                                    </object>
+                                </object>
+                            </object>
+                        </object>
+                    </object>
+                </object>
+            </object>
+        </object>
+    </object>
+</wxFormBuilder_Project>
diff --git a/eeschema/dialogs/panel_eeschema_annotation_options_base.h b/eeschema/dialogs/panel_eeschema_annotation_options_base.h
new file mode 100644
index 000000000..9d4e4a4dd
--- /dev/null
+++ b/eeschema/dialogs/panel_eeschema_annotation_options_base.h
@@ -0,0 +1,59 @@
+///////////////////////////////////////////////////////////////////////////
+// C++ code generated with wxFormBuilder (version Sep 10 2019)
+// http://www.wxformbuilder.org/
+//
+// PLEASE DO *NOT* EDIT THIS FILE!
+///////////////////////////////////////////////////////////////////////////
+
+#pragma once
+
+#include <wx/artprov.h>
+#include <wx/xrc/xmlres.h>
+#include <wx/intl.h>
+#include <wx/string.h>
+#include <wx/checkbox.h>
+#include <wx/gdicmn.h>
+#include <wx/font.h>
+#include <wx/colour.h>
+#include <wx/settings.h>
+#include <wx/statline.h>
+#include <wx/radiobox.h>
+#include <wx/radiobut.h>
+#include <wx/textctrl.h>
+#include <wx/gbsizer.h>
+#include <wx/sizer.h>
+#include <wx/statbox.h>
+#include <wx/panel.h>
+
+///////////////////////////////////////////////////////////////////////////
+
+#define wxID_FIRST_FREE 1000
+#define wxID_SHEET_X_100 1001
+#define wxID_SHEET_X_1000 1002
+
+///////////////////////////////////////////////////////////////////////////////
+/// Class PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE
+///////////////////////////////////////////////////////////////////////////////
+class PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE : public wxPanel
+{
+	private:
+
+	protected:
+		wxCheckBox* m_cbEnabled;
+		wxStaticLine* m_separator;
+		wxFlexGridSizer* m_contentSizer;
+		wxRadioBox* m_rbScope;
+		wxRadioButton* m_rbFirstFree;
+		wxTextCtrl* m_textNumberAfter;
+		wxRadioButton* m_rbSheetX100;
+		wxRadioButton* m_rbSheetX1000;
+
+		// Virtual event handlers, overide them in your derived class
+		virtual void OnEnableClick( wxCommandEvent& event ) { event.Skip(); }
+
+
+	public:
+
+		PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE( wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 500,156 ), long style = wxTAB_TRAVERSAL, const wxString& name = wxEmptyString );
+		~PANEL_EESCHEMA_ANNOTATION_OPTIONS_BASE();
+};
diff --git a/eeschema/eeschema_config.cpp b/eeschema/eeschema_config.cpp
index 527a40262..316643fe5 100644
--- a/eeschema/eeschema_config.cpp
+++ b/eeschema/eeschema_config.cpp
@@ -43,6 +43,7 @@
 #include <widgets/widget_eeschema_color_config.h>
 #include <widgets/symbol_tree_pane.h>
 #include <dialogs/panel_libedit_settings.h>
+#include <dialogs/panel_eeschema_annotation_options.h>
 #include <sch_painter.h>
 #include "sch_junction.h"
 #include "eeschema_id.h"
@@ -126,13 +127,13 @@ void SCH_EDIT_FRAME::InstallPreferences( PAGED_DIALOG* aParent,
     book->AddSubPage( new PANEL_EESCHEMA_COLOR_CONFIG( this, book ), _( "Colors" ) );
     book->AddSubPage( new PANEL_EESCHEMA_TEMPLATE_FIELDNAMES( this, book ),
                       _( "Field Name Templates" ) );
+    book->AddSubPage( new PANEL_EESCHEMA_ANNOTATION_OPTIONS( this, book ), _( "Annotation Options" ) );
 
     aHotkeysPanel->AddHotKeys( GetToolManager() );
 }
 
 
 PARAM_CFG_ARRAY& SCH_EDIT_FRAME::GetProjectFileParameters()
-
 {
     if( !m_projectFileParams.empty() )
         return m_projectFileParams;
@@ -153,6 +154,11 @@ PARAM_CFG_ARRAY& SCH_EDIT_FRAME::GetProjectFileParameters()
     m_projectFileParams.push_back( new PARAM_CFG_BOOL( wxT( "SpiceAjustPassiveValues" ),
                                             &m_spiceAjustPassiveValues, false ) );
 
+    m_projectFileParams.push_back( new PARAM_CFG_BOOL( wxT( "AutoAnnotationEnabled" ),      &m_autoAnnotateEnabled ) );
+    m_projectFileParams.push_back( new PARAM_CFG_INT(  wxT( "AutoAnnotationScope" ),        &m_autoAnnotateScopeOption, 0, 0, 1 ) );
+    m_projectFileParams.push_back( new PARAM_CFG_INT(  wxT( "AutoAnnotationAlgo" ),         (int*)&m_autoAnnotateAlgoOption, 0, 0, 2 ) );
+    m_projectFileParams.push_back( new PARAM_CFG_INT(  wxT( "AutoAnnotationFirstFreeNum" ), &m_autoAnnotateFirstFreeNumOption, 0 ) );
+
     m_projectFileParams.push_back( new PARAM_CFG_INT( wxT( "LabSize" ),
                                             &s_defaultTextSize, DEFAULT_SIZE_TEXT, 5, 1000 ) );
 
diff --git a/eeschema/sch_component.cpp b/eeschema/sch_component.cpp
index c4dc51085..5a4f6834c 100644
--- a/eeschema/sch_component.cpp
+++ b/eeschema/sch_component.cpp
@@ -44,6 +44,8 @@
 #include <netlist_object.h>
 #include <lib_item.h>
 #include <symbol_lib_table.h>
+#include <sch_reference_list.h>
+#include <refdes_utils.h>
 
 #include <dialogs/dialog_schematic_find.h>
 
@@ -55,7 +57,6 @@
 
 #include <trace_helpers.h>
 
-
 /**
  * Function toUTFTildaText
  * convert a wxString to UTF8 and replace any control characters with a ~,
@@ -718,6 +719,77 @@ void SCH_COMPONENT::SetRef( const SCH_SHEET_PATH* sheet, const wxString& ref )
     m_isInNetlist = ! ref.StartsWith( wxT( "#" ) );
 }
 
+void SCH_COMPONENT::Annotate( SCH_SHEET_PATH* aSheet, bool aEntireSchematic, ANNOTATE_OPTION_T aAlgoOption )
+{
+    SCH_REFERENCE_LIST references;
+    wxString prefix = GetPrefix();
+
+    // Build the sheet list.
+    SCH_SHEET_LIST sheets( g_RootSheet );
+
+    if ( aEntireSchematic && aAlgoOption == INCREMENTAL_BY_REF )
+    {
+        sheets.GetComponents( references, false );
+    }
+    else
+    {
+        aSheet->GetComponents( references );
+    }
+
+    int lastRefNum = 0;
+    int interval = 0;
+
+    if ( aAlgoOption != INCREMENTAL_BY_REF )
+    {
+        interval = ( aAlgoOption == SHEET_NUMBER_X_100 ? 100 : 1000 ) * aSheet->GetPageNumber() + 1;
+
+        lastRefNum++;
+    }
+
+    references.SortByReferenceOnly();
+
+    for ( unsigned i = 0; i < references.GetCount(); i++ )
+    {
+        if ( references[i].GetComp()->GetPrefix() == prefix )
+        {
+            long refNum;
+
+            references[i].Split();
+
+            if ( !references[i].GetRefNumber().ToLong( &refNum ) )
+            {
+                continue;
+            }
+
+            if ( refNum < interval )
+            {
+                continue;
+            }
+
+            // Check for a gap.
+            if ( ( refNum - interval ) - lastRefNum > 1 )
+            {
+                break;
+            }
+
+            if ( refNum > lastRefNum )
+            {
+                lastRefNum = refNum;
+            }
+        }
+    }
+
+    if ( lastRefNum < interval )
+    {
+        lastRefNum = interval;
+    }
+    else
+    {
+        lastRefNum++;
+    }
+
+    SetRef( aSheet, prefix << lastRefNum );
+}
 
 bool SCH_COMPONENT::IsAnnotated( const SCH_SHEET_PATH* aSheet )
 {
@@ -1973,3 +2045,20 @@ bool SCH_COMPONENT::ClearAllHighlightFlags()
 
     return retVal;
 }
+
+bool SCH_COMPONENT::CompareRefDes( SCH_COMPONENT* aOther )
+{
+    int ii = UTIL::RefDesStringCompare( GetRef( g_CurrentSheet ), aOther->GetRef( g_CurrentSheet ) );
+
+    if( ii == 0 )
+    {
+        ii = GetField( VALUE )->GetText().CmpNoCase( aOther->GetField( VALUE )->GetText() );
+    }
+
+    if( ii == 0 )
+    {
+        ii = GetUnit() - aOther->GetUnit();
+    }
+
+    return ii < 0;
+}
diff --git a/eeschema/sch_component.h b/eeschema/sch_component.h
index b64938a34..a08ce00e8 100644
--- a/eeschema/sch_component.h
+++ b/eeschema/sch_component.h
@@ -37,6 +37,7 @@
 #include <lib_item.h>
 #include <sch_pin.h>
 #include <sch_base_frame.h>
+#include <sch_edit_frame.h>
 
 class SCH_SCREEN;
 class SCH_SHEET_PATH;
@@ -508,6 +509,14 @@ public:
      */
     void SetRef( const SCH_SHEET_PATH* aSheet, const wxString& aReference );
 
+    /**
+     * Annotates the component with first available reference number for its reference prefix on a given sheet.
+     * @param aSheet is the sheet path to annotate the symbol on
+     * @param aEntireSchematic defines whether the scope of annotation is just the current page or the whole schematic
+     * @param aAlgoOption is the algorithm option used to annotate the symbol
+     */
+    void Annotate( SCH_SHEET_PATH* aSheet, bool aEntireSchematic, ANNOTATE_OPTION_T aAlgoOption );
+
     /**
      * Checks if the component has a valid annotation (reference) for the given sheet path
      * @param aSheet is the sheet path to test
@@ -643,6 +652,8 @@ public:
 
     void HighlightPin( LIB_PIN* aPin );
 
+    bool CompareRefDes( SCH_COMPONENT* aOther );
+
 private:
     bool doIsConnected( const wxPoint& aPosition ) const override;
 };
diff --git a/eeschema/sch_edit_frame.h b/eeschema/sch_edit_frame.h
index c7982050c..761abbc18 100644
--- a/eeschema/sch_edit_frame.h
+++ b/eeschema/sch_edit_frame.h
@@ -103,6 +103,11 @@ private:
     wxString                m_DefaultSchematicFileName;
     wxString                m_SelectedNetName;
 
+    bool                    m_autoAnnotateEnabled;
+    int                     m_autoAnnotateScopeOption;
+    ANNOTATE_OPTION_T       m_autoAnnotateAlgoOption;
+    int                     m_autoAnnotateFirstFreeNumOption;
+
     PARAM_CFG_ARRAY         m_projectFileParams;
     PARAM_CFG_ARRAY         m_configSettings;
     ERC_SETTINGS            m_ercSettings;
@@ -214,6 +219,18 @@ public:
     bool GetSpiceAjustPassiveValues() const { return m_spiceAjustPassiveValues; }
     void SetSpiceAdjustPassiveValues( bool aEnable ) { m_spiceAjustPassiveValues = aEnable; }
 
+    bool IsAutoAnnotationEnabled() { return m_autoAnnotateEnabled; }
+    void SetIsAutoAnnotationEnabled(bool value) { m_autoAnnotateEnabled = value; }
+
+    int GetAutoAnnotationScopeOption() { return m_autoAnnotateScopeOption; }
+    void SetAutoAnnotationScopeOption(int value) { m_autoAnnotateScopeOption = value; }
+
+    ANNOTATE_OPTION_T GetAutoAnnotationAlgoOption() { return m_autoAnnotateAlgoOption; }
+    void SetAutoAnnotationAlgoOption(ANNOTATE_OPTION_T value) { m_autoAnnotateAlgoOption = value; }
+
+    int GetAutoAnnotationFirstFreeNumOption() { return m_autoAnnotateFirstFreeNumOption; }
+    void SetAutoAnnotationFirstFreeNumOption(int value) { m_autoAnnotateFirstFreeNumOption = value; }
+
     /// accessor to the destination directory to use when generating plot files.
     const wxString& GetPlotDirectoryName() const { return m_plotDirectoryName; }
     void SetPlotDirectoryName( const wxString& aDirName ) { m_plotDirectoryName = aDirName; }
diff --git a/eeschema/tools/ee_selection.cpp b/eeschema/tools/ee_selection.cpp
index 84f418afb..5619670af 100644
--- a/eeschema/tools/ee_selection.cpp
+++ b/eeschema/tools/ee_selection.cpp
@@ -27,6 +27,8 @@
 #include <tools/ee_selection.h>
 #include <sch_item.h>
 #include <lib_item.h>
+#include <sch_component.h>
+#include <refdes_utils.h>
 
 EDA_ITEM* EE_SELECTION::GetTopLeftItem( bool onlyModules ) const
 {
@@ -56,3 +58,19 @@ EDA_ITEM* EE_SELECTION::GetTopLeftItem( bool onlyModules ) const
 
     return static_cast<EDA_ITEM*>( topLeftItem );
 }
+
+void EE_SELECTION::SortComponentsByRef()
+{
+    std::sort( begin(), end(), []( const EDA_ITEM* left, const EDA_ITEM* right )
+    {
+        if ( left->Type() == SCH_COMPONENT_T && right->Type() == SCH_COMPONENT_T )
+        {
+            SCH_COMPONENT* leftComponent  = (SCH_COMPONENT*) left;
+            SCH_COMPONENT* rightComponent = (SCH_COMPONENT*) right;
+
+            return leftComponent->CompareRefDes(rightComponent);
+        }
+
+        return false;
+    });
+}
\ No newline at end of file
diff --git a/eeschema/tools/ee_selection.h b/eeschema/tools/ee_selection.h
index 3b8d2f59d..da4a71c9a 100644
--- a/eeschema/tools/ee_selection.h
+++ b/eeschema/tools/ee_selection.h
@@ -32,6 +32,11 @@ class EE_SELECTION : public SELECTION
 {
 public:
     EDA_ITEM* GetTopLeftItem( bool onlyModules = false ) const override;
+
+    /**
+     * Sorts this selection by reference number of each SCH_COMPONENT (if any are present).
+     */
+    void SortComponentsByRef();
 };
 
 #endif  //  EE_SELECTION_H
\ No newline at end of file
diff --git a/eeschema/tools/sch_drawing_tools.cpp b/eeschema/tools/sch_drawing_tools.cpp
index e2945d54a..b3d8234fc 100644
--- a/eeschema/tools/sch_drawing_tools.cpp
+++ b/eeschema/tools/sch_drawing_tools.cpp
@@ -188,6 +188,11 @@ int SCH_DRAWING_TOOLS::PlaceComponent(  const TOOL_EVENT& aEvent  )
             }
             else
             {
+                if ( m_frame->IsAutoAnnotationEnabled() )
+                {
+                    component->Annotate( g_CurrentSheet, m_frame->GetAutoAnnotationScopeOption(), m_frame->GetAutoAnnotationAlgoOption() );
+                }
+
                 m_frame->AddItemToScreenAndUndoList( component );
                 component = nullptr;
 
@@ -786,7 +791,7 @@ int SCH_DRAWING_TOOLS::DrawSheet( const TOOL_EVENT& aEvent )
                 m_frame->AddItemToScreenAndUndoList( sheet );
                 m_selectionTool->AddItemToSel( sheet );
             }
-            else 
+            else
             {
                 delete sheet;
             }
diff --git a/eeschema/tools/sch_edit_tool.cpp b/eeschema/tools/sch_edit_tool.cpp
index 86899bea5..1aa7f9336 100644
--- a/eeschema/tools/sch_edit_tool.cpp
+++ b/eeschema/tools/sch_edit_tool.cpp
@@ -716,7 +716,9 @@ static KICAD_T duplicatableItems[] =
 
 int SCH_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent )
 {
-    EE_SELECTION& selection = m_selectionTool->RequestSelection( duplicatableItems );
+    // Selection has to be copied here because `SortComponentsByRef` invalidates
+    // some iterators in another object.
+    EE_SELECTION selection = m_selectionTool->RequestSelection( duplicatableItems );
 
     if( selection.GetSize() == 0 )
         return 0;
@@ -732,6 +734,11 @@ int SCH_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent )
     bool copiedSheets = false;
     SCH_SHEET_LIST initial_sheetpathList( g_RootSheet );
 
+    if ( m_frame->IsAutoAnnotationEnabled() )
+    {
+        selection.SortComponentsByRef();
+    }
+
     for( unsigned ii = 0; ii < selection.GetSize(); ++ii )
     {
         SCH_ITEM* oldItem = static_cast<SCH_ITEM*>( selection.GetItem( ii ) );
@@ -777,7 +784,15 @@ int SCH_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent )
             SCH_COMPONENT* component = (SCH_COMPONENT*) newItem;
 
             component->SetTimeStamp( GetNewTimeStamp() );
-            component->ClearAnnotation( NULL );
+
+            if ( m_frame->IsAutoAnnotationEnabled() )
+            {
+                component->Annotate( g_CurrentSheet, m_frame->GetAutoAnnotationScopeOption(), m_frame->GetAutoAnnotationAlgoOption() );
+            }
+            else
+            {
+                component->ClearAnnotation( NULL );
+            }
 
             component->SetParent( m_frame->GetScreen() );
             m_frame->AddToScreen( component );
diff --git a/eeschema/tools/sch_editor_control.cpp b/eeschema/tools/sch_editor_control.cpp
index 23cb07db3..53760bb59 100644
--- a/eeschema/tools/sch_editor_control.cpp
+++ b/eeschema/tools/sch_editor_control.cpp
@@ -52,6 +52,7 @@
 #include <invoke_sch_dialog.h>
 #include <dialogs/dialog_paste_special.h>
 #include <netlist_exporters/netlist_exporter_pspice.h>
+#include <refdes_utils.h>
 
 int SCH_EDITOR_CONTROL::New( const TOOL_EVENT& aEvent )
 {
@@ -871,7 +872,9 @@ int SCH_EDITOR_CONTROL::Redo( const TOOL_EVENT& aEvent )
 bool SCH_EDITOR_CONTROL::doCopy()
 {
     EE_SELECTION_TOOL* selTool = m_toolMgr->GetTool<EE_SELECTION_TOOL>();
-    EE_SELECTION&      selection = selTool->RequestSelection();
+    // Selection has to be copied here because `SortComponentsByRef` invalidates
+    // some iterators in another object.
+    EE_SELECTION       selection = selTool->RequestSelection();
 
     if( !selection.GetSize() )
         return false;
@@ -890,6 +893,11 @@ bool SCH_EDITOR_CONTROL::doCopy()
     STRING_FORMATTER formatter;
     SCH_LEGACY_PLUGIN plugin;
 
+    if ( m_frame->IsAutoAnnotationEnabled() )
+    {
+        selection.SortComponentsByRef();
+    }
+
     plugin.Format( &selection, &formatter );
 
     return m_toolMgr->SaveClipboard( formatter.GetString() );
@@ -1057,13 +1065,18 @@ int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent )
         {
             SCH_COMPONENT* component = (SCH_COMPONENT*) item;
 
-            if( dropAnnotations )
+            if( dropAnnotations || m_frame->IsAutoAnnotationEnabled() )
             {
+                // preserve the selected unit
+                int unit = component->GetUnit();
+
+                if ( m_frame->IsAutoAnnotationEnabled() )
+                    component->Annotate( g_CurrentSheet, m_frame->GetAutoAnnotationScopeOption(), m_frame->GetAutoAnnotationAlgoOption() );
+                else
+                    component->ClearAnnotation( nullptr );
+
                 component->SetTimeStamp( GetNewTimeStamp() );
 
-                // clear the annotation, but preserve the selected unit
-                int unit = component->GetUnit();
-                component->ClearAnnotation( nullptr );
                 component->SetUnit( unit );
             }
 

Follow ups

References