• 登入
sketchucation logo sketchucation
  • 登入
🔌 Quick Selection | Try Didier Bur's reworked classic extension that supercharges selections in SketchUp Download

Adding attributes help

已排程 已置頂 已鎖定 已移動 Developers' Forum
29 貼文 8 Posters 1.6k 瀏覽 8 Watching
正在載入更多貼文
  • 從舊到新
  • 從新到舊
  • 最多點贊
回覆
  • 在新貼文中回覆
登入後回覆
此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
  • D 離線
    Dan Rathbun
    最後由 編輯 2011年2月19日 上午12:48

    @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
    • D 離線
      Dan Rathbun
      最後由 編輯 2011年2月19日 上午12:57

      @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
      • T 離線
        TIG Moderator
        最後由 編輯 2011年2月19日 上午11:48

        @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
          最後由 編輯 2011年2月20日 上午3:29

          @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
            最後由 編輯 2011年2月20日 上午3:45

            @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
            • T 離線
              TIG Moderator
              最後由 編輯 2011年2月20日 下午12:03

              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
              • D 離線
                Dan Rathbun
                最後由 編輯 2011年2月21日 上午1:36

                @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
                  最後由 編輯 2011年2月22日 下午4:57

                  @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
                  • L 離線
                    lothcat
                    最後由 編輯 2011年2月22日 下午5:55

                    OK, so, now that I have that figured out, I'd like to know if there's a way to tell if a face is vertical or horizontal. It seems like face.plane would do that, but I've found frustratingly little about how plane works.

                    I'm not sure of the board etiquette. Should I post this question as a new topic, or is this OK?

                    1 條回覆 最後回覆 回覆 引用 0
                    • T 離線
                      TIG Moderator
                      最後由 編輯 2011年2月22日 下午7:39

                      @lothcat said:

                      OK, so, now that I have that figured out, I'd like to know if there's a way to tell if a face is vertical or horizontal. It seems like face.plane would do that, but I've found frustratingly little about how plane works.
                      I'm not sure of the board etiquette. Should I post this question as a new topic, or is this OK?

                      If it stops here no new topic needed... but if you want to talk about faces some more make a new topic...
                      Rather than face.plane try face.normal - that's the vector perpendicular to a face.
                      So if face.normal==Z_AXIS [or [0,0,1]] it's facing vertically up or face.normal==Z_AXIS.reverse [or [0,0,-1]] if facing straight down.
                      To test it a face is 'vertical' try if face.normal.z==0 - that is true as the normal has no element in the vertical [z].
                      With a bit of thought you can contrive other tests - e.g. if face.normal.y.abs != 1 is true if the normal doesn't face exactly in either of the Y/green axes direction - i.e. it faces in any direction except directly to the front or back...

                      TIG

                      1 條回覆 最後回覆 回覆 引用 0
                      • L 離線
                        lothcat
                        最後由 編輯 2011年2月22日 下午7:47

                        Thank you!

                        1 條回覆 最後回覆 回覆 引用 0
                        • eneroth3E 離線
                          eneroth3
                          最後由 編輯 2015年3月7日 上午9:57

                          @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 really wouldn't save a ruby string as an attribute and later eval it. It opens up for code injection and lets people run whatever malicious code they want on someone else's computer just by sending them a model made to take advantage of this vulnerability and wait for the moment where eval is called.

                          My website: http://julia-christina-eneroth.se/

                          1 條回覆 最後回覆 回覆 引用 0
                          • tt_suT 離線
                            tt_su
                            最後由 編輯 2015年3月11日 下午12:34

                            @eneroth3 said:

                            I really wouldn't save a ruby string as an attribute and later eval it. It opens up for code injection and lets people run whatever malicious code they want on someone else's computer just by sending them a model made to take advantage of this vulnerability and wait for the moment where eval is called.

                            +1 eval = evil

                            1 條回覆 最後回覆 回覆 引用 0
                            • D 離線
                              driven
                              最後由 編輯 2015年3月11日 下午3:18

                              @unknownuser said:

                              'he's good bad', but he ain't evil!

                              I tend to agree with this**[anchor= goto=http://www.infoq.com/articles/eval-options-in-ruby:2ingvk9h]article[/anchor:2ingvk9h]**...

                              eval has it's uses, that's why it's there...
                              john

                              learn from the mistakes of others, you may not live long enough to make them all yourself...

                              1 條回覆 最後回覆 回覆 引用 0
                              • eneroth3E 離線
                                eneroth3
                                最後由 編輯 2015年3月11日 下午10:29

                                @driven said:

                                @unknownuser said:

                                'he's good bad', but he ain't evil!

                                I tend to agree with this**[anchor= goto=http://www.infoq.com/articles/eval-options-in-ruby:13hbt2pf]article[/anchor:13hbt2pf]**...

                                eval has it's uses, that's why it's there...
                                john

                                There are cases where it's fine to use. I use it myself in my attribute editor plugin for instance to let people assign things such as 1.m or Sketchup.active_model.selection.first.length to an attribute. However executing code saved in a model opens up for people to forge an evil model that causes the code to be executed without the user knowing about it.

                                Also arrays are allowed and can be used instead of hashes when it comes to just serializing data.

                                My website: http://julia-christina-eneroth.se/

                                1 條回覆 最後回覆 回覆 引用 0
                                • S 離線
                                  slbaumgartner
                                  最後由 編輯 2015年3月11日 下午11:31

                                  I agree with Julia. Using eval is a bit like owning a pet tiger: you have to be careful what you put in the cage with it!

                                  1 條回覆 最後回覆 回覆 引用 0
                                  • tt_suT 離線
                                    tt_su
                                    最後由 編輯 2015年3月12日 上午9:43

                                    Yea, like you wouldn't feed raw user data to your database, you don't want to feed user data to eval. (I consider anything read in from the system or file to be "user data"). Validate and sanitize - this is the stuff you want to find a library that has solved all the edge cases for you.

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

                                    Advertisement