Module: YorikLib

Defined in:
Plugins/yorik_lib/yorik_lib_main_collection.rb

Overview

Library of core shared methods used by other extensions. Usage only for SketchUp plugins development.

Author:

  • Yurij Kulchevich aka yorik1984

Components and groups Methods collapse

Informations Methods collapse

Class Method Details

.about_extension_mbox(extension) ⇒ Sketchup::UI

Show information about plugin in message box

Since:

  • 1.0


194
195
196
197
# File 'Plugins/yorik_lib/yorik_lib_main_collection.rb', line 194

def self.about_extension_mbox(extension)
  UI.messagebox(extension.name + "\n" + extension.description + "\n" +
                extension.creator + "\nv " + extension.version)
end

.attribute_value(entity, name, dictionary_name = "dynamic_attributes") ⇒ String

Get value of components attributes from dictionary attached to the entity

Since:

  • 1.0


145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'Plugins/yorik_lib/yorik_lib_main_collection.rb', line 145

def self.attribute_value(entity, name, dictionary_name = "dynamic_attributes")
  name = name.downcase
  if entity.is_a?(Sketchup::ComponentInstance)
    value = entity.get_attribute dictionary_name, name
    if value == nil
      value = entity.definition.get_attribute(dictionary_name, name)
    end
    return value
  elsif entity.is_a?(Sketchup::ComponentDefinition)
    return entity.get_attribute(dictionary_name, name)
  else
    return nil
  end
end

.attributes_list(entity, dictionary_name = "dynamic_attributes") ⇒ Array

Generate collection of all components attributes from dictionary attached to the entity

Since:

  • 1.0


123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'Plugins/yorik_lib/yorik_lib_main_collection.rb', line 123

def self.attributes_list(entity, dictionary_name = "dynamic_attributes")
  list = {}
  if entity.is_a?(Sketchup::ComponentInstance)
    entity = entity.definition
    if entity.attribute_dictionaries
      if entity.attribute_dictionaries[dictionary_name]
        dictionary = entity.attribute_dictionaries[dictionary_name]
        dictionary.each_keys { |key| list[key] = true if (key[0..0] != '_') }
      end
    end
  end
  list.keys
end

.collect_by_attribute(selection, attribute_list, nested_level, dictionary_name = "dynamic_attributes", components_list = [], current_nested_level = 2) ⇒ Array

Recursive method to get collection of components with attributes from list

See Also:

Since:

  • 1.0


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'Plugins/yorik_lib/yorik_lib_main_collection.rb', line 91

def self.collect_by_attribute(selection, attribute_list, nested_level,
                         dictionary_name = "dynamic_attributes",
                         components_list = [], current_nested_level = 2)
  selection.each do |entity|
    definition = YorikLib::get_definition(entity)
    next if definition.nil?
    if current_nested_level <= nested_level &&
         definition == Sketchup::ComponentInstance
      attribute_list.each do |attribute_name|
        if YorikLib::attributes_list(entity).include?(attribute_name.downcase)
          components_list.push entity
          break
        end
      end
    end
    components_list =
        collect_by_attribute(definition.entities, attribute_list,
                             nested_level, dictionary_name, components_list,
                             current_nested_level + 1)
  end
  components_list
end

.components_selected_mbox?(selection, msg_components = "Select only components", msg_nothing = "Nothing selected") ⇒ Boolean

Show message box when selected entities are not only components or select nothing

Since:

  • 1.0


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'Plugins/yorik_lib/yorik_lib_main_collection.rb', line 169

def self.components_selected_mbox?(selection,
                              msg_components = "Select only components",
                              msg_nothing    = "Nothing selected")
  if selection.empty?
    UI.messagebox msg_nothing
    return false
  end
  selection.each do |entity|
    if !entity.is_a?(Sketchup::ComponentInstance)
      UI.messagebox msg_components
      return false
    end
  end
  true
end

.get_definition(entity) ⇒ Sketchup::ComponentDefinition or NilClass

Get definition of the entity

Since:

  • 1.0


47
48
49
50
51
52
53
54
55
# File 'Plugins/yorik_lib/yorik_lib_main_collection.rb', line 47

def self.get_definition(entity)
  if entity.is_a?(Sketchup::ComponentInstance)
    entity.definition
  elsif entity.is_a?(Sketchup::Group)
    entity.entities.parent
  else
    nil
  end
end

.nested_level(selection, deep_level = 1, nesting_levels = [1]) ⇒ Numeric

Recursive method to get maximum nested level in selected components

Since:

  • 1.0


68
69
70
71
72
73
74
75
76
# File 'Plugins/yorik_lib/yorik_lib_main_collection.rb', line 68

def self.nested_level(selection, deep_level = 1, nesting_levels = [1])
  selection.each do |entity|
    definition = YorikLib::get_definition(entity)
    next if definition.nil?
    nesting_levels.push(deep_level + 1)
    nested_level(definition.entities, deep_level + 1, nesting_levels)
  end
  nesting_levels.sort.last
end

.open_help_file(help_file_path, defaut_help_file_path, err_msg) ⇒ Object

Open help file. Html help-file will bee open in new tab of current web browser

Since:

  • 1.0


207
208
209
210
211
212
213
214
215
# File 'Plugins/yorik_lib/yorik_lib_main_collection.rb', line 207

def self.open_help_file(help_file_path, defaut_help_file_path, err_msg)
  if File.exist?(help_file_path)
    UI.openURL "file://" + help_file_path
  elsif File.exist?(defaut_help_file_path)
    UI.openURL "file://" + defaut_help_file_path
  else
    UI.messagebox (err_msg + "\n" + defaut_help_file_path)
  end
end