From 852b368d96293009c748f61f472d34d62d62344a Mon Sep 17 00:00:00 2001 From: rmthrt Date: Thu, 28 Mar 2024 12:01:12 +0100 Subject: [PATCH 1/4] add reload.rb to easily debug fixup --- README.md | 10 ++++++++++ src/sketchup-stl/exporter.rb | 2 +- src/sketchup-stl/loader.rb | 1 + src/sketchup-stl/reload.rb | 23 +++++++++++++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/sketchup-stl/reload.rb diff --git a/README.md b/README.md index 4bd0273..8242677 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,16 @@ If you're a SketchUp Ruby community member, you need to fork this repo (If you d 1. From your GitHub repo, send a Pull Request. +#### Debuging advice + +To debug, the following steps will allow to reload the app without closing sketchup on each modification: + +- uncomment the line `` Sketchup::require File.join(PLUGIN_PATH, 'reload')`` in loader.rb +- copy `` sketchup-stl.rb`` S file and `` sketchup-stl`` folder in `` SC:\Users\tks\AppData\Roaming\SketchUp\SketchUp 2023\SketchUp\Plugins`` folder + +Once Sketchup launched, you can modify scrripts and reload the extensions by going to "Extensions">"Developer"> "Reload Code Files: sketchup-stl" + + ## License diff --git a/src/sketchup-stl/exporter.rb b/src/sketchup-stl/exporter.rb index 78f73d8..c30470c 100644 --- a/src/sketchup-stl/exporter.rb +++ b/src/sketchup-stl/exporter.rb @@ -42,7 +42,7 @@ def self.select_export_file title_template = STL.translate('%s file location') default_filename = "#{model_name}.#{file_extension}" dialog_title = sprintf(title_template, default_filename) - directory = nil + directory = Sketchup.active_model.path filename = UI.savepanel(dialog_title, directory, default_filename) # Ensure the file has a file extensions if the user omitted it. if filename && File.extname(filename).empty? diff --git a/src/sketchup-stl/loader.rb b/src/sketchup-stl/loader.rb index af283a0..a8039ce 100644 --- a/src/sketchup-stl/loader.rb +++ b/src/sketchup-stl/loader.rb @@ -24,6 +24,7 @@ module STL Sketchup::require File.join(PLUGIN_PATH, 'utils') Sketchup::require File.join(PLUGIN_PATH, 'importer') Sketchup::require File.join(PLUGIN_PATH, 'exporter') + # Sketchup::require File.join(PLUGIN_PATH, 'reload') end # module STL end # module CommunityExtensions diff --git a/src/sketchup-stl/reload.rb b/src/sketchup-stl/reload.rb new file mode 100644 index 0000000..df5e8c1 --- /dev/null +++ b/src/sketchup-stl/reload.rb @@ -0,0 +1,23 @@ +module TKS + module SomePlugin + + if !defined?(@gui_loaded) + + # This assumes you have created a SketchupExtension object in a registrar + # file in the "Plugins" folder, referenced by a local constant EXTENSION. + UI.menu("Developer").add_item("Reload Code Files: sketchup_stl") { + prev_dir = Dir.pwd + Dir.chdir(__dir__) do + Dir.glob("*.rb").each { |rb_file| load(rb_file) } + rescue => error + puts error.inspect + puts error.backtrace + Dir.chdir(prev_dir) + end + } + + @gui_loaded = true + end + + end +end \ No newline at end of file From 6b70e2e35b68e89bec858cc60e65ea0bcdd43f73 Mon Sep 17 00:00:00 2001 From: rmthrt Date: Thu, 28 Mar 2024 12:13:26 +0100 Subject: [PATCH 2/4] add "export_each_main_component" : add of component selection map to handle : 'Full Model', 'One file for each Main Component', 'Selection Only' export types --- src/sketchup-stl/exporter.rb | 90 +++++++++++++++++++++++------------- 1 file changed, 59 insertions(+), 31 deletions(-) diff --git a/src/sketchup-stl/exporter.rb b/src/sketchup-stl/exporter.rb index c30470c..e9d698e 100644 --- a/src/sketchup-stl/exporter.rb +++ b/src/sketchup-stl/exporter.rb @@ -21,9 +21,9 @@ module Exporter STL_BINARY = 'Binary'.freeze OPTIONS = { - 'selection_only' => false, 'export_units' => 'Model Units', - 'stl_format' => STL_ASCII + 'stl_format' => STL_ASCII, + 'component_selection' => 'One file for each Main Component' } PREF_KEY = 'CommunityExtensions\STL\Exporter'.freeze @@ -56,17 +56,36 @@ def self.export(path, export_entities, options = OPTIONS) if RUBY_VERSION.to_f > 1.8 filemode << ':ASCII-8BIT' end - file = File.new(path , filemode) - if options['stl_format'] == STL_BINARY - file.binmode - @write_face = method(:write_face_binary) - else - @write_face = method(:write_face_ascii) - end - scale = scale_factor(options['export_units']) - write_header(file, model_name, options['stl_format']) - facet_count = find_faces(file, export_entities, 0, scale, Geom::Transformation.new) - write_footer(file, facet_count, model_name, options['stl_format']) + if OPTIONS['component_selection'] == 'One file for each Main Component' + for entity in export_entities + suffix_filename = File.basename(path, File.extname(path)) + file_path = File.dirname(path) +'/' + suffix_filename + '_' + Utils.definition(entity).name + '.stl' + file = File.new(file_path , filemode) + if options['stl_format'] == STL_BINARY + file.binmode + @write_face = method(:write_face_binary) + else + @write_face = method(:write_face_ascii) + end + scale = scale_factor(options['export_units']) + write_header(file, model_name, options['stl_format']) + facet_count = find_faces(file, Utils.definition(entity).entities, 0, scale, Geom::Transformation.new) + write_footer(file, facet_count, model_name, options['stl_format']) + end + + else + file = File.new(path , filemode) + if options['stl_format'] == STL_BINARY + file.binmode + @write_face = method(:write_face_binary) + else + @write_face = method(:write_face_ascii) + end + scale = scale_factor(options['export_units']) + write_header(file, model_name, options['stl_format']) + facet_count = find_faces(file, export_entities, 0, scale, Geom::Transformation.new) + write_footer(file, facet_count, model_name, options['stl_format']) + end end def self.find_faces(file, entities, facet_count, scale, tform) @@ -232,7 +251,10 @@ def self.get_vertex_order(positions, face_normal) # Return model.active_entities, selection, or nil def self.get_export_entities export_ents = nil - if OPTIONS['selection_only'] + if OPTIONS['component_selection'] == 'One file for each Main Component' + model = Sketchup.active_model + export_ents = model.entities + elsif OPTIONS['component_selection'] == 'Selection Only' if Sketchup.active_model.selection.length > 0 export_ents = Sketchup.active_model.selection else @@ -253,12 +275,14 @@ def self.get_export_entities def self.do_options # Read last saved options - ['selection_only', 'stl_format', 'export_units'].each do |key| + ['stl_format', 'export_units','component_selection'].each do |key| OPTIONS[key] = read_setting(key, OPTIONS[key]) end units = ['Model Units', 'Meters', 'Centimeters', 'Millimeters', 'Inches', 'Feet'] units_translated = units.map { |unit| STL.translate(unit) } + export_type = ['Full Model', 'One file for each Main Component', 'Selection Only'] + component_selection = export_type.map { |export_type| STL.translate(export_type) } formats = [STL_ASCII, STL_BINARY] formats_translated = formats.map { |format| STL.translate(format) } @@ -267,29 +291,31 @@ def self.do_options col = [0, 10, 110] first_row = 7 vspace = 30 - row = (0..4).map{|e| e * vspace + first_row} + row = (0..5).map{|e| e * vspace + first_row} row.unshift(0) window_options = { :title => STL.translate('STL Export Options'), :preferences_key => PREF_KEY, :height => 160, - :width => 290, + :width => 300, :modal => true } window = SKUI::Window.new(window_options) - # Row 1 Export Selected - chk_selection = SKUI::Checkbox.new( - 'Export only current selection', - OPTIONS['selection_only'] - ) - chk_selection.position(col[1], row[1]) - chk_selection.check if OPTIONS['selection_only'] - chk_selection.on(:change) { |control| - OPTIONS['selection_only'] = control.checked? + lst_component_selection = SKUI::Listbox.new(component_selection) + lst_component_selection.position(col[2], row[1]) + lst_component_selection.width = 169 + lst_component_selection.value = STL.translate(OPTIONS['component_selection']) + lst_component_selection.on(:change) { |control, value| + component_selection_index = component_selection.index(value) + OPTIONS['component_selection'] = export_type[component_selection_index] } - window.add_control(chk_selection) + window.add_control(lst_component_selection) + + lst_component_selection = SKUI::Label.new(STL.translate('Export type:'), lst_component_selection) + lst_component_selection.position(col[1], row[1]) + window.add_control(lst_component_selection) # # Row 2 Export Units @@ -321,22 +347,24 @@ def self.do_options OPTIONS['stl_format'] = formats[format_index] } window.add_control(lst_format) - + lbl_type = SKUI::Label.new(STL.translate('File format:'), lst_format) lbl_type.position(col[1], row[3]) window.add_control(lbl_type) - + + # # Export and Cancel Buttons # btn_export = SKUI::Button.new('Export') { |control| write_setting('export_units' , OPTIONS['export_units']) write_setting('stl_format' , OPTIONS['stl_format']) - write_setting('selection_only' , OPTIONS['selection_only']) + write_setting('component_selection' , OPTIONS['component_selection']) control.window.close export_entities = get_export_entities if export_entities - path = select_export_file + + path = select_export_file begin export(path, export_entities, OPTIONS) unless path.nil? rescue => exception From e0e1edd2658d8a70f9112c90aa93755d1315a31f Mon Sep 17 00:00:00 2001 From: rmthrt Date: Thu, 28 Mar 2024 22:23:50 +0100 Subject: [PATCH 3/4] fix to export current component ( by using Utils.definition here, we get componentdefinition instead of component instance y going through findface function) --- src/sketchup-stl/exporter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sketchup-stl/exporter.rb b/src/sketchup-stl/exporter.rb index e9d698e..93147de 100644 --- a/src/sketchup-stl/exporter.rb +++ b/src/sketchup-stl/exporter.rb @@ -69,7 +69,7 @@ def self.export(path, export_entities, options = OPTIONS) end scale = scale_factor(options['export_units']) write_header(file, model_name, options['stl_format']) - facet_count = find_faces(file, Utils.definition(entity).entities, 0, scale, Geom::Transformation.new) + facet_count = find_faces(file, [entity], 0, scale, Geom::Transformation.new) write_footer(file, facet_count, model_name, options['stl_format']) end From 79c0967ee0334bdf6afc5b6f89ebaf87732b03a2 Mon Sep 17 00:00:00 2001 From: rmthrt Date: Thu, 28 Mar 2024 22:25:48 +0100 Subject: [PATCH 4/4] filter to only get component Instances --- src/sketchup-stl/exporter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sketchup-stl/exporter.rb b/src/sketchup-stl/exporter.rb index 93147de..df8d5a6 100644 --- a/src/sketchup-stl/exporter.rb +++ b/src/sketchup-stl/exporter.rb @@ -253,7 +253,7 @@ def self.get_export_entities export_ents = nil if OPTIONS['component_selection'] == 'One file for each Main Component' model = Sketchup.active_model - export_ents = model.entities + export_ents = model.entities.grep(Sketchup::ComponentInstance) elsif OPTIONS['component_selection'] == 'Selection Only' if Sketchup.active_model.selection.length > 0 export_ents = Sketchup.active_model.selection