by TIG » Tue Jun 23, 2009 11:45 am
layer-delete.rb adds a new Method the Sketchup's Layer Class- " layer.delete()" - it deletes that layer from the model and entities that used it get the default layer instead... however, if " layer.delete(true)" then layer's geometry is deleted too. The default layer cannot be deleted, BUT if 'true' is used anything on it WILL be deleted - so use it sensibly ! v1.1 simplified defn changing and respond_to? layer testing. v1.2 operation removed. v1.3 empty temp groups used for layers without detriment. v1.4 deletes layer's geometry with (delete_geometry)==true. . - Code: Select all
=begin (c) TIG 2009 layer-delete.rb adds a new Method the Sketchup's Layer Class- "layer.delete()" - it deletes that layer from the model and entities that used it get the default layer instead... however, if "layer.delete(true)" then layer's geometry is deleted too. The default layer cannot be deleted, BUT if 'true' is used anything on it WILL be deleted - so use it sensibly !
v1.1 simplified defn changing and respond_to? layer testing. v1.2 operation removed. v1.3 empty temp groups used for layers without detriment. v1.4 deletes layer's geometry with (delete_geometry)==true. v1.5 Do not modify SketchUp Layer class (jim.foltz)
=end
module YOU
def delete_layer(delete_geometry = false) model=Sketchup.active_model ents=model.entities; defs=model.definitions; layers=model.layers if delete_geometry allents=[] ents.each{|e|allents << e if e.valid? and e.respond_to?(:layer) and e.layer==self} defs.each{|d|d.entities.each{|e| allents << e if e.valid? and e.respond_to?(:layer)and e.layer == self}} 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==self} defs.each{|d|d.entities.each{|e|e.layer = nil if e.respond_to?(:layer) and e.layer == self}} end #if group=ents.add_group();gents=group.entities ### temporarily use other layers temp = gents.add_group() temp.layer = nil (layers.to_a-[self]).each{|layer| tc = temp.copy; tc.layer=layer} model.active_layer = nil if model.active_layer == self # ensure self is not current layer layers.purge_unused ### purge self from browser group.erase! ### erase! the temporary layer user, use set as was. end #def
end # module
Last edited by Jim on Sat Dec 06, 2014 9:53 am, edited 6 times in total.
Reason: updated ruby; added code block.
TIG
-

TIG
- Global Moderator
-
- Posts: 20380
- Joined: Mon Nov 12, 2007 7:24 pm
- Location: Northumbria UK
- Name: TIG
- Operating system: Windows
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
by TIG » Tue Jun 23, 2009 2:12 pm
TIG
-

TIG
- Global Moderator
-
- Posts: 20380
- Joined: Mon Nov 12, 2007 7:24 pm
- Location: Northumbria UK
- Name: TIG
- Operating system: Windows
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
by TIG » Tue Jun 23, 2009 4:23 pm
TIG
-

TIG
- Global Moderator
-
- Posts: 20380
- Joined: Mon Nov 12, 2007 7:24 pm
- Location: Northumbria UK
- Name: TIG
- Operating system: Windows
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
by TIG » Wed Jun 24, 2009 9:33 am
TIG
-

TIG
- Global Moderator
-
- Posts: 20380
- Joined: Mon Nov 12, 2007 7:24 pm
- Location: Northumbria UK
- Name: TIG
- Operating system: Windows
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
by Al Hart » Wed Jul 08, 2009 11:03 pm
I am trying this script out, but it isn't working properly for me.
(I am trying to convert it to a routine which is passed a layer and then delete the layer which was passed)
It appear that e.layer=nil will delete an entity.
But that does not seem to work when I type in something similar from the ruby consols. It sets the entity to Layer0, but does not delete it.
Should "e.layer=nil" delete an entity?
If not, then what in the ruby file actually deletes the entity?
-

Al Hart
- PluginStore Author

-
- Posts: 1623
- Joined: Sun Nov 18, 2007 3:54 pm
- Location: Centennial, Co
- Name: Al Hart
- Operating system: Windows
- SketchUp version: pre-2013
- License type: Pro
- SketchUp use: other
- Level of SketchUp: Advanced
-
by thomthom » Thu Jul 09, 2009 7:58 am
.erase!http://code.google.com/intl/nb/apis/ske ... html#erase! "e.layer=nil" should not delete an entity. .layer only defines the layer the entity is on. If anything, it should fail or set the entity's layer to default. Are you trying to delete a layer with e.layer = nil ? .layer is a property of the entity.
-

thomthom
- PluginStore Author

