[code] Concept test for "Import All File Types" command

Continuing the discussion from Import - view all supported file types:

Just some test code to test the concept of the feature requested in the linked thread.

  • MS Windows only
  • Does not save paths from session to session
  • Does not manipulate the individual filetype’s import settings
# encoding: UTF-8

module SomeAuthor
  module ImportAllSupported
    
    extend self

    @@loaded = false unless defined?(@@loaded)
    @@last_image_dir ||= ""
    @@last_model_dir ||= ""

    IMAGE_TYPES ||= [
      '*.bmp','*.jpg','*.jpeg','*.png','*.psd','*.tif','*.tga',
    ].join(';')

    MODEL_TYPES ||= [
      '*.dae','*.ddf','*.dem','*.dwg','*.dxf','*.fbx','*.ifc','ifcZIP',
      '*.kmz','*.obj','*.wrl','*.skp','*.stl','*.xsi','*.3ds'
    ].join(';')

    def import_any_image_type
      path = @@last_image_dir.empty? ? ENV['HOME'] : @@last_image_dir
      filepath = UI.openpanel(
        'Choose file for Import', path, "All Supported Image Types|#{IMAGE_TYPES}||"
      )
      if filepath
        @@last_image_dir = filepath
        Sketchup.active_model.import(filepath)
      end
    end

    def import_any_model_type
      path = @@last_model_dir.empty? ? ENV['HOME'] : @@last_model_dir
      filepath = UI.openpanel(
        'Choose file for Import', path, "All Supported Model Types|#{MODEL_TYPES}||"
      )
      if filepath
        @@last_model_dir = filepath
        Sketchup.active_model.import(filepath)
      end
    end

    unless @@loaded
      UI.menu('File').add_item('Import any Image Type...') { import_any_image_type() }
      UI.menu('File').add_item('Import any Model Type...') { import_any_model_type() }
      @@loaded = true
    end

  end
end
2 Likes

Just a note after thinking a bit.

There is no way using the Ruby API that code can know what type of import file is chosen within the file open dialog in order to tailor the options sub-dialog to the unique import settings.

So such generic import commands would either (a) as the simple test does, rely solely upon the user’s previous settings for that file type (or SketchUp’s defaults if the user never yet imported this filetype,) …
or (b) always bring up a custom file import dialog after the user selects the file to import (as the code can only respond to the file type by “sniffing” the file extension from the path string returned by the file open dialog.)

So as I think the simple API UI.inputbox would be inadequate, and a browser dialog too much work for a test, I didn’t do this. @thomthom had some code in his GitHub repo that might be adapted to this.


ADD (a day later) : In case the reader wondered, we cannot use Sketchup::Importer subclass objects as they only support a single file type extension. (See the class’ #file_extension getter method. This method is used to create the filtered listing that appears in the file selection browser dialog.)