SketchUcation Plugin Store

 

 

Here's another mystery to me

Here's another mystery to me

Postby 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

Re: Here's another mystery to me

Postby 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

Re: Here's another mystery to me

Postby 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

Re: Here's another mystery to me

Postby 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_toolMyTool.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 # initialize triggers now

# A menu triggers this
def self.select_my_tool
  
# Only actiavte triggers now.
  
Sketchup.active_model.select_tool( @tool )
end
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
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

Re: Here's another mystery to me

Postby 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.
User avatar
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

Re: Here's another mystery to me

Postby Dan Rathbun » Mon May 28, 2012 3:01 pm

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
User avatar
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

Re: Here's another mystery to me

Postby 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

Re: Here's another mystery to me

Postby 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.
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
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

Re: Here's another mystery to me

Postby 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. :lol:
Michaelv
 
Posts: 95
Joined: Wed Mar 03, 2010 11:02 pm
Name: Michael

Re: Here's another mystery to me

Postby thomthom » Tue May 29, 2012 8:44 am

Michaelv wrote:that seems to be the illogical logic of what is happening. :lol:

Welcome to the world of SketchUp Ruby API scripting! :D
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
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

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago

Artisan Organic Toolset - a set of powerful organic modeling tools.

Premium Members get 20% discount!

Ad Machine
Robot
 
Posts: 2012


Return to Developers' Forum

cron

Who is online

Users browsing this forum: No registered users and 2 guests