sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    How to distinguish face size

    Scheduled Pinned Locked Moved Developers' Forum
    11 Posts 6 Posters 654 Views 6 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • jolranJ Offline
      jolran
      last edited by

      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..

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        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...

        TIG

        1 Reply Last reply Reply Quote 0
        • pingpinkP Offline
          pingpink
          last edited by

          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.

          πŸ˜„

          1 Reply Last reply Reply Quote 0
          • thomthomT Offline
            thomthom
            last edited by

            @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 methodround_to' for 1.23456:Float>
            (eval):894`

            Thomas Thomassen β€” SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund

            1 Reply Last reply Reply Quote 0
            • jolranJ Offline
              jolran
              last edited by

              @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 πŸ˜„

              1 Reply Last reply Reply Quote 0
              • TIGT Offline
                TIG Moderator
                last edited by

                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...

                TIG

                1 Reply Last reply Reply Quote 0
                • K Offline
                  ktkoh
                  last edited by

                  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

                  1 Reply Last reply Reply Quote 0
                  • TIGT Offline
                    TIG Moderator
                    last edited by

                    @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...

                    TIG

                    1 Reply Last reply Reply Quote 0
                    • Dan RathbunD Offline
                      Dan Rathbun
                      last edited by

                      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.)

                      I'm not here much anymore.

                      1 Reply Last reply Reply Quote 0
                      • pingpinkP Offline
                        pingpink
                        last edited by

                        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]

                        1 Reply Last reply Reply Quote 0
                        • 1 / 1
                        • First post
                          Last post
                        Buy SketchPlus
                        Buy SUbD
                        Buy WrapR
                        Buy eBook
                        Buy Modelur
                        Buy Vertex Tools
                        Buy SketchCuisine
                        Buy FormFonts

                        Advertisement