• Login
sketchucation logo sketchucation
  • Login
๐Ÿ”Œ Quick Selection | Try Didier Bur's reworked classic extension that supercharges selections in SketchUp Download

Store a hash as an value in an attribute dictionary

Scheduled Pinned Locked Moved Developers' Forum
6 Posts 4 Posters 835 Views 4 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.
  • P Offline
    Pout
    last edited by 1 Apr 2009, 21:08

    Is this possible:

    result={"1"=>"one", "2"=>"two"}
    Sketchup.active_model.set_attribute 'test','test1',result

    I tried it and when i do
    puts (Sketchup.active_model.get_attribute 'test','test1') it returns nil

    Is this because it is a hash it can not be shown or because the hash is not stored?
    If it is not possible: is there another solution?
    Off course i could create a seperate dictionary for each key in the hash but that will give me a lot of dictionaries.

    thx

    1 Reply Last reply Reply Quote 0
    • S Offline
      scottlininger
      last edited by 1 Apr 2009, 23:34

      Pout,

      I don't think you're allowed to store arrays, hashes, etc. inside attributes.

      You could convert your object into a string with .inspect and then eval it back into an object. Here's a project with utilities for that kind of thing. (Disclaimer: I've not used it personally.)

      RubyForge - Page not found

      favicon

      (rubyforge.org)

      Cheers,

      • Scott Lininger
        SketchUp Software Engineer
        Have you visited the Ruby API Docs?
      1 Reply Last reply Reply Quote 0
      • T Offline
        todd burch
        last edited by 1 Apr 2009, 23:55

        Pout, you can store anything you want in an attribute dictionary, but that doesn't mean you can get it back out how you want it!

        The term for what you are trying to do is "marshal". You can look it up in the Ruby book, or google it. YAML (Yet Another Marshaling Language) is a great example that comes with the whole ruby install, as it saves vars and values in text format we humans can read.

        If you know the keys you want to save, just use your var names as the attribute keys, and the values as the attribute values. Simple simple.

        1 Reply Last reply Reply Quote 0
        • J Offline
          Jim
          last edited by 2 Apr 2009, 00:14

          @pout said:

          Off course i could create a seperate dictionary for each key in the hash but that will give me a lot of dictionaries.

          Is your hash a simple hash like you show in your example? If so, you can create the dictionary very easily.

          
          # Create a Hash
          h = {"a"=>0, "m"=>100, "y"=>300, "n"=>100, "d"=>200}
          # Write each key=value pair to a dictionary
          h.map{|k, v| Sketchup.active_model.set_attribute("Dictionary", k, v)}
          # Retrieve the dictionary
          dic = Sketchup.active_model.attribute_dictionaries["Dictionary"]
          # output as an array
          dic.to_a
          [["a", 0], ["d", 200], ["m", 100], ["n", 100], ["y", 300]]
          

          I count one dictionary per hash.

          Hi

          1 Reply Last reply Reply Quote 0
          • P Offline
            Pout
            last edited by 2 Apr 2009, 07:40

            thanks all for the fast answers.

            The dictionary setup should be like this:
            dict, number1, hash1
            dict, number2, hash2
            dict, number3, hash3

            Jim, if i am not mistaking you put the hash key in place of number 1, number 2 and number 3 and the hash value each time as the value.
            I need one more level:
            The hash itself should be the value of number 1, number 2 and number 3
            So with your example it should be
            dict, number1, {"a"=>0, "m"=>100, "y"=>300, "n"=>100, "d"=>200}

            Todd, when i store the hash it does not give me an error. But when i return it it is always nil ๐Ÿ˜ž
            If it was stored wouldn't it return me an error telling that it can not be displayed insteda of returning nil?

            On the other hand i now have used an array to do it. Off course this is not as intelligent. So if possible i would prefer using a hash.
            Maybe important: the dictionary is a model dictionary, not an entity dictionary

            1 Reply Last reply Reply Quote 0
            • T Offline
              todd burch
              last edited by 3 Apr 2009, 12:53

              @pout said:

              Todd, when i store the hash it does not give me an error. But when i return it it is always nil ๐Ÿ˜ž

              My point exactly!

              @pout said:

              If it was stored wouldn't it return me an error telling that it can not be displayed insteda of returning nil?

              In a perfect world, yes.

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

              Advertisement