Export STL file by layer/material

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