by sm4rt » Fri Apr 08, 2011 2:25 am
Hi all! I'm currently writing a little Ruby Script which dynamically load all components (uh actually all files  ) containing inside a specific folder. (including sub-folders) The problem is that it need an empty sub-folder when some skp are present to be able to launch them... And I really don't know why ? (file in attachment, I know that it's quite dirty but it's a simple test for the moment) the problem appears on line 47 - Code: Select all
#UI.messagebox foldername $menu[folderNumber].add_item(fichier){component_loader(fichier,foldername)} #UI.messagebox foldername
If I active messagebox to view foldername before and after the calling to the function component_loader() the name is good. But inside the component_loader function, foldername refer to the parent directory if and only if there is no sub-directory inside the current folder.... I really don't understand why even if I suspect the line 42 to be guilty nevertheless I don't understand why before and after the calling of the function the name of the folder is good... - Code: Select all
foldername[fichier+"/"]=""
(here to change the directory name the parent one when the reading of the actual is over) Any ideas ?
Last edited by sm4rt on Fri Apr 08, 2011 2:07 pm, edited 1 time in total.
-
sm4rt
-
- Posts: 17
- Joined: Fri Apr 09, 2010 11:02 am
by TIG » Fri Apr 08, 2011 10:52 am
Here is a much more compact version. - Code: Select all
require 'sketchup.rb' module Sm4rt class List_Menu_Load def initialize(rep=nil, menu=nil) return nil if not rep or not menu or not File.exist?(rep) or not File.ftype(rep)=='directory' skps=[]; folders=[] Dir.entries(rep).each{|f| next if f=='.' or f=='..' path=File.join(rep, f) skps << f if File.ftype(path)!='directory' and File.extname(f).downcase=='.skp' folders << f if File.ftype(path)=='directory' } skps.each{|skp| skpc=File.basename(skp, ".*"); path=File.join(rep, skp) menu.add_item(skpc){Sm4rt::List_Menu_Load::component_loader(path)} } folders.each{|folder| path=File.join(rep, folder); smenu=menu.add_submenu(folder) Sm4rt::List_Menu_Load.new(path, smenu) if smenu } end def List_Menu_Load::component_loader(path=nil) Sketchup.active_model.import(path, true) if path end def List_Menu_Load::menu_maker() Sm4rt::List_Menu_Load.new(File.join(File.dirname(__FILE__), "MyLibrary"), menu=UI.menu("Plugins").add_submenu("MyLibrary")) end endendSm4rt::List_Menu_Load::menu_maker()
It avoids messing with base classes, global variables prone to clashing etc... It's fully self-contained and self-iterates all skps in the specified folder or its subfolders, adding an item to a menu/submenu to suit and an action for the item to import the chosen skp...
TIG
-

