"File not found or invalid" when loading component from browser!

Hi all!
I received a message “file not found or invalid” when i tried to get component from my browser! Could you pls help me solve this problem?
giphy (1)
Here is the script (its origin is from a member named John in this forum, then I modified a little bit according to my idea):

module JcB
  OSX = Sketchup.platform == :platform_osx unless defined? JcB::OSX
  NODLG = UI::WebDialog.new('noDlg', false, '', 0, 0, 0, 0, false) unless defined? JcB::NODLG
  # a html browser for components...
  module ComponentBrowser
    extend self

    def de_focus
      NODLG.show
      NODLG.close
    end

    def make_browser
      head = <<-HEAD
        <html><head>
        <title>Drop Comps</title>
        <style>
         body {
           font:caption;
           margin:0mm;
           color:GrayText;
           background-color:Window;
         }
         button{
           text-align: center;
           width: 140px;
           height:140px;
           cursor:pointer;
        }
        .grid-container > button:hover {
         box-shadow: 0 4px 16px 0 rgba(0, 0, 0, 0.5), 0 6px 30px 0 rgba(0, 0, 0, 0.4);
         background-color: window;
         text-shadow: 2px 2px 4px #000000;
         color: black;
        }
         img {
          width: 100px;
          height: 100px;
          border: 0;
         }         
         .grid-container {
            display: grid;
            grid-template-columns: auto auto auto auto;           
            grid-gap: 20px;
            background-color: window;
            padding: 20px;
          }
        .grid-container > button {
          background-color: window;
          border: 0px solid black;
          text-align: center;
          font-size: 14px;
          margin: 15px;
        }
         
        </style>
        </head>
        <body onscroll="dlgBlur()">
      HEAD

      model = Sketchup.active_model
      defs = model.definitions

      i = 0
      body = ''

      dir = File.dirname(UI.openpanel('Get Comps', '.skp'))
      skps = Dir.entries(dir).reject { |s| s unless s =~ /\.skp/ }

      return if skps.count == 0

      img_dir = File.join(dir, 'dlg_thumbnails')
      Dir.mkdir(img_dir) unless Dir.exist?(img_dir)

      body << %(<div class="grid-container">)
      skps.each do |name|
        i += 1
        basename = name.sub('.skp', '')
        img_name = basename + '.png'
        skp = File.join(dir, name)
        img = File.join(img_dir, img_name)
        Sketchup.save_thumbnail(skp, img) # unless File.exist?(img)
       
        body << %(
       
          <button  id='action#{i}';"
                    onclick="importSkp(this);"
                    onmouseout="this.id='action#{i}'" value="#{basename}">
                    <img src="#{img}" alt="#{name}">
                     <div style="margin-top: 3px;">#{basename}</div>
           </button>

          
        )
      end
         body << %(</div>)

      tail = <<-TAIL
        <script>
        function dlgBlur() { window.location='skp:dlg_blur'; }
        </script>
        <script>
        function importSkp(self) {         
            window.location='skp:import_skp'; 
            self.id = 'action';
            window.location='skp:import_skp'; 
        }
        </script>


        </body></html>
      TAIL

      html = head + body + tail
      sel = model.selection
      ents = model.entities
      scale = NODLG.screen_scale_factor.to_i
      vpw = model.active_view.vpwidth
      vph = model.active_view.vpheight
      dlgw = vpw / scale
      dlgh = (vph - 22) / scale
      min = 600
      dlg = UI::WebDialog.new(File.basename(dir), true, '', min, dlgh, 80, 112, true)
      dlg.add_action_callback('dlg_blur') do |_d|
        de_focus
        Sketchup.send_action('selectSelectionTool:')
        sel.clear unless sel.empty?
      end

      dlg.add_action_callback('import_skp') do |d|
        tl = d.get_element_value('action')
        skp = File.join(dir, tl + '.skp')
        #de_focus
        model.import(skp)
      end      
      dlg.set_html(html)
      dlg.show
      de_focus
    end
  end
end
JcB::ComponentBrowser.make_browser

This JS function is calling the Ruby callback twice. Probably should erase the 1st line of the function as you need the id set before the callback is called.

Ie, when the first callback fires, the id is unset so …

        tl = d.get_element_value('action')

… results in an empty string. Thus …

skp = File.join(dir, tl + '.skp')

… results in trying to load a file named ".skp" from dir.
This causes the error popup because there is no such skp file without a name.

1 Like

Thanks a lot Dan! Your support helps me understand the nature of problem. I have just tried to solve according to your guidance and it’s successful and problem is fixed now.
One more point, which method in Ruby should I use to put component imported at available point without using cursor?

1 Like

The #add_instance (definition, transform) method adds a component instance to the collection of entities.

1 Like

@KQV, … and before adding an instance (as dezmo says,) use DefinitionList#load instead of Model#import to get the definition loaded into the model’s components collection.

2 Likes

Tks for your support! I did it!

Thanks alot Dan! Your support is very detailed!

1 Like