sketchucation logo sketchucation
    • 登入
    ⚠️ Attention | Having issues with Sketchucation Tools 5? Report Here

    Adding attributes help

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

      I'm having a hard time figuring out how to add attributes to an entity. Basically, I have this code that creates a face, applies a material, and then applies attributes - but the attributes aren't being applied. The Google Ruby API is no help.

      I'm sure I'm just doing it wrong, but I haven't figured out what exactly I'm doing wrong yet. The Google Ruby API is no help.

      Here's my code snippet:

      face = entities.add_face pts
      face.material = agw_mat
      face.back_material = agw_mat #Everything up to here works
      UI.messagebox "Applied the Material" # debugging UI prompt

      face.set_attribute "agw_attributes", "exterior_color", agw_exterior_color #This, as far as I can tell, does diddly.

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

        What is 'agw_exterior_color' ???
        You can only save attributes that are one of the following:
        integers 0, 1, 2, -99 etc
        floats 0.0, 1.2, 3.56, -99.9 etc
        strings '000', 'cat, 'dog', '99' etc
        booleans true/false/nil
        arrays [1, 2.0, 'three', true. [1, 2, 'cat']]
        If you want to save a 'color' save it as an array - e.g. [1, 22, 33] for the RGB values and read it back and then 'convert to a color.
        If it's a material then split it into its constituent parts ['methods'] and save these perhaps as an array of allowable 'types' - e.g. my_mat_array=[mat.name, mat.color.to_a, mat.alpha] (in string,array_of_3_integers,float - e.g. ['coal',[222,222,222],1.0])
        etc if appropriate you can include data on texture, image, size etc etc... 🤓

        TIG

        1 條回覆 最後回覆 回覆 引用 0
        • L 離線
          lothcat
          最後由 編輯

          agw_exterior_color is a string - I'm only trying to add variables that are Strings or Floats. Actually, they could all be Strings, I just need to save the information in the model.

          1 條回覆 最後回覆 回覆 引用 0
          • L 離線
            lothcat
            最後由 編輯

            It's a String. That isn't the problem. I thought maybe I have the syntax wrong?

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

              @lothcat said:

              I thought maybe I have the syntax wrong?

              You should get in the habit of parenthesizing your argument lists, later versions of Ruby will require it, and Ruby 1.8.6 spits out a warning each time you don't do it.
              face.set_attribute("agw_attributes", "exterior_color", agw_exterior_color)

              I'm not here much anymore.

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

                Why don't you create a Material named like "agw_exterior_sandpiper" with the color and such.

                Then the Face object's built-in material attribute can hold a reference to your "custom exterior material".

                It's seems like your just making extra work for yourself, when it's already built-in.

                I'm not here much anymore.

                1 條回覆 最後回覆 回覆 引用 0
                • L 離線
                  lothcat
                  最後由 編輯

                  I'm not making it a material because I'm using it to model a house. Well, a wall, specifically. But a house in the real world. Once the person is done adding all the attributes and finishes their house, I'm going to have the program print out the attributes.

                  This is actually just one part of a larger construction program. The larger program will use the printed out attributes, but each wall (and floor, and ceiling, etc.) will need to be able to store them as part of the larger model.

                  I've modified them to this:

                  	 face.set_attribute ("agw_attributes", "exterior_color", agw_exterior_color)
                  	 face.set_attribute ("agw_attributes", "construction_type", agw_construction_type)
                  	 face.set_attribute ("agw_attributes", "location", agw_location)
                  

                  but it's still not working.

                  1 條回覆 最後回覆 回覆 引用 0
                  • L 離線
                    lothcat
                    最後由 編輯

                    Now it's getting even weirder...

                    I changed the lines to read:

                    value = face.set_attribute ("agw_attributes", "location", agw_location)

                    Because that's the way it always is in the examples, and I thought maybe it needs to be set as a variable to be applied. It doesn't make sense to me, but hey, I've seen programming languages insist on weirder things. So I added this code snippet:

                    if (value)
                    UI.messagebox value
                    end

                    And, weirdly enough, that works exactly like it's supposed to.

                    Maybe it's a problem with the way I'm checking to see if the attributes are being applied? I'm making my test wall, turning it into a group, and then reading my attributes with the attribute reporter script from here: http://code.google.com/apis/sketchup/docs/tutorial_attrreporting.html.

                    I'm suspecting that it isn't reporting the attributes in my custom dictionaries. Have I been banging my head against the wall trying to fix a problem I'm not having?

                    If so, how could I fix the code to show my custom dictionaries?

                    Thank you all so much! This is not only my first Sketchup project, but also my first time using Ruby, so I really need the help.

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

                      in your latest example, you would read the attribute as follows:

                      face.get_attribute("agw_attributes", "location)

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

                        Also, I have found that in the following:

                        hash = {"q"=>"t"}

                        value = face.set_attribute("a","b",hash)

                        value == hash

                        however, as the Hash class is not a valid attribute type for set_attribute, face.get_attribute("a","b") is nil.

                        I'm not sure why a valid value is returned, but this could also explain what is happening.

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

                          @kwalkerman said:

                          Also, I have found that in the following:

                          hash = {"q"=>"t"}

                          value = face.set_attribute("a","b",hash)

                          value == hash

                          however, as the Hash class is not a valid attribute type for set_attribute, face.get_attribute("a","b") is nil.

                          I'm not sure why a valid value is returned, but this could also explain what is happening.

                          Perhaps make your hashes into arrays before saving and read back and convert...

                          TIG

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

                            @tig said:

                            Make your hashes into arrays before saving and read back and convert...

                            No don't do that, you lose your keys.

                            Make your hashes into strings with hashstr = hash.inspect()
                            Save the hashstr into the dictionary.

                            To read, get the hashstr from the dictionary.
                            Then hash = eval(hashstr) to convert it back.

                            I'm not here much anymore.

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

                              @lothcat said:

                              value = face.set_attribute ("agw_attributes", "location", agw_location)

                              Do not put a space between the methodname and it's argument list.

                              If you are on Sketchup 8, you should be seeing errors generated in the console.
                              You DO have the console open when your debugging.. right?

                              The API docs still have errors, and the examples are often incorrect. There are many methods that do not return what the docs say they do. You should always check the bottom of each API doc webpage to see if one of us has posted a correction.

                              @lothcat said:

                              This is not only my first Sketchup project, but also my first time using Ruby, so I really need the help.

                              Click the "Ruby Resources" link in my signature.. follow the advice in the Ruby Newbie's Guide.

                              I'm not here much anymore.

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

                                @lothcat said:

                                ... then reading my attributes with the attribute reporter script from here: http://code.google.com/apis/sketchup/docs/tutorial_attrreporting.html.

                                I'm suspecting that it isn't reporting the attributes in my custom dictionaries.

                                You can stop banging your head (oh the poor wall..) That script only lists the dynamic attributes in the "dynamic_attributes" dictionaries attached to components.

                                For testing you should write a little script to display your custom attributes in a multiline [url=http://code.google.com/apis/sketchup/docs/ourdoc/ui.html#messagebox:1btntpsp]UI.messagebox[/url:1btntpsp]

                                Later you can get fancy and add a right-click menu item that brings up the listing.

                                Start simple... and build it better as you learn.

                                I'm not here much anymore.

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

                                  @dan rathbun said:

                                  No don't do that [make a hash into an array], you lose your keys!
                                  Make your hashes into strings with hashstr = hash.inspect()
                                  Save the hashstr into the dictionary.
                                  To read, get the hashstr from the dictionary.
                                  Then hash = eval(hashstr) to convert it back.

                                  I stand corrected.
                                  A much better solution...

                                  TIG

                                  1 條回覆 最後回覆 回覆 引用 0
                                  • L 離線
                                    lothcat
                                    最後由 編輯

                                    @dan rathbun said:

                                    If you are on Sketchup 8, you should be seeing errors generated in the console.
                                    You DO have the console open when your debugging.. right?

                                    Of course I do, but no errors. However, I'm using Sketchup 7, so that might be why.

                                    Thanks for the links! I'll definitely be using the heck out of them.

                                    @dan rathbun said:

                                    The API docs still have errors, and the examples are often incorrect. There are many methods that do not return what the docs say they do. You should always check the bottom of each API doc webpage to see if one of us has posted a correction.

                                    Believe me, I've noticed. Extremely frustrating. Stuff like that makes me want to give up programming and raise llamas for a living.

                                    The corrections have been very helpful, though.

                                    1 條回覆 最後回覆 回覆 引用 0
                                    • L 離線
                                      lothcat
                                      最後由 編輯

                                      @kwalkerman said:

                                      in your latest example, you would read the attribute as follows:

                                      face.get_attribute("agw_attributes", "location)

                                      That was crashing my plugin. So I changed it to value1 = model.get_attribute("agw_attributes", "location") - and it's returning a blank.

                                      There is no reason this line of code wouldn't work, right? So my attributes really aren't being applied after all?

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

                                        When you use set_attribute(...) it applies it to the specified entity AND it's only attached to that entity [or later copies of it].
                                        Your earlier code attached the attribute to a face... so when you run model.get_attribute(...) it quite rightly returns nil because you haven't attached that attribute to the model ??
                                        You can attached an attribute to a model BUT your code is attaching it to a face.
                                        You need to find some faces and then iterate through them and use face.get_attribute(...) on each in turn and get their attributes ???

                                        TIG

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

                                          @lothcat said:

                                          @dan rathbun said:

                                          If you are on Sketchup 8, you should be seeing errors generated in the console.
                                          You DO have the console open when your debugging.. right?

                                          Of course I do, but no errors. However, I'm using Sketchup 7, so that might be why.

                                          IF your on Sketchup 7.x (7.1M2) Free, there is no reason why you should not update to Sketchup 8.0M1 Free (as it costs you nothing.) The lastest MR of ver 8 has many fixes (including for the 'infamous' shadow bug,) that you will wish to take advantage of.

                                          In addition, there are quite a few API fixes.

                                          But one of the most important from the standpoint of Ruby coding, is that ver 8+ ships with a Ruby v1.8.6-p287 interpreter. All ver 7 Sketchup releases ship with the old obsolete initial release of Ruby v1.8.0(-p0) that has bugs in it, and many missing methods.

                                          At the very least.. you can update the ver 7 interpreter DLL (if your on PC,) to the same version and patchlevel as is distro'd with Sketchup 8.x; read instructions in this post:
                                          Ruby Interpreter DLLs (Win32).

                                          I'm not here much anymore.

                                          1 條回覆 最後回覆 回覆 引用 0
                                          • L 離線
                                            lothcat
                                            最後由 編輯

                                            @tig said:

                                            When you use set_attribute(...) it applies it to the specified entity AND it's only attached to that entity [or later copies of it].
                                            Your earlier code attached the attribute to a face... so when you run model.get_attribute(...) it quite rightly returns nil because you haven't attached that attribute to the model ??
                                            You can attached an attribute to a model BUT your code is attaching it to a face.
                                            You need to find some faces and then iterate through them and use face.get_attribute(...) on each in turn and get their attributes ???

                                            You're right, that was stupid. I have it fixed now. Thanks!

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

                                            Advertisement