Unable to load component to the file from installed folder

Continuing the discussion from How to Import file.skp in a model?:

i have created a rbz file that contain my rb file and component folder all the content in the file is unloading on the following link

" \THIS PC\AppData\Roaming\SketchUp\SketchUp 2017\SketchUp\Plugins\standard_module\standard_module\kitchen"

the component is not loading from this location for the given code
instead it loading from following location

C:\ProgramData\SketchUp\SketchUp 2017\SketchUp\standard_module\standard_module\kitchen…

kitchentool = UI::Toolbar.new "KITCHEN WITH HANDLE COMPONENT demo v2.2 "

KBU1DR = UI::Command.new("KBU1DR") {

	path = Sketchup.find_support_file "KBU1DR.skp",
	"standard_module/kitchen/"
	model = Sketchup.active_model
	definitions = model.definitions
	KBU1DR = model.definitions.load path
	model.place_component(KBU1DR)
	
}
KBU1DR.small_icon = "kitchen/icon/KBU1DR.png"
KBU1DR.large_icon = "kitchen/icon/KBU1DR.png"
KBU1DR.tooltip = "KBU1DR"
KBU1DR.status_bar_text = "KITCHEN BASE 1 DRAWER UNIT"
KBU1DR.menu_text = "KITCHEN BASE 1 DRAWER UNIT"
kitchentool = kitchentool.add_item KBU1DR
kitchentool.show

kindly help out

add your .rbz so we can check it…

john

Please do not create duplicate posts !
(The Moderator has removed the duplicate post in the linked topic thread.)


Your problem comes from using Sketchup::find_support_file,
which is for searching the application’s support directories, not appdata directories.


Use the Ruby global __dir__ method along with Ruby File::join method.

Also, always test for valid paths before using them.

  filename = "KBU1DR.skp" 
  path = File.join( __dir__, "standard_module/kitchen", filename )
  if File.exist?(path)
    model = Sketchup.active_model
    definitions = model.definitions
    @kbu1dr = model.definitions.load( path )
    if @kbu1dr # nil if the definition could not load
      model.place_component(@kbu1dr)
    else
      UI.messagebox(
        "Error: Could not load component: \"#{File.basename(filename,".*")}\""
      )
    end
  else
    UI.messagebox(
      "Error: Could not find file: \"#{filename}\" at path:\n\"#{File.dirname(path)}\""
    )
  end

Your example has an error where you assign a constant reference KBU1DR to the command, but then in it’s execute block you assign a definition reference to the same constant name. Temporary references should never be constants. They should be local or instance variables.

sorry i created a duplicate post won’t happen again…

thank you so much for the solution

can you share some link where i can learn more about ruby starting from basic

Way ahead of you. It is a pinned topic right here in this category …

1 Like

Thank you gret help

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.