• Login
sketchucation logo sketchucation
  • Login
πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

How is this possible ?

Scheduled Pinned Locked Moved Developers' Forum
12 Posts 3 Posters 546 Views 3 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.
  • D Offline
    Didier Bur
    last edited by 30 Dec 2010, 13:30

    Hi,
    While working on a new version of the compoSpray script, I've comme across that: select a 2D "faceme" component and:

    c=Sketchup.active_model.selection[0].definition
    #<Sketchup;;ComponentDefinition;0xc3f3cf0>
    c.bounds.height
    9.09494701772928e-013
    c.bounds.height < 0.001 returns false !
    c.bounds.height == 0.001 returns true !
    

    Where am I missing something, or is Ruby crazy ?

    DB

    1 Reply Last reply Reply Quote 0
    • T Offline
      thomthom
      last edited by 30 Dec 2010, 14:25

      SketchUp's internal accuracy is 1000 of an inch. So any lengths you compare that are shorter than that - or when the difference between the lengths is less than 0.001 - will compare as equal.

      Length.<
      http://code.google.com/apis/sketchup/docs/ourdoc/length.html

      @unknownuser said:

      The < method is used to see if one length is less than another length.
      For example, if l1 = 1.0.inch and l2 = 1.000001.inch then l1 == l2 so l1 < l2 should return false.

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

      1 Reply Last reply Reply Quote 0
      • T Offline
        thomthom
        last edited by 30 Dec 2010, 14:27

        hm... am I making stuff up again... πŸ˜•

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

        1 Reply Last reply Reply Quote 0
        • D Offline
          Didier Bur
          last edited by 30 Dec 2010, 14:39

          Thanks thomthom,
          and while we are at it, another weird thing:

          def test()
          case Sketchup.active_model.selection[0].class
          		  when Sketchup;;Face
          			puts "face"
          		  when Sketchup;;ComponentInstance
          			puts "instance"
          		  when Sketchup;;Group
          			puts "group"
          		  when Sketchup;;Image
          			puts "image"
          		  when Sketchup;;Edge
          			puts "edge"
          		  else
          			puts "something else"
          		end
          end
          

          This always outputs "something else" in the Console. Case with 'class' as switch isn't allowed or not effective ? 😲

          DB

          1 Reply Last reply Reply Quote 0
          • T Offline
            TIG Moderator
            last edited by 30 Dec 2010, 14:50

            This comparison weirdness doesn't happen for me.
            Do you have a script that's loading and resetting == for 'equality comparisons' in the Float class?
            I ran a BareGrep on my ../Plugins folder and found no likely culprits for ' class Float' matches - although I did find I have a Float def =~ in my Roof.rb tools which takes Floats to within 1/1000 as being equal - as in 0.0001==0 >>> false BUT 0.0001=~0 >>> true - but I don't suspect that...
            This is what I get

            y=9.09494701772928e-013
            9.09494701772928e-013
            y==0
            false
            y<0
            false
            y>0
            true
            y==0.001
            false
            y>0.001
            false
            y<0.001
            true
            (y*1000000).to_i/1000000.0
            0.0
            (y*10000000000000).to_i/10000000000000.0
            9.0e-013
            
            

            So you can see how to test for nearness to zero with the differing accuracies as the last two examples...
            BUT all == return correct results otherwise πŸ˜•

            TIG

            1 Reply Last reply Reply Quote 0
            • T Offline
              TIG Moderator
              last edited by 30 Dec 2010, 14:54

              @didier bur said:

              Thanks thomthom,
              and while we are at it, another weird thing:

              def test()
              > case Sketchup.active_model.selection[0].class
              > 		  when Sketchup;;Face
              > 			puts "face"
              > 		  when Sketchup;;ComponentInstance
              > 			puts "instance"
              > 		  when Sketchup;;Group
              > 			puts "group"
              > 		  when Sketchup;;Image
              > 			puts "image"
              > 		  when Sketchup;;Edge
              > 			puts "edge"
              > 		  else
              > 			puts "something else"
              > 		end
              > end
              

              This always outputs "something else" in the Console. Case with 'class' as switch isn't allowed or not effective ? 😲

              I found this out when writing my SKMtools class

              case v.class.to_s
                when 'Fixnum'
              

              was my fix... it returns the class name as a string and compares it with another string in the 'when'... so your method WILL work thus

                  def test()
                  case Sketchup.active_model.selection[0].class.to_s
                          when 'Sketchup;;Face'
                           puts "face"
                          when 'Sketchup;;ComponentInstance'
                           puts "instance"
                          when 'Sketchup;;Group'
                           puts "group"
                          when 'Sketchup;;Image'
                           puts "image"
                          when 'Sketchup;;Edge'
                           puts "edge"
                          else
                           puts "something else"
                        end
                  end
              

              TIG

              1 Reply Last reply Reply Quote 0
              • T Offline
                thomthom
                last edited by 30 Dec 2010, 15:28

                @tig said:

                This comparison weirdness doesn't happen for me.
                Do you have a script that's loading and resetting == for 'equality comparisons' in the Float class?

                The object type you get from Boundingbox is Length - not Float. This is why you see this difference. Length has that tolerance built in. Something one need to be aware of when you compare data in Ruby SU. Always check if you have a Float or Length.

                @tig said:

                was my fix... it returns the class name as a string and compares it with another string in the 'when'... so your method WILL work thus

                But will be very slow - same performance issue like using .typename.

                I don't know why one can't use class for switch cases - but instead you should use if,elsif along with .class or .is_? in order to maintain performance. As string operations while iterating entities is just all too big of an performance hit.

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

                1 Reply Last reply Reply Quote 0
                • D Offline
                  Didier Bur
                  last edited by 30 Dec 2010, 15:55

                  Thanks tt & tig for yur answers;
                  I'm aware of the "strings comparison time killer" that's why i want to use the class for the switch.
                  i'll use "elseif" instead 😞

                  DB

                  1 Reply Last reply Reply Quote 0
                  • T Offline
                    thomthom
                    last edited by 30 Dec 2010, 16:11

                    I really don't understand why switch own't work - since face1.class == face2.class work fine...

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

                    1 Reply Last reply Reply Quote 0
                    • T Offline
                      thomthom
                      last edited by 30 Dec 2010, 16:24

                      Ah - so then we can just do this:

                      
                      def test()
                      case Sketchup.active_model.selection[0]
                              when Sketchup;;Face
                               puts "face"
                              when Sketchup;;ComponentInstance
                               puts "instance"
                              when Sketchup;;Group
                               puts "group"
                              when Sketchup;;Image
                               puts "image"
                              when Sketchup;;Edge
                               puts "edge"
                              else
                               puts "something else"
                            end
                      end
                      
                      

                      ❓ Right?

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

                      1 Reply Last reply Reply Quote 0
                      • D Offline
                        Didier Bur
                        last edited by 30 Dec 2010, 16:26

                        Partial answer here: http://www.postal-code.com/mrhappy/blog/2007/02/01/ruby-comparing-an-objects-class-in-a-case-statement/
                        and here http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/3d83faf049160e58

                        This works:

                        case Sketchup.active_model.selection[0]
                                    when 'Sketchup;;Face'
                                     puts "face"
                        ...
                        

                        The simplest the best !

                        DB

                        1 Reply Last reply Reply Quote 0
                        • T Offline
                          TIG Moderator
                          last edited by 30 Dec 2010, 16:36

                          @didier bur said:

                          Partial answer here: http://www.postal-code.com/mrhappy/blog/2007/02/01/ruby-comparing-an-objects-class-in-a-case-statement/
                          and here http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/3d83faf049160e58

                          This works:

                          case Sketchup.active_model.selection[0]
                          >             when 'Sketchup;;Face'
                          >              puts "face"
                          > ...
                          

                          The simplest the best !

                          So in your case

                              def test()
                              case Sketchup.active_model.selection[0]
                                      when Sketchup;;Face
                                       puts "face"
                                      when Sketchup;;ComponentInstance
                                       puts "instance"
                                      when Sketchup;;Group
                                       puts "group"
                                      when Sketchup;;Image
                                       puts "image"
                                      when Sketchup;;Edge
                                       puts "edge"
                                      else
                                       puts "something else"
                                    end
                              end
                          

                          should work ??? Avoiding the .to_s step πŸ˜„

                          TIG

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

                          Advertisement