TIG
- Global Moderator
-
- Posts: 19914
- Joined: Mon Nov 12, 2007 7:24 pm
- Location: Northumbria UK
- Name: TIG
- Operating system: Windows
- SketchUp version: 2018
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
by sm4rt » Fri Apr 08, 2011 2:00 pm
damn! still impressive TIG! I've just modified it to treat folders before skps and added the ability to load more then one folder... File in attachment for those who want here is the log^^ # Copyright 2011 by sm4rt (No it's TIG one in fact)
# THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
# Name : Dynamic Component Loader 0.1.1rc1 # Description : load full libraries of components you often use by creating a menu list of your component libraries # inside the Sketchup Plugins Menu # Author : sm4rt but TIG # Usage : Simply edit/add the name you want to give for your component libraries inside "librariesNames" declaration # edit/add the corresponding folder path inside "librariesFolders" declaration # And that's it ! # you'll no longer have to search through your disk to find model you usually use # Just feed your model libs as time goes by # Script will load sub-folders too # Date : 8.Apr.2011 # Type : Loader # History: # 0.1.1rc1 (8.Apr.2011) - impressive fully versatile revision # 0.1.1beta1 (7.Apr.2011) - loud dirty unefficient version
Last edited by sm4rt on Fri Apr 08, 2011 4:52 pm, edited 2 times in total.
-
sm4rt
-
- Posts: 17
- Joined: Fri Apr 09, 2010 11:02 am
by sm4rt » Fri Apr 08, 2011 2:07 pm
Any ideas of what happens and what's wrong in my version because I really don't understand where is the mistake in fact... trying to treat files before folders but it don't change anything : - when there is a sub folder everything works good - when the actual folder is the last child one, skps do not load <= path is wrong | but path is good before entering in the load function / wrong inside it / and right after quiting the load function.... it make me crazy! 
-
sm4rt
-
- Posts: 17
- Joined: Fri Apr 09, 2010 11:02 am
by TIG » Fri Apr 08, 2011 4:15 pm
Your menu_maker code ~line #50 was wrongly worded - it wasn't looking in the right folder IF the folder wasn't in Plugins with the .rb file ! Here's the module part of the code again... - Code: Select all
module Sm4rt_CL class List_Menu_Load def initialize(rep=nil, menu=nil) return nil if not rep or not menu or not File.exist?(rep) or not File.ftype(rep)=='directory' skps=[]; folders=[] Dir.entries(rep).each{|f| next if f=='.' or f=='..' path=File.join(rep, f) skps << f if File.ftype(path)!='directory' and File.extname(f).downcase=='.skp' folders << f if File.ftype(path)=='directory' } folders.each{|folder| path=File.join(rep, folder); smenu=menu.add_submenu(folder) Sm4rt_CL::List_Menu_Load.new(path, smenu) if smenu } skps.each{|skp| skpc=File.basename(skp, ".*"); path=File.join(rep, skp) menu.add_item(skpc){Sm4rt_CL::List_Menu_Load::component_loader(path)} } enddef List_Menu_Load::component_loader(path=nil) Sketchup.active_model.import(path, true) if path enddef List_Menu_Load::menu_maker(folder=nil, name=nil) return nil if not folder or not name Sm4rt_CL::List_Menu_Load.new(folder, menu=UI.menu("Plugins").add_submenu(name)) endendendlibrariesNames.each {|value| Sm4rt_CL::List_Menu_Load::menu_maker(librariesFolders[value], value)}
Also note that I removed a redundant "submenu=[]" line 
TIG
-

