Create layers by materials

Create layers by materials

Postby zsotti » Thu Oct 13, 2011 1:50 pm

Hi there!

I am trying to figure out if there is a plugin that can do the following (or if it is technically possible to do this at all):

Automatically create layers (by material library definitions, layer name=material name) and put every face on the corresponding layer (if face material=wood, create layer with name "wood" and put face on this layer).

Thank you for your help!
Zsotti
0

zsotti 
 

Re: Create layers by materials

Postby TIG » Thu Oct 13, 2011 2:41 pm

It's relatively straightforward...
BUT cross-mixing faces onto various layers can be a recipe for disaster and confusion, because layers only control visibility and leave geometry connected across layers even if some things are not visible - therefore deleting an edge on a visible layer might have erased a face that needed the edge that was unseen at that time, and is not found to have vanished until much later... It's much safer to model raw geometry on Layer0 and put it inside groups or components, and then use layers to control visibility of these 'containers'...
IF you have to do it... try this...
Code: Select all
### Copy/paste code into a file called
### 'TIG-faces2materiallayers.rb' in the Plugins folder
module TIG
def self
  model=Sketchup.active_model
  ents=model.active_entities
  ents.each{|e|
    next unless e.class==Sketchup::Face
    name="-NIL"
    name=mat.display_name if mat=e.material
    e.layer=model.layers.add(name)
  }
end
end
### Usage: TIG.faces2materiallayers
EDIT: added the .rb version following requests
TIG-faces2materiallayers.rb
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Create layers by materials

Postby Didier Bur » Thu Oct 13, 2011 3:27 pm

Hi zsotti

What you need is exactly what my plugin "layers_materials" does: check it here:
http://rld.crai.archi.fr/rubylibrarydepot/ go to the materials section and search for layers-materials.rb
Regards,
0
Didier Bur
Ecole Nationale Supérieure d'Architecture de Nancy (F)
User avatar
Didier Bur 
 

Re: Create layers by materials

Postby TIG » Thu Oct 13, 2011 3:34 pm

Thanks Didier!
I thought it existed already... but I couldn't find it, so I wrote one of my own :roll:
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Create layers by materials

Postby Didier Bur » Thu Oct 13, 2011 3:45 pm

much more cleverly coded than mine btw... ;)
0
Didier Bur
Ecole Nationale Supérieure d'Architecture de Nancy (F)
User avatar
Didier Bur 
 

Re: Create layers by materials

Postby zsotti » Thu Oct 13, 2011 8:36 pm

Dear TIG and Didier !

Thank you very much for your quick reply and of course solution.

I have copied the script of TIG and I am going to try it as soon as possible. I will give you feedback for sure. Maybe funny for you, but it could be one of my most important plugins. I will share with you why, later on.

Thanks!

PS. Dear Didier, I was not able to browse your site. Is it my fault or is it offline sometimes?
0

zsotti 
 

Re: Create layers by materials

Postby massimo » Thu Oct 13, 2011 8:40 pm

PS. Dear Didier, I was not able to browse your site. Is it my fault or is it offline sometimes?

viewtopic.php?p=360023#p360023 ;)
0
User avatar
massimo 
 

Re: Create layers by materials

Postby Didier Bur » Fri Oct 14, 2011 11:50 am

