Checking for a group?
-
Thanks Dan, another typo - more haste less speed...
I've fixed the original... -
Thanks!
-
hello,
i went thru all snippets,
buthow can i check, if a group @NAME
exists or not in the actve model.situation
i create a group NAME by ruby
then it either is deleted or notwhith the next call ruby shall check, if group NAME is there
if not, create it
if so, delete and create it newmy trial:
entities = @NAME.entities entity1 = entities[1] status = entity1.valid? unless status == true model = Sketchup.active_model @NAME.erase! end
reports an not existing group, so no check is possible
tahnx for helping on this
stan
-
A simple
group.valid?
or - assuming the group is named 'xxx'
group=nil
Sketchup.active_model.definitions.each{|d|
next unless d.group?
if d.instances[0].name=="xxx"
group=d.instances[0]
break
end
}or
group=nil
some_entities.grep(Sketchup::Group).each{|g|
if g.name=="xxx"
group=g
break
end
} -
hi tig and yes,
@group_STAIR.erase! if @group_STAIR.valid?
works perfect.
another advantage: when i generate my group and copy it manually to the desired place
and then call the ruby again, the original is beeing deleted and the copy remains.
that is, what i wanted to achieve: when the user is satisfied with the result, he can either make a copy or make a component out of the group.
next step is done!
thanx a lot
stanps: (on the other hand i was not sure in example 2 and 3, why they did not work for me, i still investigate, must be the NAME definition, i think).
-
In Ruby, only constants are ALL_UPPERCASE
-
hi dan, have to correct it ( was only for me to see it better while working on it), thanx. (and i already study the files, you recomend for newbie's...but it's a lot to learn, of course)
and i have to correct my previous statement:
my "checking" code does not work in a new opended file. it again reports a deleted group, while searching for it.
but, as soon as i create the first group and the activate the checking-line, it works.
so now a question:
is there a way to define 1 variable (true or false) ON LOAD ?
so this variable would only be "true" when sketchup is opened and before my ruby script runs for the first time (i could overwite it then for the active session)?
then i could run the checking code "unless onload == true " or something like that.ragrds stan
-
How is your code 'encapsulated' ?
In a module or a class ?
In a module use an instance variable@onload=true
within the module but outside of any methods will set it as true initially, as the code is first loaded as SketchUp starts up.
Later when you run you first method in that module@onload=false
...
The@onload
value in remembered across instances of that code during that session of SketchUp.
For a class [Tool etc] you need a@@onload
prefixed class variable to keep it enduring across uses of that class. Again define it initially outside of any class methods but inside the class's code.When you finish processing and exit your method you can might want to reset
@onload=true
etc ? It depends on what it is used for etc...Because the user could open a new or existing SKP, and that would continue to use your already-loaded/reset code you might want to construe an
AppObserver
that resets@onload=true
whenever such things change ?However, KISS - the simplest way to think about this is to test for
group.valid?
before trying to do anything to it. It's a basic trap in scripts to trap for anticipated possible failures:
do_something() if group.valid?
Another way to trap for unexpected failures is like this:
begin do_something() rescue do_something_different() end
-
Also ...what we do is use the global methods defined in the file "sketchup.rb"
The two we use are named
file_loaded()
andfile_loaded?()
require("sketchup.rb") module AMS # <--<< your toplevel namespace module ThisPlugin # <--<< rename this ### CONSTANTS # THISFILE = "#{Module.nesting[0]};#{File.basename(__FILE__)}" ### MODULE VARS # @@loaded = false unless defined?(@@loaded) ### PROXY CLASS (for this module) # class << self ### METHODS # def loaded?() @@loaded = file_loaded?( THISFILE ) end end # proxy class ### define plugin specific classes or sub-modules here # class ThisTool ### define tool class callbacks here end ### FINAL RUN ONCE CODE # unless loaded? # set up dropdown menu here # and/or right-click popup menu # attach any initial observers here # mark this file as loaded file_loaded( THISFILE ) end end # module ThisPlugin end # module AMS
-
@ tig
hi tig, thanx,
withdef delete_model_zf #*************************************************************************** @putswitch = 0 begin @group_stair.erase! rescue end end
it worked, as soon as i understood, that rescue can also be NOTHING, so the code runs thru end the method ends
@ dan, thanx,
this is still higher programming syntax (for my brain), i tried to encapsulate in
require("sketchup.rb")
MODULE AMS
MODULE 01.rb // is it right to put hier 01.rb ??? (the work-name of the ruby)
MY CODE
END MODULE
END AMS,
but have errors. have to work it out! but i would like to put the ruby into a proper form for the first release / check. i'll get back to this later. -
module
is a keyword, it must be all lowercase.Module
is a class name, it must be title case, if you refer to the class. -
@artmusicstudio said:
MODULE 01.rb // is it right to put hier 01.rb ??? (the work-name of the ruby)
No you cannot use a
Numeric
expression as aClass
orModule
name. They must begin with alpha characters, and be title case.
Advertisement