[code] ComponentDefinition-delete (another version!)

[code] ComponentDefinition-delete (another version!)

Postby AlexMozg » Tue Mar 31, 2009 11:16 pm

This script adds a new method for a Sketchup::ComponentDefinition class.
component_definition.delete
This is another version different from the version published previously.
The method is implemented for Sketchup versions 5-7.
In Sketchup version 7 provided the most complete functionality.
Method is faster, because the method "purge_unused" is not used.
Test and enjoy! :D

At the end of the file a small example of use.
_____________
TIG version: http://www.sketchucation.com/forums/scf/viewtopic.php?f=180&t=17962
0

AlexMozg 
PluginStore Author
PluginStore Author
 

Re: [Plugin] ComponentDefinition-delete (another version!)

Postby thomthom » Tue Mar 31, 2009 11:39 pm

Wow. Two days ago I was grumbling over the lack of such a method. Now they're all over the place. :D
:thumb:
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] ComponentDefinition-delete (another version!)

Postby TIG » Wed Apr 01, 2009 9:57 am

Alex

I first tried your method to erase the definition.instances and then erase all of the definition's entities, so the empty definition is gone from the browser.
This works manually, as SketchUp removes an empty definition from the component-browser.
However, when I used the ...entities_erase(...) method in a script it emptied the definition BUT it didn't remove it from the browser - if you then picked it in the browser = BugSplat !

I can''t see any difference between your version and my early attempt - only slightly different syntax, but same result - HOWEVER yours works ! Since yours is better than mine - not needing convoluted purging - I'll withdraw mine and adjust my related tools...

.
0
Last edited by TIG on Wed Apr 01, 2009 1:27 pm, edited 1 time in total.
TIG
User avatar
TIG 
Global Moderator
 

Re: [Plugin] ComponentDefinition-delete (another version!)

Postby Edson » Wed Apr 01, 2009 12:54 pm

guys, forgive my dumbness but what exactly does this script do????

i suspect i am not alone in this. so far the presentation and discussion of this and the previous similar script was so technical that i bet very few forum members understood what it is about.

thanks.
0
edson mahfuz, architect | porto alegre • brasil
http://www.mahfuz.arq.br
User avatar
Edson 
Global Moderator
 

Re: [Plugin] ComponentDefinition-delete (another version!)

Postby thomthom » Wed Apr 01, 2009 12:57 pm

It's meant for scripters and not for end users.
Maybe it should be tagged [code] instead?
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: [Plugin] ComponentDefinition-delete (another version!)

Postby Edson » Wed Apr 01, 2009 1:14 pm

thomthom wrote:It's meant for scripters and not for end users.
Maybe it should be tagged [code] instead?

it figures. and i agree about the tagging suggestion. thanks.
0
edson mahfuz, architect | porto alegre • brasil
http://www.mahfuz.arq.br
User avatar
Edson 
Global Moderator
 

Re: [code] ComponentDefinition-delete (another version!)

Postby TIG » Wed Apr 01, 2009 1:23 pm

Some 'plugins' are 'methods', which add extra functions onto existing tools.
I'll use [ code ] for mine - good idea...

While we have built-in things like "ComponentDefinition.entities" there is [was] no way of deleting a specific definition. This particular method "ComponentDefinition.delete" mimics what you can do in the component-browser's right-click context menu, and deletes a definition and its instances. It's main use is to tidy up in a script after you have made some temporary [and ultimately unused] definitions that would clog up the browser unless they are deleted. The built-in "Definitions.purge_unused" would do it, BUT that also removes other unrelated unused definitions that you might want later !

You could use it thus: select a component instance, then in the Ruby script (or console)...
Code: Select all
SketchUp.active_model.selection[0].definition.delete

