Deleting entities on a layer doesn't work
-
Hallo!
I wrote a scribt that creates some entity objects on a layer called "Balkenlayer". Now I try to write a scribt that deletes every entity object on "Balkenlayer" to be able to execute the first scribt I wrote more then only one time without having the entity objects created by the first invoking of the scribt on the layer.
As adviced I used
mod = Sketchup.active_model mod.active_entities.to_a.each{|e| next if not e.valid?; e.erase! if e.layer==layer_array["Balkenlayer"] }
to delete the objects on Balkenlayer. But every time I invoke the first scribt that created the objects on "Balkenlayer" after the should have been deleted, Sketchup opens a window that tells me:
"your recent operation has caused visible geometry to merge with existing geometry on a hidden layer"In the next step nothing happens.
Do you know what I have to change?
Thanks for your time.
Tim -
You shouldn't normally place 'raw' geometry [edges, faces, curves etc] on any layer other than the default-layer [named 'Layer0' in the EN version]. If you do you can get edges and their faces on different layers that are on/off and have unexpected erasures when a visible edge is deleted and its invisible layered face also goes...
That said it's quite possible to write a simple method to erase [or indeed to 're-layer'] all of the model's entities on another named layer. This can be done for 'model.active_entities' i.e. all those in the current edit context, or 'model.entities' i.e. all those in the model irrespective of the edit context; or all entities inside selected [or all] groups or components..
The following should do it for the 'active-entities'...<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">"Balkenlayer"</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault">return nil if not layer </span><span class="syntaxcomment">### Traps for it not existing<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">e</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> next if not e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">valid</span><span class="syntaxkeyword">?<br /></span><span class="syntaxdefault"> e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">erase</span><span class="syntaxkeyword">! if </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">}<br /></span><span class="syntaxcomment">### Note how we work on an array made from the entities-object<br />### NOT the entities-object itself!<br />###<br />### To process all model's entities, instead you'd use<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">e</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> next if not e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">valid</span><span class="syntaxkeyword">?<br /></span><span class="syntaxdefault"> e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">erase</span><span class="syntaxkeyword">! if </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">}<br /></span><span class="syntaxcomment">### To process entities inside components/groups use<br /></span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">definitions</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">d</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> d</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">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">e</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> next if not e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">valid</span><span class="syntaxkeyword">?<br /></span><span class="syntaxdefault"> e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">erase</span><span class="syntaxkeyword">| if </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">}<br />}<br /></span><span class="syntaxcomment">### if you want to process ONLY components add<br /></span><span class="syntaxdefault">next if d</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">group</span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> </span><span class="syntaxcomment">### after the |d|<br />### or to process ONLY groups add<br /></span><span class="syntaxdefault">next if not d</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">group</span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> </span><span class="syntaxcomment">### after the |d| instead<br /></span><span class="syntaxdefault"> </span>
-
But isn't that exactly the code I already used?
-
@kiesewetter said:
But isn't that exactly the code I already used?
Not exactly the same.
It works.
Try it and see if you can any errors...
Why do you have geometry that is connected separated onto different layers.
As I explained layers do not separate geometry only its visibility.
Use groups or components of geometry - all on Layer0 [or its equivalent in your locale] - and set the group/instance layer to control visibility.
I fail to see the wider issues here -
The geometry isn't disconected. I wrote a scribt that should display some information, that is saved as an attribute of the faces, in a diagramm an text on a different layer ("balkenlayer"). Then it should be possible to go on working on the model and to invoke the scribt that shows the information on "balkenlayer" again. So far the diagramm and the text is displayed on the layer and the geometry of it seems to be erased. But when I invoke the scribt one more time, Sketchup still tells me, that:
"your recent operation has caused visible geometry to merge with existing geometry on a hidden layer"
Although I meanwhile use the following code:
layer = layer_array["Balkenlayer"] return nil if not layer mod.entities.to_a.each{|e| next if not e.valid?; e.erase! if e.layer==layer}
I don't understand why.Though there is no geometry to see if only the "Balkenlayer" is visible.
Any idea? -
Instead of erasing things check what they are...
mod=Sketchup.active_layer layer = mod.layers["Balkenlayer"] return nil if not layer mod.entities.to_a.each{|e|next if not e.valid?; puts e if e.layer==layer}
Now the Ruby Console will show a list of everything in the model's entities on layer 'Balkenlayer'.
Another way is to highlight them...mod=Sketchup.active_layer layer = mod.layers["Balkenlayer"] return nil if not layer ss=mod.selection ss.clear mod.entities.to_a.each{|e|next if not e.valid?; ss.add(e) if e.layer==layer}
OR even color them...
mod=Sketchup.active_layer layer = mod.layers["Balkenlayer"] return nil if not layer ss=mod.selection ss.clear mod.entities.to_a.each{|e|next if not e.valid?; e.material='Red' if e.layer==layer}
If the layer is 'off' switch in 'on' !
I can't understand what it is exactly that you are doing, and therefore what the problem might be.
Can you write a simple step by step list of what you do - you 'narrative' format is not fully clear to me... -
I'd recommend you add your geometry to a group and place that group on your layer. That way you ensure your temporary layer doesn't merge with existing geometry. Also, it's easier to erase it, just erase the group.
-
tt
It's as I tried to explain earlier in this thread...You should never put 'raw geometry' on a layer other than Layer0.
All geometry in the same context 'sticks together' irrespective of its layer.If you want to use layers to control visibility then use groups or components placed on those layers.
To add a face that is on a specific layer use this to put it into a group and then set the group's layer etc###... gp=mod.entities.add_group() gp.entities.add_face(p0,p1,p2,p3) gp.layer=layer ###...
-
Step by step what I want to do:
- a scribt assigns information to selected faces of the model as attributes.
- another one creates a bar chart (the entities that should be erased later) based on the attributes of the faces on a layer called "balkenlayer".
- a selection observer is used to go back to the model to go on working on it. that observer should delete the bar chart (everything on the layer "balkenlayer").
- later it schould be possible to invoke the script that created the bar chart once more.
it is working so far. in the 3. step every entity on that layer seems to be erased. but if i invoke the scribt once more as mentioned in step 4, sketchup tells me:
"your recent operation has caused visible geometry to merge with existing geometry on a hidden layer"
although the is nothing to see.
do know now what i am trying to receive?
is there maybe an array that stores the entity although it got erased or something like that?thank you guys for your time.
-
Maybe we can have a look at the whole script?
-
IF your 'barchart' were made inside a group [all text/geometry etc] then there would be no need to use layers for these unless you want to hide that group at some point before it's finally erased.
Using a @@ [or $] variable to point at the group it is then easily deleted using e.g. @@group.erase! rather than messing on with layers...
Let's see the code - if it's too 'sensitive' PM it to thomthom [or me] and we can be discreet about it...
-
I study civil engineering and the script is some kind of homework. I'll talk to my tutor in two days. After that I'll know wether I'm allowed to give you the code.
Thanks so far.
Tim
Advertisement