Selectionobserver
-
Hallo!
I tried to write a script that displays in the ruby window the name of the layer the entity is arranged at everytime it gets selected. After that the entity should be erased. I used the folling code an it didn't work...
class Selbserver < Sketchup;;SelectionObserver def onSelectionAdded(sel) sel.each{|e| layer=e.layer.name puts layer e.erase! } end sel=Sketchup.active_model.selection sel.add_observer Selbserver.new #to load it #load "C;/Users/tim/Desktop/observertest.rb" end
i have no idea, why nothing happens when i load the script an select an entity after that.
thanks for your time.
-
You can't change the entities in a selection as you refer to it [like erasing them]!
You could trysel**.to_a**.each{|e|...
as that would take a clone of it as an array...... but no promises it'll work though... -
@kiesewetter said:
I tried to write a script that displays in the ruby window the name of the layer the entity is arranged at everytime it gets selected. After that the entity should be erased.
Sketchup::SelectionObserver#onSelectionAdded() callback is NOT IMPLEMENTED. Use onSelectionBulkChange instead.
I think it's best if the entity is removed from the selection set (not erased from the model.)
module Kiesewetter @@sel_spy = nil class Selbserver < Sketchup;;SelectionObserver def onSelectionBulkChange(sel) sa = sel.to_a sa.each{|e| puts("#{e.class.name};#{e.object_id} on layer; #{e.layer.name}") sel.remove(e) } end # def callback end #class unless @@sel_spy @@sel_spy = Selbserver.new() Sketchup.active_model.selection.add_observer( @@sel_spy ) end #to load it #load "C;/Users/tim/Desktop/observertest.rb" end # module
-
As Dan says, make a list of the selected items, remove them from the selection and then erase them ?
I hadn't spotted the method you used - .onBulk... is the one to use - the others are foobar...
-
first of all, thank you guys.
ok, the ...BulkChange observer responds.
is it possible to delete instead of the entities in the selection every entity on a certain layer? -
model=Sketchup.active_model layers=model.layers layername="Layer1"
you decide which layer to use!
model.active_entities.to_a.each{|e| next if not e.valid?; e.erase! if e.layer==layers[layername] }
note how we make an array [.to_a] of the entities -
you can't work on the entities directly AND change it - weird results will happen!
the active_entities erases just things in that entities collection -
that need not be model.entities, but could be group.entities or even definition.entities
the e.valid? test avoids a crash -
e.g. if you've erased an edge so its face no longer exists when we come to look at it!
-
works with the faces, but all the lines aren't erased. any idea why?
-
@tig said:
model=Sketchup.active_model layers=model.layers layername="Layer1"
you decide which layer to use!
model.active_entities.to_a.each{|e| next if not e.valid?; e.erase! if e.layer==layers[layername] }
note how we make an array [.to_a] of the entities -
you can't work on the entities directly AND change it - weird results will happen!
the active_entities erases just things in that entities collection -
that need not be model.entities, but could be group.entities or even definition.entities
the e.valid? test avoids a crash -
e.g. if you've erased an edge so its face no longer exists when we come to look at it!
It'd be faster to fetch the reference to the layer you want to erase from before the loop. Retrieving a reference to a layer by looking up a string reference is slower.
<span class="syntaxdefault">model </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br />layer </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">layers</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'Layer1'</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_a</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each</span><span class="syntaxkeyword">{</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">erase</span><span class="syntaxkeyword">!</span><span class="syntaxdefault"> if e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">valid</span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">&&</span><span class="syntaxdefault"> e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">layer </span><span class="syntaxkeyword">== </span><span class="syntaxdefault">layer<br /></span><span class="syntaxkeyword">}</span><span class="syntaxdefault"> </span>
-
@kiesewetter said:
works with the faces, but all the lines aren't erased. any idea why?
The edges aren't on the same layer?
-
They are not on the layer ?
To find an object's layer Select it and open Entity Info...
The code snippet I offered erases everything on a particular layer - it is not careful about things like if the edge goes so does its face even if that face is NOT on that layer...
It was a simplistic example - you can add all sorts of checks too.
If you only want to layer faces but erase their edges then use something like...
Firstly change it to
layer=layers[layername]
### as tt recommends...then inside the 'each' loop...
...
if e.class==Sketchup;;Face and e.layer==layer edges=e.edges e.erase! edges.each{|ed| ed.erase! if not ed.faces[0] } end
...
which erases the face if it's on the specified layer... AND having made a list of that face's edges, it erases any of them that are now 'faceless' as a result... -
the edges were on the same layer (tested it with test=e.layer; puts test)
could it be that lines only can be deleted, if they don't border a face?
i executed the script to delete the entities twice, in the first step only the faces were deleted. in the second step the lines were erased.
-
@kiesewetter said:
could it be that lines only can be deleted, if they don't border a face?
Erasing an edge will also erase any connected faces.
-
hmmm... ok. in this case i don't understand why the lines behaved like that. not that important.
ok, thanks to you once again.
Advertisement