Pass url from component name field to web dialog inside SketchUp

If selected component has url in name field: open web dialog inside SketchUp with that link.

My intention is to view reference image of selected component without leaving SketchUp. It will be used only by me so I don’t worry what if I have other text in that field.

I am wondering how hard it would be to implement such thing in Ruby.

Proof of concept…
Name a component-instance or group say:
file:///C:/
Select it and in the Ruby Console:
UI.openURL(Sketchup.active_model.selection[0].name)
This will open a Windows Explorer window for your C: drive.
Alternatively, name it say:
www.sketchup.com
The repeated openURL command now opens that URL in your default web-browser.

To make a Ruby API command that opens the ‘name’ URL in a webdialog or htmldialog wholly within SketchUp needs you to first create a ‘dialog’ and then pass it the desired URL with set_url etc…

3 Likes

For older versions of SketchUp (2017 and below) … I would suggest using the .description property (of the definition) for a reference URL, rather than the .name property.


Ref: Class: Sketchup::ComponentDefinition — SketchUp Ruby API Documentation

Starting from SketchUp 2018+, the ComponentDefinition class contains a new default attribute dictionary named “SU_DefinitionSet” with default keys named named “Price”, “Size”, “Url”.

Here is an (untested) example.
Change the toplevel namespace module to something unique, and feel free to change the plugin submodule name:

module Author
 module ShowRefURL

  extend self
  
  @@loaded ||= false

  def show_ref_doc(comp)
    url = comp.definition.get_attribute('SU_DefinitionSet','Url') rescue nil
    if url.nil?
      # check plugin specific dictionary
      key = Module.nesting[0].name.gsub('::','_')
      url = comp.definition.get_attribute(key,'Url')
    end
    if url.nil?
      # check description field for subtext "URL(http://someplace)"
      info = comp.definition.description
      unless info.empty?
        link = info.scan(/URL\(.+\)/)
        unless link.empty?
          # get substring inside parenthesis
          url = link.first[4..-2]
          # convert backslashes to slash and strip whitespace
          url = url.gsub(/\\\\|\\/,'/').strip
        end
      end
    end
    # could also pass the url to another method here to open a WebDialog 
    url ? UI.openURL(url) : UI.messagebox('No URL for this component!')
  end

  unless @@loaded
    UI.add_context_menu_handler do |context_menu|
      if Sketchup.active_model.selection.single_object? &&
      Sketchup.active_model.selection[0].respond_to?(:definition)
        context_menu.add_item("Show Reference Doc") {
          show_ref_doc(Sketchup.active_model.selection[0])
        }
      end
    end
    @@loaded = true
  end

 end
end

I did not write the setter command yet as it would only be needed if you wish to use a plugin specific attribute dictionary.

If you embed a "URL(http://someplace)" substring in the component’s Description field, then you use the Component Browser interface to add or change the URL. (The description field appears to the right of the component’s thumbnail image, and just below the name field. Neither field has a caption in the GUI.)

The 2018+ version built-in default "SU_DefinitionSet" attribute dictionary should use the Component Edit interface, under “Advanced Attributes”.

See: https://help.sketchup.com/en/article/3000124
(and set version at top right of help page to SketchUp 2018.)

1 Like

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