I’m getting some errors in the Ruby Console for my code…one on line 81 for a \n
(new line) syntax and on line 185 where SketchUp states there should be a }
instead of a :
which I have there so that the program can enable or disable the given command ("Create Trim"
). I’ve tried changing these areas to SketchUp’s suggestion just to see what happens (If I actually did the code wouldn’t do what I want) and just get more frustrating errors. Any and all advice is appreciated! I’ve been working on this tirelessly and am hoping I can start polishing it up and working on any bugs it may have. Also, if things look messed up…I had to space down some lines to make them fit (so you don’t need to scroll left to right to read code). Thank you so much!
#--------------------------------------------------------------------------
require File.join(File.dirname(__FILE__), 'CaydenWilson_ProTrim.rb')
#--------------------------------------------------------------------------
module CaydenWilson
module ProTrim
def prompt_user_selection
selection = Sketchup.active_model.selection
#if faces or groups are in selection
#alert user to try again and close program
if !only_edges_selected?(selection)
UI.messagebox('Your selection cannot include faces or groups.
Please select a trim path using edges only.' , MB_OK)
return
end #if
paths = collect_paths_from_selection
#if no selected path exists, alert user to retry and close program
if !paths
result = UI.messagebox('There is no selected path.
Please select a trim path using edges only.' , MB_OK)
return false if result = IDOK
end #if
end #prompt_user_selection
def model_trim_command()
trim = get_trim_choice(input)
return unless trim #close if user cancels program
trim_component_def = load_trim_component(trim)
#If .skp file unretreiveable, alert user and close program
if !trim_component_def
result = UI.messagebox('File cannot be loaded.
Please select a different trim profile.' , MB_OK)
return false if result == IDOK
else
result = extrude_trim_profile(trim_component_def, paths)
#Alert user modeling was successful and close program
if result
result = UI.messagebox('Trim created successfully.' , MB_OK)
return false if result == IDOK
end #if
end #if/else
end #model_trim_command
def get_trim_choice()
#Prompt user to choose trim profile from drop down menu
prompts = ["Select your trim profile from the drop down menu: "]
defaults = ["trim.skp"]
trimlist = [trim_name_data]
fields = [trimlist.join('|')]
input = UI.inputbox(prompts, defaults, trimlist, fields,
"Select Trim Profile")
return unless input
trim = input[0]
end #get_trim_choice
def load_trim_component(input)
@profiles_path = File.join(__dir__, 'Profiles')
#"Profiles" subfolder of this file's folder
if Dir.exist?(@profiles_path)
Dir.chdir(@profiles_path) {
@trimfiles = Dir['*.skp']
}
else
Dir.mkdir(@profiles_path)
end #if/else
#Creating a json file that has descriptions of
#trim files reloaded into a hash
require 'json'
json = @trimhash.to_join
#Write to disk
filepath = File.join(@profiles_path, 'trim_profiles.json')
begin
File.write(filepath, json, mode: "w", encoding: "UTF-8")
rescue =>
UI.messagebox("There was an error with the file.
Please choose a different trim profile.")
puts err.inspect
end #begin/rescue statement
@trimhash = JSON.parse(json)
@trimhash.keys
@trimhash["trim_desc"]
end #load_trim_component
def get_trim_material_data()
#Prompt user to choose trim profile from drop down menu
prompts = ["Material Name: ", "HEX Value: "]
defaults = ["", ""]
list = ["", ""]
input = UI.inputbox(prompts, defaults, list, "Choose Trim Color")
return unless input
material_name = input[0]
material_color = input[1]
end #get_trim_material_data
def create_trim_matreial(material_name, material_color)
materials = Sketchup.active_model.materials[0]
materials.each {|material| if material.display_name == material_name
materialExists = true
else
materialExists = false
end #check for material
}
#If material nonexistant create and add to pallet
if materialExists == false
#creating material
material = materials.add material_name
material.color = material_color
end #if
end #create_trim_material
def extrude_trim_profile(trim_component_def, paths)
@entities = Sketchup.active_model.active_entities
#Explodes loaded component
group = nil
undo(mode, "Create New Trim")do
group = entities.add_group #Added at orgin
instance = group.entities.add_instance(trimdef, IDENTITY)
group.transform!(transgroup)
instance.explode
face = group.entities.grep(Sketchup::Face).first
face.followme(edges)
end #undo
return group
#Preparing face for extrusion
connected = face.all_connected
face.material = trim
face.back_material = trim
#Extrudes trim profiles along path(s)
face.followme(paths.grep(Sketchup::Edge))
#Alerts user modeling is done
result = UI.messagebox('Your trim profile was created successfully.' ,
MB_OK)
return false if result == IDOK
end #extrude_trim_profile
def import_custom_profile()
custom_path = get_new_profile_path()
if custom_path
require 'filetutils' unless defined? FileUtils
begin
FileUtils.cp(custom_path, @profiles_path)
rescue IOError => err
UI.messagebox("Error copying profile to folder.\n#{err.message}")
else
puts "Profile Copy Cancelled"
return
end #if/else/if...
end #import_custom_profile
def help()
dialog = UI::HtmlDialog.new({
:dialog_title => "ProTrim Help and Support",
:scrollable => true,
:resizable => false,
:width => 1080,
:height => 720,
:left => 500,
:top => 500,
:style => UI::HtmlDialog::STYLE_UTILITY
})
dialog.set_url("https://caydenwilson017.wixsite.com/cwdesigns1")
dialog.show
end #dialog
end #help
#Checking if UI is loaded. If not, loads UI
if !@loaded
submenu = UI.menu("Extensions").add_submenu("ProTrim")
tb = UI::Toolbar.new("ProTrim")
cmd = UI::Command.new("Create Trim") {
model_trim_command()
}
cmd.menu_text = "Create Trim"
cmd.set_validation_proc {
selection.empty? MF_GRAYED | MF_DISABLED : MF_ENABLED
}
UI.submenu("ProTrim").add_item(cmd)
#Command that allows user to import their custom trim profiles
cmd = UI::Command.new("Import Custom Profile") {
import_custom_profile()
}
cmd.menu_text = "Import Custom Profile"
UI.submenu("ProTrim").add_item(cmd)
#Command directing user to support site
cmd = UI::Command.new("Help") {
help()
}
cmd.menu_text = "Help and Support"
UI.submenu("ProTrim").add_item(cmd)
@loaded = true
end #@loaded
end #ProTrim
end #CaydenWilson