[Code] layers.purge_unused
-
I tried using this code to transfer all groups and component layers to layer0 and then purge all unused leaving dimension and label layers. (Loose geometry is dealt with separately). The problem is that some layer names remain in the inspector even though empty.
model = Sketchup.active_model layers = model.layers entities = model.entities entities.each do |entity| if entity.class == Sketchup;;Group || entity.class == Sketchup;;ComponentInstance entity.layer = layers[0] end end layers.purge_unused UI.show_inspector("Layers")
Have I done something silly?
-
It works with groups for me.
I suspect that the bug is in the way you deal with loose geometry. Do you change layer of all entities in component definitions and inside groups? -
@unknownuser said:
It works with groups for me.
It doesn't seem to discriminate. Layers generally comprise components and groups. Each entity and its layer is sent to a javascript array and that seems an OK mixture
@unknownuser said:
I suspect that the bug is in the way you deal with loose geometry.
I think my comment about loose geometry is a bit of a red herring as I did not do anything with it before the purge. Anyway, any loose geometry is in layer0 as Gaieus' dictum.
@unknownuser said:
Do you change layer of all entities in component definitions and inside groups?
No just top level as above. I did try active_entities but that did not make any difference (I never really understood the active bit). Perhaps you could expand a little on what you had in mind about this.
Many thanks
Chris
-
@chrisglasier said:
No just top level as above. I did try active_entities but that did not make any difference (I never really understood the active bit). Perhaps you could expand a little on what you had in mind about this.
model.entities
- is always the top level - regardless.
model.active_entities
- is the entities of the open group/component.After you run your script, and some layers remain - do they disappear when you do a purge from SU?
-
@thomthom said:
model.entities
- is always the top level - regardless.
model.active_entities
- is the entities of the open group/component.OK goddit thanks
@thomthom said:
After you run your script, and some layers remain - do they disappear when you do a purge from SU?
No. I also tried saving the file and opening it again (in case something got stuck!!).
Perhaps ... ?
Chris
-
Hi,
Just an: as groups are stored like components, maybe that changing the layer of a group doesn't change it's internal definition ?
Check for internal compos definitions and scan their entities to see if they are on their original layer. In such a case, SU doesn't allow to delete the layer. -
Your code only modifies groups/components to be placed on layer 0. It assumes that all geometry is also placed on layer 0.
Seeing how Tomasz reports it working for him I'm guessing there is some entity that's not located on layer 0. If you turn on Color By Layer (ensuring edges are coloured by material as well) - see if you see anything with a colour other than what layer 0 is set to.
-
@didier bur said:
Hi,
Just an: as groups are stored like components, maybe that changing the layer of a group doesn't change it's internal definition ?
Check for internal compos definitions and scan their entities to see if they are on their original layer. In such a case, SU doesn't allow to delete the layer.Ah yea! iterating over the top level entities won't do it. In addition all model.definitions has to be iterated. that's it!
-
In fact, since you only want to modify groups components you don't have to check the top level at all.
Just scan the definition array for Group/Component definitions (ignoring Image definitions) and loop over the instances for each definition.
-
Thanks all ... some overnight sleuthing is required ...
-
I think this will work:
model = Sketchup.active_model layers = model.layers model.definitions.each { |d| # Ignore Images and definitions with no instances next if d.image? || d.count_instances == 0 # Set Group/ComponentInstance to layer0 d.instances.each { |i| i.layer = layers[0] } } layers.purge_unused UI.show_inspector("Layers")
-
@thomthom said:
I think this will work: ...
Yes thanks it does reduce the layer count from 23 to 20, of which 12 are correctly retained for non real world replica dimensions and labels.
But I have to do more sleuthing ...
Chris
-
@chrisglasier said:
But I have to do more sleuthing ...
Here are the results so far:
I deleted all loose geometry
The total number of groups and components matches the javascript array indicating all have been passed to layer0 using Thomas' definition loop (thanks again for that).
Each entity and any children in the expanded outliner view returns layer0 in entity info.
But see this:
The selected column shows in its original layer colour and can be turned on and off with the original layer checkbox - yet the entity info shows layer0. And if I make the columns layer active and hide layer0, nothing is displayed or indicated in th outliner. The same is true for the remaining seemingly "unpurgable" layers.
What think?
Chris
-
I just noticed that there are 7 section planes -
the same number as the unpurgable layers. I don't know much about these. Perhaps it is just coincidental?
Chris
-
@chrisglasier said:
I just noticed that there are 7 section planes - Perhaps it is just coincidental?
Yes it is - all section planes are on layer0 (obvious to me now).
Here is the current code:
@dlg.add_action_callback("sceneLayersPurge") {|d, p| model = Sketchup.active_model layers = model.layers entities = model.entities array = [] a = 0 model.definitions.each { |d| # Ignore Images and definitions with no instances next if d.image? || d.count_instances == 0 # Set Group/ComponentInstance to layer0 d.instances.each { |i| if i.class == Sketchup;;Group name = i.name end if i.class == Sketchup;;ComponentInstance name = i.definition.name end original = i.layer.name i.layer = layers[0] array[a] = [name,original,i.layer.name] array[a] = array[a].join(",") a = a + 1 } } layers.purge_unused UI.show_inspector("Layers") array = array.join(";") cmd = "sceneLayersPurgeReceive('#{array}');" @dlg.execute_script (cmd) }
and here are samples from the js report showing name, original layer, final layer
I'm running out of sleuthing ideas!
Chris
-
Have you tried manually making the problem layers invisible? If so, does anything disappear? Also, try manually deleting the layer. If it's truly empty, SU will quietly remove it. If it's not empty, SU will warn you, and ask where you want to move the things. CB.
-
Can you supply the model you are working with? It might help figure it out faster.
Chris
-
Thanks all. I need to rethink this before asking people to venture further down a blind alley.
I can reduce a Sketchup up model to two plain text lists. One lists components and groups and the other lists scenes. Both lists are turned into javascript multi-level arrays.
But I don't want to wipe out any established relationships between components and layers, layers and scenes, scenes and styles and scenes and property settings. I thought I could do that by manipulating the model but I am thinking it is probably better to capture the data and regenerate the whole thing.
If you are interested in why I want to do this please see the discussion in the Modelur thread.
Thanks and regards
Chris
Advertisement