Here's another mystery to me
-
<span class="syntaxdefault">def initialize<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">mod </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">ents </span><span class="syntaxkeyword">= @</span><span class="syntaxdefault">mod</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_entities<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">sel </span><span class="syntaxkeyword">= @</span><span class="syntaxdefault">mod</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">selection<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">group </span><span class="syntaxkeyword">= @</span><span class="syntaxdefault">ents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_group<br />puts </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">group</span><span class="syntaxkeyword">.class<br /></span><span class="syntaxdefault">end<br /><br />def operation<br />puts </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 </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">set of 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 initialize<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">mod </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">ents </span><span class="syntaxkeyword">= @</span><span class="syntaxdefault">mod</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_entities<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">sel </span><span class="syntaxkeyword">= @</span><span class="syntaxdefault">mod</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">selection<br /> <br />end<br /><br />def operation<br /></span><span class="syntaxkeyword">@</span><span class="syntaxdefault">group </span><span class="syntaxkeyword">= @</span><span class="syntaxdefault">ents</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_group<br />puts </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 </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">set of 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?
-
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? -
-
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">( </span><span class="syntaxdefault">MyTool</span><span class="syntaxkeyword">.new )<br /> </span><span class="syntaxdefault"></span>
In this case both
initialize
andactivate
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 </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">MyTool</span><span class="syntaxkeyword">.new </span><span class="syntaxcomment"># initialize triggers now<br /><br /># A menu triggers this<br /></span><span class="syntaxdefault">def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">select_my_tool<br /> </span><span class="syntaxcomment"># Only actiavte triggers now.<br /> </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">( @</span><span class="syntaxdefault">tool </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">end<br /></span>
-
Also get the Standard Ruby Reference for 1.8.6:
RUBY PROGRAMMING REFERENCESAnd read the old "Pick-Axe" book. "Programming Ruby"
http://phrogz.net/ProgrammingRuby/frameset.htmlsee the chapter(s) on Classes
-
@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 Rubyprivate
method, that is ALWAYS called by the standard Ruby constructor methodnew()
. In addition, thenew()
method passes any arguments it gets, into theinitialize()
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 theinitialize
method return the new instance object itself, and thenew()
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 areset()
method that sets all instance variables back to a "start" state. But the intialize method can ALSO call thatreset()
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
-
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? -
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.
-
@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.
-
@michaelv said:
that seems to be the illogical logic of what is happening.
Welcome to the world of SketchUp Ruby API scripting!
Advertisement