• Login
sketchucation logo sketchucation
  • Login
🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

Here's another mystery to me

Scheduled Pinned Locked Moved Developers' Forum
10 Posts 3 Posters 216 Views 3 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.
  • M Offline
    Michaelv
    last edited by Michaelv 28 May 2012, 02:08

    <span class="syntaxdefault">def&nbsp;initialize<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">@</span><span class="syntaxdefault">mod&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">@</span><span class="syntaxdefault">ents&nbsp;</span><span class="syntaxkeyword">=&nbsp;@</span><span class="syntaxdefault">mod</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_entities<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">@</span><span class="syntaxdefault">sel&nbsp;</span><span class="syntaxkeyword">=&nbsp;@</span><span class="syntaxdefault">mod</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">selection<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">@</span><span class="syntaxdefault">group&nbsp;</span><span class="syntaxkeyword">=&nbsp;@</span><span class="syntaxdefault">ents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_group<br />puts&nbsp;</span><span class="syntaxkeyword">@</span><span class="syntaxdefault">group</span><span class="syntaxkeyword">.class<br /></span><span class="syntaxdefault">end<br /><br />def&nbsp;operation<br />puts&nbsp;</span><span class="syntaxkeyword">@</span><span class="syntaxdefault">group</span><span class="syntaxkeyword">.class<br />@</span><span class="syntaxdefault">group</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_curve&nbsp;</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">set&nbsp;of&nbsp;points</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">end</span>
    

    Doesn't work and tells me:
    First puts tells me twice (yes twice, yet there is only one puts) that it's a "Sketchup::Group" and I only call the initialize method once.
    then the second puts tells me that @group is a "FalseClass" object
    but (just moved the @group declaration)

    <span class="syntaxdefault">def&nbsp;initialize<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">@</span><span class="syntaxdefault">mod&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">@</span><span class="syntaxdefault">ents&nbsp;</span><span class="syntaxkeyword">=&nbsp;@</span><span class="syntaxdefault">mod</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_entities<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">@</span><span class="syntaxdefault">sel&nbsp;</span><span class="syntaxkeyword">=&nbsp;@</span><span class="syntaxdefault">mod</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">selection<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />end<br /><br />def&nbsp;operation<br /></span><span class="syntaxkeyword">@</span><span class="syntaxdefault">group&nbsp;</span><span class="syntaxkeyword">=&nbsp;@</span><span class="syntaxdefault">ents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_group<br />puts&nbsp;</span><span class="syntaxkeyword">@</span><span class="syntaxdefault">group</span><span class="syntaxkeyword">.class<br />@</span><span class="syntaxdefault">group</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_curve&nbsp;</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">set&nbsp;of&nbsp;points</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">end</span>
    

    Works just fine, and confirms that @group is a "Sketchup::Group"

    I'm working around it,but anybody knows why or what I missed?

    1 Reply Last reply Reply Quote 0
    • M Offline
      Michaelv
      last edited by 28 May 2012, 02:15

      OK I found out why I get two puts in the first instance.
      I was calling the initialize method in my activate method.
      As it turns out when the tool activates it calls both, so it was calling activate twice essentially. I confirmed with a incremental test, initialize was called twice, once automatically when the tool activates, and once when the tool "activate" method called it (itself automatically called when the tool activates).

      I never saw mention of this anywhere. And a search here returned nothing apparently.

      An interesting quirk however.
      If I declared my test variable (@test = 0) in the activate method, and then increment in the initialize method, it would tell me that @test was nil and it couldn't increment the nil class (+= 1 operator).
      Only when I placed it into the initialize method did it work (and then skipped for the second call of course).
      So declaring an instance variable in the activate method and the initialize method have different results, and a variable declared in activate, is overridden (even canceled if absent) in the initialize method. Did I miss something again?

      1 Reply Last reply Reply Quote 0
      • M Offline
        Michaelv
        last edited by 28 May 2012, 02:29

        Yes I did the reverse and it worked.
        So the initialize method is called first no matter what.
        (I declared @test in there)
        Then the activate method is called.
        (I only incremented @test, and now it didn't tell me it was a nil class).

        1 Reply Last reply Reply Quote 0
        • T Offline
          thomthom
          last edited by 28 May 2012, 09:42

          initialize is the method that Ruby calls when you create an instance of a class. It's not the SketchUp API that does that.

          activate is the method the SketchUp API calls when the tool is ready.

          A common thing that people do is:

          <span class="syntaxdefault"><br />Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">select_tool</span><span class="syntaxkeyword">(&nbsp;</span><span class="syntaxdefault">MyTool</span><span class="syntaxkeyword">.new&nbsp;)<br />&nbsp;</span><span class="syntaxdefault"></span>
          

          In this case both initialize and activate will be called each time.

          However, if you reuse the tool instance, then initialize is only ever run once.

          <span class="syntaxdefault"><br /></span><span class="syntaxkeyword">@</span><span class="syntaxdefault">tool&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">MyTool</span><span class="syntaxkeyword">.new&nbsp;</span><span class="syntaxcomment">#&nbsp;initialize&nbsp;triggers&nbsp;now<br /><br />#&nbsp;A&nbsp;menu&nbsp;triggers&nbsp;this<br /></span><span class="syntaxdefault">def&nbsp;self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">select_my_tool<br />&nbsp;&nbsp;</span><span class="syntaxcomment">#&nbsp;Only&nbsp;actiavte&nbsp;triggers&nbsp;now.<br />&nbsp;&nbsp;</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">select_tool</span><span class="syntaxkeyword">(&nbsp;@</span><span class="syntaxdefault">tool&nbsp;</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">end<br /></span>
          

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

          1 Reply Last reply Reply Quote 0
          • D Offline
            Dan Rathbun
            last edited by 28 May 2012, 14:01

            Also get the Standard Ruby Reference for 1.8.6:
            RUBY PROGRAMMING REFERENCES

            And read the old "Pick-Axe" book. "Programming Ruby"
            http://phrogz.net/ProgrammingRuby/frameset.html

            see the chapter(s) on Classes

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • D Offline
              Dan Rathbun
              last edited by 28 May 2012, 14:02

              @michaelv said:

              I never saw mention of this anywhere. And a search here returned nothing apparently.

              1) You should never need to call initialize() yourself. (It's an internal Ruby method.)

              initialize() is a Standard Ruby private method, that is ALWAYS called by the standard Ruby constructor method new(). In addition, the new() method passes any arguments it gets, into the initialize() method of the newly created instance object, so you should ALWAYS allow for possible arguments (.. ie, train yourself to do this as good programming practice.) Also it's good to always have the initialize method return the new instance object itself, and the new() method will ALWAYS does so.

              module Author
              
                class SomeCustomClass
              
                  private
              
                  def initialize(*args)
              
                    # do things with the args Array here, if needed.
              
                    # make assignments to instance variables here, etc.
              
                    return self
              
                  end # def
              
                end # class
              
              end # module
              

              Now in a custom Tool class, it is normal to want to have a reset() method that sets all instance variables back to a "start" state. But the intialize method can ALSO call that reset() method ONCE, the first time when the instance is created.

              So:

              module Author
              
                class SomeToolClass
              
                  attr_reader(;tool_state)
              
                  private
              
                  def initialize(*args)
                    # do things with the args Array here, if needed.
                    reset()
                    return self
                  end # def
              
                  def reset()
                    # make assignments to instance variables here, etc.
                    #
                    # set the @tool_state var back to 0
                    @tool_state = 0
                  end # def
              
                end # class
              
              end # module
              

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • M Offline
                Michaelv
                last edited by 28 May 2012, 17:59

                Great. Thank you guys.
                I had a feeling it was a ruby method and that is why it was called first, I just couldn't find the info when I searched all my reference material and the web.
                Dan I will work on that, thee is more here that I currently understand but I will work at it until I get it. Thanks for the links.
                I got the part about initialize getting arguments passed by new, I already used that in some plugins. I also use a reset method when I need it.

                Now what about the group that becomes a false class when created in initialize or activate (which was my original question), and that only works if I create it just before I put something into it (in the same method).
                Any reason why it is so? Something else I didn't get?

                1 Reply Last reply Reply Quote 0
                • T Offline
                  thomthom
                  last edited by 28 May 2012, 19:42

                  Matbe add_group returned false because it failed? not sure, but does it happen when you at don´t call initialize manually like you did before?

                  Empty groups can dissappaer if some other operation is commited.

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

                  1 Reply Last reply Reply Quote 0
                  • M Offline
                    Michaelv
                    last edited by 28 May 2012, 23:00

                    @thomthom said:

                    Matbe add_group returned false because it failed? not sure, but does it happen when you at don´t call initialize manually like you did before?

                    Empty groups can dissappaer if some other operation is commited.

                    The puts after the add_group call (in initialize or activate), recognizes it as Sketchup::Group but in the other method where I use it, it becomes a FalseClass object.

                    Yes the empty group disappeared essentially, since it became a FalseClass object.
                    It happens when I call initialize "manually" (which I take you mean within the activate method), when I set the group in the activate method, or whether initialize is called automatically.

                    I suspect it is what you said: "empty group disappear when some other operation is committed", that seems to be the illogical logic of what is happening. 😆

                    1 Reply Last reply Reply Quote 0
                    • T Offline
                      thomthom
                      last edited by 29 May 2012, 07:44

                      @michaelv said:

                      that seems to be the illogical logic of what is happening. 😆

                      Welcome to the world of SketchUp Ruby API scripting! 😄

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

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

                      Advertisement