[Code] Example: Simple CSV Export for Selected Components

Example: Simple CSV Export for Selected Components

# encoding: UTF-8
#
module YourNamespace  # <---<<< Change to your unique namespace name
  module SomePlugin  # <---<<< Change to some appropriate extension name

    extend self

    VERSION ||= '1.0.0'
    DC_DICT ||= 'dynamic_attributes'

    def build_csv(insts)
      # Create a string with the field headers record:
      csv_text = 'Name,X,Y,Z,LenX,LenY,LenZ' << "\n"

      # Iterate the chosen component instances and append record data:
      insts.each do |inst|
        origin = inst.transformation.origin
        # Use the Length#to_s method to output text in model units:
        x, y, z = origin.x.to_s, origin.y.to_s, origin.z.to_s
        # Numeric values are returned as Float, so convert
        # to Length so we can leverage it's #to_s method:
        units = dc_attr(inst,'_lengthunits')
        if units == 'CENTIMETERS'
          lenx = dc_attr(inst,'lenx').cm.to_s rescue ''
          leny = dc_attr(inst,'leny').cm.to_s rescue ''
          lenz = dc_attr(inst,'lenz').cm.to_s rescue ''
        else # assume units == 'INCHES'
          lenx = dc_attr(inst,'lenx').to_l.to_s rescue ''
          leny = dc_attr(inst,'leny').to_l.to_s rescue ''
          lenz = dc_attr(inst,'lenz').to_l.to_s rescue ''
        end
        # Define a literal array object for this data record:
        data = [ name(inst), x, y, z, lenx, leny, lenz ]
        # Concatenate the data values separated by a comma,
        # using the Array#join method:
        record = data.join(',')
        # Append the record with a newline character to the text:
        csv_text << record << "\n"
      end

      return csv_text
    end

    def dc_attr(inst,attr)
      # Always check instance first:
      val = inst.get_attribute(DC_DICT,attr)
      if val.nil? # Does not differ from definition:
        val = inst.definition.get_attribute(DC_DICT,attr)
      end
      #puts "Value for #{attr} is: #{val}"
      return val
    end

    def export_csv
      # Get a list of the target component instances:
      model = Sketchup.active_model
      insts = model.selection.grep(Sketchup::ComponentInstance)
      # Code could further filter the items by say those whose
      # name begins with 'Cabinet' substring. Example:
      # insts.select! {|inst| name(inst).start_with?('Cabinet') }

      return false if insts.empty? # bailout

      # Prompt for the csv data filename and path:
      pathname = UI.savepanel('CSV Save As...','*.csv')

      # Write out the csv data text to a file if user did not cancel:
      File.write(pathname, build_csv(insts)) if pathname
    end

    def name(inst)
      name = inst.name
      if name.empty?
        if inst.definition.attribute_dictionary(DC_DICT)
          name = dc_attr(inst,'name')
          name = dc_attr(inst,'_name') unless name
        else # not a Dynamic Component
          name = inst.definition.name
        end
      end
      return name
    end

    # RUN ONCE AT STARTUP
    if !defined?(@loaded)
      ::UI.menu('File').add_item('My CSV Selection') { export_csv() }
      @loaded = true
    end

  end # this extension submodule
end # your namepsace module
5 Likes