Remove layer
-
hi, I ask how I can remove a layer from ruby, I could not find a way to do this,
thanks in advance -
@dacastror said:
hi, I ask how I can remove a layer from ruby, I could not find a way to do this,
thanks in advance
First you must iterate all entities in the model ADD within all definitions entities, and change the layer if it matches...
e.layer=nil if e.layer.name=="layer_to_go"
OR us another layer...
e.layer=layers.add('new_layer')
Now you can do
layers.purge_unused
and the now unused layer is deleted...
If you have an unused layer you'd like to retain you need to make some temporary entities using it, so that layer doesn't get purged. -
TIG is being modest, but he actually wrote a snippet for it : http://sketchucation.com/forums/viewtopic.php?t=20086
But don't use it as is, you need to modify it to use the method in your own namespace.
Here is how I did it (There may be some mistakes, I'm just starting ruby ! So check it, but it works fine for me) :### LAYER DELETE METHOD ### ------------------------------------------------------ #Simple modification of TIG's snippet delete-layer.rb #Basically, move or delete layer content, then creates an entry for every layer except one to delete, then purge layers, then delete entries def self.deleteLayer(layer, delete_geometry=false) model=Sketchup.active_model ents=model.entities; defs=model.definitions; layers=model.layers model.start_operation ("Delete layer") if delete_geometry allents=[] ents.each{|e|allents<<e if e.valid? and e.respond_to?(;layer)and e.layer==layer} defs.each{|d|d.entities.each{|e|allents<<e if e.valid? and e.respond_to?(;layer)and e.layer==layer}} allents.each{|e|e.erase! if e.valid?} else ### move geom to Layer0 etc ents.each{|e|e.layer=nil if e.respond_to?(;layer)and e.layer==layer} defs.each{|d|d.entities.each{|e|e.layer=nil if e.respond_to?(;layer)and e.layer==layer}} end#if group=ents.add_group();gents=group.entities ### temporarily use other layers temp=gents.add_group() temp.layer=nil (layers.to_a-[layer]).each{|layer|tc=temp.copy;tc.layer=layer} model.active_layer=nil if model.active_layer==layer ### ensure layer is not current layer layers.purge_unused ### purge layer from browser group.erase! ### erase! the temporary layer user, use set as was. model.commit_operation end#def
Call it with something like
self.deleteLayer(layers[layerName])
-
I think a more popular command would be "Remove Lawyer"...
-
I did write a whole raft of 'missing' layer methods...
I just thought I'd let you learn by doing... -
Here is another option but I don't know if its compatible with MAC OS
Sketchup.send_action(21537) #Delete Layer
-
@tig said:
I did write a whole raft of 'missing' layer methods...
I just thought I'd let you learn by doing...I learn by looking at what you wrote !
Advertisement