Getting the extensions menu with no extension

Hi all,

I will explain my strange title right away :slight_smile: I created import/export plug-ins from my firm format to SketchUp and back and put them in a dedicated sub-menu of the “Extensions” menu with the following code:

snexport = "sdm_export.rb"
snimport = "sdm_import.rb"
unless file_loaded? snexport and file_loaded? snimport
  submenu = UI.menu("Plugins").add_submenu("SDM...") 
  submenu.add_item("SDM export...") { SDM::export }
  submenu.add_item("SDM import...") { SDM::import }
  submenu.add_item("About...") { SDM::about }
  file_loaded snexport
  file_loaded snimport
end

Those plug-ins are then installed from a setup. My problem is, if I try to install my plug-ins on a brand new SketchUp version, where no other extension has been installed, it does not work : it behaves as if the “Extensions” menu did not exist… If there are extensions already installed, everything works fine.

I thought of forcing the creation of the “Extensions” menu but a- it does not seem possible b- I don’t think that’s the right thing to do :wink:

What do I do wrong ?

Thanks for you help and have a good day :slight_smile:

I don’t know why it fails…
No Extensions menu is made if there are no extensions loaded, but your code should make the menu items as it loads…
But why not load the two plugins’ RB files, before making these menu item ?
That way the submenu might see it has something to add/show ?

Hi TIG,

And thanks for your answer. You mean that I should put the two “file_loaded” lines before the whole menu thing ?

Thanks a lot for your help and have a good day :slight_smile:

file_loaded is meant to be called at the end of a file that loads code, to tell SketchpUp’s management system that this file has been loaded. The management system can then be consulted via the companion method file_loaded? to check whether the file is already in memory and avoid doing setup actions again. But neither of these methods actually loads anything. Putting those lines earlier won’t alter that fact!

That said, like @TIG I’ve not encountered this myself and without seeing all of your code I can’t guess why it might be happening. Normally SketchUp creates the Extensions menu “on demand” when items are added to it.

Thanks a lot for your answer.

So this part of code should work and thus… the truth is out there :wink:

Thanks anyway : at least I know this part is correct. I’ll try to make different tests to see what happens.

Have all a good day :slight_smile:

Submenu titles should not have a ... (ellipsis). The ellipsis means that this menu item opens a dialog box. Since it opens a submenu it should just be "SDM".


Also, traditionally, importers and exporter commands go on the File menu.

You may not be aware but the API also allows 3rd party importers to be interfaced with SketchUp’s import dialog using the API class Sketchup::Importer.

You could use this and well as your own menu item, and that way user’s could find it either way.

1 Like

Ellipsis actually means more information is needed before an action is carried out. If the goal with clicking a menu entry is to open an additional window, … should not be used as that action is carried out as soon as you click. If the goal is something else, e.g. saving, and the extra window is just a step on the way, then … should be used.

2 Likes

file_loaded? and file_loaded simply takes any kind of string - which should be unique. And is a simple mechanism to prevent executing a chunk of code twice. And this is mostly just for development when you might reload files using load. Easiest way to get a unique string is to use __FILE__.

For your code snippet, just make sure you have loaded the files with the functions you are calling from the menu block.

require "my_extension/sdm_export.rb"
require "my_extension/sdm_import.rb"

unless file_loaded?(__FILE__)
  submenu = UI.menu("Plugins").add_submenu("SDM...") 
  submenu.add_item("SDM export...") { SDM::export }
  submenu.add_item("SDM import...") { SDM::import }
  submenu.add_item("About...") { SDM::about }
  file_loaded(__FILE__)
end

Wow ! I did not know that :slight_smile: Thank you.

This will simplify my code :slight_smile:

And as for the “…” and the menu where the importer/exporter belong, the API class Sketchup::Importer and so on, I will discuss that with my boss. Personaly I think it is better to do as you say but they really want the little “About” dialog and I am not sure how to fit it in if I pu my importer/exporter where they belong…

Anyway, thanks again and have a good day :slight_smile:

Rather than file_loaded? which, somewhat unexpectedly, requires a separate call to file_loaded, and to my knowledge is defined in sketchup.rb, I prefer a pure Ruby approach with instance variables.

unless @loaded
  @loaded = true
  # Stuff...
end

Remember that @loaded is scoped to whatever object the code is written within, so if you need multiple load guards they need to be in different classes/modules.

1 Like

Sketchup::Importer interface is preferred. One place to go for import - for the user. Unfortunately we don’t have an exporter interface paired to match. (It’s on our list though.)

By the way - we have a number of examples and tools on our GitHub page: https://github.com/SketchUp/

2 Likes

Thanks again to all of you for your help :slight_smile: I will be able to improve my code and I made an evolution demand so we can handle the import/export plug-ins in the correct menus as you told me :slight_smile:

2 Likes