# SketchUp STL String Encoding Issues

**URL:** <https://forums.sketchup.com/t/sketchup-stl-string-encoding-issues/15860>\
**Category:** Ruby API\
**Created:** [October 3, 2015, 11:42am UTC](https://forums.sketchup.com/t/sketchup-stl-string-encoding-issues/15860 "2015-10-03T11:42:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jim\_foltz](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/jim_foltz/32/74787_2.png) [@jim\_foltz](https://forums.sketchup.com/u/jim_foltz)\
**Post date:** [October 3, 2015, 11:42am UTC](https://forums.sketchup.com/t/sketchup-stl-string-encoding-issues/15860/1 "2015-10-03T11:42:59Z")

</div>

The SketchUp STL extension has a bug report where `Sketchup.file_loaded?` throws an error when it attempts to `.downcase` a string containing non-ascii characters.

Do we know how to definitely fix these encoding issues yet? They seem to be an ongoing problem.

Here are the bug reports:

> <https://github.com/SketchUp/sketchup-stl/issues/154>
>
> I am Korean and are not good at English. Please understand.
> This error was fixed… #134 from. But, Korean language in the same error occurs.
> 
> \`\`\`
> C:/Users/박민규/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/sketchup-stl/importer.rb File road error
> Error: #\<ArgumentError: invalid byte sequence in CP949\>
> C:/Program Files/SketchUp/SketchUp 2015/Tools/sketchup.rb:36:in \`downcase'
> C:/Program Files/SketchUp/SketchUp 2015/Tools/sketchup.rb:36:in \`file\_loaded?'
> C:/Users/박민규/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/sketchup-stl/importer.rb:415:in \`\<top (required)\>'
> C:/Users/박민규/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/sketchup-stl/loader.rb:20:in \`require'
> C:/Users/박민규/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/sketchup-stl/loader.rb:20:in \`\<module:STL\>'
> C:/Users/박민규/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/sketchup-stl/loader.rb:8:in \`\<module:CommunityExtensions\>'
> C:/Users/박민규/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/sketchup-stl/loader.rb:7:in \`\<top (required)\>'
> C:/Program Files/SketchUp/SketchUp 2015/Tools/extensions.rb:197:in \`require'
> C:/Program Files/SketchUp/SketchUp 2015/Tools/extensions.rb:197:in \`load'
> C:/Users/박민규/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/sketchup-stl.rb:58:in \`register\_extension'
> C:/Users/박민규/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/sketchup-stl.rb:58:in \`\<module:STL\>'
> C:/Users/박민규/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/sketchup-stl.rb:12:in \`\<module:CommunityExtensions\>'
> C:/Users/박민규/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/sketchup-stl.rb:11:in \`\<top (required)\>'
> C:/Users/박민규/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/sketchup-stl/exporter.rb File road error
> Error: #\<ArgumentError: invalid byte sequence in CP949\>
> C:/Program Files/SketchUp/SketchUp 2015/Tools/sketchup.rb:36:in \`downcase'
> C:/Program Files/SketchUp/SketchUp 2015/Tools/sketchup.rb:36:in \`file\_loaded?'
> C:/Users/박민규/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/sketchup-stl/exporter.rb:315:in \`\<module:Exporter\>'
> C:/Users/박민규/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/sketchup-stl/exporter.rb:11:in \`\<module:STL\>'
> C:/Users/박민규/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/sketchup-stl/exporter.rb:10:in \`\<module:CommunityExtensions\>'
> C:/Users/박민규/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/sketchup-stl/exporter.rb:9:in \`\<top (required)\>'
> C:/Users/박민규/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/sketchup-stl/loader.rb:21:in \`require'
> C:/Users/박민규/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/sketchup-stl/loader.rb:21:in \`\<module:STL\>'
> C:/Users/박민규/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/sketchup-stl/loader.rb:8:in \`\<module:CommunityExtensions\>'
> C:/Users/박민규/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/sketchup-stl/loader.rb:7:in \`\<top (required)\>'
> C:/Program Files/SketchUp/SketchUp 2015/Tools/extensions.rb:197:in \`require'
> C:/Program Files/SketchUp/SketchUp 2015/Tools/extensions.rb:197:in \`load'
> C:/Users/박민규/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/sketchup-stl.rb:58:in \`register\_extension'
> C:/Users/박민규/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/sketchup-stl.rb:58:in \`\<module:STL\>'
> C:/Users/박민규/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/sketchup-stl.rb:12:in \`\<module:CommunityExtensions\>'
> C:/Users/박민규/AppData/Roaming/SketchUp/SketchUp 2015/SketchUp/Plugins/sketchup-stl.rb:11:in \`\<top (required)\>'
> \`\`\`
> 
> Up to date, and rb a result of the latest issue been applied to load the file. Fix please

---

<div class="post-metadata">

**Author:** ![TIG](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tig/32/198_2.png) [@TIG](https://forums.sketchup.com/u/TIG)\
**Post date:** [October 3, 2015, 12:12pm UTC](https://forums.sketchup.com/t/sketchup-stl-string-encoding-issues/15860/2 "2015-10-03T12:12:27Z")

</div>

Why do you not try to forced the string’s encoding to UTF-8 before doing anything else with it ?  
You already do that when setting the extension-loader’s path.

```
current_path = File.dirname( __FILE__ )
if current_path.respond_to?(:force_encoding)
  current_path.force_encoding("UTF-8")
end

```

But when you do the menu creation you do not.

```
unless file_loaded?( __FILE__ )
  Sketchup.register_importer(CommunityExtensions::STL::Importer.new)
  file_loaded( __FILE__ )
end

```

If you tweak it perhaps as shown below, then does it work ?

```
if defined?(Encoding)
  me = __FILE__.force_encoding("UTF-8")
else    
  me = __FILE__
end
unless file_loaded?(me)
  Sketchup.register_importer(CommunityExtensions::STL::Importer.new)
  file_loaded(me)
end

```

Repeat this ‘me’ setup for all similar tests in any of the files using `file_loaded` ??  
OR do NOT pass the whole ` __FILE__ ` thus:

```
begin
  me = File.basename( __FILE__ )
  me = "CE_STL_importer" unless me
rescue
  me = "CE_STL_importer"
end
unless file_loaded?(me)
      Sketchup.register_importer(CommunityExtensions::STL::Importer.new)
      file_loaded(me)
end

```

Or even setting ‘me’ as a simple “string” anyway - thus side stepping the File hiccups and encoding issue entirely.

The `file_loaded` expects a UTF-8 string, so give it one, one way or another…

---

<div class="post-metadata">

**Author:** ![jim\_foltz](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/jim_foltz/32/74787_2.png) [@jim\_foltz](https://forums.sketchup.com/u/jim_foltz)\
**Post date:** [October 3, 2015, 12:45pm UTC](https://forums.sketchup.com/t/sketchup-stl-string-encoding-issues/15860/3 "2015-10-03T12:45:02Z")

</div>

We have a comment `# Ensure to open the file in with no encoding.` but I am unsure why now.

Your solution seems too obvious, but I’ll try it anyhow. Haha, thanks.

---

<div class="post-metadata">

**Author:** ![john\_drivenupthewall](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@john\_drivenupthewall](https://forums.sketchup.com/u/john_drivenupthewall)\
**Post date:** [October 3, 2015, 12:50pm UTC](https://forums.sketchup.com/t/sketchup-stl-string-encoding-issues/15860/4 "2015-10-03T12:50:21Z")

</div>

@TIG, @jim_foltz,

This is really a question, as mac’s don’t have the issue…

Isn’t the error really in sketchup.rb?

if I grep out all three **.downcase** SketchUp still loads everything and runs fine…

why does it need to _downcase_ at all?

I can never find a any links to why you would need to downcase a path, although it’s quite common…

john

---

<div class="post-metadata">

**Author:** ![jim\_foltz](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/jim_foltz/32/74787_2.png) [@jim\_foltz](https://forums.sketchup.com/u/jim_foltz)\
**Post date:** [October 3, 2015, 12:56pm UTC](https://forums.sketchup.com/t/sketchup-stl-string-encoding-issues/15860/5 "2015-10-03T12:56:04Z")

</div>

> [@john\_drivenupthewall](#):
>
> why does it need to downcase at all?

Plugins were inconsistent when requiring sketchup.rb. Some used `require "sketchup.rb"`, others `require "Sketchup.rb"`, so the file `sketchup.rb` was being required more than once. This caused some problem I can’t quite recall - maybe it was only duplicate menu items for the Ruby Console. Downcasing all required files solved it.

---

<div class="post-metadata">

**Author:** ![jim\_foltz](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/jim_foltz/32/74787_2.png) [@jim\_foltz](https://forums.sketchup.com/u/jim_foltz)\
**Post date:** [October 3, 2015, 1:11pm UTC](https://forums.sketchup.com/t/sketchup-stl-string-encoding-issues/15860/6 "2015-10-03T13:11:49Z")

</div>

> [@john\_drivenupthewall](#):
>
> Isn’t the error really in sketchup.rb?

Well, maybe. That’s what I was thinking also, but wanted to leave the question more open.

Apparently, you can’t `downcase` every CP949 or CP950 encoded string, and the file path is encoded using CP950. We can force the file encoding to utf-8 before downcasing, but is that the right thing to do? There are some indications online that it is not a clean conversion.

Should this be done in `sketchup.rb` file, or is the extension author responsible? In the short term, it must be up to each author.

I also read where this may have been fixed in Ruby 2.1.

---

<div class="post-metadata">

**Author:** ![TIG](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tig/32/198_2.png) [@TIG](https://forums.sketchup.com/u/TIG)\
**Post date:** [October 3, 2015, 2:23pm UTC](https://forums.sketchup.com/t/sketchup-stl-string-encoding-issues/15860/7 "2015-10-03T14:23:10Z")

</div>

But as I said… your code already changes to UTF-8 for the ‘Extension’ set up…

For `file_loaded` usage you do not need to pass the ` __FILE__ ` - although many of us do !  
You can parse off the `File.basename( __FILE__ )` and use that, or set a UTF-8 ‘string’ avoid even any potential File issues…  
The `file_loaded` list is unlikely to contain an earlier entry based on either of those, unless the user has multiple Plugins folders in `$:` loading the same code, or the ‘string’ is moronically set up like ‘_my\_plugin_’, where it might get repeated by others…

---

<div class="post-metadata">

**Author:** ![john\_drivenupthewall](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@john\_drivenupthewall](https://forums.sketchup.com/u/john_drivenupthewall)\
**Post date:** [October 3, 2015, 3:15pm UTC](https://forums.sketchup.com/t/sketchup-stl-string-encoding-issues/15860/8 "2015-10-03T15:15:04Z")

</div>

> [@jim\_foltz](#):
>
> Should this be done in sketchup.rb file, or is the extension author responsible?

would the extension author need to ‘fix’ encoding without the _ **downcase** _ imposed by sketchup.rb?

for example, would this not avoid the require “sketchup.rb” / require “Sketchup.rb” issue and any down-casing errors?

```ruby
# Call this method to see if a file is already loaded.
def file_loaded?(filename)
    $loaded_files.include? (File.basename(filename, ".*").downcase || filename)
end

# Call this function at the end of a file that you are loading to
# let the system know that you have loaded it.
def file_loaded(filename)
    $loaded_files.push(filename) if not file_loaded?(filename)
end

```

john

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [October 4, 2015, 4:07am UTC](https://forums.sketchup.com/t/sketchup-stl-string-encoding-issues/15860/9 "2015-10-04T04:07:09Z")

</div>

> [@john\_drivenupthewall](#):
>
> for example, would this not avoid the require “sketchup.rb” / require “Sketchup.rb” issue and any down-casing errors?

NO. Not exactly. You need to use **`rescue`** in modifier position, not **`||`**.  
Also the use of parenthesis will be interpreted as wrapping the parameter list, so should not have a space following the method name:

```ruby
def file_loaded?(filename)
  $loaded_files.include?(File.basename(filename, ".*").downcase rescue filename)
end

```

or:

```ruby
def file_loaded?(filepath)
  filename = File.basename(filepath, ".*")
  filename.downcase! rescue nil
  $loaded_files.include?(filename)
end

```

* * *

But I’ve been preaching that it is unnecessary to push paths of any kind into **`$loaded_files`** , as they are already pushed into **`$LOADED_FEATURES`**.

So instead do this (but NOT from toplevel nor console!):

```ruby
# Avoid File.basename & other string methods.
thisfile = caller(0).split(':').first
# Downcase it suppressing errors
thisfile.downcase! rescue nil
# Prepend it with Namespace qualification:
thisfile = "#{Module::nesting[0].name}:#{thisfile}"
begin
  if file_loaded?(thisfile)
    #
    # do once code
    #
    file_loaded(thisfile)
  end
rescue
  # handle error in sketchup.rb
end

```

* * *

I have also preached to “steal” the methods from “_sketchup.rb_” add them to a mixin module, FIX THEM and use this mixin **instead** of “_sketchup.rb_”.

I have logged several bug reports for “_sketchup.rb_”, that have not yet been repaired.

If anyone wants I have a test “sketchup.rb” override (that I had written several cycles back, but no longer works as a toggle-able extension in the “Plugins” folder, since SU2015 because “Tools” is loaded before “Plugins”. So you’d have to replace it I think.)

PM me if ya want to check it out. It could be wrapped in a mixin module I suppose.

---

<div class="post-metadata">

**Author:** ![jim\_foltz](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/jim_foltz/32/74787_2.png) [@jim\_foltz](https://forums.sketchup.com/u/jim_foltz)\
**Post date:** [October 4, 2015, 9:18am UTC](https://forums.sketchup.com/t/sketchup-stl-string-encoding-issues/15860/10 "2015-10-04T09:18:19Z")

</div>

Is there really a good reason to use `file_loaded?` and `file_loaded` methods?

If they are only for preventing duplicate menu items, there are better ways. If a user **is** getting duplicate menu items, then it would a clear indication something is wrong with their setup. It would act as a warning in that case and is desirable.

Extension authors should probably stop using these methods altogether for released extensions.

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [October 4, 2015, 11:03am UTC](https://forums.sketchup.com/t/sketchup-stl-string-encoding-issues/15860/11 "2015-10-04T11:03:10Z")

</div>

> [@jim\_foltz](#):
>
> If they are only for preventing duplicate menu items, there are better ways.

I think that is the primary reason.

But I often have other “run once” code. If it (the run once code) is all in one file (even though the extension has multiple files,) which is good practice,… I use some module variable like:

```ruby
@@loaded = false unless defined?(@@loaded)

# at the right place
if not @@loaded
  # run once code
  @@loaded=true
end

```

If you wish to test multiple files, well just define your own **`@@loaded_files`** array within your plugin module. You control what is pushed into it, and how it is tested for members.
