Application Development Exploding Component Instances
-
Hello,
I want to explode a list of nested components inside groups or components through their definitions.
m=Sketchup.active_model s=m.selection sel=m.definitions.each{|d|s.add d.instances if d.name=~/#{"Sophie"}/} sophie=ents.grep(Sketchup;;ComponentInstance) sophie.each{|e| e.explode }
With this code I select all "Sophie" in my SketchUp models and explode all the "Parents" components of "Sophie".
How to explode "Shophie" without modifying the parent components?
And how to explode the definition "Chris" at the same time as "Shophie"?
Thank you in advance for your help.
David
-
As usual you are making it more complicated that it needs to be - and the reference 'ents' appears from nowhere too !
Try this approach...name="Sophie" # or ="Chris" etc to make it reusable... model=Sketchup.active_model matches=model.definitions.each.find_all{|d| d.name =~ /#{name}/ } matches.each{|match| match.instances.each{|i| i.explode } }
This matches "Sophie", "Sophie#1", "Sophie#2" etc
But it'll also match "My_Sophie" !
If you want just the start "Sophie" etc used.name =~ /^#{name}/
ord.name =~ /^#{name}$/
for exactly "Sophie".
Although if you want an exact match used.name == name
or even simpler ?
match=model.definitions[name] ### then use match.instances... if match
It does not remove the now unused "Sophie" definition[s] from the model's definitions list !
If you want to do this, then run your code inside amodel.start_operation(... model.commit_operation
block, and after exploding the instances of 'match
' use
usematch.entities.clear!
- this will 'empty' the definition and when thr operation is committed SketchUp's Garbage-Collection removes the empty definition from the model's list... -
Bonjour TIG,
Avec votre code:
name="Sophie" model=Sketchup.active_model matches=model.definitions.each.find_all{|d| d.name =~ /#{name}/ } matches.each{|match| match.instances.each{|i| i.explode } }
j’obtiens le message d'erreur suivant dans la console ruby:
Error; #<LocalJumpError; no block given> <main>;2;in `each' <main>;2;in `<main>' -e;1;in `eval' nil
Comment corriger cette erreur ?
Merci
-
Sorry, I hadn't tested it.
Here is the corrected code:name="Sophie" # or ="Chris" etc to make it reusable... model=Sketchup.active_model matches=model.definitions.find_all{|d| d.name =~ /#{name}/ } matches.each{|match| match.instances.each{|i| i.explode } }
I had an errant '.each' in it, when the '.find_all' was all that is needed...
-
Wonderful it works very well!
To explode several definitions, I simply did this:
name=['Sophie','CUBE'] model=Sketchup.active_model matches=model.definitions.find_all{|d| d.name =~ /#{name}/ } matches.each{|match| match.instances.each{|i| i.explode } }
-
sometime it's worth writing 'visual code' to see how the ruby works...
click to see gif...
this is based on Tigs example
def explode_named(*args) model = Sketchup.active_model defs = model.definitions sel = model.selection view = model.active_view names = Regexp.union(args) matches = defs.find_all{|d| d.name =~ names } model.start_operation('Explode Inards') matches.each{|match| match.instances.each{|i| sel.add(i) view.refresh sleep 0.2 i.explode } } defs.purge_unused model.commit_operation # end
to run
explode_named("Sophie","Chris")
john
-
Really perfect your solution driven!
Thank you
-
**Dirven, I chose to use your method that works well.
When components are exploded with dynamic materials, the textures will apply to the basic geometry.
Thus the rendering engines can easily manage the maping coordinates to make beautiful 3D renderings.
In the case of Click-Cuisine 2:
Each piece of furniture can contain between 10 and 50 components to explode, so for a kitchen with 25 modules it takes 2 minutes of loading.
I set the value "sleep", to "0" to go faster but that is insufficient.
Is it possible to execute code faster or activate the SketchUp progress bar?
Thank you**
-
it's basically TIG's code + a couple of tweaks, I made it 'visualised' to help you 'see' what's happening because I find it useful myself...
none of these are needed at all and the code will run faster...
sel.add(i) view.refresh sleep 0.2
so either comment them out or delete them for 'production code'...
any 'progress bar' will add even more overhead and can only slow the progress more...
john
-
The loading time decreased from 2 minutes to 4 seconds.
Thanks Driven
Advertisement