From b525e3be551584ecc7f90afce09d2ce6b412841c Mon Sep 17 00:00:00 2001
From: Lorenzo Marcantonio <l.marcantonio@logossrl.com>
Date: Tue, 9 Apr 2013 18:00:46 +0200
Subject: [PATCH] Factored out text anchor drawing More layer classification
 cleanup

---
 common/class_layer_box_selector.cpp           |  4 +-
 common/dsnlexer.cpp                           |  2 +-
 common/eda_text.cpp                           | 18 ++-----
 common/gr_basic.cpp                           | 13 +++++
 common/wxwineda.cpp                           |  1 +
 cvpcb/cvpcb.h                                 |  1 -
 gerbview/class_gbr_layer_box_selector.cpp     |  8 +--
 gerbview/class_gbr_layer_box_selector.h       |  6 +--
 gerbview/class_gbr_layout.cpp                 |  6 +--
 gerbview/class_gbr_layout.h                   |  4 +-
 .../dialogs/dialog_print_using_printer.cpp    |  2 +-
 gerbview/export_to_pcbnew.cpp                 |  2 +-
 gerbview/gerbview_frame.cpp                   |  8 +--
 gerbview/gerbview_frame.h                     |  4 +-
 gerbview/readgerb.cpp                         |  1 +
 gerbview/select_layers_to_pcb.cpp             |  4 +-
 include/class_board_design_settings.h         | 20 +++-----
 include/class_layer_box_selector.h            |  8 +--
 include/drawtxt.h                             |  3 ++
 include/eda_text.h                            |  1 +
 include/gr_basic.h                            |  3 ++
 include/layers_id_colors_and_visibility.h     | 51 ++++++++++++-------
 kicad/prjconfig.cpp                           |  1 +
 pcbnew/basepcbframe.cpp                       |  2 +-
 pcbnew/class_board.cpp                        | 38 +++++++-------
 pcbnew/class_board.h                          | 34 ++++++-------
 pcbnew/class_board_design_settings.cpp        | 22 ++------
 pcbnew/class_marker_pcb.cpp                   |  2 +-
 pcbnew/class_module.cpp                       | 14 ++---
 pcbnew/class_pad_draw_functions.cpp           |  2 +-
 pcbnew/class_pcb_layer_box_selector.cpp       | 12 ++---
 pcbnew/class_pcb_layer_box_selector.h         |  6 +--
 pcbnew/class_pcb_layer_widget.cpp             | 44 ++++++++--------
 pcbnew/class_text_mod.cpp                     | 10 +---
 pcbnew/class_track.cpp                        | 17 ++-----
 .../dialog_graphic_item_properties.cpp        |  6 ++-
 ...og_graphic_item_properties_for_Modedit.cpp |  6 +--
 pcbnew/dialogs/dialog_layers_setup.cpp        |  2 +-
 pcbnew/eagle_plugin.cpp                       | 18 +++----
 pcbnew/edgemod.cpp                            |  2 +-
 pcbnew/edit.cpp                               |  2 +-
 pcbnew/onleftclick.cpp                        |  6 +--
 pcbnew/onrightclick.cpp                       |  2 +-
 pcbnew/pcad2kicadpcb_plugin/pcb_arc.cpp       |  2 +-
 pcbnew/pcad2kicadpcb_plugin/pcb_line.cpp      |  4 +-
 pcbnew/pcad2kicadpcb_plugin/pcb_pad.cpp       |  2 +-
 pcbnew/pcbnew.h                               |  3 +-
 pcbnew/swap_layers.cpp                        |  2 +-
 pcbnew/zones_test_and_combine_areas.cpp       |  2 +-
 49 files changed, 210 insertions(+), 223 deletions(-)

diff --git a/common/class_layer_box_selector.cpp b/common/class_layer_box_selector.cpp
index 3ec360025b..b273240118 100644
--- a/common/class_layer_box_selector.cpp
+++ b/common/class_layer_box_selector.cpp
@@ -106,14 +106,14 @@ void LAYER_BOX_SELECTOR::ResyncBitmapOnly()
 }
 
 
-void LAYER_BOX_SELECTOR::SetBitmapLayer( wxBitmap& aLayerbmp, LAYER_NUM aLayerIndex )
+void LAYER_BOX_SELECTOR::SetBitmapLayer( wxBitmap& aLayerbmp, LAYER_NUM aLayer )
 {
     wxMemoryDC bmpDC;
     wxBrush    brush;
 
     // Prepare Bitmap
     bmpDC.SelectObject( aLayerbmp );
-    brush.SetColour( MakeColour( GetLayerColor( aLayerIndex ) ) );
+    brush.SetColour( MakeColour( GetLayerColor( aLayer ) ) );
     brush.SetStyle( wxSOLID );
 
     bmpDC.SetBrush( brush );
diff --git a/common/dsnlexer.cpp b/common/dsnlexer.cpp
index b2eb6880c9..9efadcecca 100644
--- a/common/dsnlexer.cpp
+++ b/common/dsnlexer.cpp
@@ -29,7 +29,7 @@
 #include <cstdlib>         // bsearch()
 #include <cctype>
 
-
+#include <macros.h>
 #include <dsnlexer.h>
 
 //#include "fctsys.h"
diff --git a/common/eda_text.cpp b/common/eda_text.cpp
index 0f59228954..6e2c70a0fe 100644
--- a/common/eda_text.cpp
+++ b/common/eda_text.cpp
@@ -269,22 +269,12 @@ void EDA_TEXT::DrawOneLineOfText( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
     if( aDrawMode != -1 )
         GRSetDrawMode( aDC, aDrawMode );
 
-    /* Draw text anchor, if allowed */
+    // Draw text anchor, if requested
     if( aAnchor_color != UNSPECIFIED_COLOR )
     {
-
-        int anchor_size = aDC->DeviceToLogicalXRel( 2 );
-
-        aAnchor_color = (EDA_COLOR_T) ( aAnchor_color & MASKCOLOR );
-
-        int cX = aPos.x + aOffset.x;
-        int cY = aPos.y + aOffset.y;
-
-        GRLine( aPanel->GetClipBox(), aDC, cX - anchor_size, cY,
-                cX + anchor_size, cY, 0, aAnchor_color );
-
-        GRLine( aPanel->GetClipBox(), aDC, cX, cY - anchor_size,
-                cX, cY + anchor_size, 0, aAnchor_color );
+        GRDrawAnchor( aPanel->GetClipBox(), aDC,
+                      aPos.x + aOffset.x, aPos.y + aOffset.y,
+                      DIM_ANCRE_TEXTE, aAnchor_color );
     }
 
     if( aFillMode == SKETCH )
diff --git a/common/gr_basic.cpp b/common/gr_basic.cpp
index 9ed374f4be..9ff0cd4f3e 100644
--- a/common/gr_basic.cpp
+++ b/common/gr_basic.cpp
@@ -1551,3 +1551,16 @@ EDA_COLOR_T ColorFindNearest( const wxColour &aColor )
 
     return candidate;
 }
+
+void GRDrawAnchor( EDA_RECT *aClipBox, wxDC *aDC, int x, int y, 
+                   int aSize, EDA_COLOR_T aColor )
+{
+        int anchor_size = aDC->DeviceToLogicalXRel( aSize );
+
+        GRLine( aClipBox, aDC,
+                x - anchor_size, y,
+                x + anchor_size, y, 0, aColor );
+        GRLine( aClipBox, aDC,
+                x, y - anchor_size,
+                x, y + anchor_size, 0, aColor );
+}
diff --git a/common/wxwineda.cpp b/common/wxwineda.cpp
index 93192e28db..4a92cc1b7a 100644
--- a/common/wxwineda.cpp
+++ b/common/wxwineda.cpp
@@ -30,6 +30,7 @@
 #include <wxstruct.h>
 #include <dialog_helpers.h>
 #include <base_units.h>
+#include <macros.h>
 
 
 /*******************************************************/
diff --git a/cvpcb/cvpcb.h b/cvpcb/cvpcb.h
index 5399a555e1..08b8b6e209 100644
--- a/cvpcb/cvpcb.h
+++ b/cvpcb/cvpcb.h
@@ -28,7 +28,6 @@ typedef boost::ptr_vector< COMPONENT_INFO > COMPONENT_LIST;
 extern const wxString FootprintAliasFileExtension;
 extern const wxString RetroFileExtension;
 
-extern const wxString RetroFileWildcard;
 extern const wxString FootprintAliasFileWildcard;
 
 extern const wxString titleLibLoadError;
diff --git a/gerbview/class_gbr_layer_box_selector.cpp b/gerbview/class_gbr_layer_box_selector.cpp
index 038aac142f..681c9b75ac 100644
--- a/gerbview/class_gbr_layer_box_selector.cpp
+++ b/gerbview/class_gbr_layer_box_selector.cpp
@@ -65,18 +65,18 @@ void GBR_LAYER_BOX_SELECTOR::Resync()
 
 
 // Returns a color index from the layer id
-EDA_COLOR_T GBR_LAYER_BOX_SELECTOR::GetLayerColor( LAYER_NUM aLayerIndex ) const
+EDA_COLOR_T GBR_LAYER_BOX_SELECTOR::GetLayerColor( LAYER_NUM aLayer ) const
 {
     GERBVIEW_FRAME* frame = (GERBVIEW_FRAME*) GetParent()->GetParent();
 
-    return frame->GetLayerColor( aLayerIndex );
+    return frame->GetLayerColor( aLayer );
 }
 
 
 // Returns the name of the layer id
-wxString GBR_LAYER_BOX_SELECTOR::GetLayerName( LAYER_NUM aLayerIndex ) const
+wxString GBR_LAYER_BOX_SELECTOR::GetLayerName( LAYER_NUM aLayer ) const
 {
     wxString name;
-    name.Printf( _( "Layer %d" ), aLayerIndex + 1 );
+    name.Printf( _( "Layer %d" ), aLayer + 1 );
     return name;
 }
diff --git a/gerbview/class_gbr_layer_box_selector.h b/gerbview/class_gbr_layer_box_selector.h
index 0ba19a2647..b6e4d8ec64 100644
--- a/gerbview/class_gbr_layer_box_selector.h
+++ b/gerbview/class_gbr_layer_box_selector.h
@@ -37,15 +37,15 @@ public:
 
     // Returns a color index from the layer id
     // Virtual function
-    EDA_COLOR_T GetLayerColor( LAYER_NUM aLayerIndex ) const;
+    EDA_COLOR_T GetLayerColor( LAYER_NUM aLayer ) const;
 
     // Returns true if the layer id is enabled (i.e. is it should be displayed)
     // Virtual function
-    bool IsLayerEnabled( LAYER_NUM aLayerIndex ) const { return true; };
+    bool IsLayerEnabled( LAYER_NUM aLayer ) const { return true; };
 
     // Returns the name of the layer id
     // Virtual function
-    wxString GetLayerName( LAYER_NUM aLayerIndex ) const;
+    wxString GetLayerName( LAYER_NUM aLayer ) const;
 };
 
 #endif //CLASS_GBR_LAYER_BOX_SELECTOR_H
diff --git a/gerbview/class_gbr_layout.cpp b/gerbview/class_gbr_layout.cpp
index 345722f2f5..ad475e07f6 100644
--- a/gerbview/class_gbr_layout.cpp
+++ b/gerbview/class_gbr_layout.cpp
@@ -24,12 +24,12 @@ GBR_LAYOUT::~GBR_LAYOUT()
 
 /* Function IsLayerVisible
  * tests whether a given layer is visible
- * param aLayerIndex = The index of the layer to be tested
+ * param aLayer = The layer to be tested
  * return bool - true if the layer is visible.
  */
-bool GBR_LAYOUT::IsLayerVisible( LAYER_NUM aLayerIndex ) const
+bool GBR_LAYOUT::IsLayerVisible( LAYER_NUM aLayer ) const
 {
-    return m_printLayersMask & GetLayerMask( aLayerIndex );
+    return m_printLayersMask & GetLayerMask( aLayer );
 }
 
 
diff --git a/gerbview/class_gbr_layout.h b/gerbview/class_gbr_layout.h
index cc497703b5..ea16415dff 100644
--- a/gerbview/class_gbr_layout.h
+++ b/gerbview/class_gbr_layout.h
@@ -99,10 +99,10 @@ public:
     /**
      * Function IsLayerVisible
      * tests whether a given layer is visible
-     * @param aLayerIndex = The index of the layer to be tested
+     * @param aLayer = The layer to be tested
      * @return bool - true if the layer is visible.
      */
-    bool    IsLayerVisible( LAYER_NUM aLayerIndex ) const;
+    bool    IsLayerVisible( LAYER_NUM aLayer ) const;
 
 #if defined(DEBUG)
     void    Show( int nestLevel, std::ostream& os ) const;  // overload
diff --git a/gerbview/dialogs/dialog_print_using_printer.cpp b/gerbview/dialogs/dialog_print_using_printer.cpp
index 31d21469ad..e49e2bdf6c 100644
--- a/gerbview/dialogs/dialog_print_using_printer.cpp
+++ b/gerbview/dialogs/dialog_print_using_printer.cpp
@@ -139,7 +139,7 @@ void DIALOG_PRINT_USING_PRINTER::InitValues( )
 
     layer_max = NB_LAYERS;
     // Create layer list
-    for(LAYER_NUM ii = FIRST_LAYER; ii < layer_max; ++ii )
+    for( LAYER_NUM ii = FIRST_LAYER; ii < layer_max; ++ii )
     {
         LAYER_MSK mask = GetLayerMask( ii );
         msg = _( "Layer" );
diff --git a/gerbview/export_to_pcbnew.cpp b/gerbview/export_to_pcbnew.cpp
index 24dda6bbbb..e2635cd59b 100644
--- a/gerbview/export_to_pcbnew.cpp
+++ b/gerbview/export_to_pcbnew.cpp
@@ -195,7 +195,7 @@ bool GBR_TO_PCB_EXPORTER::ExportPcb( LAYER_NUM* LayerLookUpTable )
         LAYER_NUM layer = gerb_item->GetLayer();
         LAYER_NUM pcb_layer_number = LayerLookUpTable[layer];
 
-        if( pcb_layer_number < FIRST_LAYER || pcb_layer_number > LAST_NON_COPPER_LAYER )
+        if( !IsPcbLayer( pcb_layer_number ) )
             continue;
 
         if( pcb_layer_number > LAST_COPPER_LAYER )
diff --git a/gerbview/gerbview_frame.cpp b/gerbview/gerbview_frame.cpp
index ab0c0e8217..b5d1dff3ed 100644
--- a/gerbview/gerbview_frame.cpp
+++ b/gerbview/gerbview_frame.cpp
@@ -558,15 +558,15 @@ void GERBVIEW_FRAME::SetVisibleLayers( LAYER_MSK aLayerMask )
 /**
  * Function IsLayerVisible
  * tests whether a given layer is visible
- * @param aLayerIndex = The index of the layer to be tested
+ * @param aLayer = The layer to be tested
  * @return bool - true if the layer is visible.
  */
-bool GERBVIEW_FRAME::IsLayerVisible( LAYER_NUM aLayerIndex ) const
+bool GERBVIEW_FRAME::IsLayerVisible( LAYER_NUM aLayer ) const
 {
     if( ! m_DisplayOptions.m_IsPrinting )
-        return m_LayersManager->IsLayerVisible( aLayerIndex );
+        return m_LayersManager->IsLayerVisible( aLayer );
     else
-        return GetLayout()->IsLayerVisible( aLayerIndex );
+        return GetLayout()->IsLayerVisible( aLayer );
 }
 
 
diff --git a/gerbview/gerbview_frame.h b/gerbview/gerbview_frame.h
index 36ccf8e7e3..ba1f079c9d 100644
--- a/gerbview/gerbview_frame.h
+++ b/gerbview/gerbview_frame.h
@@ -313,10 +313,10 @@ public:
     /**
      * Function IsLayerVisible
      * tests whether a given layer is visible
-     * @param aLayerIndex = The index of the layer to be tested
+     * @param aLayer = The layer to be tested
      * @return bool - true if the layer is visible.
      */
-    bool    IsLayerVisible( LAYER_NUM aLayerIndex ) const;
+    bool    IsLayerVisible( LAYER_NUM aLayer ) const;
 
     /**
      * Function GetVisibleElementColor
diff --git a/gerbview/readgerb.cpp b/gerbview/readgerb.cpp
index 45f0541b32..0fffdc9328 100644
--- a/gerbview/readgerb.cpp
+++ b/gerbview/readgerb.cpp
@@ -11,6 +11,7 @@
 #include <class_GERBER.h>
 
 #include <html_messagebox.h>
+#include <macros.h>
 
 /* Read a gerber file, RS274D or RS274X format.
  */
diff --git a/gerbview/select_layers_to_pcb.cpp b/gerbview/select_layers_to_pcb.cpp
index def5128ef1..b9f4158982 100644
--- a/gerbview/select_layers_to_pcb.cpp
+++ b/gerbview/select_layers_to_pcb.cpp
@@ -360,12 +360,12 @@ void LAYERS_MAP_DIALOG::OnSelectLayer( wxCommandEvent& event )
     }
 
     LAYER_NUM jj = m_layersLookUpTable[m_buttonTable[ii]];
-    if( ( jj < FIRST_LAYER ) || ( jj > NB_LAYERS ) )
+    if( !IsValidLayer( jj ) )
         jj = LAYER_N_BACK;  // (Defaults to "Copper" layer.)
 
     jj = m_Parent->SelectPCBLayer( jj, m_exportBoardCopperLayersCount, true );
 
-    if( ( jj < FIRST_LAYER ) || ( jj > NB_LAYERS ) )
+    if( !IsValidLayer( jj ) )
         return;
 
     if( jj != m_layersLookUpTable[m_buttonTable[ii]] )
diff --git a/include/class_board_design_settings.h b/include/class_board_design_settings.h
index 3fed1d53c5..0eedc23d22 100644
--- a/include/class_board_design_settings.h
+++ b/include/class_board_design_settings.h
@@ -75,26 +75,22 @@ public:
     /**
      * Function IsLayerVisible
      * tests whether a given layer is visible
-     * @param aLayerIndex = The index of the layer to be tested
+     * @param aLayer = The layer to be tested
      * @return bool - true if the layer is visible.
      */
-    bool IsLayerVisible( LAYER_NUM aLayerIndex ) const
+    bool IsLayerVisible( LAYER_NUM aLayer ) const
     {
-        // @@IMB: Altough Pcbnew uses only 29, GerbView uses all 32 layers
-        if( aLayerIndex < FIRST_LAYER || aLayerIndex >= NB_LAYERS )
-            return false;
-
         // If a layer is disabled, it is automatically invisible
-        return bool( m_VisibleLayers & m_EnabledLayers & GetLayerMask( aLayerIndex ) );
+        return m_VisibleLayers & m_EnabledLayers & GetLayerMask( aLayer );
     }
 
     /**
      * Function SetLayerVisibility
      * changes the visibility of a given layer
-     * @param aLayerIndex = The index of the layer to be changed
+     * @param aLayer = The layer to be changed
      * @param aNewState = The new visibility state of the layer
      */
-    void SetLayerVisibility( LAYER_NUM aLayerIndex, bool aNewState );
+    void SetLayerVisibility( LAYER_NUM aLayer, bool aNewState );
 
     /**
      * Function GetVisibleElements
@@ -158,12 +154,12 @@ public:
     /**
      * Function IsLayerEnabled
      * tests whether a given layer is enabled
-     * @param aLayerIndex = The index of the layer to be tested
+     * @param aLayer = The of the layer to be tested
      * @return bool - true if the layer is enabled
      */
-    bool IsLayerEnabled( LAYER_NUM aLayerIndex ) const
+    bool IsLayerEnabled( LAYER_NUM aLayer ) const
     {
-        return bool( m_EnabledLayers & GetLayerMask( aLayerIndex ) );
+        return m_EnabledLayers & GetLayerMask( aLayer );
     }
 
     /**
diff --git a/include/class_layer_box_selector.h b/include/class_layer_box_selector.h
index e7248584aa..e313b6242a 100644
--- a/include/class_layer_box_selector.h
+++ b/include/class_layer_box_selector.h
@@ -35,15 +35,15 @@ public:
 
     // Returns a color index from the layer id
     // Virtual function because GerbView uses its own functions in a derived class
-    virtual EDA_COLOR_T GetLayerColor( LAYER_NUM aLayerIndex ) const = 0;
+    virtual EDA_COLOR_T GetLayerColor( LAYER_NUM aLayer ) const = 0;
 
     // Returns the name of the layer id
     // Virtual pure function because GerbView uses its own functions in a derived class
-    virtual wxString GetLayerName( LAYER_NUM aLayerIndex ) const = 0;
+    virtual wxString GetLayerName( LAYER_NUM aLayer ) const = 0;
 
     // Returns true if the layer id is enabled (i.e. is it should be displayed)
     // Virtual function pure because GerbView uses its own functions in a derived class
-    virtual bool IsLayerEnabled( LAYER_NUM aLayerIndex ) const = 0;
+    virtual bool IsLayerEnabled( LAYER_NUM aLayer ) const = 0;
 
    // Get Current Item #
     int GetChoice();
@@ -66,7 +66,7 @@ public:
 
 protected:
    // Fills the layer bitmap aLayerbmp with the layer color
-    void SetBitmapLayer( wxBitmap& aLayerbmp, LAYER_NUM aLayerIndex );
+    void SetBitmapLayer( wxBitmap& aLayerbmp, LAYER_NUM aLayer );
 };
 
 #define DECLARE_LAYERS_HOTKEY(list) int list[NB_LAYERS] = \
diff --git a/include/drawtxt.h b/include/drawtxt.h
index 53ef4ddea8..43c638e39e 100644
--- a/include/drawtxt.h
+++ b/include/drawtxt.h
@@ -10,6 +10,9 @@
 #include <base_struct.h>
 #include <eda_text.h>               // EDA_TEXT_HJUSTIFY_T and EDA_TEXT_VJUSTIFY_T
 
+/// Minimum dimension in pixel for drawing text
+#define MIN_TEXT_SIZE   5
+
 class EDA_DRAW_PANEL;
 class PLOTTER;
 
diff --git a/include/eda_text.h b/include/eda_text.h
index d620d8d093..3ceb8da49e 100644
--- a/include/eda_text.h
+++ b/include/eda_text.h
@@ -62,6 +62,7 @@ enum EDA_DRAW_MODE_T {
 
 #define DEFAULT_SIZE_TEXT 60    /* default text height (in mils or 1/1000") */
 #define TEXT_NO_VISIBLE   1     //< EDA_TEXT::m_Attribut(e?) visibility flag.
+#define DIM_ANCRE_TEXTE  2      // Anchor size for text
 
 
 /**
diff --git a/include/gr_basic.h b/include/gr_basic.h
index e8d290bb6e..207c63447d 100644
--- a/include/gr_basic.h
+++ b/include/gr_basic.h
@@ -260,4 +260,7 @@ void GRSFilledRect( EDA_RECT* ClipBox, wxDC* DC, int x1, int y1,
 void GRLineArray(  EDA_RECT* aClipBox, wxDC* aDC,std::vector<wxPoint>& aLines,
                    int aWidth, EDA_COLOR_T aColor );
 
+void GRDrawAnchor( EDA_RECT* aClipBox, wxDC *aDC, int x, int y, int aSize, 
+                   EDA_COLOR_T aColor );
+
 #endif      /* define GR_BASIC */
diff --git a/include/layers_id_colors_and_visibility.h b/include/layers_id_colors_and_visibility.h
index c7dccae948..1dd41130f0 100644
--- a/include/layers_id_colors_and_visibility.h
+++ b/include/layers_id_colors_and_visibility.h
@@ -216,39 +216,52 @@ enum PCB_VISIBLE
     END_PCB_VISIBLE_LIST  // sentinel
 };
 
-
 /**
- * Function IsValidPcbLayerIndex
- * tests whether a given integer is a valid layer index
- * @param aLayerIndex = Layer index to test
+ * Function IsValidLayer
+ * tests whether a given integer is a valid layer index, i.e. can
+ * be safely put in a LAYER_NUM
+ * @param aLayerIndex = Layer index to test. It can be an int, so its
+ * useful during I/O
  * @return true if aLayerIndex is a valid layer index
  */
-inline bool IsValidPcbLayerIndex( LAYER_NUM aLayerIndex )
+inline bool IsValidLayer( int aLayerIndex )
 {
-    return aLayerIndex >= FIRST_LAYER && aLayerIndex < NB_PCB_LAYERS;
+    return aLayerIndex >= FIRST_LAYER && aLayerIndex < NB_LAYERS;
 }
 
 /**
- * Function IsValidCopperLayerIndex
- * tests whether an integer is a valid copper layer index
- * @param aLayerIndex = Layer index to test
- * @return true if aLayerIndex is a valid copper layer index
+ * Function IsPcbLayer
+ * tests whether a layer is a valid layer for pcbnew
+ * @param aLayer = Layer to test
+ * @return true if aLayer is a layer valid in pcbnew
  */
-inline bool IsValidCopperLayerIndex( LAYER_NUM aLayerIndex )
+inline bool IsPcbLayer( LAYER_NUM aLayer )
 {
-    return aLayerIndex >= FIRST_COPPER_LAYER && aLayerIndex <= LAST_COPPER_LAYER;
+    return aLayer >= FIRST_LAYER && aLayer < NB_PCB_LAYERS;
 }
 
 /**
- * Function IsValidNonCopperLayerIndex
- * tests whether an integer is a valid non copper layer index
- * @param aLayerIndex = Layer index to test
- * @return true if aLayerIndex is a valid non copper layer index
+ * Function IsCopperLayer
+ * tests whether a layer is a copper layer
+ * @param aLayer = Layer  to test
+ * @return true if aLayer is a valid copper layer 
  */
-inline bool IsValidNonCopperLayerIndex( LAYER_NUM aLayerIndex )
+inline bool IsCopperLayer( LAYER_NUM aLayer )
 {
-    return aLayerIndex >= FIRST_NON_COPPER_LAYER
-        && aLayerIndex <= LAST_NON_COPPER_LAYER;
+    return aLayer >= FIRST_COPPER_LAYER 
+        && aLayer <= LAST_COPPER_LAYER;
+}
+
+/**
+ * Function IsNonCopperLayer
+ * tests whether a layer is a non copper layer
+ * @param aLayer = Layer to test
+ * @return true if aLayer is a non copper layer
+ */
+inline bool IsNonCopperLayer( LAYER_NUM aLayer )
+{
+    return aLayer >= FIRST_NON_COPPER_LAYER
+        && aLayer <= LAST_NON_COPPER_LAYER;
 }
 
 /* IMPORTANT: If a layer is not a front layer not necessarily is true
diff --git a/kicad/prjconfig.cpp b/kicad/prjconfig.cpp
index 8737574d99..12d2b991ba 100644
--- a/kicad/prjconfig.cpp
+++ b/kicad/prjconfig.cpp
@@ -38,6 +38,7 @@
 #include <wildcards_and_files_ext.h>
 #include <vector>
 #include <build_version.h>
+#include <macros.h>
 
 #include <wx/dir.h>
 #include <wx/filename.h>
diff --git a/pcbnew/basepcbframe.cpp b/pcbnew/basepcbframe.cpp
index a4eedd1c48..42f2432810 100644
--- a/pcbnew/basepcbframe.cpp
+++ b/pcbnew/basepcbframe.cpp
@@ -315,7 +315,7 @@ void PCB_BASE_FRAME::SwitchLayer( wxDC* DC, LAYER_NUM layer )
 
     // Copper layers cannot be selected unconditionally; how many
     // of those layers are currently enabled needs to be checked.
-    if( IsValidCopperLayerIndex( layer ) )
+    if( IsCopperLayer( layer ) )
     {
         // If only one copper layer is enabled, the only such layer
         // that can be selected to is the "Copper" layer (so the
diff --git a/pcbnew/class_board.cpp b/pcbnew/class_board.cpp
index 804d0d1875..eb7c6d540b 100644
--- a/pcbnew/class_board.cpp
+++ b/pcbnew/class_board.cpp
@@ -356,23 +356,23 @@ bool BOARD::SetLayer( LAYER_NUM aIndex, const LAYER& aLayer )
 }
 
 
-wxString BOARD::GetLayerName( LAYER_NUM aLayerIndex ) const
+wxString BOARD::GetLayerName( LAYER_NUM aLayer ) const
 {
-    if( !IsValidPcbLayerIndex( aLayerIndex ) )
+    if( !IsPcbLayer( aLayer ) )
         return wxEmptyString;
 
     // All layer names are stored in the BOARD.
-    if( IsLayerEnabled( aLayerIndex ) )
+    if( IsLayerEnabled( aLayer ) )
     {
         // Standard names were set in BOARD::BOARD() but they may be
         // over-ridden by BOARD::SetLayerName().
         // For copper layers, return the actual copper layer name,
         // otherwise return the Standard English layer name.
-        if( aLayerIndex < FIRST_NON_COPPER_LAYER )
-            return m_Layer[aLayerIndex].m_Name;
+        if( IsCopperLayer( aLayer ) )
+            return m_Layer[aLayer].m_Name;
     }
 
-    return GetStandardLayerName( aLayerIndex );
+    return GetStandardLayerName( aLayer );
 }
 
 
@@ -422,9 +422,9 @@ wxString BOARD::GetStandardLayerName( LAYER_NUM aLayerNumber )
 }
 
 
-bool BOARD::SetLayerName( LAYER_NUM aLayerIndex, const wxString& aLayerName )
+bool BOARD::SetLayerName( LAYER_NUM aLayer, const wxString& aLayerName )
 {
-    if( !IsValidCopperLayerIndex( aLayerIndex ) )
+    if( !IsCopperLayer( aLayer ) )
         return false;
 
     if( aLayerName == wxEmptyString || aLayerName.Len() > 20 )
@@ -439,15 +439,15 @@ bool BOARD::SetLayerName( LAYER_NUM aLayerIndex, const wxString& aLayerName )
     // replace any spaces with underscores before we do any comparing
     NameTemp.Replace( wxT( " " ), wxT( "_" ) );
 
-    if( IsLayerEnabled( aLayerIndex ) )
+    if( IsLayerEnabled( aLayer ) )
     {
         for( LAYER_NUM i = FIRST_COPPER_LAYER; i < NB_COPPER_LAYERS; ++i )
         {
-            if( i != aLayerIndex && IsLayerEnabled( i ) && NameTemp == m_Layer[i].m_Name )
+            if( i != aLayer && IsLayerEnabled( i ) && NameTemp == m_Layer[i].m_Name )
                 return false;
         }
 
-        m_Layer[aLayerIndex].m_Name = NameTemp;
+        m_Layer[aLayer].m_Name = NameTemp;
 
         return true;
     }
@@ -456,30 +456,30 @@ bool BOARD::SetLayerName( LAYER_NUM aLayerIndex, const wxString& aLayerName )
 }
 
 
-LAYER_T BOARD::GetLayerType( LAYER_NUM aLayerIndex ) const
+LAYER_T BOARD::GetLayerType( LAYER_NUM aLayer ) const
 {
-    if( !IsValidCopperLayerIndex( aLayerIndex ) )
+    if( !IsCopperLayer( aLayer ) )
         return LT_SIGNAL;
 
     //@@IMB: The original test was broken due to the discontinuity
     // in the layer sequence.
-    if( IsLayerEnabled( aLayerIndex ) )
-        return m_Layer[aLayerIndex].m_Type;
+    if( IsLayerEnabled( aLayer ) )
+        return m_Layer[aLayer].m_Type;
 
     return LT_SIGNAL;
 }
 
 
-bool BOARD::SetLayerType( LAYER_NUM aLayerIndex, LAYER_T aLayerType )
+bool BOARD::SetLayerType( LAYER_NUM aLayer, LAYER_T aLayerType )
 {
-    if( !IsValidCopperLayerIndex( aLayerIndex ) )
+    if( !IsCopperLayer( aLayer ) )
         return false;
 
     //@@IMB: The original test was broken due to the discontinuity
     // in the layer sequence.
-    if( IsLayerEnabled( aLayerIndex ) )
+    if( IsLayerEnabled( aLayer ) )
     {
-        m_Layer[aLayerIndex].m_Type = aLayerType;
+        m_Layer[aLayer].m_Type = aLayerType;
         return true;
     }
 
diff --git a/pcbnew/class_board.h b/pcbnew/class_board.h
index fe92536160..bd0b33ea4e 100644
--- a/pcbnew/class_board.h
+++ b/pcbnew/class_board.h
@@ -451,12 +451,12 @@ public:
      * Function IsLayerVisible
      * is a proxy function that calls the correspondent function in m_BoardSettings
      * tests whether a given layer is visible
-     * @param aLayerIndex = The index of the layer to be tested
+     * @param aLayer = The layer to be tested
      * @return bool - true if the layer is visible.
      */
-    bool IsLayerVisible( LAYER_NUM aLayerIndex ) const
+    bool IsLayerVisible( LAYER_NUM aLayer ) const
     {
-        return m_designSettings.IsLayerVisible( aLayerIndex );
+        return m_designSettings.IsLayerVisible( aLayer );
     }
 
     /**
@@ -588,26 +588,26 @@ public:
 
     /**
      * Function GetLayerName
-     * returns the name of a layer given by aLayerIndex.  Copper layers may
+     * returns the name of a layer given by aLayer.  Copper layers may
      * have custom names.
      *
-     * @param aLayerIndex = A layer index, like LAYER_N_BACK, etc.
+     * @param aLayer = A layer, like LAYER_N_BACK, etc.
      *
      * @return wxString -   the layer name, which for copper layers may
      *                      be custom, else standard.
      */
-    wxString GetLayerName( LAYER_NUM aLayerIndex ) const;
+    wxString GetLayerName( LAYER_NUM aLayer ) const;
 
     /**
      * Function SetLayerName
-     * changes the name of the layer given by aLayerIndex.
+     * changes the name of the layer given by aLayer.
      *
-     * @param aLayerIndex A layer index, like LAYER_N_BACK, etc.
+     * @param aLayer A layer, like LAYER_N_BACK, etc.
      * @param aLayerName The new layer name
      * @return bool - true if aLayerName was legal and unique among other
-     *   layer names at other layer indices and aLayerIndex was within range, else false.
+     *   layer names at other layer indices and aLayer was within range, else false.
      */
-    bool SetLayerName( LAYER_NUM aLayerIndex, const wxString& aLayerName );
+    bool SetLayerName( LAYER_NUM aLayer, const wxString& aLayerName );
 
     /**
      * Function GetStandardLayerName
@@ -626,23 +626,23 @@ public:
 
     /**
      * Function GetLayerType
-     * returns the type of the copper layer given by aLayerIndex.
+     * returns the type of the copper layer given by aLayer.
      *
-     * @param aLayerIndex A layer index, like LAYER_N_BACK, etc.
+     * @param aLayer A layer index, like LAYER_N_BACK, etc.
      * @return LAYER_T - the layer type, or LAYER_T(-1) if the
      *  index was out of range.
      */
-    LAYER_T GetLayerType( LAYER_NUM aLayerIndex ) const;
+    LAYER_T GetLayerType( LAYER_NUM aLayer ) const;
 
     /**
      * Function SetLayerType
-     * changes the type of the layer given by aLayerIndex.
+     * changes the type of the layer given by aLayer.
      *
-     * @param aLayerIndex A layer index, like LAYER_N_BACK, etc.
+     * @param aLayer A layer index, like LAYER_N_BACK, etc.
      * @param aLayerType The new layer type.
-     * @return bool - true if aLayerType was legal and aLayerIndex was within range, else false.
+     * @return bool - true if aLayerType was legal and aLayer was within range, else false.
      */
-    bool SetLayerType( LAYER_NUM aLayerIndex, LAYER_T aLayerType );
+    bool SetLayerType( LAYER_NUM aLayer, LAYER_T aLayerType );
 
     /**
      * Function SetLayerColor
diff --git a/pcbnew/class_board_design_settings.cpp b/pcbnew/class_board_design_settings.cpp
index 633a685507..f1f5ce0c88 100644
--- a/pcbnew/class_board_design_settings.cpp
+++ b/pcbnew/class_board_design_settings.cpp
@@ -186,21 +186,16 @@ void BOARD_DESIGN_SETTINGS::SetVisibleAlls()
 
 void BOARD_DESIGN_SETTINGS::SetVisibleLayers( LAYER_MSK aMask )
 {
-    // Although Pcbnew uses only 29, GerbView uses all 32 layers
     m_VisibleLayers = aMask & m_EnabledLayers & FULL_LAYERS;
 }
 
 
-void BOARD_DESIGN_SETTINGS::SetLayerVisibility( LAYER_NUM aLayerIndex, bool aNewState )
+void BOARD_DESIGN_SETTINGS::SetLayerVisibility( LAYER_NUM aLayer, bool aNewState )
 {
-    // Altough Pcbnew uses only 29, GerbView uses all 32 layers
-    if( aLayerIndex < 0 || aLayerIndex >= 32 )
-        return;
-
-    if( aNewState && IsLayerEnabled( aLayerIndex ) )
-        m_VisibleLayers |= GetLayerMask( aLayerIndex );
+    if( aNewState && IsLayerEnabled( aLayer ) )
+        m_VisibleLayers |= GetLayerMask( aLayer );
     else
-        m_VisibleLayers &= ~GetLayerMask( aLayerIndex );
+        m_VisibleLayers &= ~GetLayerMask( aLayer );
 }
 
 
@@ -245,12 +240,5 @@ void BOARD_DESIGN_SETTINGS::SetEnabledLayers( LAYER_MSK aMask )
     m_VisibleLayers &= aMask;
 
     // update m_CopperLayerCount to ensure its consistency with m_EnabledLayers
-    m_CopperLayerCount = 0;
-
-    unsigned shiftMask = aMask;
-    for( LAYER_NUM ii = FIRST_LAYER; aMask && ii < NB_COPPER_LAYERS; ++ii, shiftMask >>= 1 )
-    {
-        if( shiftMask & 1 )
-            m_CopperLayerCount++;
-    }
+    m_CopperLayerCount = LayerMaskCountSet( aMask & ALL_CU_LAYERS);
 }
diff --git a/pcbnew/class_marker_pcb.cpp b/pcbnew/class_marker_pcb.cpp
index d7c4beb2e4..88b361fbba 100644
--- a/pcbnew/class_marker_pcb.cpp
+++ b/pcbnew/class_marker_pcb.cpp
@@ -88,7 +88,7 @@ MARKER_PCB::~MARKER_PCB()
  */
 bool MARKER_PCB::IsOnLayer( LAYER_NUM aLayer ) const
 {
-    return IsValidCopperLayerIndex( aLayer );
+    return IsCopperLayer( aLayer );
 }
 
 void MARKER_PCB::GetMsgPanelInfo( std::vector< MSG_PANEL_ITEM >& aList )
diff --git a/pcbnew/class_module.cpp b/pcbnew/class_module.cpp
index 5a4bf89a33..b6317bb3d5 100644
--- a/pcbnew/class_module.cpp
+++ b/pcbnew/class_module.cpp
@@ -181,21 +181,13 @@ MODULE::~MODULE()
 void MODULE::DrawAncre( EDA_DRAW_PANEL* panel, wxDC* DC, const wxPoint& offset,
                         int dim_ancre, GR_DRAWMODE draw_mode )
 {
-    int anchor_size = DC->DeviceToLogicalXRel( dim_ancre );
-
     GRSetDrawMode( DC, draw_mode );
 
     if( GetBoard()->IsElementVisible( ANCHOR_VISIBLE ) )
     {
-        EDA_COLOR_T color = g_ColorsSettings.GetItemColor( ANCHOR_VISIBLE );
-        GRLine( panel->GetClipBox(), DC,
-                m_Pos.x - offset.x - anchor_size, m_Pos.y - offset.y,
-                m_Pos.x - offset.x + anchor_size, m_Pos.y - offset.y,
-                0, color );
-        GRLine( panel->GetClipBox(), DC,
-                m_Pos.x - offset.x, m_Pos.y - offset.y - anchor_size,
-                m_Pos.x - offset.x, m_Pos.y - offset.y + anchor_size,
-                0, color );
+        GRDrawAnchor( panel->GetClipBox(), DC, m_Pos.x, m_Pos.y, 
+                      dim_ancre,
+                      g_ColorsSettings.GetItemColor( ANCHOR_VISIBLE ) );
     }
 }
 
diff --git a/pcbnew/class_pad_draw_functions.cpp b/pcbnew/class_pad_draw_functions.cpp
index 884a637314..23147ec956 100644
--- a/pcbnew/class_pad_draw_functions.cpp
+++ b/pcbnew/class_pad_draw_functions.cpp
@@ -229,7 +229,7 @@ void D_PAD::Draw( EDA_DRAW_PANEL* aPanel, wxDC* aDC, GR_DRAWMODE aDraw_mode,
     // layer so we can see pads on paste or solder layer and the size of the
     // mask
     if( ( aDraw_mode & GR_ALLOW_HIGHCONTRAST ) &&
-        DisplayOpt.ContrastModeDisplay && screen->m_Active_Layer >= FIRST_NON_COPPER_LAYER )
+        DisplayOpt.ContrastModeDisplay && !IsCopperLayer( screen->m_Active_Layer ) )
     {
         if( IsOnLayer( screen->m_Active_Layer ) )
         {
diff --git a/pcbnew/class_pcb_layer_box_selector.cpp b/pcbnew/class_pcb_layer_box_selector.cpp
index be7c847f33..5cdcedf17d 100644
--- a/pcbnew/class_pcb_layer_box_selector.cpp
+++ b/pcbnew/class_pcb_layer_box_selector.cpp
@@ -83,33 +83,33 @@ void PCB_LAYER_BOX_SELECTOR::Resync()
 
 
 // Returns true if the layer id is enabled (i.e. is it should be displayed)
-bool PCB_LAYER_BOX_SELECTOR::IsLayerEnabled( LAYER_NUM aLayerIndex ) const
+bool PCB_LAYER_BOX_SELECTOR::IsLayerEnabled( LAYER_NUM aLayer ) const
 {
     PCB_BASE_FRAME* pcbFrame = (PCB_BASE_FRAME*) GetParent()->GetParent();
     BOARD* board = pcbFrame->GetBoard();
     wxASSERT( board != NULL );
 
-    return board->IsLayerEnabled( aLayerIndex );
+    return board->IsLayerEnabled( aLayer );
 }
 
 
 // Returns a color index from the layer id
-EDA_COLOR_T PCB_LAYER_BOX_SELECTOR::GetLayerColor( LAYER_NUM aLayerIndex ) const
+EDA_COLOR_T PCB_LAYER_BOX_SELECTOR::GetLayerColor( LAYER_NUM aLayer ) const
 {
     PCB_BASE_FRAME* pcbFrame = (PCB_BASE_FRAME*) GetParent()->GetParent();
     BOARD* board = pcbFrame->GetBoard();
     wxASSERT( board != NULL );
 
-    return board->GetLayerColor( aLayerIndex );
+    return board->GetLayerColor( aLayer );
 }
 
 
 // Returns the name of the layer id
-wxString PCB_LAYER_BOX_SELECTOR::GetLayerName( LAYER_NUM aLayerIndex ) const
+wxString PCB_LAYER_BOX_SELECTOR::GetLayerName( LAYER_NUM aLayer ) const
 {
     PCB_BASE_FRAME* pcbFrame = (PCB_BASE_FRAME*) GetParent()->GetParent();
     BOARD* board = pcbFrame->GetBoard();
     wxASSERT( board != NULL );
 
-    return board->GetLayerName( aLayerIndex );
+    return board->GetLayerName( aLayer );
 }
diff --git a/pcbnew/class_pcb_layer_box_selector.h b/pcbnew/class_pcb_layer_box_selector.h
index ef75213cc4..e56087d90a 100644
--- a/pcbnew/class_pcb_layer_box_selector.h
+++ b/pcbnew/class_pcb_layer_box_selector.h
@@ -40,15 +40,15 @@ public:
 
     // Returns a color index from the layer id
     // Virtual function
-    EDA_COLOR_T GetLayerColor( LAYER_NUM aLayerIndex ) const;
+    EDA_COLOR_T GetLayerColor( LAYER_NUM aLayer ) const;
 
     // Returns true if the layer id is enabled (i.e. is it should be displayed)
     // Virtual function
-    bool IsLayerEnabled( LAYER_NUM aLayerIndex ) const;
+    bool IsLayerEnabled( LAYER_NUM aLayer ) const;
 
     // Returns the name of the layer id
     // Virtual function
-    wxString GetLayerName( LAYER_NUM aLayerIndex ) const;
+    wxString GetLayerName( LAYER_NUM aLayer ) const;
 };
 
 #endif //CLASS_PCB_PCB_LAYER_BOX_SELECTOR_H
diff --git a/pcbnew/class_pcb_layer_widget.cpp b/pcbnew/class_pcb_layer_widget.cpp
index e115e44f1c..e60e8129ba 100644
--- a/pcbnew/class_pcb_layer_widget.cpp
+++ b/pcbnew/class_pcb_layer_widget.cpp
@@ -163,7 +163,7 @@ void PCB_LAYER_WIDGET::onPopupSelection( wxCommandEvent& event )
         {
             wxCheckBox* cb = (wxCheckBox*) getLayerComp( row, 3 );
             LAYER_NUM layer = getDecodedId( cb->GetId() );
-            if( IsValidCopperLayerIndex( layer ) )
+            if( IsCopperLayer( layer ) )
             {
                 lastCu = row;
                 break;
@@ -176,7 +176,7 @@ void PCB_LAYER_WIDGET::onPopupSelection( wxCommandEvent& event )
             wxCheckBox* cb = (wxCheckBox*) getLayerComp( row, 3 );
             LAYER_NUM layer = getDecodedId( cb->GetId() );
 
-            if( IsValidCopperLayerIndex( layer ) )
+            if( IsCopperLayer( layer ) )
             {
                 bool loc_visible = visible;
                 if( force_active_layer_visible && (layer == myframe->getActiveLayer() ) )
@@ -262,36 +262,36 @@ void PCB_LAYER_WIDGET::SyncLayerVisibilities()
 void PCB_LAYER_WIDGET::ReFill()
 {
     BOARD*  brd = myframe->GetBoard();
-    LAYER_NUM layer;
-
-    int enabledLayers = brd->GetEnabledLayers();
+    int     enabledLayers = brd->GetEnabledLayers();
 
     ClearLayerRows();
 
     // show all coppers first, with front on top, back on bottom, then technical layers
-
-    layer = LAYER_N_FRONT;
-    if( enabledLayers & GetLayerMask( layer ) )
-    {
-        AppendLayerRow( LAYER_WIDGET::ROW(
-            brd->GetLayerName( layer ), layer, brd->GetLayerColor( layer ), _("Front copper layer"), true ) );
-    }
-
-    for( layer = LAYER_N_FRONT-1; layer >= 1; --layer )
+    for( LAYER_NUM layer = LAYER_N_FRONT; layer >= FIRST_LAYER; --layer )
     {
         if( enabledLayers & GetLayerMask( layer ) )
         {
+            const wxChar *dsc;
+            switch( layer )
+            {
+            case LAYER_N_FRONT:
+                dsc = _("Front copper layer");
+                break;
+
+            case LAYER_N_BACK:
+                dsc = _("Back copper layer");
+                break;
+
+            default:
+                dsc = _("Inner copper layer");
+                break;
+            }
+
             AppendLayerRow( LAYER_WIDGET::ROW(
-                brd->GetLayerName( layer ), layer, brd->GetLayerColor( layer ), _("An innner copper layer"), true ) );
+                brd->GetLayerName( layer ), layer, brd->GetLayerColor( layer ), dsc, true ) );
         }
     }
 
-    layer = LAYER_N_BACK;
-    if( enabledLayers & GetLayerMask( layer ) )
-    {
-        AppendLayerRow( LAYER_WIDGET::ROW(
-            brd->GetLayerName( layer ), layer, brd->GetLayerColor( layer ), _("Back copper layer"), true ) );
-    }
 
     // technical layers are shown in this order:
     static const struct {
@@ -315,7 +315,7 @@ void PCB_LAYER_WIDGET::ReFill()
 
     for( unsigned i=0;  i<DIM(techLayerSeq);  ++i )
     {
-        layer = techLayerSeq[i].layerId;
+        LAYER_NUM layer = techLayerSeq[i].layerId;
 
         if( !(enabledLayers & GetLayerMask( layer )) )
             continue;
diff --git a/pcbnew/class_text_mod.cpp b/pcbnew/class_text_mod.cpp
index b4e55d91b3..a02944b39a 100644
--- a/pcbnew/class_text_mod.cpp
+++ b/pcbnew/class_text_mod.cpp
@@ -288,14 +288,8 @@ void TEXTE_MODULE::Draw( EDA_DRAW_PANEL* panel, wxDC* DC, GR_DRAWMODE draw_mode,
     if( brd->IsElementVisible( ANCHOR_VISIBLE ) )
     {
         EDA_COLOR_T anchor_color = brd->GetVisibleElementColor(ANCHOR_VISIBLE);
-        int anchor_size = DC->DeviceToLogicalXRel( 2 );
-
-        GRLine( panel->GetClipBox(), DC,
-                pos.x - anchor_size, pos.y,
-                pos.x + anchor_size, pos.y, 0, anchor_color );
-        GRLine( panel->GetClipBox(), DC,
-                pos.x, pos.y - anchor_size,
-                pos.x, pos.y + anchor_size, 0, anchor_color );
+        GRDrawAnchor( panel->GetClipBox(), DC, pos.x, pos.y, 
+                      DIM_ANCRE_TEXTE, anchor_color );
     }
 
     // Draw the text proper, with the right attributes
diff --git a/pcbnew/class_track.cpp b/pcbnew/class_track.cpp
index 96586180e1..3eb4364dbd 100644
--- a/pcbnew/class_track.cpp
+++ b/pcbnew/class_track.cpp
@@ -55,7 +55,7 @@
 static bool ShowClearance( const TRACK* aTrack )
 {
     // maybe return true for tracks and vias, not for zone segments
-    return aTrack->GetLayer() <= LAST_COPPER_LAYER
+    return IsCopperLayer( aTrack->GetLayer() )
            && ( aTrack->Type() == PCB_TRACE_T || aTrack->Type() == PCB_VIA_T )
            && ( ( DisplayOpt.ShowTrackClearanceMode == SHOW_CLEARANCE_NEW_AND_EDITED_TRACKS_AND_VIA_AREAS
                   && ( aTrack->IsDragging() || aTrack->IsMoving() || aTrack->IsNew() ) )
@@ -87,7 +87,8 @@ inline bool IsNear( wxPoint& p1, wxPoint& p2, int max_dist )
 }
 
 
-TRACK* GetTrace( TRACK* aStartTrace, TRACK* aEndTrace, const wxPoint& aPosition, LAYER_MSK aLayerMask )
+TRACK* GetTrace( TRACK* aStartTrace, TRACK* aEndTrace, const wxPoint& aPosition,
+                 LAYER_MSK aLayerMask )
 {
     TRACK* PtSegm;
 
@@ -389,14 +390,8 @@ void TRACK::Flip( const wxPoint& aCentre )
     m_Start.y = aCentre.y - (m_Start.y - aCentre.y);
     m_End.y   = aCentre.y - (m_End.y - aCentre.y);
 
-    if( Type() == PCB_VIA_T )
-    {
-        // Huh?  Wouldn't it be better to us Type() != VIA and get rid of these brackets?
-    }
-    else
-    {
+    if( Type() != PCB_VIA_T )
         SetLayer( FlipLayer( GetLayer() ) );
-    }
 }
 
 
@@ -406,10 +401,6 @@ SEARCH_RESULT TRACK::Visit( INSPECTOR* inspector, const void* testData,
 {
     KICAD_T stype = *scanTypes;
 
-#if 0 && defined(DEBUG)
-    std::cout << GetClass().mb_str() << ' ';
-#endif
-
     // If caller wants to inspect my type
     if( stype == Type() )
     {
diff --git a/pcbnew/dialogs/dialog_graphic_item_properties.cpp b/pcbnew/dialogs/dialog_graphic_item_properties.cpp
index 9b4525e962..0c29dcfc14 100644
--- a/pcbnew/dialogs/dialog_graphic_item_properties.cpp
+++ b/pcbnew/dialogs/dialog_graphic_item_properties.cpp
@@ -156,13 +156,15 @@ void DIALOG_GRAPHIC_ITEM_PROPERTIES::initDlg( )
 
     PutValueInLocalUnits( *m_DefaultThicknessCtrl, thickness );
 
-    for( LAYER_NUM layer=FIRST_NON_COPPER_LAYER; layer <= LAST_NON_COPPER_LAYER;  ++layer )
+    for( LAYER_NUM layer = FIRST_NON_COPPER_LAYER; 
+         layer <= LAST_NON_COPPER_LAYER; ++layer )
     {
         m_LayerSelectionCtrl->Append( m_parent->GetBoard()->GetLayerName( layer ) );
     }
 
     LAYER_NUM layer = m_Item->GetLayer();
-    // Control:
+    
+    // It has to be an aux layer
     if ( layer < FIRST_NON_COPPER_LAYER )
         layer = FIRST_NON_COPPER_LAYER;
     if ( layer > LAST_NON_COPPER_LAYER )
diff --git a/pcbnew/dialogs/dialog_graphic_item_properties_for_Modedit.cpp b/pcbnew/dialogs/dialog_graphic_item_properties_for_Modedit.cpp
index a7ed5de1e0..9af075cc13 100644
--- a/pcbnew/dialogs/dialog_graphic_item_properties_for_Modedit.cpp
+++ b/pcbnew/dialogs/dialog_graphic_item_properties_for_Modedit.cpp
@@ -196,10 +196,10 @@ void DIALOG_MODEDIT_FP_BODY_ITEM_PROPERTIES::OnOkClick( wxCommandEvent& event )
     }
 
     LAYER_NUM layer = m_layerId[idx];
-    if( IsValidCopperLayerIndex( layer ) )
+    if( IsCopperLayer( layer ) )
     {
-        /* an edge is put on a copper layer, and it is very dangerous. a
-         *confirmation is requested */
+        /* an edge is put on a copper layer: this it is very dangerous. a
+         * confirmation is requested */
         if( !IsOK( NULL,
                    _( "The graphic item will be on a copper layer. This is very dangerous. Are you sure?" ) ) )
             return;
diff --git a/pcbnew/dialogs/dialog_layers_setup.cpp b/pcbnew/dialogs/dialog_layers_setup.cpp
index 4cccbfc508..d06f501394 100644
--- a/pcbnew/dialogs/dialog_layers_setup.cpp
+++ b/pcbnew/dialogs/dialog_layers_setup.cpp
@@ -580,7 +580,7 @@ wxString DIALOG_LAYERS_SETUP::getLayerName( LAYER_NUM aLayer )
 {
     wxString ret;
 
-    wxASSERT( aLayer >= FIRST_COPPER_LAYER && aLayer <= LAST_COPPER_LAYER );
+    wxASSERT( IsCopperLayer( aLayer ) );
 
     wxTextCtrl*  ctl = (wxTextCtrl*) getName( aLayer );
 
diff --git a/pcbnew/eagle_plugin.cpp b/pcbnew/eagle_plugin.cpp
index fd2384fe2d..00df1c0879 100644
--- a/pcbnew/eagle_plugin.cpp
+++ b/pcbnew/eagle_plugin.cpp
@@ -1475,7 +1475,7 @@ void EAGLE_PLUGIN::loadPlain( CPTREE& aGraphics )
             ERECT   r( gr->second );
             LAYER_NUM layer = kicad_layer( r.layer );
 
-            if( IsValidCopperLayerIndex( layer ) )
+            if( IsCopperLayer( layer ) )
             {
                 // use a "netcode = 0" type ZONE:
                 ZONE_CONTAINER* zone = new ZONE_CONTAINER( m_board );
@@ -1910,7 +1910,7 @@ void EAGLE_PLUGIN::packageWire( MODULE* aModule, CPTREE& aTree ) const
     EWIRE   w( aTree );
     LAYER_NUM layer = kicad_layer( w.layer );
 
-    if( IsValidNonCopperLayerIndex( layer ) )  // skip copper package wires
+    if( IsNonCopperLayer( layer ) )  // skip copper package wires
     {
         wxPoint start( kicad_x( w.x1 ), kicad_y( w.y1 ) );
         wxPoint end(   kicad_x( w.x2 ), kicad_y( w.y2 ) );
@@ -2120,7 +2120,7 @@ void EAGLE_PLUGIN::packageRectangle( MODULE* aModule, CPTREE& aTree ) const
     ERECT   r( aTree );
     LAYER_NUM layer = kicad_layer( r.layer );
 
-    if( IsValidNonCopperLayerIndex( layer ) )  // skip copper "package.rectangle"s
+    if( IsNonCopperLayer( layer ) )  // skip copper "package.rectangle"s
     {
         EDGE_MODULE* dwg = new EDGE_MODULE( aModule, S_POLYGON );
         aModule->GraphicalItems().PushBack( dwg );
@@ -2153,7 +2153,7 @@ void EAGLE_PLUGIN::packagePolygon( MODULE* aModule, CPTREE& aTree ) const
     EPOLYGON    p( aTree );
     LAYER_NUM layer = kicad_layer( p.layer );
 
-    if( IsValidNonCopperLayerIndex( layer ) )  // skip copper "package.rectangle"s
+    if( IsNonCopperLayer( layer ) )  // skip copper "package.rectangle"s
     {
         EDGE_MODULE* dwg = new EDGE_MODULE( aModule, S_POLYGON );
         aModule->GraphicalItems().PushBack( dwg );
@@ -2259,7 +2259,7 @@ void EAGLE_PLUGIN::packageSMD( MODULE* aModule, CPTREE& aTree ) const
     ESMD    e( aTree );
     LAYER_NUM layer = kicad_layer( e.layer );
 
-    if( !IsValidCopperLayerIndex( layer ) )
+    if( !IsCopperLayer( layer ) )
     {
         return;
     }
@@ -2351,7 +2351,7 @@ void EAGLE_PLUGIN::loadSignals( CPTREE& aSignals )
                 EWIRE   w( it->second );
                 LAYER_NUM layer = kicad_layer( w.layer );
 
-                if( IsValidCopperLayerIndex( layer ) )
+                if( IsCopperLayer( layer ) )
                 {
                     TRACK*  t = new TRACK( m_board );
 
@@ -2386,8 +2386,8 @@ void EAGLE_PLUGIN::loadSignals( CPTREE& aSignals )
                 LAYER_NUM layer_front_most = kicad_layer( v.layer_front_most );
                 LAYER_NUM layer_back_most  = kicad_layer( v.layer_back_most );
 
-                if( IsValidCopperLayerIndex( layer_front_most ) &&
-                    IsValidCopperLayerIndex( layer_back_most ) )
+                if( IsCopperLayer( layer_front_most ) &&
+                    IsCopperLayer( layer_back_most ) )
                 {
                     int     kidiam;
                     int     drillz = kicad( v.drill );
@@ -2464,7 +2464,7 @@ void EAGLE_PLUGIN::loadSignals( CPTREE& aSignals )
                 EPOLYGON p( it->second );
                 LAYER_NUM layer = kicad_layer( p.layer );
 
-                if( IsValidCopperLayerIndex( layer ) )
+                if( IsCopperLayer( layer ) )
                 {
                     // use a "netcode = 0" type ZONE:
                     ZONE_CONTAINER* zone = new ZONE_CONTAINER( m_board );
diff --git a/pcbnew/edgemod.cpp b/pcbnew/edgemod.cpp
index 9d3c0c9aab..f02c0f54a5 100644
--- a/pcbnew/edgemod.cpp
+++ b/pcbnew/edgemod.cpp
@@ -202,7 +202,7 @@ void FOOTPRINT_EDIT_FRAME::Edit_Edge_Layer( EDGE_MODULE* aEdge )
     if( new_layer < 0 )
         return;
 
-    if( IsValidCopperLayerIndex( new_layer ) )
+    if( IsCopperLayer( new_layer ) )
     {
         /* an edge is put on a copper layer, and it is very dangerous. a
          *confirmation is requested */
diff --git a/pcbnew/edit.cpp b/pcbnew/edit.cpp
index 51f4c5f8e8..ee0e094ee0 100755
--- a/pcbnew/edit.cpp
+++ b/pcbnew/edit.cpp
@@ -1248,7 +1248,7 @@ void PCB_EDIT_FRAME::SwitchLayer( wxDC* DC, LAYER_NUM layer )
 
     // Copper layers cannot be selected unconditionally; how many
     // of those layers are currently enabled needs to be checked.
-    if( IsValidCopperLayerIndex( layer ) )
+    if( IsCopperLayer( layer ) )
     {
         // If only one copper layer is enabled, the only such layer
         // that can be selected to is the "Back" layer (so the
diff --git a/pcbnew/onleftclick.cpp b/pcbnew/onleftclick.cpp
index d1c780643a..aa9e169ebf 100644
--- a/pcbnew/onleftclick.cpp
+++ b/pcbnew/onleftclick.cpp
@@ -243,7 +243,7 @@ void PCB_EDIT_FRAME::OnLeftClick( wxDC* aDC, const wxPoint& aPosition )
             if( GetToolId() == ID_PCB_ARC_BUTT )
                 shape = S_ARC;
 
-            if( getActiveLayer() <= LAST_COPPER_LAYER )
+            if( IsCopperLayer( getActiveLayer() ) )
             {
                 DisplayError( this, _( "Graphic not authorized on Copper layers" ) );
                 break;
@@ -267,7 +267,7 @@ void PCB_EDIT_FRAME::OnLeftClick( wxDC* aDC, const wxPoint& aPosition )
         break;
 
     case ID_TRACK_BUTT:
-        if( getActiveLayer() > LAST_COPPER_LAYER )
+        if( !IsCopperLayer( getActiveLayer() ) )
         {
             DisplayError( this, _( "Tracks on Copper layers only " ) );
             break;
@@ -366,7 +366,7 @@ void PCB_EDIT_FRAME::OnLeftClick( wxDC* aDC, const wxPoint& aPosition )
         break;
 
     case ID_PCB_DIMENSION_BUTT:
-        if( getActiveLayer() <= LAST_COPPER_LAYER )
+        if( IsCopperLayer( getActiveLayer() ) )
         {
             DisplayError( this, _( "Dimension not authorized on Copper layers" ) );
             break;
diff --git a/pcbnew/onrightclick.cpp b/pcbnew/onrightclick.cpp
index c2b2b73638..538368545d 100644
--- a/pcbnew/onrightclick.cpp
+++ b/pcbnew/onrightclick.cpp
@@ -204,7 +204,7 @@ bool PCB_EDIT_FRAME::OnRightClick( const wxPoint& aMousePos, wxMenu* aPopMenu )
                 AddMenuItem( aPopMenu, ID_POPUP_PCB_DELETE_DRAWING,
                              _( "Delete Drawing" ), KiBitmap( delete_xpm ) );
 
-                if( item->GetLayer() > LAST_COPPER_LAYER )
+                if( !IsCopperLayer( item->GetLayer() ) )
                     AddMenuItem( aPopMenu, ID_POPUP_PCB_DELETE_DRAWING_LAYER,
                                  _( "Delete All Drawings on Layer" ), KiBitmap( delete_xpm ) );
             }
diff --git a/pcbnew/pcad2kicadpcb_plugin/pcb_arc.cpp b/pcbnew/pcad2kicadpcb_plugin/pcb_arc.cpp
index 7adda1fda4..5a5a21fda9 100644
--- a/pcbnew/pcad2kicadpcb_plugin/pcb_arc.cpp
+++ b/pcbnew/pcad2kicadpcb_plugin/pcb_arc.cpp
@@ -150,7 +150,7 @@ void PCB_ARC::Flip()
 
 void PCB_ARC::AddToModule( MODULE* aModule )
 {
-    if( IsValidNonCopperLayerIndex( m_KiCadLayer ) )
+    if( IsNonCopperLayer( m_KiCadLayer ) )
     {
         EDGE_MODULE* arc = new EDGE_MODULE( aModule, S_ARC );
         aModule->GraphicalItems().PushBack( arc );
diff --git a/pcbnew/pcad2kicadpcb_plugin/pcb_line.cpp b/pcbnew/pcad2kicadpcb_plugin/pcb_line.cpp
index 280551795b..ad18b26748 100644
--- a/pcbnew/pcad2kicadpcb_plugin/pcb_line.cpp
+++ b/pcbnew/pcad2kicadpcb_plugin/pcb_line.cpp
@@ -116,7 +116,7 @@ void PCB_LINE::Flip()
 
 void PCB_LINE::AddToModule( MODULE* aModule )
 {
-    if( IsValidNonCopperLayerIndex( m_KiCadLayer ) )
+    if( IsNonCopperLayer( m_KiCadLayer ) )
     {
         EDGE_MODULE* segment = new EDGE_MODULE( aModule, S_SEGMENT );
         aModule->GraphicalItems().PushBack( segment );
@@ -134,7 +134,7 @@ void PCB_LINE::AddToModule( MODULE* aModule )
 
 void PCB_LINE::AddToBoard()
 {
-    if( IsValidCopperLayerIndex( m_KiCadLayer ) )
+    if( IsCopperLayer( m_KiCadLayer ) )
     {
         TRACK* track = new TRACK( m_board );
         m_board->m_Track.Append( track );
diff --git a/pcbnew/pcad2kicadpcb_plugin/pcb_pad.cpp b/pcbnew/pcad2kicadpcb_plugin/pcb_pad.cpp
index 53a27fce03..4ec4338efa 100644
--- a/pcbnew/pcad2kicadpcb_plugin/pcb_pad.cpp
+++ b/pcbnew/pcad2kicadpcb_plugin/pcb_pad.cpp
@@ -319,7 +319,7 @@ void PCB_PAD::AddToBoard()
         if( width == 0 || height == 0 )
             THROW_IO_ERROR( wxT( "pad or via with zero size" ) );
 
-        if( IsValidCopperLayerIndex( m_KiCadLayer ) )
+        if( IsCopperLayer( m_KiCadLayer ) )
         {
             SEGVIA* via = new SEGVIA( m_board );
             m_board->m_Track.Append( via );
diff --git a/pcbnew/pcbnew.h b/pcbnew/pcbnew.h
index bea9f057bf..b829be7ce2 100644
--- a/pcbnew/pcbnew.h
+++ b/pcbnew/pcbnew.h
@@ -29,8 +29,7 @@
 #define FLG_START 0             // Flag used in locate routines
 #define FLG_END   1             // Flag used in locate routines
 
-#define DIM_ANCRE_MODULE 3       /* Anchor size (footprint center) */
-#define DIM_ANCRE_TEXTE  2       /* Anchor size (Text center) */
+#define DIM_ANCRE_MODULE 3       // Anchor size (footprint center)
 
 
 #define TEXTS_MIN_SIZE  DMils2iu( 50 )      ///< Minimum text size in Pcbnew units value (50 * 0.0001 mils)
diff --git a/pcbnew/swap_layers.cpp b/pcbnew/swap_layers.cpp
index 58be1f5469..545c063770 100644
--- a/pcbnew/swap_layers.cpp
+++ b/pcbnew/swap_layers.cpp
@@ -291,7 +291,7 @@ void WinEDA_SwapLayerFrame::Sel_Layer( wxCommandEvent& event )
 
     jj = m_Parent->SelectLayer( jj, UNDEFINED_LAYER, UNDEFINED_LAYER, true );
 
-    if( (jj < FIRST_LAYER) || (jj > NB_PCB_LAYERS) )
+    if( !IsValidLayer( jj ) )
         return;
 
     // No change if the selected layer matches the layer being edited.
diff --git a/pcbnew/zones_test_and_combine_areas.cpp b/pcbnew/zones_test_and_combine_areas.cpp
index c476d79bf5..32a4a411c0 100644
--- a/pcbnew/zones_test_and_combine_areas.cpp
+++ b/pcbnew/zones_test_and_combine_areas.cpp
@@ -62,7 +62,7 @@ bool BOARD::OnAreaPolygonModified( PICKED_ITEMS_LIST* aModifiedZonesList,
         CombineAllAreasInNet( aModifiedZonesList, modified_area->GetNet(), true );
     }
 
-    if( layer >= FIRST_NON_COPPER_LAYER )    // Refill non copper zones on this layer
+    if( !IsCopperLayer( layer ) )       // Refill non copper zones on this layer
     {
         for( unsigned ia = 0; ia < m_ZoneDescriptorList.size(); ia++ )
             if( m_ZoneDescriptorList[ia]->GetLayer() == layer )