Export STL file by layer/material

I frequently use the SketchupSTL extension to export STL files. But I need to export a model in parts, either grouped by layers or by materials, so that I have separate STL files for each grouping. Does anyone know how to accomplish this? I do it manually, but I would like something more automatic (without diving into writing my own extension, just yet).

If this is not possible directly to STL, maybe some other export format that could be converted to many STL files via another tool…

The export STL extension options has a checkbox for “Export only current selection” which would mean you would need to export manually part by part, but it would work.

Indeed, that’s my current process, hoping for sometime more automatic.

A basic by-layer exporter “add-on” which uses the STL Extension would look like this:

module JF
   module ExportStlByLayer

      menu = defined?(JF.menu) ? JF.menu("JF Extensions") : UI.menu("Plugins")
      menu.add_item("Export STL by Layer") { main }

      STL_ASCII  = 'ASCII'.freeze
      STL_BINARY = 'Binary'.freeze

      def self.main
         model = Sketchup.active_model
         faces_array = model.active_entities.grep(Sketchup::Face)
         faces = {}
         faces_array.each {|face|
            faces[face.layer.name] ||= []
            faces[face.layer.name].push(face)
         }
         options = {
            'selection_only' => false,
            'export_units'   => 'Millimeters',
            'stl_format'     => STL_ASCII
         }
         faces.each { |layer_name, entities|
            CommunityExtensions::STL::Exporter.export(
               "C:/Users/Jim/tmp/#{layer_name}.stl",
               entities,
               options
            )
         }
      end

   end
end

1 Like

Wow, that is more than I expected! Thanks Jim! So I just have to save that as an .rbz file and manually install it as an extension? Or does this run from the ruby console?

The code is not ready to be used as an Extension - it’s just an example. You would likely need to change the export path for one thing. I’m unfortunately out of time for now.

OK no worries, I will figure out the rest. Thanks!

The code will work if you change the output directory. You’ll just need to put the code in a .rb file, and then put the file in the Plugins folder.

Thanks. I managed to encapsulate it in an extension, but much easier to do what you said.

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