← Back to team overview

kicad-developers team mailing list archive

Re: PATCH: fix kicad2step non-ASCII filename problem in MinGW

 

The attached patch is the latest version and fixes the non-ASCII
filename chars issue in kicad2step. If OCE is built in MinGW
with the hack I provided then everything works as expected
under Windows+MinGW.

The sole difference with the previous patch is that kicad2step.cpp
was modified to replace:

wxIMPLEMENT_APP_CONSOLE( KICAD2MCAD );

with

wxIMPLEMENT_APP( KICAD2MCAD );

I don't understand why one works but not the other. The
CONSOLE version results in a main( int, char** ) type of
entry point and the Windows CRT converts UTF16 to ASCII,
which naturally fails for most UTF16 code points. I presume
a similar problem will exist for other external tools such as
idf2vrml.

I guess for now we have to look at this as a workaround for
a bug in wxWidgets + MinGW. I suspect things start to go
wrong in wx/app.h:

#if wxUSE_UNICODE && defined(__VISUALC__)
    #define wxIMPLEMENT_WXWIN_MAIN_CONSOLE                                    \
        int wmain(int argc, wchar_t **argv)                                   \
        {                                                                     \
            wxDISABLE_DEBUG_SUPPORT();                                        \
                                                                              \
            return wxEntry(argc, argv);                                       \
        }
#else // Use standard main()
    #define wxIMPLEMENT_WXWIN_MAIN_CONSOLE                                    \
        int main(int argc, char **argv)                                       \
        {                                                                     \
            wxDISABLE_DEBUG_SUPPORT();                                        \
                                                                              \
            return wxEntry(argc, argv);                                       \
        }
#endif

I tried to change #if wxUSE_UNICODE && defined(__VISUALC__)
to include __MINGW32__ and __MINGW64__, but a subsequent
build told me WinMain() was not found. I gave up trying to find
the specific problem at that point.

- Cirilo


On Sun, Mar 5, 2017 at 3:52 PM, Cirilo Bernardo
<cirilo.bernardo@xxxxxxxxx> wrote:
> This patch fixes the non-ASCII filename problem in
> Windows+MinGW.
>
> When this patch is applied, and OCE is built under
> MinGW with the patch I previously supplied, all
> 3D model viewing and STEP export appear to work
> mostly as expected. There is one remaining
> issue to resolve which is correctly passing the
> UTF8 filename from pcbnew to kicad2step and
> ensuring that the appropriate output file is created.
> At the moment I'm not sure if the problem is in
> kicad2step or my hack of OCE, but I'm looking
> into it.
>
> - Cirilo
From ad6caa2edbec0eaf6c22df4b6c22b1ebf22de329 Mon Sep 17 00:00:00 2001
From: Cirilo Bernardo <cirilo.bernardo@xxxxxxxxx>
Date: Sun, 5 Mar 2017 15:22:31 +1100
Subject: [PATCH 1/2] Fix kicad2step non-ASCII filename characters issue in
 Windows+MinGW

---
 utils/kicad2step/CMakeLists.txt      | 13 ++++++++++---
 utils/kicad2step/pcb/kicadmodule.cpp |  3 ++-
 utils/kicad2step/pcb/oce_utils.cpp   | 10 +++++-----
 3 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/utils/kicad2step/CMakeLists.txt b/utils/kicad2step/CMakeLists.txt
index bdfff3390..a60153f42 100644
--- a/utils/kicad2step/CMakeLists.txt
+++ b/utils/kicad2step/CMakeLists.txt
@@ -1,13 +1,14 @@
 include_directories( BEFORE
-        pcb
-        ${CMAKE_CURRENT_SOURCE_DIR}
+    pcb
+    ${CMAKE_CURRENT_SOURCE_DIR}
+    ${CMAKE_SOURCE_DIR}/include
 )
 
 include_directories( SYSTEM 
     ${OCE_INCLUDE_DIRS}
 )
 