-
- Posts: 19457
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: Thomas Thomassen
- Operating system: Windows
- SketchUp version: 2019
- License type: Pro
- SketchUp use: other
- Level of SketchUp: Advanced
-
by TIG » Thu Jul 09, 2009 10:13 am
Al Hart wrote:I am trying this script out, but it isn't working properly for me. (I am trying to convert it to a routine which is passed a layer and then delete the layer which was passed) It appears that e.layer=nil will delete an entity. But that does not seem to work when I type in something similar from the ruby console. It sets the entity to Layer0, but does not delete it. Should "e.layer=nil" delete an entity? If not, then what in the ruby file actually deletes the entity? e.layer=nil (assuming e is an entity) it should set that entity's layer to nil - which is the default layer (in en='Layer0') My code moves everything on the layer_to_go onto the 'nil' layer = default, then deletes the layer_to_go. You could rewrite it to delete everything on the layer_to_go, BUT it doesn't do that at the moment... Are you sure it's deleting stuff - is Layer0 switched off so it's not visible ? Not having seen your code we can't help fix it... Could you publish the snippet ? Why not use the original code just as I gave it - it only works on an actual layer, and layer 'nil' doesn't exist...
TIG
-

TIG
- Global Moderator
-
- Posts: 20380
- Joined: Mon Nov 12, 2007 7:24 pm
- Location: Northumbria UK
- Name: TIG
- Operating system: Windows
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
by Didier Bur » Thu Jul 09, 2009 12:54 pm
Hi TIG, I was just considering the same feature for the new release of my Layer Manager. I thought it could work (from the user point of view) just like AutoCad did (some versions ago), so it asks wether you want to: - delete the entire content of the layer before deleting the layer, - move the entities to default or another layer. it would have to scan for groups containing objects on the layer to delete, and for compos definitions too. Before developping it, do you think this feature will be useful for users, assuming that there will be other tools in the Layer Manager to easily move entities from one layer to another ? Thanks in advance,
Didier Bur
-

Didier Bur
-
- Posts: 1413
- Joined: Wed Nov 14, 2007 10:07 pm
- Location: Nancy, France
- Name: Didier Bur
- Operating system: Windows
- SketchUp version: 2020
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
by Al Hart » Thu Jul 09, 2009 1:33 pm
thomthom wrote:.erase!http://code.google.com/intl/nb/apis/ske ... html#erase! "e.layer=nil" should not delete an entity. .layer only defines the layer the entity is on. If anything, it should fail or set the entity's layer to default. Are you trying to delete a layer with e.layer = nil ? .layer is a property of the entity.
I see my mistake now. I assumed that the .rb was deleting a layer and everything in it. But it was moving everything in the layer to Layer0 (that is what e.layer = nil was doing), and then deleting only the layer name. It was a clever idea to create a dummy group with an entry in every other layer so that purge layer would not delete the other other empty layers. Is there no way to delete a layer directly? I will change my version of the script to delete the layer and its contents. I agree with the later post that it might be better to add a parameter to layer.delete to: a.) delete all entities, b.) move entities to Layer0, or c.) ask the user what he wants.
-

Al Hart
- PluginStore Author

-
- Posts: 1623
- Joined: Sun Nov 18, 2007 3:54 pm
- Location: Centennial, Co
- Name: Al Hart
- Operating system: Windows
- SketchUp version: pre-2013
- License type: Pro
- SketchUp use: other
- Level of SketchUp: Advanced
-
by chrisglasier » Thu Jul 09, 2009 3:07 pm
Sorry I just want this topic to appear in "Your posts" - i.e mine
With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.
-

chrisglasier
- PluginStore Author

-
- Posts: 1055
- Joined: Tue Jun 03, 2008 9:11 am
- Location: Sri Lanka
- Name: chrisglasier
- Operating system: Windows
- SketchUp version: 2017
- License type: Free/Make
- SketchUp use: architecture
- Level of SketchUp: Intermediate
-
by thomthom » Thu Jul 09, 2009 3:21 pm
Al Hart wrote:Is there no way to delete a layer directly?
Not via the API, erase! method for layers and materials are missing. There's also missing methods to edit names etc... 
-

thomthom
- PluginStore Author

-
- Posts: 19457
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: Thomas Thomassen
- Operating system: Windows
- SketchUp version: 2019
- License type: Pro
- SketchUp use: other
- Level of SketchUp: Advanced
-
by Al Hart » Thu Jul 09, 2009 4:13 pm
chrisglasier wrote:Sorry I just want this topic to appear in "Your posts" - i.e mine
I don't understand the statement above ^^^ Did you alter this thread in some way? [Edit: Ah I see - you added a post to the thread so you could follow it easier. It might be better to just add a nice comment - like - "Thank you all, this is usefull information", or you could subscribe to the thread instead.}
-

Al Hart
- PluginStore Author

-
- Posts: 1623
- Joined: Sun Nov 18, 2007 3:54 pm
- Location: Centennial, Co
- Name: Al Hart
- Operating system: Windows
- SketchUp version: pre-2013
- License type: Pro
- SketchUp use: other
- Level of SketchUp: Advanced
-
by Chris Fullmer » Thu Jul 09, 2009 4:16 pm
No, but now he can find it by looking at his profile and listing all his posts. And so can I.....but I'm not really involved with layers at this point. Good luck!
Chris
-

