[Code] layers.purge_unused

[Code] layers.purge_unused

Postby chrisglasier » Wed Jul 29, 2009 9:40 am

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.


Code: Select all

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?
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Re: [Code]layers.purge_unused

Postby tomasz » Wed Jul 29, 2009 10:06 am

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?
0

tomasz 
SU2TH & SU2KT Developer
 

Re: [Code]layers.purge_unused

Postby chrisglasier » Wed Jul 29, 2009 10:45 am

Tomasz wrote: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

Tomasz wrote: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.

Tomasz wrote: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
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Re: [Code]layers.purge_unused

Postby thomthom » Wed Jul 29, 2009 11:01 am

chrisglasier wrote: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?
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Code]layers.purge_unused

Postby chrisglasier » Wed Jul 29, 2009 11:11 am

thomthom wrote:
model.entities - is always the top level - regardless.
model.active_entities - is the entities of the open group/component.


OK goddit thanks

thomthom wrote: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
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Re: [Code]layers.purge_unused

Postby Didier Bur » Wed Jul 29, 2009 11:24 am

Hi,
Just an :idea: : 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.
0
Didier Bur
Ecole Nationale Supérieure d'Architecture de Nancy (F)
User avatar
Didier Bur 
 

Re: [Code]layers.purge_unused

Postby thomthom » Wed Jul 29, 2009 11:41 am

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.
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Code]layers.purge_unused

Postby thomthom » Wed Jul 29, 2009 11:43 am

Didier Bur wrote:Hi,
Just an :idea: : 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!
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Code]layers.purge_unused

Postby thomthom » Wed Jul 29, 2009 11:44 am

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.
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Code]layers.purge_unused

Postby chrisglasier » Wed Jul 29, 2009 11:52 am

Thanks all ... some overnight sleuthing is required ...
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Re: [Code]layers.purge_unused

Postby thomthom » Wed Jul 29, 2009 12:06 pm

I think this will work:
Code: Select all
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")
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Code]layers.purge_unused

Postby chrisglasier » Wed Jul 29, 2009 12:35 pm

thomthom wrote: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
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Re: [Code]layers.purge_unused

Postby chrisglasier » Thu Jul 30, 2009 1:36 am

chrisglasier wrote: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:

Layer.png



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
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Re: [Code]layers.purge_unused

Postby chrisglasier » Thu Jul 30, 2009 2:01 am

I just noticed that there are 7 section planes -

Layer02.png


the same number as the unpurgable layers. I don't know much about these. Perhaps it is just coincidental?

Chris
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Re: [Code]layers.purge_unused

Postby chrisglasier » Thu Jul 30, 2009 8:12 am

chrisglasier wrote: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:

Code: Select all

@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

Layers034.png


I'm running out of sleuthing ideas!

Chris
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

Re: [Code]layers.purge_unused

Postby daiku » Thu Jul 30, 2009 2:22 pm

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.
0
User avatar
daiku 
PluginStore Author
PluginStore Author
 

Re: [Code]layers.purge_unused

Postby Chris Fullmer » Thu Jul 30, 2009 5:37 pm

Can you supply the model you are working with? It might help figure it out faster.

Chris
0
Lately you've been tan, suspicious for the winter.
All my Plugins I've written
User avatar
Chris Fullmer 
SketchUp Team
SketchUp Team
 

Re: [Code]layers.purge_unused

Postby chrisglasier » Fri Jul 31, 2009 4:10 am

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
0
With JSON machines we can analyse what is to be achieved so that IT can help with automation to achieve it.
User avatar
chrisglasier 
PluginStore Author
PluginStore Author
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Developers' Forum

Who is online

Users browsing this forum: No registered users and 9 guests

Visit our sponsors: