diff --git a/3d-viewer/3d_canvas/create_layer_items.cpp b/3d-viewer/3d_canvas/create_layer_items.cpp
index f624e46378..e0d3a72ec6 100644
--- a/3d-viewer/3d_canvas/create_layer_items.cpp
+++ b/3d-viewer/3d_canvas/create_layer_items.cpp
@@ -219,10 +219,10 @@ void CINFO3D_VISU::createLayers( REPORTER *aStatusTextReporter )
     layer_id.clear();
     layer_id.reserve( m_copperLayersCount );
 
-    for( unsigned i = 0; i < DIM( cu_seq ); ++i )
+    for( unsigned i = 0; i < arrayDim( cu_seq ); ++i )
         cu_seq[i] = ToLAYER_ID( B_Cu - i );
 
-    for( LSEQ cu = cu_set.Seq( cu_seq, DIM( cu_seq ) ); cu; ++cu )
+    for( LSEQ cu = cu_set.Seq( cu_seq, arrayDim( cu_seq ) ); cu; ++cu )
     {
         const PCB_LAYER_ID curr_layer_id = *cu;
 
@@ -973,7 +973,7 @@ void CINFO3D_VISU::createLayers( REPORTER *aStatusTextReporter )
 
     // User layers are not drawn here, only technical layers
 
-    for( LSEQ seq = LSET::AllNonCuMask().Seq( teckLayerList, DIM( teckLayerList ) );
+    for( LSEQ seq = LSET::AllNonCuMask().Seq( teckLayerList, arrayDim( teckLayerList ) );
          seq;
          ++seq )
     {
diff --git a/3d-viewer/3d_viewer/eda_3d_viewer.cpp b/3d-viewer/3d_viewer/eda_3d_viewer.cpp
index c5f2b993d7..d188ef8d7d 100644
--- a/3d-viewer/3d_viewer/eda_3d_viewer.cpp
+++ b/3d-viewer/3d_viewer/eda_3d_viewer.cpp
@@ -172,8 +172,8 @@ EDA_3D_VIEWER::EDA_3D_VIEWER( KIWAY *aKiway, PCB_BASE_FRAME *aParent,
     // Create the status line
     static const int status_dims[4] = { -1, 130, 130, 170 };
 
-    wxStatusBar *status_bar = CreateStatusBar( DIM( status_dims ) );
-    SetStatusWidths( DIM( status_dims ), status_dims );
+    wxStatusBar *status_bar = CreateStatusBar( arrayDim( status_dims ) );
+    SetStatusWidths( arrayDim( status_dims ), status_dims );
 
     CreateMenuBar();
     ReCreateMainToolbar();
diff --git a/3d-viewer/common_ogl/cogl_att_list.cpp b/3d-viewer/common_ogl/cogl_att_list.cpp
index dc32810b6a..f1ae10dcb6 100644
--- a/3d-viewer/common_ogl/cogl_att_list.cpp
+++ b/3d-viewer/common_ogl/cogl_att_list.cpp
@@ -75,7 +75,7 @@ const int COGL_ATT_LIST::m_openGL_attributes_list[] = {
 #define ATT_WX_GL_SAMPLE_BUFFERS_DATA 11
 
 int COGL_ATT_LIST::m_openGL_attributes_list_to_use[
-                                        DIM( COGL_ATT_LIST::m_openGL_attributes_list ) ] = { 0 };
+                                        arrayDim( COGL_ATT_LIST::m_openGL_attributes_list ) ] = { 0 };
 
 
 const int *COGL_ATT_LIST::GetAttributesList( bool aUseAntiAliasing )
diff --git a/common/colors_design_settings.cpp b/common/colors_design_settings.cpp
index 46971c0ed7..d275d905f5 100644
--- a/common/colors_design_settings.cpp
+++ b/common/colors_design_settings.cpp
@@ -100,15 +100,15 @@ COLORS_DESIGN_SETTINGS::COLORS_DESIGN_SETTINGS( FRAME_T aFrameType )
     m_frameType = aFrameType;
     m_legacyMode = false;
 
-    for( unsigned src = 0, dst = 0; dst < DIM( m_LayersColors ); ++dst )
+    for( unsigned src = 0, dst = 0; dst < arrayDim( m_LayersColors ); ++dst )
     {
         m_LayersColors[dst] = COLOR4D( default_layer_color[src++] );
 
-        if( src >= DIM( default_layer_color ) )
+        if( src >= arrayDim( default_layer_color ) )
             src = 0;        // wrap the source.
     }
 
-    for( unsigned src = 0, dst = LAYER_VIAS; src < DIM( default_items_color ); ++dst, ++src )
+    for( unsigned src = 0, dst = LAYER_VIAS; src < arrayDim( default_items_color ); ++dst, ++src )
     {
         m_LayersColors[dst] = COLOR4D( default_items_color[src] );
     }
@@ -125,7 +125,7 @@ COLORS_DESIGN_SETTINGS::COLORS_DESIGN_SETTINGS( FRAME_T aFrameType )
 
 COLOR4D COLORS_DESIGN_SETTINGS::GetLayerColor( LAYER_NUM aLayer ) const
 {
-    if( (unsigned) aLayer < DIM( m_LayersColors ) )
+    if( (unsigned) aLayer < arrayDim( m_LayersColors ) )
     {
         return m_legacyMode ? m_LayersColors[aLayer].AsLegacyColor()
                             : m_LayersColors[aLayer];
@@ -136,7 +136,7 @@ COLOR4D COLORS_DESIGN_SETTINGS::GetLayerColor( LAYER_NUM aLayer ) const
 
 void COLORS_DESIGN_SETTINGS::SetLayerColor( LAYER_NUM aLayer, COLOR4D aColor )
 {
-    if( (unsigned) aLayer < DIM( m_LayersColors ) )
+    if( (unsigned) aLayer < arrayDim( m_LayersColors ) )
     {
         m_LayersColors[aLayer] = aColor;
     }
@@ -145,7 +145,7 @@ void COLORS_DESIGN_SETTINGS::SetLayerColor( LAYER_NUM aLayer, COLOR4D aColor )
 
 COLOR4D COLORS_DESIGN_SETTINGS::GetItemColor( int aItemIdx ) const
 {
-    if( (unsigned) aItemIdx < DIM( m_LayersColors ) )
+    if( (unsigned) aItemIdx < arrayDim( m_LayersColors ) )
     {
         return m_legacyMode ? m_LayersColors[aItemIdx].AsLegacyColor()
                             : m_LayersColors[aItemIdx];
@@ -157,7 +157,7 @@ COLOR4D COLORS_DESIGN_SETTINGS::GetItemColor( int aItemIdx ) const
 
 void COLORS_DESIGN_SETTINGS::SetItemColor( int aItemIdx, COLOR4D aColor )
 {
-    if( (unsigned) aItemIdx < DIM( m_LayersColors ) )
+    if( (unsigned) aItemIdx < arrayDim( m_LayersColors ) )
     {
         m_LayersColors[aItemIdx] = aColor;
     }
@@ -166,7 +166,7 @@ void COLORS_DESIGN_SETTINGS::SetItemColor( int aItemIdx, COLOR4D aColor )
 
 void COLORS_DESIGN_SETTINGS::SetAllColorsAs( COLOR4D aColor )
 {
-    for( unsigned ii = 0; ii < DIM(m_LayersColors); ii++ )
+    for( unsigned ii = 0; ii < arrayDim(m_LayersColors); ii++ )
         m_LayersColors[ii] = aColor;
 }
 
@@ -175,7 +175,7 @@ void COLORS_DESIGN_SETTINGS::SetAllColorsAs( COLOR4D aColor )
 
 void COLORS_DESIGN_SETTINGS::setupConfigParams()
 {
-    wxASSERT( DIM( m_LayersColors ) >= PCB_LAYER_ID_COUNT );
+    wxASSERT( arrayDim( m_LayersColors ) >= PCB_LAYER_ID_COUNT );
 
     wxString currprefix = GetConfigPrefix();
 
diff --git a/common/dialogs/dialog_page_settings.cpp b/common/dialogs/dialog_page_settings.cpp
index 726c5b4079..1d297cace8 100644
--- a/common/dialogs/dialog_page_settings.cpp
+++ b/common/dialogs/dialog_page_settings.cpp
@@ -26,7 +26,7 @@
  */
 
 #include <fctsys.h>
-#include <macros.h>              // DIM()
+#include <macros.h>              // arrayDim()
 #include <common.h>
 #include <project.h>
 #include <confirm.h>
@@ -94,7 +94,7 @@ void EDA_DRAW_FRAME::Process_PageSettings( wxCommandEvent& event )
     // units that do not allow too large draw areas
     wxSize maxPageSize( MAX_PAGE_SIZE_EDITORS_MILS, MAX_PAGE_SIZE_EDITORS_MILS );
 
-    for( unsigned ii = 0; ii < DIM( smallSizeFrames ); ii++ )
+    for( unsigned ii = 0; ii < arrayDim( smallSizeFrames ); ii++ )
     {
         if( IsType( smallSizeFrames[ii] ) )
         {
@@ -157,7 +157,7 @@ void DIALOG_PAGES_SETTINGS::initDialog()
     // The first shows translated strings, the second contains not translated strings
     m_paperSizeComboBox->Clear();
 
-    for( unsigned ii = 0; ii < DIM(pageFmts); ii++ )
+    for( unsigned ii = 0; ii < arrayDim(pageFmts); ii++ )
     {
         m_pageFmt.Add( pageFmts[ii] );
         m_paperSizeComboBox->Append( wxGetTranslation( pageFmts[ii] ) );
@@ -721,7 +721,7 @@ void DIALOG_PAGES_SETTINGS::GetPageLayoutInfoFromDialog()
 
         unsigned i;
 
-        for( i=0;  i < DIM( papers );  ++i )
+        for( i=0;  i < arrayDim( papers );  ++i )
         {
             if( paperType.Contains( papers[i] ) )
             {
@@ -730,7 +730,7 @@ void DIALOG_PAGES_SETTINGS::GetPageLayoutInfoFromDialog()
             }
         }
 
-        wxASSERT( i != DIM(papers) );   // dialog UI match the above list?
+        wxASSERT( i != arrayDim(papers) );   // dialog UI match the above list?
 
         m_layout_size = pageInfo.GetSizeMils();
 
diff --git a/common/eda_base_frame.cpp b/common/eda_base_frame.cpp
index 4dd4f37304..73ea6830bc 100644
--- a/common/eda_base_frame.cpp
+++ b/common/eda_base_frame.cpp
@@ -450,7 +450,7 @@ void EDA_BASE_FRAME::GetKicadHelp( wxCommandEvent& event )
         wxString helpFile;
         // Search for "getting_started_in_kicad.html" or "getting_started_in_kicad.pdf"
         // or "Getting_Started_in_KiCad.html" or "Getting_Started_in_KiCad.pdf"
-        for( unsigned ii = 0; ii < DIM( names ); ii++ )
+        for( unsigned ii = 0; ii < arrayDim( names ); ii++ )
         {
             helpFile = SearchHelpFileFullPath( search, names[ii] );
 
diff --git a/common/eda_doc.cpp b/common/eda_doc.cpp
index 6d3ea02087..ddb6ce1e0b 100644
--- a/common/eda_doc.cpp
+++ b/common/eda_doc.cpp
@@ -103,7 +103,7 @@ bool GetAssociatedDocument( wxWindow* aParent,
         wxT( "file:" )
     };
 
-    for( unsigned ii = 0; ii < DIM(url_header); ii++ )
+    for( unsigned ii = 0; ii < arrayDim(url_header); ii++ )
     {
         if( aDocName.First( url_header[ii] ) == 0 )   // looks like an internet url
         {
diff --git a/common/gestfich.cpp b/common/gestfich.cpp
index ef5e425180..9ccc77a0e7 100644
--- a/common/gestfich.cpp
+++ b/common/gestfich.cpp
@@ -188,7 +188,7 @@ wxString FindKicadFile( const wxString& shortname )
     };
 
     // find binary file from possibilities list:
-    for( unsigned i=0;  i<DIM(possibilities);  ++i )
+    for( unsigned i=0;  i<arrayDim(possibilities);  ++i )
     {
 #ifndef __WXMAC__
         fullFileName = possibilities[i] + shortname;
@@ -309,7 +309,7 @@ wxString KicadDatasPath()
 #endif
         };
 
-        for( unsigned i=0;  i<DIM(possibilities);  ++i )
+        for( unsigned i=0;  i<arrayDim(possibilities);  ++i )
         {
             data_path = possibilities[i];
 
diff --git a/common/kiway.cpp b/common/kiway.cpp
index 08fe54b351..9db1e71949 100644
--- a/common/kiway.cpp
+++ b/common/kiway.cpp
@@ -149,7 +149,7 @@ KIFACE*  KIWAY::KiFACE( FACE_T aFaceId, bool doLoad )
 {
     // Since this will be called from python, cannot assume that code will
     // not pass a bad aFaceId.
-    if( unsigned( aFaceId ) >= DIM( m_kiface ) )
+    if( unsigned( aFaceId ) >= arrayDim( m_kiface ) )
     {
         // @todo : throw an exception here for python's benefit, at least that
         // way it gets some explanatory text.
@@ -481,7 +481,7 @@ bool KIWAY::ProcessEvent( wxEvent& aEvent )
 
 void KIWAY::OnKiwayEnd()
 {
-    for( unsigned i=0;  i < DIM( m_kiface );  ++i )
+    for( unsigned i=0;  i < arrayDim( m_kiface );  ++i )
     {
         if( m_kiface[i] )
             m_kiface[i]->OnKifaceEnd();
diff --git a/common/legacy_gal/eda_draw_frame.cpp b/common/legacy_gal/eda_draw_frame.cpp
index 1e22efda03..cba6629aea 100644
--- a/common/legacy_gal/eda_draw_frame.cpp
+++ b/common/legacy_gal/eda_draw_frame.cpp
@@ -205,7 +205,7 @@ EDA_DRAW_FRAME::EDA_DRAW_FRAME( KIWAY* aKiway, wxWindow* aParent,
         GetTextSize( wxT( "Add layer alignment target" ), stsbar ).x + 10,
     };
 
-    SetStatusWidths( DIM( dims ), dims );
+    SetStatusWidths( arrayDim( dims ), dims );
 
     // Create child subwindows.
     GetClientSize( &m_FrameSize.x, &m_FrameSize.y );
diff --git a/common/legacy_wx/eda_draw_frame.cpp b/common/legacy_wx/eda_draw_frame.cpp
index 56851af91f..24dc78e0b9 100644
--- a/common/legacy_wx/eda_draw_frame.cpp
+++ b/common/legacy_wx/eda_draw_frame.cpp
@@ -207,7 +207,7 @@ EDA_DRAW_FRAME::EDA_DRAW_FRAME( KIWAY* aKiway, wxWindow* aParent,
         GetTextSize( wxT( "Add layer alignment target" ), stsbar ).x + 10,
     };
 
-    SetStatusWidths( DIM( dims ), dims );
+    SetStatusWidths( arrayDim( dims ), dims );
 
     // Create child subwindows.
     GetClientSize( &m_FrameSize.x, &m_FrameSize.y );
diff --git a/common/lset.cpp b/common/lset.cpp
index 2c17854799..e568c6fe28 100644
--- a/common/lset.cpp
+++ b/common/lset.cpp
@@ -185,7 +185,7 @@ LSEQ LSET::CuStack() const
         B_Cu,           // 31
     };
 
-    return Seq( sequence, DIM( sequence ) );
+    return Seq( sequence, arrayDim( sequence ) );
 }
 
 
@@ -209,7 +209,7 @@ LSEQ LSET::Technicals( LSET aSetToOmit ) const
 
     LSET subset = ~aSetToOmit & *this;
 
-    return subset.Seq( sequence, DIM( sequence ) );
+    return subset.Seq( sequence, arrayDim( sequence ) );
 }
 
 
@@ -225,7 +225,7 @@ LSEQ LSET::Users() const
         Margin,
    };
 
-   return Seq( sequence, DIM( sequence ) );
+   return Seq( sequence, arrayDim( sequence ) );
 }
 
 
@@ -252,7 +252,7 @@ LSEQ LSET::TechAndUserUIOrder() const
         B_Fab,
    };
 
-   return Seq( sequence, DIM( sequence ) );
+   return Seq( sequence, arrayDim( sequence ) );
 }
 
 
@@ -302,7 +302,7 @@ std::string LSET::FmtHex() const
         if( nibble && !( nibble % 8 ) )
             ret += '_';
 
-        assert( ndx < DIM( hex ) );
+        assert( ndx < arrayDim( hex ) );
 
         ret += hex[ndx];
     }
@@ -468,7 +468,7 @@ LSEQ LSET::SeqStackupBottom2Top() const
         Edge_Cuts,
     };
 
-    return Seq( sequence, DIM( sequence ) );
+    return Seq( sequence, arrayDim( sequence ) );
 }
 
 
@@ -668,7 +668,7 @@ LSET LSET::InternalCuMask()
         In30_Cu,
     };
 
-    static const LSET saved( cu_internals, DIM( cu_internals ) );
+    static const LSET saved( cu_internals, arrayDim( cu_internals ) );
     return saved;
 }
 
diff --git a/common/marker_base.cpp b/common/marker_base.cpp
index e605f0e125..c77cdbb4c5 100644
--- a/common/marker_base.cpp
+++ b/common/marker_base.cpp
@@ -56,7 +56,7 @@ static const VECTOR2I MarkerShapeCorners[] =
     VECTOR2I( 1,  8 ),
     VECTOR2I( 0,  0 )
 };
-const unsigned CORNERS_COUNT = DIM( MarkerShapeCorners );
+const unsigned CORNERS_COUNT = arrayDim( MarkerShapeCorners );
 
 /*******************/
 /* Classe MARKER_BASE */
diff --git a/common/pgm_base.cpp b/common/pgm_base.cpp
index 19a6b943ef..4e4c358e0b 100644
--- a/common/pgm_base.cpp
+++ b/common/pgm_base.cpp
@@ -675,7 +675,7 @@ bool PGM_BASE::SetLanguage( bool first_time )
         m_common_settings->Read( languageCfgKey, &languageSel );
 
         // Search for the current selection
-        for( unsigned ii = 0; ii < DIM( s_Languages ); ii++ )
+        for( unsigned ii = 0; ii < arrayDim( s_Languages ); ii++ )
         {
             if( s_Languages[ii].m_Lang_Label == languageSel )
             {
@@ -717,7 +717,7 @@ bool PGM_BASE::SetLanguage( bool first_time )
         wxString languageSel;
 
         // Search for the current selection language name
-        for( unsigned ii = 0; ii < DIM( s_Languages ); ii++ )
+        for( unsigned ii = 0; ii < arrayDim( s_Languages ); ii++ )
         {
             if( s_Languages[ii].m_WX_Lang_Identifier == m_language_id )
             {
@@ -756,9 +756,9 @@ bool PGM_BASE::SetLanguage( bool first_time )
 void PGM_BASE::SetLanguageIdentifier( int menu_id )
 {
     wxLogTrace( traceLocale, "Select language ID %d from %d possible languages.",
-                menu_id, DIM( s_Languages ) );
+                menu_id, arrayDim( s_Languages ) );
 
-    for( unsigned ii = 0; ii < DIM( s_Languages ); ii++ )
+    for( unsigned ii = 0; ii < arrayDim( s_Languages ); ii++ )
     {
         if( menu_id == s_Languages[ii].m_KI_Lang_Identifier )
         {
@@ -814,7 +814,7 @@ void PGM_BASE::AddMenuLanguageList( wxMenu* MasterMenu )
 
     menu = new wxMenu;
 
-    for( unsigned ii = 0; ii < DIM( s_Languages ); ii++ )
+    for( unsigned ii = 0; ii < arrayDim( s_Languages ); ii++ )
     {
         wxString label;
 
@@ -835,7 +835,7 @@ void PGM_BASE::AddMenuLanguageList( wxMenu* MasterMenu )
                  KiBitmap( language_xpm ) );
 
     // Set Check mark on current selected language
-    for( unsigned ii = 0;  ii < DIM( s_Languages );  ii++ )
+    for( unsigned ii = 0;  ii < arrayDim( s_Languages );  ii++ )
     {
         if( m_language_id == s_Languages[ii].m_WX_Lang_Identifier )
             menu->Check( s_Languages[ii].m_KI_Lang_Identifier, true );
diff --git a/common/project.cpp b/common/project.cpp
index 7b93755b9f..15b783402e 100644
--- a/common/project.cpp
+++ b/common/project.cpp
@@ -48,7 +48,7 @@ void PROJECT::ElemsClear()
 {
     // careful here, this should work, but the virtual destructor may not
     // be in the same link image as PROJECT.
-    for( unsigned i = 0;  i < DIM( m_elems );  ++i )
+    for( unsigned i = 0;  i < arrayDim( m_elems );  ++i )
     {
         SetElem( ELEM_T( i ), NULL );
     }
@@ -172,7 +172,7 @@ void PROJECT::SetRString( RSTRING_T aIndex, const wxString& aString )
 {
     unsigned ndx = unsigned( aIndex );
 
-    if( ndx < DIM( m_rstrings ) )
+    if( ndx < arrayDim( m_rstrings ) )
     {
         m_rstrings[ndx] = aString;
     }
@@ -187,7 +187,7 @@ const wxString& PROJECT::GetRString( RSTRING_T aIndex )
 {
     unsigned ndx = unsigned( aIndex );
 
-    if( ndx < DIM( m_rstrings ) )
+    if( ndx < arrayDim( m_rstrings ) )
     {
         return m_rstrings[ndx];
     }
@@ -206,7 +206,7 @@ PROJECT::_ELEM* PROJECT::GetElem( ELEM_T aIndex )
 {
     // This is virtual, so implement it out of line
 
-    if( unsigned( aIndex ) < DIM( m_elems ) )
+    if( unsigned( aIndex ) < arrayDim( m_elems ) )
     {
         return m_elems[aIndex];
     }
@@ -218,7 +218,7 @@ void PROJECT::SetElem( ELEM_T aIndex, _ELEM* aElem )
 {
     // This is virtual, so implement it out of line
 
-    if( unsigned( aIndex ) < DIM( m_elems ) )
+    if( unsigned( aIndex ) < arrayDim( m_elems ) )
     {
 #if defined(DEBUG) && 0
         if( aIndex == ELEM_SCH_PART_LIBS )
diff --git a/common/tool/tool_dispatcher.cpp b/common/tool/tool_dispatcher.cpp
index 689a3cb501..54fe1bac56 100644
--- a/common/tool/tool_dispatcher.cpp
+++ b/common/tool/tool_dispatcher.cpp
@@ -268,7 +268,7 @@ bool isKeySpecialCode( int aKeyCode )
 
     bool isInList = false;
 
-    for( unsigned ii = 0; ii < DIM( special_keys ) && !isInList; ii++ )
+    for( unsigned ii = 0; ii < arrayDim( special_keys ) && !isInList; ii++ )
     {
         if( special_keys[ii] == aKeyCode )
             isInList = true;
diff --git a/eeschema/autoplace_fields.cpp b/eeschema/autoplace_fields.cpp
index ffed3ca7d1..33db9720e0 100644
--- a/eeschema/autoplace_fields.cpp
+++ b/eeschema/autoplace_fields.cpp
@@ -322,7 +322,7 @@ protected:
             { SIDE_LEFT,    pins_on_side( SIDE_LEFT ) },
             { SIDE_BOTTOM,  pins_on_side( SIDE_BOTTOM ) },
         };
-        std::vector<SIDE_AND_NPINS> sides( sides_init, sides_init + DIM( sides_init ) );
+        std::vector<SIDE_AND_NPINS> sides( sides_init, sides_init + arrayDim( sides_init ) );
 
         int orient = m_component->GetOrientation();
         int orient_angle = orient & 0xff; // enum is a bitmask
@@ -386,7 +386,7 @@ protected:
     std::vector<SIDE_AND_COLL> get_colliding_sides()
     {
         SIDE sides_init[] = { SIDE_RIGHT, SIDE_TOP, SIDE_LEFT, SIDE_BOTTOM };
-        std::vector<SIDE> sides( sides_init, sides_init + DIM( sides_init ) );
+        std::vector<SIDE> sides( sides_init, sides_init + arrayDim( sides_init ) );
         std::vector<SIDE_AND_COLL> colliding;
 
         // Iterate over all sides and find the ones that collide
diff --git a/eeschema/eeschema.cpp b/eeschema/eeschema.cpp
index 185f33e654..f7f4b37c61 100644
--- a/eeschema/eeschema.cpp
+++ b/eeschema/eeschema.cpp
@@ -174,7 +174,7 @@ static COLOR4D s_layerColor[LAYER_ID_COUNT];
 COLOR4D GetLayerColor( SCH_LAYER_ID aLayer )
 {
     unsigned layer = ( aLayer );
-    wxASSERT( layer < DIM( s_layerColor ) );
+    wxASSERT( layer < arrayDim( s_layerColor ) );
     return s_layerColor[layer];
 }
 
@@ -187,7 +187,7 @@ void SetLayerColor( COLOR4D aColor, SCH_LAYER_ID aLayer )
         aColor.Darken( 0.01 );
 
     unsigned layer = aLayer;
-    wxASSERT( layer < DIM( s_layerColor ) );
+    wxASSERT( layer < arrayDim( s_layerColor ) );
     s_layerColor[layer] = aColor;
 }
 
diff --git a/eeschema/lib_pin.cpp b/eeschema/lib_pin.cpp
index 4e89190fee..a04ed3d771 100644
--- a/eeschema/lib_pin.cpp
+++ b/eeschema/lib_pin.cpp
@@ -57,7 +57,7 @@ static const int pin_orientation_codes[] =
     PIN_UP,
     PIN_DOWN
 };
-#define PIN_ORIENTATION_CNT DIM( pin_orientation_codes )
+#define PIN_ORIENTATION_CNT arrayDim( pin_orientation_codes )
 
 // small margin in internal units between the pin text and the pin line
 #define PIN_TEXT_MARGIN 4
diff --git a/eeschema/sch_screen.cpp b/eeschema/sch_screen.cpp
index ef05a2f2e0..d0846e7ac7 100644
--- a/eeschema/sch_screen.cpp
+++ b/eeschema/sch_screen.cpp
@@ -99,10 +99,10 @@ SCH_SCREEN::SCH_SCREEN( KIWAY* aKiway ) :
 
     SetZoom( 32 );
 
-    for( unsigned i = 0; i < DIM( SchematicZoomList ); i++ )
+    for( unsigned i = 0; i < arrayDim( SchematicZoomList ); i++ )
         m_ZoomList.push_back( SchematicZoomList[i] );
 
-    for( unsigned i = 0; i < DIM( SchematicGridList ); i++ )
+    for( unsigned i = 0; i < arrayDim( SchematicGridList ); i++ )
         AddGrid( SchematicGridList[i] );
 
     SetGrid( wxRealPoint( 50, 50 ) );   // Default grid size.
diff --git a/gerbview/dialogs/panel_gerbview_settings.cpp b/gerbview/dialogs/panel_gerbview_settings.cpp
index c4bb8bb336..5319c95275 100644
--- a/gerbview/dialogs/panel_gerbview_settings.cpp
+++ b/gerbview/dialogs/panel_gerbview_settings.cpp
@@ -41,7 +41,7 @@ bool PANEL_GERBVIEW_SETTINGS::TransferDataToWindow( )
     m_BoxUnits->SetSelection( m_Parent->GetUserUnits() ? 1 : 0 );
     m_ShowPageLimitsOpt->SetValue( m_Parent->m_DisplayOptions.m_DisplayPageLimits );
 
-    for( unsigned i = 0;  i < DIM( g_GerberPageSizeList );  ++i )
+    for( unsigned i = 0;  i < arrayDim( g_GerberPageSizeList );  ++i )
     {
         if( g_GerberPageSizeList[i] == m_Parent->GetPageSettings().GetType() )
         {
diff --git a/gerbview/gbr_screen.cpp b/gerbview/gbr_screen.cpp
index 0d113071b1..f8d186ef0b 100644
--- a/gerbview/gbr_screen.cpp
+++ b/gerbview/gbr_screen.cpp
@@ -103,10 +103,10 @@ static GRID_TYPE gbrGridList[] =
 GBR_SCREEN::GBR_SCREEN( const wxSize& aPageSizeIU ) :
     BASE_SCREEN( SCREEN_T )
 {
-    for( unsigned i = 0; i < DIM( gbrZoomList );  ++i )
+    for( unsigned i = 0; i < arrayDim( gbrZoomList );  ++i )
         m_ZoomList.push_back( gbrZoomList[i] );
 
-    for( unsigned i = 0; i < DIM( gbrGridList );  ++i )
+    for( unsigned i = 0; i < arrayDim( gbrGridList );  ++i )
         AddGrid( gbrGridList[i] );
 
     // Set the working grid size to a reasonable value
diff --git a/gerbview/gerber_file_image.cpp b/gerbview/gerber_file_image.cpp
index e1ca42fe11..e40ced885f 100644
--- a/gerbview/gerber_file_image.cpp
+++ b/gerbview/gerber_file_image.cpp
@@ -101,7 +101,7 @@ GERBER_FILE_IMAGE::GERBER_FILE_IMAGE( int aLayer ) :
 
     ResetDefaultValues();
 
-    for( unsigned ii = 0; ii < DIM( m_Aperture_List ); ii++ )
+    for( unsigned ii = 0; ii < arrayDim( m_Aperture_List ); ii++ )
         m_Aperture_List[ii] = 0;
 }
 
@@ -110,7 +110,7 @@ GERBER_FILE_IMAGE::~GERBER_FILE_IMAGE()
 {
     m_Drawings.DeleteAll();
 
-    for( unsigned ii = 0; ii < DIM( m_Aperture_List ); ii++ )
+    for( unsigned ii = 0; ii < arrayDim( m_Aperture_List ); ii++ )
     {
         delete m_Aperture_List[ii];
     }
@@ -132,7 +132,7 @@ D_CODE* GERBER_FILE_IMAGE::GetDCODEOrCreate( int aDCODE, bool aCreateIfNoExist )
 {
     unsigned ndx = aDCODE - FIRST_DCODE;
 
-    if( ndx < (unsigned) DIM( m_Aperture_List ) )
+    if( ndx < (unsigned) arrayDim( m_Aperture_List ) )
     {
         // lazily create the D_CODE if it does not exist.
         if( aCreateIfNoExist )
@@ -152,7 +152,7 @@ D_CODE* GERBER_FILE_IMAGE::GetDCODE( int aDCODE ) const
 {
     unsigned ndx = aDCODE - FIRST_DCODE;
 
-    if( ndx < (unsigned) DIM( m_Aperture_List ) )
+    if( ndx < (unsigned) arrayDim( m_Aperture_List ) )
     {
         return m_Aperture_List[ndx];
     }
@@ -266,7 +266,7 @@ int GERBER_FILE_IMAGE::GetDcodesCount()
 {
     int count = 0;
 
-    for( unsigned ii = 0; ii < DIM( m_Aperture_List ); ii++ )
+    for( unsigned ii = 0; ii < arrayDim( m_Aperture_List ); ii++ )
     {
         if( m_Aperture_List[ii] )
             if( m_Aperture_List[ii]->m_InUse || m_Aperture_List[ii]->m_Defined )
diff --git a/gerbview/gerbview_config.cpp b/gerbview/gerbview_config.cpp
index 64cf8868c9..5580efd74d 100644
--- a/gerbview/gerbview_config.cpp
+++ b/gerbview/gerbview_config.cpp
@@ -123,10 +123,10 @@ PARAM_CFG_ARRAY& GERBVIEW_FRAME::GetConfigurationSettings()
         wxT("ColorLayer28Ex"), wxT("ColorLayer29Ex"), wxT("ColorLayer30Ex"), wxT("ColorLayer31Ex"),
     };
 
-    wxASSERT( DIM(keys) == DIM(color_default) );
-    wxASSERT( DIM(keys) <= DIM(g_ColorsSettings.m_LayersColors) && DIM(keys) <= DIM(color_default) );
+    wxASSERT( arrayDim(keys) == arrayDim(color_default) );
+    wxASSERT( arrayDim(keys) <= arrayDim(g_ColorsSettings.m_LayersColors) && arrayDim(keys) <= arrayDim(color_default) );
 
-    for( unsigned i = 0; i < DIM(keys);  ++i )
+    for( unsigned i = 0; i < arrayDim(keys);  ++i )
     {
         COLOR4D* prm = &g_ColorsSettings.m_LayersColors[ GERBER_DRAW_LAYER( i ) ];
 
diff --git a/gerbview/gerbview_layer_widget.cpp b/gerbview/gerbview_layer_widget.cpp
index 78ebe98516..2fda30193c 100644
--- a/gerbview/gerbview_layer_widget.cpp
+++ b/gerbview/gerbview_layer_widget.cpp
@@ -121,7 +121,7 @@ void GERBER_LAYER_WIDGET::ReFillRender()
         RR( _( "Background" ),       LAYER_PCB_BACKGROUND,   BLACK,    _( "PCB Background" ), true, false )
     };
 
-    for( unsigned row=0;  row<DIM(renderRows);  ++row )
+    for( unsigned row=0;  row<arrayDim(renderRows);  ++row )
     {
         if( renderRows[row].color != COLOR4D::UNSPECIFIED )       // does this row show a color?
             renderRows[row].color = myframe->GetVisibleElementColor( renderRows[row].id );
@@ -130,7 +130,7 @@ void GERBER_LAYER_WIDGET::ReFillRender()
             renderRows[row].state = myframe->IsElementVisible( renderRows[row].id );
     }
 
-    AppendRenderRows( renderRows, DIM(renderRows) );
+    AppendRenderRows( renderRows, arrayDim(renderRows) );
 }
 
 
diff --git a/include/macros.h b/include/macros.h
index f794b26696..d5a5cf7706 100644
--- a/include/macros.h
+++ b/include/macros.h
@@ -94,9 +94,12 @@ static inline const wxChar* GetChars( const wxString& s )
     return (const wxChar*) s.c_str();
 }
 
-/// # of elements in an array
-#define DIM( x )    unsigned( sizeof(x) / sizeof( (x)[0] ) )    // not size_t
-
+/// # of elements in an array.  This implements type-safe compile time checking
+template <typename T, std::size_t N>
+constexpr std::size_t arrayDim(T const (&)[N]) noexcept
+{
+    return N;
+}
 
 /**
  * Function MIRROR
diff --git a/kicad/files-io.cpp b/kicad/files-io.cpp
index 104abfd640..be98b922db 100644
--- a/kicad/files-io.cpp
+++ b/kicad/files-io.cpp
@@ -188,7 +188,7 @@ void KICAD_MANAGER_FRAME::OnArchiveFiles( wxCommandEvent& event )
 
 	wxArrayString files;
 
-    for( unsigned ii = 0; ii < DIM( extentionList ); ii++ )
+    for( unsigned ii = 0; ii < arrayDim( extentionList ); ii++ )
         wxDir::GetAllFiles( currdirname, &files, extentionList[ii] );
 
     files.Sort();
diff --git a/pagelayout_editor/pl_editor_frame.cpp b/pagelayout_editor/pl_editor_frame.cpp
index ea279f73c6..5a9057f0b2 100644
--- a/pagelayout_editor/pl_editor_frame.cpp
+++ b/pagelayout_editor/pl_editor_frame.cpp
@@ -120,7 +120,7 @@ PL_EDITOR_FRAME::PL_EDITOR_FRAME( KIWAY* aKiway, wxWindow* aParent ) :
         GetTextSize( _( "Inches" ), stsbar ).x + 10
     };
 
-    SetStatusWidths( DIM( dims ), dims );
+    SetStatusWidths( arrayDim( dims ), dims );
 
     m_auimgr.SetManagedWindow( this );
     m_auimgr.SetArtProvider( new EDA_DOCKART( this ) );
diff --git a/pagelayout_editor/pl_editor_screen.cpp b/pagelayout_editor/pl_editor_screen.cpp
index 8a412ed7bc..72566e9d96 100644
--- a/pagelayout_editor/pl_editor_screen.cpp
+++ b/pagelayout_editor/pl_editor_screen.cpp
@@ -80,10 +80,10 @@ static GRID_TYPE pl_editorGridList[] =
 PL_EDITOR_SCREEN::PL_EDITOR_SCREEN( const wxSize& aPageSizeIU ) :
     BASE_SCREEN( SCREEN_T )
 {
-    for( unsigned i = 0; i < DIM( pl_editorZoomList );  ++i )
+    for( unsigned i = 0; i < arrayDim( pl_editorZoomList );  ++i )
         m_ZoomList.push_back( pl_editorZoomList[i] );
 
-    for( unsigned i = 0; i < DIM( pl_editorGridList );  ++i )
+    for( unsigned i = 0; i < arrayDim( pl_editorGridList );  ++i )
         AddGrid( pl_editorGridList[i] );
 
     // pl_editor uses the same frame position as schematic and board editors
diff --git a/pcbnew/board_items_to_polygon_shape_transform.cpp b/pcbnew/board_items_to_polygon_shape_transform.cpp
index dde7a7e64b..78ec4a5f5a 100644
--- a/pcbnew/board_items_to_polygon_shape_transform.cpp
+++ b/pcbnew/board_items_to_polygon_shape_transform.cpp
@@ -1303,7 +1303,7 @@ void    CreateThermalReliefPadPolygon( SHAPE_POLY_SET& aCornerBuffer,
 
         stub.NewOutline();
 
-        for( unsigned ii = 0; ii < DIM( stubBuffer ); ii++ )
+        for( unsigned ii = 0; ii < arrayDim( stubBuffer ); ii++ )
         {
             wxPoint cpos = stubBuffer[ii];
             RotatePoint( &cpos, aPad.GetOrientation() );
@@ -1325,7 +1325,7 @@ void    CreateThermalReliefPadPolygon( SHAPE_POLY_SET& aCornerBuffer,
         stub.RemoveAllContours();
         stub.NewOutline();
 
-        for( unsigned ii = 0; ii < DIM( stubBuffer ); ii++ )
+        for( unsigned ii = 0; ii < arrayDim( stubBuffer ); ii++ )
         {
             wxPoint cpos = stubBuffer[ii];
             RotatePoint( &cpos, aPad.GetOrientation() );
diff --git a/pcbnew/class_board.cpp b/pcbnew/class_board.cpp
index 66b8488b97..1f5d2b6ceb 100644
--- a/pcbnew/class_board.cpp
+++ b/pcbnew/class_board.cpp
@@ -558,7 +558,7 @@ void BOARD::PopHighLight()
 
 bool BOARD::SetLayerDescr( PCB_LAYER_ID aIndex, const LAYER& aLayer )
 {
-    if( unsigned( aIndex ) < DIM( m_Layer ) )
+    if( unsigned( aIndex ) < arrayDim( m_Layer ) )
     {
         m_Layer[ aIndex ] = aLayer;
         return true;
diff --git a/pcbnew/dialogs/dialog_create_array.cpp b/pcbnew/dialogs/dialog_create_array.cpp
index a374b8f92d..a93696fb2d 100644
--- a/pcbnew/dialogs/dialog_create_array.cpp
+++ b/pcbnew/dialogs/dialog_create_array.cpp
@@ -62,8 +62,8 @@ DIALOG_CREATE_ARRAY::DIALOG_CREATE_ARRAY( PCB_BASE_FRAME* aParent, bool enableNu
         _( "Alphabet, minus IOSQXZ" ),
         _( "Alphabet, full 26 characters" )
     };
-    m_choicePriAxisNumbering->Set( DIM( charSetDescriptions ), charSetDescriptions );
-    m_choiceSecAxisNumbering->Set( DIM( charSetDescriptions ), charSetDescriptions );
+    m_choicePriAxisNumbering->Set( arrayDim( charSetDescriptions ), charSetDescriptions );
+    m_choiceSecAxisNumbering->Set( arrayDim( charSetDescriptions ), charSetDescriptions );
 
     m_choicePriAxisNumbering->SetSelection( 0 );
     m_choiceSecAxisNumbering->SetSelection( 0 );
diff --git a/pcbnew/dialogs/dialog_export_svg.cpp b/pcbnew/dialogs/dialog_export_svg.cpp
index 5015b0a51f..268c7dd922 100644
--- a/pcbnew/dialogs/dialog_export_svg.cpp
+++ b/pcbnew/dialogs/dialog_export_svg.cpp
@@ -126,7 +126,7 @@ DIALOG_EXPORT_SVG::~DIALOG_EXPORT_SVG()
 
         wxString layerKey;
 
-        for( unsigned layer = 0; layer < DIM(m_boxSelectLayer);  ++layer )
+        for( unsigned layer = 0; layer < arrayDim(m_boxSelectLayer);  ++layer )
         {
             if( !m_boxSelectLayer[layer].first )
                 continue;
@@ -194,7 +194,7 @@ LSET DIALOG_EXPORT_SVG::getCheckBoxSelectedLayers() const
 {
     LSET ret;
 
-    for( unsigned layer = 0; layer < DIM(m_boxSelectLayer);  ++layer )
+    for( unsigned layer = 0; layer < arrayDim(m_boxSelectLayer);  ++layer )
     {
         if( !m_boxSelectLayer[layer].first )
             continue;
diff --git a/pcbnew/dialogs/dialog_gendrill.cpp b/pcbnew/dialogs/dialog_gendrill.cpp
index 3f4e7ff488..61a65a18d3 100644
--- a/pcbnew/dialogs/dialog_gendrill.cpp
+++ b/pcbnew/dialogs/dialog_gendrill.cpp
@@ -376,7 +376,7 @@ void DIALOG_GENDRILL::GenDrillAndMapFiles( bool aGenDrill, bool aGenMap )
 
     unsigned choice = (unsigned) m_Choice_Drill_Map->GetSelection();
 
-    if( choice >= DIM( filefmt ) )
+    if( choice >= arrayDim( filefmt ) )
         choice = 1;
 
     // Create output directory if it does not exist (also transform it in
diff --git a/pcbnew/dialogs/dialog_pad_properties.cpp b/pcbnew/dialogs/dialog_pad_properties.cpp
index bccb0c5e90..c2bd7eeb27 100644
--- a/pcbnew/dialogs/dialog_pad_properties.cpp
+++ b/pcbnew/dialogs/dialog_pad_properties.cpp
@@ -975,7 +975,7 @@ void DIALOG_PAD_PROPERTIES::PadTypeSelected( wxCommandEvent& event )
 {
     int ii = m_PadType->GetSelection();
 
-    if( (unsigned)ii >= DIM( code_type ) ) // catches < 0 also
+    if( (unsigned)ii >= arrayDim( code_type ) ) // catches < 0 also
         ii = 0;
 
     bool hasHole, hasConnection;
@@ -1025,7 +1025,7 @@ void DIALOG_PAD_PROPERTIES::OnUpdateUI( wxUpdateUIEvent& event )
 {
     int ii = m_PadType->GetSelection();
 
-    if( (unsigned)ii >= DIM( code_type ) ) // catches < 0 also
+    if( (unsigned)ii >= arrayDim( code_type ) ) // catches < 0 also
         ii = 0;
 
     bool hasHole, hasConnection;
@@ -1905,7 +1905,7 @@ void DIALOG_PAD_PROPERTIES::onAddPrimitive( wxCommandEvent& event )
     wxString shapelist[] = { _( "Segment" ), _( "Arc" ), _( "Ring/Circle" ), _( "Polygon" ) };
 
     int type = wxGetSingleChoiceIndex( _( "Shape type:" ), _( "Add Primitive" ),
-                                       DIM( shapelist ), shapelist, 0, this );
+                                       arrayDim( shapelist ), shapelist, 0, this );
 
     STROKE_T listtype[] = { S_SEGMENT, S_ARC, S_CIRCLE, S_POLYGON };
 
diff --git a/pcbnew/dialogs/dialog_print_pcbnew.cpp b/pcbnew/dialogs/dialog_print_pcbnew.cpp
index eb0c9bd17f..00c1d54d6f 100644
--- a/pcbnew/dialogs/dialog_print_pcbnew.cpp
+++ b/pcbnew/dialogs/dialog_print_pcbnew.cpp
@@ -269,7 +269,7 @@ void DIALOG_PRINT_PCBNEW::setListBoxValue( wxCheckListBox* aList, bool aValue )
 
 bool DIALOG_PRINT_PCBNEW::isLayerEnabled( unsigned int aLayer ) const
 {
-    wxCHECK( aLayer < DIM( m_layers ), false );
+    wxCHECK( aLayer < arrayDim( m_layers ), false );
     const auto& layerInfo = m_layers[aLayer];
 
     if( layerInfo.first )
@@ -281,7 +281,7 @@ bool DIALOG_PRINT_PCBNEW::isLayerEnabled( unsigned int aLayer ) const
 
 void DIALOG_PRINT_PCBNEW::enableLayer( unsigned int aLayer, bool aValue )
 {
-    wxCHECK( aLayer < DIM( m_layers ), /* void */ );
+    wxCHECK( aLayer < arrayDim( m_layers ), /* void */ );
     const auto& layerInfo = m_layers[aLayer];
     layerInfo.first->Check( layerInfo.second, aValue );
 }
@@ -293,7 +293,7 @@ int DIALOG_PRINT_PCBNEW::setLayerSetFromList()
     int& pageCount = settings()->m_pageCount;
     pageCount = 0;
 
-    for( unsigned int layer = 0; layer < DIM( m_layers ); ++layer )
+    for( unsigned int layer = 0; layer < arrayDim( m_layers ); ++layer )
     {
         if( isLayerEnabled( layer ) )
         {
diff --git a/pcbnew/dialogs/panel_setup_layers.cpp b/pcbnew/dialogs/panel_setup_layers.cpp
index 0bc48a56d8..2f67dd1b48 100644
--- a/pcbnew/dialogs/panel_setup_layers.cpp
+++ b/pcbnew/dialogs/panel_setup_layers.cpp
@@ -104,7 +104,7 @@ static LSEQ dlg_layers()
         Dwgs_User,
     };
 
-    return LSEQ( layers, layers + DIM( layers ) );
+    return LSEQ( layers, layers + arrayDim( layers ) );
 }
 
 
@@ -315,7 +315,7 @@ void PANEL_SETUP_LAYERS::showPresets( LSET enabledLayers )
 {
     int presetsNdx = 0;     // the "Custom" setting, matches nothing
 
-    for( unsigned i=1; i<DIM( presets );  ++i )
+    for( unsigned i=1; i<arrayDim( presets );  ++i )
     {
         if( enabledLayers == presets[i] )
         {
@@ -460,7 +460,7 @@ void PANEL_SETUP_LAYERS::OnPresetsChoice( wxCommandEvent& event )
     if( presetNdx <= 0 )        // the Custom setting controls nothing.
         return;
 
-    if( presetNdx < (int) DIM(presets) )
+    if( presetNdx < (int) arrayDim(presets) )
     {
         m_enabledLayers = presets[ presetNdx ];
         LSET copperSet = m_enabledLayers & LSET::AllCuMask();
diff --git a/pcbnew/eagle_plugin.cpp b/pcbnew/eagle_plugin.cpp
index 4bcfa5d6ec..34510224f1 100644
--- a/pcbnew/eagle_plugin.cpp
+++ b/pcbnew/eagle_plugin.cpp
@@ -320,7 +320,7 @@ void EAGLE_PLUGIN::clear_cu_map()
 {
     // All cu layers are invalid until we see them in the <layers> section while
     // loading either a board or library.  See loadLayerDefs().
-    for( unsigned i = 0;  i < DIM(m_cu_map);  ++i )
+    for( unsigned i = 0;  i < arrayDim(m_cu_map);  ++i )
         m_cu_map[i] = -1;
 }
 
@@ -435,7 +435,7 @@ void EAGLE_PLUGIN::loadLayerDefs( wxXmlNode* aLayers )
 
 #if 0 && defined(DEBUG)
     printf( "m_cu_map:\n" );
-    for( unsigned i=0; i<DIM(m_cu_map);  ++i )
+    for( unsigned i=0; i<arrayDim(m_cu_map);  ++i )
     {
         printf( "\t[%d]:%d\n", i, m_cu_map[i] );
     }
@@ -2165,7 +2165,7 @@ PCB_LAYER_ID EAGLE_PLUGIN::kicad_layer( int aEagleLayer ) const
     int kiLayer;
 
     // eagle copper layer:
-    if( aEagleLayer >= 1 && aEagleLayer < int( DIM( m_cu_map ) ) )
+    if( aEagleLayer >= 1 && aEagleLayer < int( arrayDim( m_cu_map ) ) )
     {
         kiLayer = m_cu_map[aEagleLayer];
     }
diff --git a/pcbnew/exporters/export_d356.cpp b/pcbnew/exporters/export_d356.cpp
index f40486236f..6bb616c04f 100644
--- a/pcbnew/exporters/export_d356.cpp
+++ b/pcbnew/exporters/export_d356.cpp
@@ -391,7 +391,7 @@ void PCB_EDIT_FRAME::GenD356File( wxCommandEvent& aEvent )
     // CUST 1 would be metric but gerbtool simply ignores it!
     fprintf( file, "P  CODE 00\n" );
     fprintf( file, "P  UNITS CUST 0\n" );
-    fprintf( file, "P  DIM   N\n" );
+    fprintf( file, "P  arrayDim   N\n" );
     write_D356_records( d356_records, file );
     fprintf( file, "999\n" );
 
diff --git a/pcbnew/exporters/export_gencad.cpp b/pcbnew/exporters/export_gencad.cpp
index 160135f2cf..30f8d72ff0 100644
--- a/pcbnew/exporters/export_gencad.cpp
+++ b/pcbnew/exporters/export_gencad.cpp
@@ -660,7 +660,7 @@ static void CreatePadsShapesSection( FILE* aFile, BOARD* aPcb )
                  fmt_mask( mask ).c_str(),
                  via->GetDrillValue() / SCALE_FACTOR );
 
-        for( LSEQ seq = mask.Seq( gc_seq, DIM( gc_seq ) );  seq;  ++seq )
+        for( LSEQ seq = mask.Seq( gc_seq, arrayDim( gc_seq ) );  seq;  ++seq )
         {
             PCB_LAYER_ID layer = *seq;
 
@@ -687,7 +687,7 @@ static void CreatePadsShapesSection( FILE* aFile, BOARD* aPcb )
         LSET pad_set = pad->GetLayerSet() & master_layermask;
 
         // the special gc_seq
-        for( LSEQ seq = pad_set.Seq( gc_seq, DIM( gc_seq ) );  seq;  ++seq )
+        for( LSEQ seq = pad_set.Seq( gc_seq, arrayDim( gc_seq ) );  seq;  ++seq )
         {
             PCB_LAYER_ID layer = *seq;
 
diff --git a/pcbnew/exporters/export_vrml.cpp b/pcbnew/exporters/export_vrml.cpp
index edbc5250df..3503a5d6d9 100644
--- a/pcbnew/exporters/export_vrml.cpp
+++ b/pcbnew/exporters/export_vrml.cpp
@@ -177,7 +177,7 @@ public:
 
     MODEL_VRML() : m_OutputPCB( (SGNODE*) NULL )
     {
-        for( unsigned i = 0; i < DIM( m_layer_z );  ++i )
+        for( unsigned i = 0; i < arrayDim( m_layer_z );  ++i )
             m_layer_z[i] = 0;
 
         m_holes.GetArcParams( m_iMaxSeg, m_arcMinLen, m_arcMaxLen );
@@ -254,7 +254,7 @@ public:
 
     double GetLayerZ( LAYER_NUM aLayer )
     {
-        if( unsigned( aLayer ) >= DIM( m_layer_z ) )
+        if( unsigned( aLayer ) >= arrayDim( m_layer_z ) )
             return 0;
 
         return m_layer_z[ aLayer ];
diff --git a/pcbnew/files.cpp b/pcbnew/files.cpp
index 4b093af718..b19c892f72 100644
--- a/pcbnew/files.cpp
+++ b/pcbnew/files.cpp
@@ -117,7 +117,7 @@ bool AskLoadBoardFileName( wxWindow* aParent, int* aCtl, wxString* aFileName, bo
     }
     else
     {
-        for( unsigned ii = 2; ii < DIM( loaders ); ++ii )
+        for( unsigned ii = 2; ii < arrayDim( loaders ); ++ii )
         {
             if( !fileFilters.IsEmpty() )
                 fileFilters += wxChar( '|' );
diff --git a/pcbnew/kicad_plugin.cpp b/pcbnew/kicad_plugin.cpp
index 4ccc2e94dd..2c40cd6681 100644
--- a/pcbnew/kicad_plugin.cpp
+++ b/pcbnew/kicad_plugin.cpp
@@ -657,7 +657,7 @@ void PCB_IO::formatBoardLayers( BOARD* aBoard, int aNestLevel ) const
         F_Fab
     };
 
-    for( LSEQ seq = aBoard->GetEnabledLayers().Seq( non_cu, DIM( non_cu ) );  seq;  ++seq )
+    for( LSEQ seq = aBoard->GetEnabledLayers().Seq( non_cu, arrayDim( non_cu ) );  seq;  ++seq )
     {
         PCB_LAYER_ID layer = *seq;
 
diff --git a/pcbnew/layer_widget.cpp b/pcbnew/layer_widget.cpp
index 4269a4a408..73f78bad50 100644
--- a/pcbnew/layer_widget.cpp
+++ b/pcbnew/layer_widget.cpp
@@ -890,7 +890,7 @@ public:
             LAYER_WIDGET::ROW( wxT("layer_4_you"), 3, BLUE, wxT("BLUE"), false ),
         };
 
-        lw->AppendLayerRows( layerRows, DIM(layerRows) );
+        lw->AppendLayerRows( layerRows, arrayDim(layerRows) );
 
         // add some render rows
         static const LAYER_WIDGET::ROW renderRows[] = {
@@ -899,7 +899,7 @@ public:
             LAYER_WIDGET::ROW( wxT("With Oval Eyes"), 1, BROWN, wxT("My eyes are upon you") ),
         };
 
-        lw->AppendRenderRows( renderRows, DIM(renderRows) );
+        lw->AppendRenderRows( renderRows, arrayDim(renderRows) );
 
         lw->SelectLayerRow( 1 );
 
diff --git a/pcbnew/pcb_edit_frame.cpp b/pcbnew/pcb_edit_frame.cpp
index 7d3b6c6afc..d635d6bff2 100644
--- a/pcbnew/pcb_edit_frame.cpp
+++ b/pcbnew/pcb_edit_frame.cpp
@@ -707,7 +707,7 @@ void PCB_EDIT_FRAME::enableGALSpecificMenus()
 
         bool enbl = IsGalCanvasActive();
 
-        for( unsigned ii = 0; ii < DIM( id_list ); ii++ )
+        for( unsigned ii = 0; ii < arrayDim( id_list ); ii++ )
         {
             if( GetMenuBar()->FindItem( id_list[ii] ) )
                 GetMenuBar()->FindItem( id_list[ii] )->Enable( enbl );
diff --git a/pcbnew/pcb_layer_widget.cpp b/pcbnew/pcb_layer_widget.cpp
index 2a179dbc5b..fbef1ab1b0 100644
--- a/pcbnew/pcb_layer_widget.cpp
+++ b/pcbnew/pcb_layer_widget.cpp
@@ -145,7 +145,7 @@ COLOR4D PCB_LAYER_WIDGET::getBackgroundLayerColor()
 
 bool PCB_LAYER_WIDGET::isAllowedInFpMode( int aId )
 {
-    for( unsigned ii = 0; ii < DIM( s_allowed_in_FpEditor ); ii++ )
+    for( unsigned ii = 0; ii < arrayDim( s_allowed_in_FpEditor ); ii++ )
         if( s_allowed_in_FpEditor[ii] == aId )
             return true;
 
@@ -394,7 +394,7 @@ void PCB_LAYER_WIDGET::ReFillRender()
     // Because s_render_rows is created static, we must explicitly call
     // wxGetTranslation for texts which are internationalized (tool tips
     // and item names)
-    for( unsigned row=0;  row<DIM(s_render_rows);  ++row )
+    for( unsigned row=0;  row<arrayDim(s_render_rows);  ++row )
     {
         LAYER_WIDGET::ROW renderRow = s_render_rows[row];
 
@@ -554,7 +554,7 @@ void PCB_LAYER_WIDGET::ReFill()
         { B_Fab,            _( "Footprint assembly on board's back" ) }
     };
 
-    for( unsigned i=0;  i<DIM( non_cu_seq );  ++i )
+    for( unsigned i=0;  i<arrayDim( non_cu_seq );  ++i )
     {
         PCB_LAYER_ID layer = non_cu_seq[i].layerId;
 
diff --git a/pcbnew/pcb_painter.cpp b/pcbnew/pcb_painter.cpp
index 63e9d22f5a..348fdad9c1 100644
--- a/pcbnew/pcb_painter.cpp
+++ b/pcbnew/pcb_painter.cpp
@@ -62,7 +62,7 @@ PCB_RENDER_SETTINGS::PCB_RENDER_SETTINGS()
     m_selectionCandidateColor = COLOR4D( 0.0, 1.0, 0.0, 0.75 );
 
     // By default everything should be displayed as filled
-    for( unsigned int i = 0; i < DIM( m_sketchMode ); ++i )
+    for( unsigned int i = 0; i < arrayDim( m_sketchMode ); ++i )
     {
         m_sketchMode[i] = false;
     }
diff --git a/pcbnew/pcb_screen.cpp b/pcbnew/pcb_screen.cpp
index 2d9bf960b7..ebbc66a189 100644
--- a/pcbnew/pcb_screen.cpp
+++ b/pcbnew/pcb_screen.cpp
@@ -160,13 +160,10 @@ static GRID_TYPE pcbGridList[] =
 PCB_SCREEN::PCB_SCREEN( const wxSize& aPageSizeIU ) :
     BASE_SCREEN( SCREEN_T )
 {
-    // D(wxSize displayz = wxGetDisplaySize();)
-    // D(printf( "displayz x:%d y:%d lastZoomFactor: %.16g\n", displayz.x, displayz.y, pcbZoomList[DIM(pcbZoomList)-1] );)
-
-    for( unsigned i = 0; i < DIM( pcbZoomList );  ++i )
+    for( unsigned i = 0; i < arrayDim( pcbZoomList );  ++i )
         m_ZoomList.push_back( pcbZoomList[i] );
 
-    for( unsigned i = 0; i < DIM( pcbGridList );  ++i )
+    for( unsigned i = 0; i < arrayDim( pcbGridList );  ++i )
         AddGrid( pcbGridList[i] );
 
     // Set the working grid size to a reasonable value (in 1/10000 inch)
@@ -190,5 +187,5 @@ PCB_SCREEN::~PCB_SCREEN()
 
 int PCB_SCREEN::MilsToIuScalar()
 {
-    return (int)IU_PER_MILS;
+    return static_cast<int>( IU_PER_MILS );
 }
diff --git a/pcbnew/plot_board_layers.cpp b/pcbnew/plot_board_layers.cpp
index f33fd73798..e6592e510c 100644
--- a/pcbnew/plot_board_layers.cpp
+++ b/pcbnew/plot_board_layers.cpp
@@ -672,7 +672,7 @@ void PlotLayerOutlines( BOARD* aBoard, PLOTTER* aPlotter,
 
     SHAPE_POLY_SET outlines;
 
-    for( LSEQ seq = aLayerMask.Seq( plot_seq, DIM( plot_seq ) );  seq;  ++seq )
+    for( LSEQ seq = aLayerMask.Seq( plot_seq, arrayDim( plot_seq ) );  seq;  ++seq )
     {
         PCB_LAYER_ID layer = *seq;