by Michaelv » Mon May 28, 2012 3:08 am
- Code: Select all
def initialize @mod = Sketchup.active_model @ents = @mod.active_entities @sel = @mod.selection @group = @ents.add_group puts @group.class end
def operation puts @group.class @group.entities.add_curve (set of points) end
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) - Code: Select all
def initialize @mod = Sketchup.active_model @ents = @mod.active_entities @sel = @mod.selection end
def operation @group = @ents.add_group puts @group.class @group.entities.add_curve (set of points) end
Works just fine, and confirms that @group is a "Sketchup::Group" I'm working around it,but anybody knows why or what I missed?
Last edited by thomthom on Mon May 28, 2012 10:36 am, edited 1 time in total.
Reason: Changed larger code blocks into [code] tag so formatting is preserved.
-
Michaelv
-
- Posts: 95
- Joined: Wed Mar 03, 2010 11:02 pm
- Name: Michael
by Michaelv » Mon May 28, 2012 3:15 am
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?
-
Michaelv
-
- Posts: 95
- Joined: Wed Mar 03, 2010 11:02 pm
- Name: Michael
by Michaelv » Mon May 28, 2012 3:29 am
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).
-
Michaelv
-
- Posts: 95
- Joined: Wed Mar 03, 2010 11:02 pm
- Name: Michael
by thomthom » Mon May 28, 2012 10:42 am
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: - Code: Select all
Sketchup.active_model.select_tool( MyTool.new )
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. - Code: Select all
@tool = MyTool.new def self.select_my_tool Sketchup.active_model.select_tool( @tool ) end
-

thomthom
- Global Moderator
-
- Posts: 17642
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: thomthom
- Operating system: Windows
- SketchUp version: 8
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
-
by Dan Rathbun » Mon May 28, 2012 2:56 pm
Michaelv wrote: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.- Code: Select all
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: - Code: Select all
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
Last edited by Dan Rathbun on Mon May 28, 2012 3:02 pm, edited 1 time in total.
-

Dan Rathbun
- Top SketchUcator
-
- Posts: 4076
- Joined: Tue Oct 06, 2009 3:06 am
- Location: Florida, USA
- Name: Dan Rathbun
- Operating system: Windows
- SketchUp version: 8
- License type: Pro
- SketchUp use: education
- Level of SketchUp: Advanced
by Michaelv » Mon May 28, 2012 6:59 pm
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?
-
Michaelv
-
- Posts: 95
- Joined: Wed Mar 03, 2010 11:02 pm
- Name: Michael
by thomthom » Mon May 28, 2012 8:42 pm
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
- Global Moderator
-
- Posts: 17642
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: thomthom
- Operating system: Windows
- SketchUp version: 8
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
-
by Michaelv » Tue May 29, 2012 12:00 am
thomthom wrote: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
-
- Posts: 95
- Joined: Wed Mar 03, 2010 11:02 pm
- Name: Michael
by thomthom » Tue May 29, 2012 8:44 am
Michaelv wrote:that seems to be the illogical logic of what is happening. 
Welcome to the world of SketchUp Ruby API scripting! 
-

thomthom
- Global Moderator
-
- Posts: 17642
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: thomthom
- Operating system: Windows
- SketchUp version: 8
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
-
Return to Developers' Forum
|