[This finds the instance's definition and deletes it from the model data-base, and erases any other instances that were placed - please don't use it in the console except as an example since it's quicker to use the context-menu in the browser...]

If you read the notes at the start of such scripts they should clarify what they do...

This script - and for example its loosely related "ComponentInstance-add_entities.rb" [developed because in Ruby you could only 'add entities' to a ComponentDefinition before this was written] - get developed to sort out specific problems with other developers' scripts - however publishing here does allow others to see them - they might also have need of them or similar ideas...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [code] ComponentDefinition-delete (another version!)

Postby thomthom » Wed Apr 01, 2009 10:09 pm

I think this method should delete any existing instances belonging to the definition as it doesn't remove the definition when there's instances around.

EDIT: hmm... this is odd. When I run it through the console on a fresh model: Sketchup.active_model.definitions[0].delete it deletes Sang and removes it from the Component Browser.

But when I implement it into my working plugin, the component remains in the browser and will bugsplat if I try to place it... hm...
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] ComponentDefinition-delete (another version!)

Postby AlexMozg » Wed Apr 01, 2009 11:27 pm

thomthom wrote:But when I implement it into my working plugin, the component remains in the browser and will bugsplat if I try to place it... hm...

Therefore I foresaw additional possibility of the use of this method.
Use this method with the argument false, if you do not want to begin a new operation and correct forming "undo".
Essence consists in correct determination of start [ start_operation ]
and end [ commit_operation ] of operation!
It most important, bugsplat is otherwise possible!
Code: Select all
def my_new_plugin
   model = Sketchup.active_model
   model.start_operation "New Operation"
   definitions = model.definitions
#....................................
#....................................
#....................................
   definitions[0].delete(false) if definitions.to_a[0]
   definitions[1].delete(false) if definitions.to_a[1]
   definitions[2].delete(false) if definitions.to_a[2]
#....................................
#....................................
#....................................
   model.commit_operation
end


If I understood correctly...
----------------------
P.S
When I tested, bugsplat repeated oneself too often... :(
I maximally tried to foresee all possibilities of bugsplat appearance and prevent them.
----------------------
By the way, my example at the end of file "AT-ComponentDefinition-delete.rb" it is possible to delete or comment out if he will not be used.
0

AlexMozg 
PluginStore Author
PluginStore Author
 

Re: [code] ComponentDefinition-delete (another version!)

Postby thomthom » Thu Apr 02, 2009 7:27 am

I removed the example code. I also didn't use a start_operation on my own code.
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] ComponentDefinition-delete (another version!)

Postby AlexMozg » Thu Apr 02, 2009 7:41 am

Bugsplat also can be at the use of method of "erase!" between "start_operation" and "commit_operation".
0

AlexMozg 
PluginStore Author
PluginStore Author
 

Re: [code] ComponentDefinition-delete (another version!)

Postby thomthom » Thu Apr 02, 2009 7:42 am

Another thought: Not too sure if it's good that there's two differents methods available with the same name. What if one plugin implements one method and another plugin implements the other. Since the method integrates with the SU native class there's no way to avoid conflict.

Can we decide on one?
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] ComponentDefinition-delete (another version!)

Postby AlexMozg » Thu Apr 02, 2009 7:58 am

You can use a not method (ComponentDefinition.delete), but idea:
Code: Select all
definition.entities.erase_entities definition.entities.to_a

between
model.start_operation ("")
and
model.commit_operation

Sometimes so simpler...
0

AlexMozg 
PluginStore Author
PluginStore Author
 

Re: [code] ComponentDefinition-delete (another version!)

Postby thomthom » Thu Apr 02, 2009 8:30 am

AlexMozg wrote:You can use a not method (ComponentDefinition.delete), but idea:
Code: Select all
definition.entities.erase_entities definition.entities.to_a

between
model.start_operation ("")
and
model.commit_operation

Sometimes so simpler...

So that's the core of it? The commit operation initiates SU to purge the empty definition? I couldn't really work out how your code worked. So I suppose that something in my code interferes with this?
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] ComponentDefinition-delete (another version!)

Postby TIG » Thu Apr 02, 2009 9:39 am

thomthom wrote:Another thought: Not too sure if it's good that there's two different methods available with the same name. What if one plugin implements one method and another plugin implements the other. Since the method integrates with the SU native class there's no way to avoid conflict.

Can we decide on one?

I'll remove mine from the forum now...
As AlexM points out using the:
Code: Select all
model.start_operation("Delete Definition")
  definition.entities.erase_entities(definition.entities.to_a)
model.commit_operation

