Exportvertices2csv.rb as a teaching material
-
Hi
I'm now studying Ruby Code and using TIG's
"exportvertices2csv"as a teaching material.I think TIG is putting all the code in initialize,in order to make the code simple.
Although I know that is a wise way. I'm now practicing to rapping it in instance method.In order to devide it in to "initialize part" and "outputting csv part"
I've wrote these code.=begin (c) TIG 2011 Type Exportvertices2csv.new in the Ruby Console. Exports all Vertices is a Selection to a X,Y,Z 'CSV' file. Edit sep="," if something other than a separating comma is desired e.g. ';' Make sep="\t" if a TSV file is desired and change ext="csv" to ext="tsv". It uses the current Model Units/accuracy with the approximate '~ ' and unit suffix [if any] removed; e.g. change Model Units to 'meters' 3dp to get exported csv in meters 1.234 - don't use 'fraction' 1' 2 1/2" formats, always use a 'decimal' format. 1.0 20110105 First issue. 1.1 20110116 Wrapped in protective class. =end require 'sketchup.rb' ### class Exportvertices2csv def printcsv(verts) pts=[] verts.each{|v|pts << v.position.x.to_s.gsub(/^~ /,'').to_f.to_s+sep+v.position.y.to_s.gsub(/^~ /,'').to_f.to_s+sep+v.position.z.to_s.gsub(/^~ /,'').to_f.to_s} model=Sketchup.active_model path=model.path if not path or path=="" path=Dir.pwd title="Untitled" else path=File.dirname(path) title=model.title end#if ofile=File.join(path,title+'_verts.'+ext).tr("\\","/") begin file=File.new(ofile,"w") rescue### trap if open UI.messagebox("Vertices File;\n\n "+ofile+"\n\nCannot be written - it's probably already open.\nClose it and try making it again...\n\nExiting...") return nil end pts.each{|pt|file.puts(pt)} file.close puts (pts.length.to_s)+" vertices written to\n"+ofile begin UI.openURL("file;/"+ofile) rescue end end#def def initialize() sep="," ### <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ext="csv" ### <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< model=Sketchup.active_model ss=model.selection verts=[] ss.each{|e|verts << e.vertices if e.class==Sketchup;;Face or e.class==Sketchup;;Edge} if not verts UI.messagebox("No Vertices were Selected.\nExiting.") return nil end#if verts.flatten!.uniq! self.printcsv(verts) end #def end#class ###
but recieve this error message.
Exportvertices2csv.new
Error: #<NoMethodError: undefined method `printcsv' for #Array:0xde0858c>does someone have any idea?
Sincerely yours
-
line 58
self.printcsv(verts)
change to
printcsv(verts)
my head can't think this through though, and it might need to be
Exportvertices2csv.printcsv(verts)
So try one of those, or maybe someone else will come through and help refine my answer
-
line 53 (
verts
is not a boolean object.)
Should be:if verts.empty?
-
@chris fullmer said:
line 58
self.printcsv(verts)
change to
printcsv(verts)
Correct Chris. The method is an instance method.
BUT.. this code is a perfect example of something that should be a module, and not a class.
-
Thankyou all!
The problem was solved.Dan Rathbun
Do you mean the whole class "Exportvertices2csv"
should be a module?
Or, the instance method "printcsv"should be a module?
Advertisement