Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export each main component sequentially #183

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
92 changes: 60 additions & 32 deletions src/sketchup-stl/exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?
Expand All @@ -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, [entity], 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)
Expand Down Expand Up @@ -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.grep(Sketchup::ComponentInstance)
elsif OPTIONS['component_selection'] == 'Selection Only'
if Sketchup.active_model.selection.length > 0
export_ents = Sketchup.active_model.selection
else
Expand All @@ -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) }
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/sketchup-stl/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 23 additions & 0 deletions src/sketchup-stl/reload.rb
Original file line number Diff line number Diff line change
@@ -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