sketchucation logo sketchucation
    • 登入
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    ⚠️ Important | Libfredo 15.6b introduces important bugfixes for Fredo's Extensions Update

    Create file with accent in the path

    已排程 已置頂 已鎖定 已移動 Developers' Forum
    23 貼文 3 Posters 1.2k 瀏覽 3 Watching
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • IltisI 離線
      Iltis
      最後由 編輯

      Hello,
      I'm working on a plugin and have this encoding problem : if there is "è" in the path, the file is not created :

      # Open a file for writing
      	  File.open(filepath, "w"){ |file|
      		...
      		  file.puts("Blabla")
      		  
      		}
      	  }
      

      Do you know how to fix it?
      (Test mith Sk7 on Windows7).
      Thanks and regards,
      Renaud.

      1 條回覆 最後回覆 回覆 引用 0
      • IltisI 離線
        Iltis
        最後由 編輯

        Thank you very much.
        I will try with the last version of SketchUp.

        The link is dead.

        1 條回覆 最後回覆 回覆 引用 0
        • Dan RathbunD 離線
          Dan Rathbun
          最後由 編輯

          Unicode support in Ruby comes only in v1.9 and higher.
          So, with SketchUp Ruby, only in versions 2014 and higher.

          SketchUp 2013 and earlier versions can only run Ruby 1.8.x, which does not have unicode support.

          TIG wrote a library that can be used for rudimentary unicode characters in path strings.
          [Code] PCFileTools

          I'm not here much anymore.

          1 條回覆 最後回覆 回覆 引用 0
          • Dan RathbunD 離線
            Dan Rathbun
            最後由 編輯

            @iltis said:

            The link is dead.

            Sorry.. I fixed it: [Code] PCFileTools

            💭

            I'm not here much anymore.

            1 條回覆 最後回覆 回覆 引用 0
            • tt_suT 離線
              tt_su
              最後由 編輯

              If you really need to handle Unicode filenames in Ruby 1.8 you would need to write a Ruby C Extension that makes calls to the Windows Unicode version of its file functions.

              1 條回覆 最後回覆 回覆 引用 0
              • IltisI 離線
                Iltis
                最後由 編輯

                Thank you for your answer, but it's too high for me, ruby's newbie.

                Actually, if the file is not created, nothing happen, the user can't know that a problem occured.
                Is it possible to know if there is a problem?

                => If the file "filepath" is not created then Messagebox "You're using SketchUp 2013 or earlier version, the path of your file name can't use accented characters like é,e,è. The file will not be created. If you want to use accented characters in the path, you must use SketchUp 2014 minimum".

                Sorry for my "frenglish".
                Regards,
                Renaud.

                1 條回覆 最後回覆 回覆 引用 0
                • tt_suT 離線
                  tt_su
                  最後由 編輯

                  I'd think there would be Ruby exceptions raised so you could try to catch these and present a failure message. You could also explicitly see if the file exist after you have tried to create it: File.exist?()

                  1 條回覆 最後回覆 回覆 引用 0
                  • IltisI 離線
                    Iltis
                    最後由 編輯

                    File.exist? doesn't work, for same reasons... 😞

                    1 條回覆 最後回覆 回覆 引用 0
                    • tt_suT 離線
                      tt_su
                      最後由 編輯

                      @iltis said:

                      File.exist? doesn't work, for same reasons... 😞

                      Doesn't work, as in it raises an error? Then you can rescue from that error and provide your message to the user.

                      1 條回覆 最後回覆 回覆 引用 0
                      • IltisI 離線
                        Iltis
                        最後由 編輯

                        Mhhh, I've got a problem with my test :

                        UI.messagebox(filepath)
                        		  # Open a file for writing
                        		  File.open(filepath, "w"){ |file|
                        			selection = Sketchup.active_model.selection
                        			 ...{
                                                    ...{
                        
                        				# Write the coordinates to the file.
                        				file.puts("#{u};#{v}")
                        			  }
                        			}
                        		  }
                        		 UI.messagebox("FileTest")
                        		 if FileTest.exists?(filepath)
                        			UI.messagebox("The file is there")
                        		 else
                        			UI.messagebox("The file is not there")
                        		 end
                        

                        The first messagebox give me the path : "C:\Users\Tarzan\Documents\aiR-C2\MiniCut2d\Bibliothèque\test.txt"
                        But no more messagebox after the "File.open"... do you know why?
                        But if I understand this topic (http://sketchucation.com/forums/viewtopic.php?t=20289), the ".exists?" is not the solution.

                        1 條回覆 最後回覆 回覆 引用 0
                        • tt_suT 離線
                          tt_su
                          最後由 編輯

                          In the Ruby Console do you see error messages? What I meant was to catch there errors, using begin, rescue.

                          1 條回覆 最後回覆 回覆 引用 0
                          • IltisI 離線
                            Iltis
                            最後由 編輯

                            OK, I see the error in the Ruby Console (thank you for the tip) :

                            Error: #<Errno::ENOENT: No such file or directory - C:/Users/Tarzan/Documents/aiR-C2/MiniCut2d/Bibliothèque/test.f2xy>
                            ...

                            I will try to catch it.

                            1 條回覆 最後回覆 回覆 引用 0
                            • IltisI 離線
                              Iltis
                              最後由 編輯

                              OK, this code works on SU8 :

                              		  begin
                              		  File.open(filepath, "w"){ |file|
                              			...
                              		  }
                              		 	UI.messagebox("The file is here ;" + filepath)
                              		rescue
                              		 	UI.messagebox("Error, the file was not created. Be careful if you are using an older version of SketchUp, the full file path must not contain special or accented characters. The full file path you requested is " + filepath)
                              		 end
                              

                              The "è" looks "è" in the messagebox, but the user know how to fix the problem.

                              I try to use it in SketchUp 2014, but I've got an error 😞

                              Erreur de chargement du fichier faces2xy.rb
                              Error; #<SyntaxError; C;/Users/Tarzan/AppData/Roaming/SketchUp/SketchUp 2014/SketchUp/Plugins/faces2xy.rb;18; syntax error, unexpected ',', expecting ')'
                              		  filepath = UI.savepanel ("Export selected faces",nil,"*.f2xy")
                              C;/Users/Tarzan/AppData/Roaming/SketchUp/SketchUp 2014/SketchUp/Plugins/faces2xy.rb;18; Can't assign to nil
                              		  filepath = UI.savepanel ("Export selected faces",nil,"*.f2xy")
                              C;/Users/Tarzan/AppData/Roaming/SketchUp/SketchUp 2014/SketchUp/Plugins/faces2xy.rb;18; syntax error, unexpected ')', expecting ;; or '[' or '.'
                              		  filepath = UI.savepanel ("Export selected faces",nil,"*.f2xy")
                              
                              

                              Do you know how to fix this (in all SU versions...)?
                              Thanks,
                              Renaud

                              1 條回覆 最後回覆 回覆 引用 0
                              • tt_suT 離線
                                tt_su
                                最後由 編輯

                                @iltis said:

                                The "è" looks "è" in the messagebox, but the user know how to fix the problem.

                                That's odd - if the string is UTF-8 encoded then it should look fine in SketchUp regardless.
                                Have you saved your RB files as UTF-8 without BOM?

                                @iltis said:

                                I try to use it in SketchUp 2014, but I've got an error 😞

                                I'm not sure, but I think it could be that space you have between the method name and the parentheses: filepath = UI.savepanel ("Export selected faces",nil,"*.f2xy")

                                But the error could be cascading some elsewhere... hard to tell without knowing the source code.

                                Btw, it's recommended not to catch all exceptions, but only the ones you expect.
                                http://www.skorks.com/2009/09/ruby-exceptions-and-exception-handling/

                                1 條回覆 最後回覆 回覆 引用 0
                                • IltisI 離線
                                  Iltis
                                  最後由 編輯

                                  Bingo! 2/2. It works fine now. Is "UTF-8 without BOM" the best to use? (Default value?)
                                  Thank you for the quick answer!
                                  I will see how to catch only the file exception.

                                  1 條回覆 最後回覆 回覆 引用 0
                                  • IltisI 離線
                                    Iltis
                                    最後由 編輯

                                    Mhh, the error is

                                    Error; #<Errno;;ENOENT; No such file or directory - C;/Users/Tarzan/Documents/aiR-C2/MiniCut2d/Bibliothèque/test.f2xy>
                                    

                                    I don't understand how to only catch this type of exception... (not in the "Ruby Exception Hierarchy"). I'll see this later.

                                    1 條回覆 最後回覆 回覆 引用 0
                                    • tt_suT 離線
                                      tt_su
                                      最後由 編輯

                                      @iltis said:

                                      Bingo! 2/2. It works fine now. Is "UTF-8 without BOM" the best to use?

                                      I recommend that because without the BOM you can load the file in Ruby 1.8. With the BOM Ruby 1.8 will not load it.

                                      1 條回覆 最後回覆 回覆 引用 0
                                      • tt_suT 離線
                                        tt_su
                                        最後由 編輯

                                        @iltis said:

                                        Mhh, the error is

                                        Error; #<Errno;;ENOENT; No such file or directory - C;/Users/Tarzan/Documents/aiR-C2/MiniCut2d/Bibliothèque/test.f2xy>
                                        

                                        I don't understand how to only catch this type of exception... (not in the "Ruby Exception Hierarchy"). I'll see this later.

                                        You should be able to catch Errno::ENOENT - it's a class inherited form StandardError.

                                        Errno::ENOENT.ancestors [Errno::ENOENT, SystemCallError, StandardError, Exception, Object, PP::ObjectMixin, JSON::Ext::Generator::GeneratorMethods::Object, Kernel, BasicObject]

                                        Though, it might be system dependant... Maybe catching SystemCallError is a good one to catch for these types of errors.

                                        1 條回覆 最後回覆 回覆 引用 0
                                        • IltisI 離線
                                          Iltis
                                          最後由 編輯

                                          OK, thank you.

                                          1 條回覆 最後回覆 回覆 引用 0
                                          • Dan RathbunD 離線
                                            Dan Rathbun
                                            最後由 編輯

                                            Like this:

                                              begin
                                                File.open(filepath, "w"){ |file|
                                                  #...
                                                }
                                                UI.messagebox("The file is here ;" + filepath)
                                              rescue SystemCallError => e
                                                if e.message =~ /(No such file or directory)/
                                                  UI.messagebox("Error, the file was not created. Be careful if you are using an older version of SketchUp, the full file path must not contain special or accented characters. The full file path you requested is " + filepath)
                                                else
                                                  fail() #re-raise the last exception
                                                end
                                              end
                                            

                                            I'm not here much anymore.

                                            1 條回覆 最後回覆 回覆 引用 0
                                            • 1
                                            • 2
                                            • 1 / 2
                                            • 第一個貼文
                                              最後的貼文
                                            Buy SketchPlus
                                            Buy SUbD
                                            Buy WrapR
                                            Buy eBook
                                            Buy Modelur
                                            Buy Vertex Tools
                                            Buy SketchCuisine
                                            Buy FormFonts

                                            Advertisement