The site was down yesterday after the PC is was on burned out :(
I'm actually restoring the saved database copy of it...
0
Didier Bur
Ecole Nationale Supérieure d'Architecture de Nancy (F)
User avatar
Didier Bur 
 

Re: Create layers by materials

Postby zsotti » Sat Oct 15, 2011 6:40 pm

Hi !

What I have promised, my feedback:

1 / TIG's script - surely my fault (I am a rookie in ruby scripting), copied the RB in plugins folder with the right file name, but I have not found the 'command' in any menu. I have tried to write a menu definition according other RB scripts but I did not succeed, sorry.

2 / Didier's script - after his server came back to life I was shocked about how many scripts he has written. Congratulations! I have downloaded the right one and it does (in two steps) exactly what I need.

Thank you for your support!
Zsotti
0

zsotti 
 

Re: Create layers by materials

Postby TIG » Sat Oct 15, 2011 8:05 pm

My script does not have a menu entry.
It's usage as described is
TIG.faces2materiallayers
in the Ruby Console
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Create layers by materials

Postby rbg » Tue Jun 05, 2018 11:12 am

Hi there,

is there a chance to make this script work for sketchup 2018?

The Tig version doesnt work

and the Didier makes the layers but doesnt put the faces in the layers.

kind regards

Richard
0

rbg 
 

Re: Create layers by materials

Postby TIG » Tue Jun 05, 2018 3:57 pm

Edit the ancient RB file [in a plain-text editor, like Notepad++ or BBedit] and change the line:
def self
to read:
def self.faces2materiallayers
It should then work...
Then in the Ruby Console, to run it, you use:
TIG.faces2materiallayers
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Create layers by materials

Postby rbg » Wed Jun 06, 2018 2:51 pm

Thanks for the quick reply TIG

I have tried your adjustment and then i get this error code:

------

TIG.faces2materiallayers
Error: #<NameError: undefined local variable or method `mat' for TIG:Module>
C:/Users/richard/AppData/Roaming/SketchUp/SketchUp 2018/SketchUp/Plugins/TIG-faces2materiallayers.rb:11:in `block in faces2materiallayers'
C:/Users/richard/AppData/Roaming/SketchUp/SketchUp 2018/SketchUp/Plugins/TIG-faces2materiallayers.rb:8:in `each'
C:/Users/richard/AppData/Roaming/SketchUp/SketchUp 2018/SketchUp/Plugins/TIG-faces2materiallayers.rb:8:in `faces2materiallayers'
<main>:in `<main>'
SketchUp:1:in `eval'

----

do you have any suggestions?

kind regards

Richard
0

rbg 
 

Re: Create layers by materials

Postby TIG » Wed Jun 06, 2018 6:55 pm

This is a very very ancient RB...
Try this alternative...
Code: Select all
### Copy/paste code into a file called
### 'TIG-faces2materiallayers.rb' in the Plugins folder
module TIG
def self.faces2materiallayers()
  model=Sketchup.active_model
  model.active_entities.grep(Sketchup::Face).each{|face|
    next unless mat=face.material
    name=mat.display_name
    face.layer=model.layers.add(name)
  }
end
end
### Usage: TIG.faces2materiallayers

I think it'll work...
However, assigning layers to geometry, like faces and edges, is usually a very bad idea !!!!
They should almost always be given Layer0.
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Create layers by materials

Postby marcelp102 » Tue Feb 12, 2019 1:41 pm

Dear TIG, after entering TIG.faces2materiallayers in ruby scriptbox I only get []. Any help?
0

marcelp102 
 

Re: Create layers by materials

Postby TIG » Tue Feb 12, 2019 7:16 pm

For the avoidance of doubt, you must paste the whole of the code into the Ruby Console.
Then to run it use TIG.faces2materiallayers
For it to work there must be faces in the current active entities context, and they must have materials assigned to their fronts.
Faces within groups/components are NOT considered, unless you first edit the container to change the active entities context...

If those conditions are met, then any faces should be reassigned to a Layer named after their [front] material, which is made if it does not exist.

To reiterate my earlier comments, moving raw geometry to a layer other than Layer0 is generally not a good idea...
But if you want to do it then this way should work...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Create layers by materials

Postby avanoppen » Thu May 07, 2020 8:05 am

Hi TIG,

I'm dying to try the script. Question though: since Sketchup introduced tags as a replacement for layers, how would the script change? Looking forward to your reply, Gus
0

avanoppen 
 

Re: Create layers by materials

Postby TIG » Thu May 07, 2020 3:18 pm

It doesn't change much at all - under the hood the API still calls them 'layers'.
Very similar to the way it still sees 'scenes' as 'pages' - changed because Layout arrived...
0
TIG
User avatar
TIG 
Global Moderator
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Plugins

Who is online

Users browsing this forum: Google Bot, nil batista, x095760 and 13 guests

Visit our sponsors: