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

Load text file and assign to variables

Scheduled Pinned Locked Moved Developers' Forum
7 Posts 2 Posters 895 Views 2 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.
  • A Offline
    artmusicstudio
    last edited by 13 Oct 2013, 09:58

    hi,
    when i reopen my text-file with parameters (1 parameter per line)

    by


    count = 0

    File.open('C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/zf/partemp2.txt', 'r') do |f1|
    while line = f1.gets
    puts line

    end  
    

    end


    i can see the values listed in the console // OK

    BUT

    when i try


    count = 0

    File.open('C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/zf/partemp2.txt', 'r') do |f1|
    while line = f1.gets
    puts line

    value[count]=f1.gets
          count = count + 1
    end  
    

    end


    it does not work

    ma question is:

    how can i assign the result of f1.gets to a row of variables in this loop?

    thanx you

    stan

    1 Reply Last reply Reply Quote 0
    • T Offline
      TIG Moderator
      last edited by 13 Oct 2013, 10:59

      I think perhaps an easier format to think about would be:

      file = 'C;/Program Files (x86)/Google/Google SketchUp 8/Plugins/zf/partemp2.txt'
      IO.readlines(file).each_with_index{|line, i|
        line.chomp! ### removes the '\n' from the end of the line
        next if line.empty? ### this traps for empty lines
        next if line =~ /^[#]/ ### lines starting with e.g. '#' are ignored; allows for headers/comments/disabling
        puts i
        puts line
        ### or more likely do your stuff using the string 'line'
      }
      

      Note: if you don't need the index 'i', then use ' ).each{|line|' instead...

      TIG

      1 Reply Last reply Reply Quote 0
      • A Offline
        artmusicstudio
        last edited by 13 Oct 2013, 11:42

        hi tig and hello again,
        this is great and worked inmedialtely in the ruby console.
        so:
        i counts the lines
        line gets the value per line

        maybe a small tip (i still think too much in macro-basic)

        when i retrieve those values form the file,
        this syntax makes problems:

        assign variables from file (like reading an array from a loop)

        a = line[1]
        b = line[2]
        c = line[3]

        how could i build an array within the import routine? i tried so many ways and can#t get it...
        thanx stan

        1 Reply Last reply Reply Quote 0
        • T Offline
          TIG Moderator
          last edited by 13 Oct 2013, 12:04

              file = 'C;/Program Files (x86)/Google/Google SketchUp 8/Plugins/zf/partemp2.txt'
              lines=[]
              IO.readlines(file).each{|line|
                line.chomp! ### removes the '\n' from the end of the line
                next if line.empty? ### this traps for empty lines
                lines << line
              }
          

          NOT sure why you need separate variables... this makes an array of the lines called 'lines'
          Now lines[0] is the first line, lines[1] is the second line etc [note how an array index starts at 0 not 1 !]
          To find the last line use lines[-1] etc...
          So you have reference to each lines using this method, without extra variables...
          If you really want to set up a series of variables then you can do it thus...

          ### you need to set up instance variables, @v_a etc, so they get referenced outside of the {} block, plain v_a won't be referenced outside and would therefore be inaccessible...
          v='@v_a'
          lines.each{|e|
            eval("#{v}='#{e}'")
            v.next!
          }
          

          Now @v_a will return lines[0], @v_b gives lines[1] etc... BUT as I said sticking with lines[0], lines[1] etc seems far simpler and easier to use/control... ๐Ÿ˜’

          TIG

          1 Reply Last reply Reply Quote 0
          • A Offline
            artmusicstudio
            last edited by 13 Oct 2013, 12:37

            hi tig,
            thanx a lot, i inserted the new import routine and tried different variantion.
            ruby gives back:
            load '01.rb'
            true
            Error: #<NameError: undefined local variable or method lines' for #<Object:0x3d31654 @v_b="90", @v_a="300", @v_c="100">> C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/01.rb:96:in draw_stairs'
            C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/01.rb:48
            C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/01.rb:96:in call' C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/01.rb:96 load '01.rb' true Error: #<NoMethodError: undefined method /' for "300":String>
            C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/01.rb:173:in draw_stairs' C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/01.rb:48 C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/01.rb:96:in call'
            C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/01.rb:96

            so the situation is:

            i am created an object in skp, which can be controlled in diverse dimension by an input menue (parameters)

            when the object is created, i save the parameters in a text file (parttemp2.txt):

            content of parttemp2.txt now for testing:
            300
            90
            100

            it seems, that reloading these numbers gives back STRINGS and not numbers

            as <ou also said, i would like to handle these imported parameters by

            parameter01 = lines[0]
            parameter02 = lines[1]

            and so on.

            but it simply does not work now for me. have to study more about formats (you know, i am on ruby since few days)

            this is the whole pert of the code: (where WIDTH shall become the value of line 0 in the text-file)


            require 'sketchup.rb'
            #require 'offset.rb'
            #require 'makesoftsmooth.rb'
            .
            Sketchup.send_action "showRubyPanel:"

            #Add a menu item to launch our plugin.
            UI.menu("PlugIns").add_item("01-ZF-Stairs"){
            UI.messagebox("Outdoor-Stairs - Script - 131011-V 0.1j - Zdenek Fajfrlik")

            #result = UI.messagebox "New Definition?", MB_YESNO
            #if result == 6 # Yes

            UI.messagebox("New Definition")

            end

            #count = 0

            #File.open('C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/zf/partemp.txt', 'r') do |f1|
            #while line = f1.gets
            #puts line

            #width = f1.gets  
            #end  
            

            #end

            file = 'C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/zf/partemp2.txt'
            lines=[]
            IO.readlines(file).each{|line|
            line.chomp! ### removes the '\n' from the end of the line
            next if line.empty? ### this traps for empty lines
            lines << line
            }

            you need to set up instance variables, @v_a etc, so they get referenced outside of the {} block, plain v_a won't be referenced outside and would therefore be inaccessible...

            v='@v_a'
            lines.each{|e|
            eval("#{v}='#{e}'")
            v.next!
            }

            Call

            draw_object
            }

            def draw_object

            #***************************************************************************
            #delete previous model

            model = Sketchup.active_model
            entities = model.active_entities
            entities.to_a.each { | entity| entity.erase! }

            #***************************************************************************

            set start default values for MENUE 1-4

            width          =  @v_a
            lwidthplus     =  @v_b
            rwidthplus     =  @v_c
            
            #width          = 500
            #lwidthplus     = 10
            #rwidthplus     = 10
            
            stairs         = 10      
            run            = 30
            rise           = 13
            thickness      = 3
            fill           = 4
            overlap        = 6
            il             = 6
            rc             = 3
            re             = 0
            stepdevidex    = 1
            
            sl             = 100
            sr             = 100
            mr             = 80
            pmod           = 3
            pradius        = 15
            thradius       = 20
            mhradius       = 4
            lines          = 4
            ro             = 4
            rol            = 5
            ror            = 5
            
            wangenbreitel  = 30
            wangenhoehel   = 10
            wangeslopel    = 45.0
            rampoffsetl    = 50
            rampoffsetly   = 30
            rampoffsetlz   = 99.0
            
            wangenbreiter  = 30
            wangenhoeher   = 10
            wangesloper    = 45.0
            rampoffsetr    = 50
            rampoffsetry   = 30
            rampoffsetrz   = 99.0
            

            #***************************************************************************

            thank you very very much for your time and helping!
            STAN

            1 Reply Last reply Reply Quote 0
            • T Offline
              TIG Moderator
              last edited by 13 Oct 2013, 13:39

              Please use the 'code' BBS tags for large blocks of code - it's very hard to read otherwise.

              You do NOT need to assign a whole load of variables with eval...
              Just use ' lines' OR whatever you call it as the array of values...
              To find the first one use lines[0] and so on...

              You cannot pass variables across methods unless you pass them as arguments after the method [def] name OR the methods are in the same module/class and are instance ones - starting with @....

              You are making this far more convoluted than it needs to be...

              Can we go back a few steps...
              What is it you are trying to do exactly - I get part of it so far?
              What is in the file you are reading in exactly [data/units/etc]?

              You are reading in strings, NOT numbers, so convert it to a float using .to_f
              Then you can use / etc on them...
              parameter01 = lines[0].to_f
              BUT if you know they are in cm or mm you need to use say:
              parameter01 = lines[0].to_f.mm
              etc...

              You are getting in a muddle... ๐Ÿ˜’

              TIG

              1 Reply Last reply Reply Quote 0
              • A Offline
                artmusicstudio
                last edited by 13 Oct 2013, 14:11

                hi tig,
                that's it .
                my logic was ok, your tip for the conversion of string to float was the point.

                basicly it works like this:

                i have menues for parameters

                then the geometry is created

                before the end of the ruby i save all parameters to a text file (by the way still looking for the syntax to save more of them line by line)

                then the user can recall the ruby and select
                -my last parameters
                -defaults

                and so on and so on

                again another step!!!!

                regards stan ( i'll report , when i get further)

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

                Advertisement