sketchucation logo sketchucation
    • 登入
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    🔌 Smart Spline | Fluid way to handle splines for furniture design and complex structures. Download

    [Info] Allowable Classes for "set_attribute"

    已排程 已置頂 已鎖定 已移動 Developers' Forum
    57 貼文 12 Posters 6.9k 瀏覽 12 Watching
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • K 離線
      kwalkerman
      最後由 kwalkerman 編輯

      Under the Ruby API for "set_attribute" we get:

      Arguments:
      dict_name: The name of an attribute dictionary.
      key: An attribute key.
      value: The value for the attribute.

      Returns:
      value: the newly set value if successful

      but, it doesn't tell you what type of values are allowable. Here is my short list:

      allowed:

      String
      Fixnum
      Float
      Array -- For arrays, every element in the array needs to be one of the allowed types on this list.
      TrueClass
      FalseClass
      Length
      Geom::Point3d (tested under SU8 M1)
      Geom::Vector3d (tested under SU8 M1)

      not allowed:

      Entity
      Hash -- this can be stored following Dan's guidelines below.

      Be careful with hash, because if you have an array, and one of the array values is a hash, it will return the array, but instead of returning the hash, it will return nil.

      Additions? Corrections? Comments?

      --
      Karen

      1 條回覆 最後回覆 回覆 引用 0
      • thomthomT 離線
        thomthom
        最後由 編輯

        Yea - wish the API docs would spesify this.
        CAn't remember if the TrueClass and FalseClass worked..

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

        1 條回覆 最後回覆 回覆 引用 0
        • Dan RathbunD 離線
          Dan Rathbun
          最後由 編輯

          @kwalkerman said:

          not allowed:

          Hash -- (kind of strange, as array is allowed)

          A JSON string is a hash definition, almost verbatim Ruby, with quotes.
          The diference is that the => are replaced with colon. JSON (JavaScript Object Notation,) strings are handy because you can pass them to webdialogs. They can contain nested Arrays, and I think even nested JSONs.

          But if your data is going to stay within Ruby, and you want "record type" data fields with keynames and values, then use a hash.

          It's VERY easy to store a hash in an attribute.

          (Get ready to smack your forehead.)

          
          my_hash = {'Name'=>'Dan','Age'=>49,'Stature'=>5.5,
                     'FavFoods'=>['Mac and Cheese','SpiceCake','Coffee']}
          
          some_entity.set_attribute( "dict_name", "hash_att", my_hash.inspect )
          

          I'm not here much anymore.

          1 條回覆 最後回覆 回覆 引用 0
          • J 離線
            Jim
            最後由 編輯

            When you say Arrays are allowed, what about the types of the elements?

            Hi

            1 條回覆 最後回覆 回覆 引用 0
            • thomthomT 離線
              thomthom
              最後由 編輯

              @dan rathbun said:

              @kwalkerman said:

              It's VERY easy to store a hash in an attribute.

              How do you turn it back into an hash when you read it?

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

              1 條回覆 最後回覆 回覆 引用 0
              • J 離線
                Jim
                最後由 編輯

                Unless your Hash string includes quotes, in which case you need to escape those first.

                Hi

                1 條回覆 最後回覆 回覆 引用 0
                • Dan RathbunD 離線
                  Dan Rathbun
                  最後由 編輯

                  @jim said:

                  Unless your Hash string includes quotes, in which case you need to escape those first.

                  NOPE.. Ruby does it for you.

                  ` hashStr = %( {"Stature"=>5.5, "Name"=>"Dan", "FavFoods"=>["Mac and Cheese", "SpiceCake", "Coffee"], "Age"=>49})

                  hashStr.inspect

                  " {"Stature"=>5.5, "Name"=>"Dan", "FavFoods"=>["Mac and Cheese", "SpiceCake", "Coffee"], "Age"=>49}"

                  my_hash = eval(hashStr)

                  {"Stature"=>5.5, "Name"=>"Dan", "FavFoods"=>["Mac and Cheese", "SpiceCake", "Coffee"], "Age"=>49}

                  my_hash.class

                  Hash`

                  I'm not here much anymore.

                  1 條回覆 最後回覆 回覆 引用 0
                  • J 離線
                    Jim
                    最後由 編輯

                    Huh, I thought I had problems with that in the past.

                    Hi

                    1 條回覆 最後回覆 回覆 引用 0
                    • Dan RathbunD 離線
                      Dan Rathbun
                      最後由 編輯

                      @jim said:

                      Huh, I thought I had problems with that in the past.

                      Could be.

                      • Might have been using Ruby 1.8.0* Might have been conflicts between PC (1.8.0) and Mac (1.8.5)* I imagine you could confuse Ruby with poor use of delimiters, or maybe embedded newlines?
                        I would always recommend using .inspect to build the string, as it:

                      • Correctly knows how to iterate the hash, and any nested structures inside it, like nested arrays, nested hashes, etc.* For objects that it does not know how to 'convert' it will make an 'info string': "#Sketchup::Face:0x3F56D20A" (or similar)* Uses the same delimiters (double quotes,) for all strings and hash keys (which allows the use of a single quote for possesion apostrophe. ie: "Dan's idea!"

                      I'm not here much anymore.

                      1 條回覆 最後回覆 回覆 引用 0
                      • Dan RathbunD 離線
                        Dan Rathbun
                        最後由 編輯

                        @kwalkerman said:

                        Be careful with hash, because if you have an array, and one of the array values is a hash, it will return the array, but instead of returning the hash, it will return nil.

                        See the post directly above.

                        The "tip" on using .inspect (to build the attribute strings,) for hashes, goes also for arrays.
                        The .inspect method will convert hashes nested inside arrays, and arrays nested inside hashes, etc. Multiple levels deep as well.

                        I'm not here much anymore.

                        1 條回覆 最後回覆 回覆 引用 0
                        • K 離線
                          kwalkerman
                          最後由 編輯

                          Dan,

                          Very nice. I will definitely use this. Too bad it's not embedded in SU though.

                          Jim - I have found that any sub-components of arrays also need to be one of the allowable classes.

                          Thom - trueclass and falseclass also work. I'm updating the first post accordingly.

                          --
                          Karen

                          1 條回覆 最後回覆 回覆 引用 0
                          • Z 離線
                            zitoun
                            最後由 編輯

                            VERY useful thread, thanks !
                            Please make it kind of sticky: I've been chasing an uncatchable bug for hours, ignoring this information... Others might like to know this in the future!

                            The light at the end of the tunnel is a train.

                            1 條回覆 最後回覆 回覆 引用 0
                            • Dan RathbunD 離線
                              Dan Rathbun
                              最後由 編輯

                              To recover a hash-string from an attribute, you eval() it into a reference.

                              
                              hashStr = some_entity.get_attribute( "dict_name", "hash_att" )
                              
                              my_hash = eval(hashStr)
                              
                              

                              I'm not here much anymore.

                              1 條回覆 最後回覆 回覆 引用 0
                              • thomthomT 離線
                                thomthom
                                最後由 編輯

                                Length classes also seem to be stored and recovered properly. I'd thought that maybe it got read back as Float, but in my tests I seem to get Length - anyone confirm?

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

                                1 條回覆 最後回覆 回覆 引用 0
                                • thomthomT 離線
                                  thomthom
                                  最後由 編輯

                                  @kwalkerman said:

                                  Array -- good to store by doing "array.inspect" first, see Dan's comments below

                                  Only if the array contains hashes. If the array only use the other allowable types there is no need. Might be less overhead since it's not parsing between strings.

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

                                  1 條回覆 最後回覆 回覆 引用 0
                                  • thomthomT 離線
                                    thomthom
                                    最後由 編輯

                                    Geom::Point3d also seems to be possible to store:


                                    ` pt1=Geom::Point3d.new(1,2,3)
                                    Point3d(1, 2, 3)
                                    pt2=Geom::Point3d.new(4,5,6)
                                    Point3d(4, 5, 6)
                                    pt3=Geom::Point3d.new(7,8,9)
                                    Point3d(7, 8, 9)

                                    pt_array = [ pt1, pt2, pt3 ]
                                    [Point3d(1, 2, 3), Point3d(4, 5, 6), Point3d(7, 8, 9)]

                                    model.set_attribute('test', 'bar', pt_array )
                                    [Point3d(1, 2, 3), Point3d(4, 5, 6), Point3d(7, 8, 9)]

                                    model.get_attribute('test', 'bar' )
                                    [Point3d(1, 2, 3), Point3d(4, 5, 6), Point3d(7, 8, 9)]`


                                    I even saved the model and reopened it just to make sure it worked across sessions.

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

                                    1 條回覆 最後回覆 回覆 引用 0
                                    • TIGT 離線
                                      TIG Moderator
                                      最後由 編輯

                                      Although storing/reading-back a 'point3d' or a 'vector3d' as an array [.to_a] would be 'safer' ?
                                      Presumably a 'transformation' is not storeable unless it's first made into an array ?

                                      TIG

                                      1 條回覆 最後回覆 回覆 引用 0
                                      • thomthomT 離線
                                        thomthom
                                        最後由 編輯

                                        @tig said:

                                        Although storing/reading-back a 'point3d' or a 'vector3d' as an array [.to_a] would be 'safer' ?

                                        I was storing points as arrays - because I just assumed point3d's would not work. But they appear to do so. In which case I'd prefer to do so unless there is any known issues.

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

                                        1 條回覆 最後回覆 回覆 引用 0
                                        • K 離線
                                          kwalkerman
                                          最後由 編輯

                                          Nice. I just tested it as well. I'll add it to the list.

                                          It also seems to work as an attribute dictionary key:

                                          p1 = Geom::Point3d.new(1,2,3)
                                          p2 = Geom::Point3d.new(4,5,6)

                                          entity.set_attribute "k", p1, p2

                                          --
                                          Karen

                                          1 條回覆 最後回覆 回覆 引用 0
                                          • thomthomT 離線
                                            thomthom
                                            最後由 編輯

                                            @kwalkerman said:

                                            It also seems to work as an attribute dictionary key:

                                            hmm... interesting.

                                            I a haven't tried, but I'd think that Vector3d should work as well. (Though one can never be sure until it's tested...)

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

                                            1 條回覆 最後回覆 回覆 引用 0
                                            • 1
                                            • 2
                                            • 3
                                            • 1 / 3
                                            • 第一個貼文
                                              最後的貼文
                                            Buy SketchPlus
                                            Buy SUbD
                                            Buy WrapR
                                            Buy eBook
                                            Buy Modelur
                                            Buy Vertex Tools
                                            Buy SketchCuisine
                                            Buy FormFonts

                                            Advertisement