Chris Fullmer
- SketchUp Team

-
- Posts: 6916
- Joined: Wed Nov 21, 2007 3:21 am
- Location: Boulder, CO
- Name: Chris Fullmer
- Operating system: Windows
- SketchUp version: 2013
- License type: Pro
- SketchUp use: landscape architecture
- Level of SketchUp: Advanced
-
by TIG » Thu Jul 09, 2009 11:18 pm
Didier Bur wrote:Hi TIG, I was just considering the same feature for the new release of my Layer Manager. I thought it could work (from the user point of view) just like AutoCad did (some versions ago), so it asks wether you want to: - delete the entire content of the layer before deleting the layer, - move the entities to default or another layer. it would have to scan for groups containing objects on the layer to delete, and for compos definitions too. Before developping it, do you think this feature will be useful for users, assuming that there will be other tools in the Layer Manager to easily move entities from one layer to another ? Thanks in advance,
Didier Please feel free to use my ideas in your upgraded Layer Manager... I think it could be useful... As presently configured my code moves any entities on layer_to_go to the default layer [ each...e.layer=nil] BUT instead you could move them to another designated layer [ each...e.layer=another_layer] OR simply delete them [ each...e.erase!] instead, in your suggested additional options... My code already makes 'nil' every entity on layer_to_go in the model AND inside every definition [which includes groups], before removing the layer, using the same method you could change the entity's layer or erase it... Note that to ensure the layer goes from the browser you need your code to be inside a start...commit.
TIG
-

TIG
- Global Moderator
-
- Posts: 20380
- Joined: Mon Nov 12, 2007 7:24 pm
- Location: Northumbria UK
- Name: TIG
- Operating system: Windows
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
by Didier Bur » Thu Jul 09, 2009 11:27 pm
Thanks TIG, yur programming is more compact and clever than mine was on this one ! 
Didier Bur
-

Didier Bur
-
- Posts: 1413
- Joined: Wed Nov 14, 2007 10:07 pm
- Location: Nancy, France
- Name: Didier Bur
- Operating system: Windows
- SketchUp version: 2020
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
by chrisglasier » Fri Jul 10, 2009 12:43 am
Al Hart wrote:... Ah I see - you added a post to the thread so you could follow it easier.
Yes sorry Al et al (both for being lazy and somewhat terse) but as you can see I am addicted to putting lots of things in one easily accessible place/machine: Preview device version 2.png Another proposed turn of the interface indicates why I have a big interest in deleting layers containing components rather than dimensions and labels: Preview reform version 2.png - and of course a big thank you to TIG and the others contributing to this topic. My regards Chris
With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.
-

chrisglasier
- PluginStore Author

-
- Posts: 1055
- Joined: Tue Jun 03, 2008 9:11 am
- Location: Sri Lanka
- Name: chrisglasier
- Operating system: Windows
- SketchUp version: 2017
- License type: Free/Make
- SketchUp use: architecture
- Level of SketchUp: Intermediate
-
by TIG » Sat Aug 22, 2009 12:57 pm
Now with v1.4 deletes layer's geometry with (delete_geometry)==true viewtopic.php?p=166985#p166985
TIG
-

TIG
- Global Moderator
-
- Posts: 20380
- Joined: Mon Nov 12, 2007 7:24 pm
- Location: Northumbria UK
- Name: TIG
- Operating system: Windows
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
by Pout » Fri May 07, 2010 9:09 am
thomthom wrote:Al Hart wrote:Is there no way to delete a layer directly?
Not via the API, erase! method for layers and materials are missing. There's also missing methods to edit names etc... 
ThomThom, You can edit a layer name with layer.name= Although you will only see the changed layername when reopening the layer window. (I don't know however what is done with SU entities on that layer, since i only rename empty layers in my script)
-
Pout
-
- Posts: 272
- Joined: Thu Aug 21, 2008 10:46 am
by TIG » Fri May 07, 2010 9:17 am
Using layer.name="xxx" simply changes that layer name to "xxx" - entities etc aren't affected as they refer to the layer by its id not its name. A simple way to change a layer IF it exists is something like - Code: Select all
model=Sketchup.active_model model.layers["ABC"].name="XYZ" if model.layers["ABC"]
TIG
-

TIG
- Global Moderator
-
- Posts: 20380
- Joined: Mon Nov 12, 2007 7:24 pm
- Location: Northumbria UK
- Name: TIG
- Operating system: Windows
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
by Pout » Fri May 07, 2010 9:37 am
Great thx Tig for the extra explanation
Now if only this was possible for materials to (without having to duplicate the entities first)
BTW Tig, do you know if is possible to refresh the Layer window?
-
Pout
-
- Posts: 272
- Joined: Thu Aug 21, 2008 10:46 am
by Ad Machine » 5 minutes ago
-
Ad Machine
- Robot
-
- Posts: 2012
-
Return to Developers' Forum
|