When i run the code the result is 0;0. The components are loaded with my floor builder so every new component gets a #
Is this the reason why it is not working? when i load it from the component tab is works well. When i understand this i can see how to transfer this to a tekst file. In the image you see the column selected what should be count as example (count should be 6x)
Also @dictionary_name needs to be set to "dynamic_attributes".
However do not use an instance variable at the toplevel objectspace (which is Object. They’ll propagate into all other classes.)
For testing at the console just use a local variable like all the others. (Later inside your class or module, you can use @dictionary_name.)
# set dictionary name
dictionary_name = "dynamic_attributes"
# get numerical attribute and convert to Length class object
attribute = definition.get_attribute(dictionary_name, 'lenz').to_l
# convert Length object to String expressed in current model units
height = Sketchup.format_length( attribute )
# create a textline of information
textline = "#{definition.name} (#{height}) total: #{number}"
# output information to console
puts textline
# get a path:
path = UI::savepanel("Save Report As...","report.txt")
if path
# save the textline:
File::write( path, textline )
end
Thank you both. Monday im back at the office and going to play With it. The code I did I saw on the sketchup site. So very basic. But this helps a lot to me
I give the code a try it gives me the error you can see under the picture. When i change the textline with this: # create a textline of information
textline = “#{“Enkelzijdige kolom”} (#{3000.mm}) total: #{20}” it is working fine. So in the beginning something went wrong.
I’d open the file for writing, then use puts for each line, then puts the total, then close the file.
I’d also change it’s file-type to “.csv”, so that it works with Excel more easily ?
Assuming you have some variables preset…
path = UI.savepanel("Save Report As...", "Report.csv")
begin
file = File.open(path, "w")
### opened with 'w' to Write - overwrites any existing contents - 'a' to Append, 'r' to Read...
file.puts("Type,Height.Count\n") ### The header with an extra empty line [\n]
### Do your assembly of the data for the various lines here,
### incrementing the 'total_length' and 'total_count' - perhaps in a loop
file.puts("#{this_name},#{this_height},#{this_count}")
### Now add the final total footer
file.puts("____________________________________________")
file.puts("Total,#{total_length},#{total_count}")
file.close ### Remember to close it !
rescue
UI.messagebox("File #{path} could not be opened !\nIt might be open in another Application ?\nIf so, close it and retry...")
end
The format method just formats data into one big long textline.
I often save the format strings as module constants and use the consant in the call using format.
In that loop you build up a text report by repeatedly appending your textlines to a string object.
path = UI::savepanel("Save Report As...","report.txt")
if path
# Create a data format string with newline:
DATALINE = "%-20s (%8s) count: %-s\n"
dictionary_name = "dynamic_attributes"
# Set Report Captions:
@report = format( DATALINE, "Type", "Height", "Count" )
# Iterate the found component definitions:
#
# EDIT: SEE NEXT POST <<===================<<<<<
# Save the Report:
File::write( path, @report )
end
Oh OK I see that you wish to group the same heights onto one line with the height total at the end.
I’ll need to think a bit on this.
EDIT:
Ok when you change a DC’s attributes, the DC engine makes it a unique component, so it copies the definition, and gives the new one a name with “#number” appened.
ie, “Enkelzijdige kolom#1”, “Enkelzijdige kolom#2”, etc.
So when searching for matching definitions, you need to use a Regular Expression to match all definitions whose name begins with “Enkelzijdige kolom”.
Hi. Thank you for having 2 new codes that are working. When I was running the code I discoverd a problem in my floor builder. It was making every column and beam that were the same unique. So first of all I changed the floor builder code and now it’s working right. Time to work out the report code futher but in the code of Dan, How can I add empty lines so you can make it more readable as a lay-out? Maybe I Come back with new questions later.
When you use the code to count objects I saw a strange thing. When you Load an component and you decide not to use it and delete it. You still get that object in the text file. The total number is zero but the name stays. I think something is going wrong with the code