-add_executable( kicad2step
+set( K2S_FILES
     kicad2step.cpp
     pcb/3d_resolver.cpp
     pcb/base.cpp
@@ -21,6 +22,12 @@ add_executable( kicad2step
     sexpr/sexpr_parser.cpp
 )
 
+if( MINGW )
+    list( APPEND K2S_FILES ${CMAKE_SOURCE_DIR}/common/streamwrapper.cpp )
+endif( MINGW )
+
+add_executable( kicad2step ${K2S_FILES} )
+
 target_link_libraries( kicad2step ${wxWidgets_LIBRARIES} ${LIBS_OCE} )
 
 if( APPLE )
diff --git a/utils/kicad2step/pcb/kicadmodule.cpp b/utils/kicad2step/pcb/kicadmodule.cpp
index 532b02c9c..03ffff415 100644
--- a/utils/kicad2step/pcb/kicadmodule.cpp
+++ b/utils/kicad2step/pcb/kicadmodule.cpp
@@ -360,7 +360,8 @@ bool KICADMODULE::ComposePCB( class PCBMODEL* aPCB, S3D_RESOLVER* resolver,
 
     for( auto i : m_models )
     {
-        std::string fname( resolver->ResolvePath( i->m_modelname.c_str() ).ToUTF8() );
+        std::string fname( resolver->ResolvePath(
+            wxString::FromUTF8Unchecked( i->m_modelname.c_str() ) ).ToUTF8() );
 
         if( aPCB->AddComponent( fname, m_refdes, LAYER_BOTTOM == m_side ? true : false,
             newpos, m_rotation, i->m_offset, i->m_rotation ) )
diff --git a/utils/kicad2step/pcb/oce_utils.cpp b/utils/kicad2step/pcb/oce_utils.cpp
index 8fa45cb04..cce6d1fd2 100644
--- a/utils/kicad2step/pcb/oce_utils.cpp
+++ b/utils/kicad2step/pcb/oce_utils.cpp
@@ -31,6 +31,7 @@
 
 #include "oce_utils.h"
 #include "kicadpad.h"
+#include "streamwrapper.h"
 
 #include <IGESCAFControl_Reader.hxx>
 #include <IGESCAFControl_Writer.hxx>
@@ -153,7 +154,7 @@ enum FormatType
 
 FormatType fileType( const char* aFileName )
 {
-    wxFileName lfile( aFileName );
+    wxFileName lfile( wxString::FromUTF8Unchecked( aFileName ) );
 
     if( !lfile.FileExists() )
     {
@@ -172,16 +173,15 @@ FormatType fileType( const char* aFileName )
     else if( ext == "emn" || ext == "EMN" )
         return FMT_EMN;     // PCB assembly
 
-    std::ifstream ifile;
-    ifile.open( aFileName );
+    OPEN_ISTREAM( ifile, aFileName );
 
-    if( !ifile.is_open() )
+    if( ifile.fail() )
         return FMT_NONE;
 
     char iline[82];
     memset( iline, 0, 82 );
     ifile.getline( iline, 82 );
-    ifile.close();
+    CLOSE_STREAM( ifile );
     iline[81] = 0;  // ensure NULL termination when string is too long
 
     // check for STEP in Part 21 format
-- 
2.11.0


From e214d57fa39b607a4a296549c98cec603c3c604e Mon Sep 17 00:00:00 2001
From: Cirilo Bernardo <cirilo.bernardo@xxxxxxxxx>
Date: Tue, 7 Mar 2017 14:52:17 +1100
Subject: [PATCH 2/2] Fix character mangling on kicad2step command line

---
 utils/kicad2step/kicad2step.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/utils/kicad2step/kicad2step.cpp b/utils/kicad2step/kicad2step.cpp
index e70caece1..d6c105d3f 100644
--- a/utils/kicad2step/kicad2step.cpp
+++ b/utils/kicad2step/kicad2step.cpp
@@ -84,7 +84,7 @@ static const wxCmdLineEntryDesc cmdLineDesc[] =
     };
 
 
-wxIMPLEMENT_APP_CONSOLE( KICAD2MCAD );
+wxIMPLEMENT_APP( KICAD2MCAD );
 
 
 bool KICAD2MCAD::OnInit()
-- 
2.11.0


Follow ups

References