it has been discovered how to do what we want anyway without a new method at all...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [code] ComponentDefinition-delete (another version!)

Postby Frankn » Fri Feb 14, 2014 5:20 am

TIG wrote:it has been discovered how to do what we want anyway without a new method at all...


Sorry to bring back a very old post/topic but I'm trying to delete definitions and I tried using the code AlexMozg wrote but I'm having problems with it and to be honest I don't really understand how the code works so I'm not sure what's going wrong.

The error I get is... Error: #<TypeError: reference to deleted ComponentDefinition>

Is there another way to do this if not what should I be looking for in regards to the problem I'm having?

Thanks.
0

Frankn 
 

Re: [code] ComponentDefinition-delete (another version!)

Postby TIG » Fri Feb 14, 2014 1:21 pm

If you want to delete ALL definitions in a model [use with care!]
A one-liner like:
Code: Select all
m=Sketchup.active_model;m.start_operation('!');m.definitions.each{|d|d.entities.clear!};m.commit_operation
does it.

BUT if you want to delete just the definitions of Selected component-instances [and of course the instances will vanish too] use:
Code: Select all
m=Sketchup.active_model;m.start_operation('!');ds=[];m.selection.grep(Sketchup::ComponentInstance).each{|i|ds<<i.definition};ds.uniq.each{|d|d.entities.clear!};m.commit_operation
this way you make an array of all affected definitions and process those just once each - otherwise, if you have two instances selected of the same definition, then the second instance processed has an invalid 'deleted definition' so it will fail etc...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [code] ComponentDefinition-delete (another version!)

Postby Frankn » Sat Feb 15, 2014 4:25 am

Thanks TIG,

The second code is what I'm looking to do but I'm getting this error...
Error: #<NoMethodError: undefined method `confirm_operation' for #<Sketchup::Model:0x8e9a4b4>>

Also I tried selecting the definition I want to delete with this but it doesn't seem to work
Code: Select all
@model.selection.add(#<Sketchup::ComponentDefinition:0xbcd77a0>)


I forgot to mention that if I manually select the instance it does delete it and the parent from the components viewer but leaves the subcomponents.

I'm still trying to figure out what your code does, this is a little over my pay grade! :?
0

Frankn 
 

Re: [code] ComponentDefinition-delete (another version!)

Postby Frankn » Sat Feb 15, 2014 5:51 am

Ok so after a beer break I figured out why I couldn't select the instance... I was trying to select the definition! :oops:

When I comment out m.confirm_operation everything seems to work but obviously there's something wrong with the code or I wouldn't need to comment it out.
0

Frankn 
 

Re: [code] ComponentDefinition-delete (another version!)

Postby Frankn » Sat Feb 15, 2014 7:48 am

Here's something interesting/weird that's happening...

After I delete the definition I iterate through model.definitions just to make sure everything was getting deleted but I found that the parent instance isn't getting deleted and the names of the subcomponents aren't getting deleted either... which causes the new component being created to be renamed with #1 at the end...

Any idea why?
0

Frankn 
 

Re: [code] ComponentDefinition-delete (another version!)

Postby TIG » Sat Feb 15, 2014 12:29 pm

It's 'commit_operation' NOT 'confirm_operation' - my typo :oops: - it's now corrected in my earlier code...
You can manually select objects including instances, and just the instances' definitions will be processed.
You can only add 'entities' to a Selection [like geometry or instances] in code.
NOT a 'definition' - which is NOT an 'entity'... :?
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [code] ComponentDefinition-delete (another version!)

Postby Frankn » Sat Feb 15, 2014 5:08 pm

TIG wrote:It's 'commit_operation' NOT 'confirm_operation' - my typo :oops: - it's now corrected in my earlier code...
You can manually select objects including instances, and just the instances' definitions will be processed.
You can only add 'entities' to a Selection [like geometry or instances] in code.
NOT a 'definition' - which is NOT an 'entity'... :?


Thanks you sir! That works perfectly! I should of caught that but I just figured it was a command I didn't know of yet! :oops: :D
0

Frankn 
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Developers' Forum

Who is online

Users browsing this forum: AmirBilu and 15 guests