Add a report file module to toolbar

Hi,

I have a code that i can use to report beams (c-profiel) When i want to add this to a toolbar it doesn’t work.

I discovered that it has something to do with “DATALINE”

Does anyone have a solution to get this to working in a toolbar?

This is the code:


def Rfile.main
model = Sketchup.active_model
model.start_operation "Rfile"

definition = Sketchup.active_model.definitions["C-profiel"]
number = definition.count_instances
instance = definition.instances[0]
name = instance.name


#get a path:

path = UI::savepanel("Save Report As...","report.txt")
if path

# Create a data format string with newline:
  DATALINE = "%-20s   %-25s   %-8s  %-8s   %-8s  %-s\n"

  dictionary_name = "dynamic_attributes"

# Set Report Captions:
  @report = format( DATALINE, "ONDERDEEL", "ARTIKEL", "AANTAL", "LENGTE", "HOOGTE", "KLEUR")
   

  @report << format( DATALINE, "", "", "" , "", "", "" )

# Iterate the found component definitions:  

dname = "C-profiel"
deflist = Sketchup.active_model.definitions
matches = deflist.find_all {|d|
  !d.group? && !d.image? && d.name =~ /\A(#{dname})/
}

total = 0
matches.each {|definition|
  number = definition.count_used_instances
  total += number  

  attribute = definition.get_attribute(dictionary_name, 'lenx').to_f.to_mm/25.4
  height = Sketchup.format_length( attribute )

  @report << format( DATALINE, definition.name, name, number, height, "-", "-" )
 }
 
  # Save the Report:
  File::write( path, @report )

end

end
end
 
     toolbar = UI::Toolbar.new "Rfile"
     cmd = UI::Command.new("Rfile") { Rfile.main}
     cmd.small_icon = "ToolPencilSmall.png"
     cmd.large_icon = "ToolPencilLarge.png"
     cmd.tooltip = "Rfile"
     cmd.status_bar_text = "Rfile"
     cmd.menu_text = "Rfile"
     toolbar = toolbar.add_item cmd
     toolbar.show

What IS the DATALINE error ?

Before you try to ‘write’ the report try adding a line:

p @report

Run it with rhe Ruby Console open.
Any errors ?
What is the outputted string ?
Do you have the file already open in say Excel - you can’t then write to it !

Try this writing alternative…

begin
  file=File.open(path, 'w')
  file.puts(@report)
  file.close
rescue Exception => e
  UI.messagebox(e)
end

You must close the file you write to…

Read up on Ruby File writing and begin/rescue/ensure etc

it is easy to see what’s wrong with DATALINE…

Error: #<SyntaxError: <main>:16: dynamic constant assignment
  DATALINE = "%-20s   %-25s   %-8s  %-8s   %-8s  %-s\n"

make it lowercase and it works, but your code has other Ruby syntax anomalies as well…

john

1 Like

As @john_drivenupthewall says…
Change it to something else - say:
@DATALINE = ...
to avoid it being seen as a Constant…
A Constant can’t be reassigned in a def method - at least not in a simple =.
IF you want it as a Constant preset it just once, within your module as it loads, but outside of any def methods…

Then fix the other issues…

1 Like

What do you mean ? The button does not appear on the toolbar ? The Toolbar does not appear ?

“something” does not help us help you.

Paste some error messages. Be more specific.


P.S. - there was another thread where you got this code. I explained to you in that topic how format strings worked or where to read about them, didn’t I ?

The Solution was to make the DATALINE lowercase and now it’s working as it should. Problem Dan was that the button didn’t appear in the toolbar. But now it’s working!

Going to study more on format strings also.

Thank you for the support

1 Like

Yes the DATALINE constant (and all other constants) should be at the top of the module, outside any method.