TIG
- Global Moderator
-
- Posts: 19914
- Joined: Mon Nov 12, 2007 7:24 pm
- Location: Northumbria UK
- Name: TIG
- Operating system: Windows
- SketchUp version: 2018
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
by sm4rt » Fri Apr 08, 2011 4:46 pm
No I was speaking about my version of the first post.... Because there is non sens there^^
Actually the last one I send works good for me... thanks to you^^ (and your last suggestion don't or is a good solution for declaring Absolute path personally I prefers Relative declaration like you did...
But I've suppressed "submenus=[]" though...
-
sm4rt
-
- Posts: 17
- Joined: Fri Apr 09, 2010 11:02 am
by glro » Thu Apr 26, 2012 11:20 am
i use this plugin as an alternative to sketchup native component browser; but, even with only one subfolder in the component folder, it takes a lot of room on the screen CLibraries_Loader.rb from menu.jpg would it be possible to have a button, or a key shortcut, that would open a window with only the chosen folder in sketchup component folder; by clicking on this folder, it would show its content, and it would be possible to pick up a component; like this CLibraries_Loader.rb from menu reduced.jpg less room on the screen, easier to follow the path to the chosen component with the mouse
-
glro
-
- Posts: 389
- Joined: Mon Nov 30, 2009 9:45 am
- Location: Spain
- Name: glro
- Operating system: Windows
- SketchUp version: 8
- License type: Pro
- SketchUp use: engineering and mechanical design
- Level of SketchUp: Intermediate
by krazyman » Fri Apr 19, 2013 9:27 pm
Is there any way to access folders outside of the SketchUp root folder? It doesn't read a folder if I enter the address as "C:\Library" or anything without the "../Components" type of notation.
-
krazyman
-
- Posts: 1
- Joined: Tue Sep 09, 2008 5:34 pm
by glro » Thu Feb 19, 2015 9:56 pm
any chance to get it working with sketchup 15?
-
glro
-
- Posts: 389
- Joined: Mon Nov 30, 2009 9:45 am
- Location: Spain
- Name: glro
- Operating system: Windows
- SketchUp version: 8
- License type: Pro
- SketchUp use: engineering and mechanical design
- Level of SketchUp: Intermediate
by CadFather » Fri Feb 20, 2015 1:42 am
it kind of does, the code looks for the comps in two folders but they must be where the plugin itself is (plugins folder usually). - Code: Select all
require 'sketchup.rb' #order = ["Components", "MyLibrary"] librariesNames = ["MyLibrary", "Components"] librariesFolders = { "Components" => "Components", "MyLibrary" => "MyLibrary"} module Sm4rt_CL class List_Menu_Load def initialize(rep=nil, menu=nil) return nil if not rep or not menu or not File.exist?(rep) or not File.ftype(rep)=='directory' skps=[]; folders=[] Dir.entries(rep).each{|f| next if f=='.' or f=='..' path=File.join(rep, f) skps << f if File.ftype(path)!='directory' and File.extname(f).downcase=='.skp' folders << f if File.ftype(path)=='directory' } folders.each{|folder| path=File.join(rep, folder); smenu=menu.add_submenu(folder) Sm4rt_CL::List_Menu_Load.new(path, smenu) if smenu } skps.each{|skp| skpc=File.basename(skp, ".*"); path=File.join(rep, skp) menu.add_item(skpc){Sm4rt_CL::List_Menu_Load::component_loader(path)} } end#initialize def List_Menu_Load::component_loader(path=nil) Sketchup.active_model.import(path, true) if path end#component_loader def List_Menu_Load::menu_maker(folder=nil, name=nil) return nil if not folder or not name Sm4rt_CL::List_Menu_Load.new(File.join(File.dirname(__FILE__), folder), menu=UI.menu("Plugins").add_submenu(name)) end#menu_maker end#class end#module ### auto-run at startup librariesNames.each {|value| Sm4rt_CL::List_Menu_Load::menu_maker(librariesFolders[value], value)} ###
-

CadFather
- PluginStore Author

-
- Posts: 1030
- Joined: Fri Nov 16, 2007 11:03 am
- Location: London, Milano, Sicily
- Name: CadFather
- Operating system: Windows
- SketchUp version: 2014
- License type: Pro
- SketchUp use: landscape architecture
- Level of SketchUp: Advanced
-
by glro » Fri Feb 20, 2015 9:05 am
Thank you
i didn't notice any change from the version i already have though
it did work perfectly in SU8 it doesn't work for me anymore in SU15
components are in the components folder, the plugin is in the plugins folder
in SU15, they are far apart, maybe that's why
adding this file in my plugins folder, i do have 2 more lines in the plugins menu > "Mylibray" > "components" but they don't unfold, and Launchup doesn't propose me components anymore when i type the name
-
glro
-
- Posts: 389
- Joined: Mon Nov 30, 2009 9:45 am
- Location: Spain
- Name: glro
- Operating system: Windows
- SketchUp version: 8
- License type: Pro
- SketchUp use: engineering and mechanical design
- Level of SketchUp: Intermediate
by CadFather » Fri Feb 20, 2015 11:02 am
yes, i posted the code so you'd be sure of the one i used.
notice i took out the '../' from the 2 folders paths (start of script) - which made the script look in the directory above to the comps folder. (so it only works on su8)
if you put the folders in the same directory as the script it works. at least it does for me (check the case matches too)
need to look more into to see why it doesn't when you change the path to full (ie. "c/Program Files/SketchUp...etc etc)
-

CadFather
- PluginStore Author

-
- Posts: 1030
- Joined: Fri Nov 16, 2007 11:03 am
- Location: London, Milano, Sicily
- Name: CadFather
- Operating system: Windows
- SketchUp version: 2014
- License type: Pro
- SketchUp use: landscape architecture
- Level of SketchUp: Advanced
-
by glro » Fri Feb 20, 2015 12:10 pm
CadFather wrote:yes, i posted the code so you'd be sure of the one i used.
notice i took out the '../' from the 2 folders paths (start of script) - which made the script look in the directory above to the comps folder. (so it only works on su8)
if you put the folders in the same directory as the script it works. at least it does for me (check the case matches too)
need to look more into to see why it doesn't when you change the path to full (ie. "c/Program Files/SketchUp...etc etc)
OK now i understand it works for me also muchas gracias i shall try to change the path too...
-
glro
-
- Posts: 389
- Joined: Mon Nov 30, 2009 9:45 am
- Location: Spain
- Name: glro
- Operating system: Windows
- SketchUp version: 8
- License type: Pro
- SketchUp use: engineering and mechanical design
- Level of SketchUp: Intermediate
by Ad Machine » 5 minutes ago
-
Ad Machine
- Robot
-
- Posts: 2012
-
Return to Plugins
|