How to distinguish face size
-
Hi , I have some questions.
I'm trying to distinguish face sizes with the part-number by using set_attribute( Generate part number and displaying colored faces) , and get_attribute ( Export in Excel ). But , I can't make same face size area to have same color , and count the pieces.
What should I revise to make it works?
Thanks so much
<span class="syntaxdefault">def set_attribute<br /><br /> model </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br /> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">start_operation</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'Colour by same area and dimensions'</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> entities </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities<br /> face</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">selection</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> face</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">set_attribute</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Attribute"</span><span class="syntaxkeyword">,</span><span class="syntaxstring">"Glass"</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">true</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> colors </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Color</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">names <br /> attribute_name </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> colors</span><span class="syntaxkeyword">[</span><span class="syntaxdefault"> rand</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> colors</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">length</span><span class="syntaxkeyword">-</span><span class="syntaxdefault">1 </span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> m </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">materials</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> attribute_name </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> m</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">color </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> attribute_name<br /> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each </span><span class="syntaxkeyword">{</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> next unless e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Face</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">material </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> m<br /> <br /> </span><span class="syntaxkeyword">}</span><span class="syntaxdefault"> <br /> <br /></span><span class="syntaxcomment">### this gives the face your-attribute called Glass that is true<br /></span><span class="syntaxdefault">end<br /><br />def get_attribute<br /><br /> model </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br /> entities </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities<br /> face</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">selection</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> context </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> face</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">get_attribute</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Attribute"</span><span class="syntaxkeyword">,</span><span class="syntaxstring">"Glass"</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> if context<br /> </span><span class="syntaxcomment">#===============#<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># PART-NUMBER # GL-01 , GL-02 , GL-03 , ....<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment">#===============#<br /></span><span class="syntaxdefault"> puts </span><span class="syntaxstring">"GL-01"<br /></span><span class="syntaxdefault"> <br /> </span><span class="syntaxcomment">#===============#<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># DIMENSIONS # <br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment">#===============# <br /></span><span class="syntaxdefault"> <br /> glas </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> face</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">bounds<br /> puts </span><span class="syntaxstring">"Width ; #{(glas.width*(0.0254)).to_f.round_to(2).to_s }"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">+</span><span class="syntaxdefault"> </span><span class="syntaxstring">" m"<br /></span><span class="syntaxdefault"> puts </span><span class="syntaxstring">"Height ; #{(glas.depth*(0.0254)).to_f.round_to(2).to_s }"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">+</span><span class="syntaxdefault"> </span><span class="syntaxstring">" m"<br /></span><span class="syntaxdefault"> <br /> </span><span class="syntaxcomment">#===============#<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># QUANTITY # <br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment">#===============#<br /></span><span class="syntaxdefault"> <br /> num</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">0<br /> ss</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">selection<br /> if ss</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault"> ents</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">ss</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_a<br /> else<br /> ents</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_a<br /> end</span><span class="syntaxcomment">#if<br /></span><span class="syntaxdefault"> defs</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">definitions<br /> ents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|</span><span class="syntaxdefault">num</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">num</span><span class="syntaxkeyword">+(</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">)</span><span class="syntaxdefault">if e</span><span class="syntaxkeyword">.class==</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Face</span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault"> defs</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">d</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> d</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each</span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> n</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">d</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">instances</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">length<br /> num</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">num</span><span class="syntaxkeyword">+(</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">*</span><span class="syntaxdefault">n</span><span class="syntaxkeyword">)</span><span class="syntaxdefault">if e</span><span class="syntaxkeyword">.class==</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Face<br /> </span><span class="syntaxkeyword">}</span><span class="syntaxdefault"> <br /> </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault"> puts </span><span class="syntaxstring">"Quantity ; #{num}"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">+</span><span class="syntaxdefault"> </span><span class="syntaxstring">" Pcs"<br /></span><span class="syntaxdefault"> <br /> </span><span class="syntaxcomment">#===============#<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># AREA # <br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment">#===============#<br /></span><span class="syntaxdefault"> <br /> area </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">face</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">area</span><span class="syntaxkeyword">*(</span><span class="syntaxdefault">0.00694444444</span><span class="syntaxkeyword">)).</span><span class="syntaxdefault">to_f</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">round_to</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">2</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">to_s <br /> <br /> </span><span class="syntaxcomment">#area = (face.area /(1.m*1.m)).to_f.round_to(2).to_s + " SQ.M"<br /><br /></span><span class="syntaxdefault"> puts </span><span class="syntaxstring">"Area ; #{area}"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">+</span><span class="syntaxdefault"> </span><span class="syntaxstring">" SQ.Ft"<br /></span><span class="syntaxdefault"> <br /> else<br /> puts </span><span class="syntaxstring">"This glass does not have Part-Number"<br /></span><span class="syntaxdefault"> end</span><span class="syntaxcomment">#if<br /></span><span class="syntaxdefault"> <br /> </span><span class="syntaxcomment">#return num<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment">#===============#<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># Export Excel # <br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment">#===============# <br /></span><span class="syntaxkeyword">=</span><span class="syntaxdefault">begin <br /> out_name </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">"#{File.basename(model.path).split("</span><span class="syntaxkeyword">.</span><span class="syntaxstring">")[0]}"</span><span class="syntaxdefault"> <br /> if out_name </span><span class="syntaxkeyword">==</span><span class="syntaxdefault"> </span><span class="syntaxstring">''</span><span class="syntaxdefault"> then out_name </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">'Report'</span><span class="syntaxdefault"> <br /> out_name </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">"#{out_name}_faceareas.xlsx"</span><span class="syntaxdefault"> <br /><br /><br /> unless </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">out_name </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">savepanel</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> </span><span class="syntaxstring">"Where shall I save the output?"</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">"."</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> out_name </span><span class="syntaxkeyword">)).</span><span class="syntaxdefault">nil</span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> then <br /><br /> <br /> area_file </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> File</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault"> out_name </span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">"w+"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> <br /><br /> <br /> if area_file</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">nil</span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> then <br /> UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">messagebox</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> </span><span class="syntaxstring">'File error!'</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> <br /> return <br /> end <br /><br /><br /> face</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each do </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">f</span><span class="syntaxkeyword">|</span><span class="syntaxdefault"> <br /> area_file</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">puts</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> </span><span class="syntaxstring">"#{f.area}"</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> <br /> <br /> end <br /> else <br /> User aborted <br /> end <br /><br /> area_file</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">close <br />end <br /></span><span class="syntaxkeyword">=</span><span class="syntaxdefault">end<br />end</span>
-
Not sure about your rounding method ? round_to ?
Float=>round can take a parameter in Ruby 1.9, not in Ruby 1.8.~
Could be totally wrong here, but it look fishy to me. If I'm wrong (which we soon will see) I've at least learnt something new today..
-
Notwithstanding some of the methods you are using to format strings - suggest you look up
sprintf
etc...An .xls file is 'binary' and it is specially encoded for Excel to read, as is the newer .xlsx file type.
You on he other hand are writing plain text strings to a file.
It will never work in Excel by writing a file like that.You have two choices...
Change the file suffix to '.csv', this is a Comma-Separated-Variable format that will open in Excel - it will not have any formating.
You will need to assemble a string including commas separators that you then 'puts' to the file; otherwise you'll have a single column of entries...
You can link a csv file into an xls file dynamically so as you change the csv's contents you can update the xls's display; that way you can format the xls and just use the data from the csv export...OR if you are on PC you could learn how to access Excel using WIN32OLE.so - I have done it in the past - reading and writing xls files direct BUT it is much more convoluted... You can edit formats, cells and cells contents formulas etc... look up this crib http://davidsulc.com/blog/2011/03/27/using-ruby-and-win32ole-to-manipulate-excel/ there are several others if you Google...
-
Thank you kindly jolran and TIG.
@jolran , I always use round_to and it can run on SU 7,8.
Do I have to change to be ' float ' ?
I'm not expert in ruby.@TIG , I will study about the export which you suggested to me.
I don't know how to do automatic part-numbers meanwhile changing each colored faces separately , then report sizes , pieces ,and area.
I'm trying to figure it out.
-
@pingpink said:
@jolran , I always use round_to and it can run on SU 7,8.
Then you have a plugin that modifies the base Float class. (which is a big no-no)
If you try it with a plain vanilla installation of SketchUp you get an error:
1.23456.round_to(2) Error: #<NoMethodError: (eval):894: undefined method
round_to' for 1.23456:Float>
(eval):894` -
@unknownuser said:
@jolran , I always use round_to and it can run on SU 7,8.
Do I have to change to be ' float ' ?Listen to these two guys first and foremost. they know what they are talking about..
In any case look up methods compatible with Ruby 1.8.6~7 . I think SU 7,8 use just about same Ruby versions..
"The Pragmatic Programmer's Guide" should be relative to 1.8 I think, if you have missed that one by a chance. The "round" method is described there as well.
Just bought a Ruby book and half of it was for Ruby 1.9 and up
-
For example
sprintf("%.2f", 1.23456) 1.23
This is a String - which will be good for a text-file/report... but
sprintf("%.2f", 1.23456).to_f 1.23
Is a Float again if desired... -
I have been looking at rounding calculated dimensions in my woodworking joint program and this is what I came up with. I only accept inches and millimeters as sketchup units and then round the calculations to the precision set for the active model. It is not very compact code but seems to work.
mod = Sketchup.active_model # Open model ent = mod.entities # All entities in model sel = mod.selection # Current selection @num = 1.0 #UI.messagebox("Test Number = " + @num.to_s) prompts = ["Test Number = "] defaults = [@num.to_s] results = inputbox prompts, defaults, "Test Round Function" if !results then;Sketchup.send_action "selectSelectionTool;";return;end @num = results[0].to_l #Test Units @opt_LEN_No = Sketchup.active_model.options["UnitsOptions"]["LengthUnit"] @opt_Format_No = Sketchup.active_model.options["UnitsOptions"]["LengthFormat"] @opt_Precision_No = Sketchup.active_model.options["UnitsOptions"]["LengthPrecision"] @opt_Precision_No = @opt_Precision_No.to_s UI.messagebox("Precision = " + @opt_Precision_No) case @opt_Precision_No when "0" if @opt_Format_No == 0 then @round = 1.0 end if @opt_LEN_No == 2 then @round = 0.0393701 end if @opt_Format_No == 3 then @round = 1.0 end when "1" if @opt_Format_No == 0 then @round = 0.1 end if @opt_LEN_No == 2 then @round = 0.00393701 end if @opt_Format_No == 3 then @round = 0.5 end when "2" if @opt_Format_No == 0 then @round = 0.01 end if @opt_LEN_No == 2 then @round = 0.000393701 end if @opt_Format_No == 3 then @round = 0.25 end when "3" if @opt_Format_No == 0 then @round = 0.001 end if @opt_LEN_No == 2 then @round = 0.0000393701 end if @opt_Format_No == 3 then @round = 0.125 end when "4" if @opt_Format_No == 0 then @round = 0.0001 end if @opt_LEN_No == 2 then @round = 0.00000393701 end if @opt_Format_No == 3 then @round = 0.0625 end when "5" if @opt_Format_No == 0 then @round = 0.00001 end if @opt_LEN_No == 2 then @round = 0.000000393701 end if @opt_Format_No == 3 then @round = 0.03125 end when "6" if @opt_Format_No == 0 then @round = 0.000001 end if @opt_LEN_No == 2 then @round = 0.0000000393701 end if @opt_Format_No == 3 then @round = 0.015625 end end #case UI.messagebox("Round To = " + @round.to_s) @num_R = ((@num + (@round/2.0))/@round).to_i*@round UI.messagebox(@num.to_s + " Round to " + @round.to_l.to_s + " = " + @num_R.to_l.to_s) Sketchup.active_model.entities.add_line [@num_R,0,0],[@num_R,@num_R,0]
Keith
-
@pingpig
I think you miss the point a bit.
Float.round_to()
does not exist as a standard method for the version of Ruby that ships with SketchUp.
Therefore is you are not making it yourself by adding to the standard methods - which is generally advised against, and alternative ways are recommended - then you must have another script loading that creates that method. Thus if you don't have that script loading, or another user of your script doesn't have it the 'round_to
' method will break and the script will fail with errors...
The example you posted extends/modifies the Float base class in a number of ways [unwisely] - some methods are not needed - e.g.Float.abs
- because these come built-in to Ruby ! Sketchup also adds its own additional functions to convert to and from radians, and numeric ways of dealing with lengths etc... Please read the API documents... AND some general Ruby help publications... Dan Rathbun has provided several good threads and links at SCF that will help you...My example of the '
sprintf()
' uses a simple built-in Ruby method that is used for formating floats in several ways - typically into strings at a fixed d.p., although you can convert that back into a 'rounded' float if desired using.to_f
... -
The post of that modification of
Fixnum
should be removed! (It is very poorly done.. overrides base methods, some of which return float instead of integer.) -
Thank you very much !
Actually , I refer "round_to" in a class Fixnum below my main codes. I don't see any error.
It's my new knowledge about sprintf("%.2f", 1.23456)
[ruby]class Fixnum def abs rx = self if rx < 0 rx = -rx end return rx end def to_meter ufactor = 1.m rx = (self) / ufactor return rx end def round_to(x) (self * 10**x).round.to_f / 10**x end def ceil_to(x) (self * 10**x).ceil.to_f / 10**x end def floor_to(x) (self * 10**x).floor.to_f / 10**x end def rad_to_deg (self / Math;;PI) * 180.0 end def deg_to_rad (self * Math;;PI) / 180.0 end def comma_format s = self.to_s if s.include? ?. pre, post = s.split '.' "#{pre.reverse.gsub( /\d{3}(?=\d)/, '\&,' ).reverse}.#{post}" else s.reverse.gsub( /\d{3}(?=\d)/, '\&,' ).reverse end end end
[/ruby][/code]
Advertisement