This is a follow up question to the recent question I had regarding converting components to groups.
Once I’ve successfully loaded the component and then inserted it into my container group and exploded it I no longer have use for the component definition and would like to completely remove it from the model.
I’ve tried to utilize both purge_unused and remove to accomplish this but both of these methods throws errors. I am basically passing these methods the original component definition that I loaded (after exploding the instance).
This feature is only available for SketchUp 2018. For users of older versions, you better hope (or remind) the user to purge the model from time to time. If you purge unused definitions through a script you would also purge those that are not related to your extension and that the user might want to keep.
That’s what I was afraid of, I guess this is not going to work.
So in previous versions of SketchUp (2015, 2016 and 2017) there is no way to remove a specific component definition from the model other than to purge all unused components which as you suggest I would rather not do.
Correct. The kludgy workaround to prevent purging other definitions was to create a temp group and insert an instance of the other unused definitions - then perform a purge. (Not - pretty, I know. The #remove method should have been added a long time ago.)
And if Ruby code still has a reference to the definition after removing its contained entities? Does it immediately become an invalid reference (#<DeletedEntity>) to which you cannot add entities anymore, or does SketchUp purge it as soon as there is no reference any more?
I think you have to pass an Array to erase_entities.
Also I changed the name data_compdef to a more generic name, and would recommend to have this extracted to a generic erase_definition method. Extracting things to separate methods with celar names, even if it’s just one line of code, can make it much easier to read, understand and maintain the code in the future.
On a tangential note, erasing groups or components with entities.clear!
is (for some mysterious reason) much, much faster on heavy geometry than using group.erase!
Group.erase! may be shared with Edge.erase! and Face.erase! and do totally unnecessary checks to see if the entity in question was binding other entities that needs to be erased too, I don’t know. That should however be implemented at a deeper level so it’s not super likely.
I tried to reproduce this but failed, but I know I have encountered a huge difference between them many times. Nevertheless, I think the difference (when it’s there…) may be somehow related to another thing which is easy to reproduce.
Below is a test script with two runs that differs by a time factor factor of >20 (skp2018/windows).
A lot of geometry is produced in 10 groups and they are then erased. If the whole operation is enclosed within a single start/commit it takes about a minute. If a new start operation is inserted before we erase the groups (start/start/commit) the operation takes 2 seconds. This is exactly the same “pull the plug” behavior I’ve encountered by using entities.clear! instead of group.erase! (even though it doesn’t make a difference in this particular script).
That’s an interesting one. I see the difference you describe:
----------------
Second start used: false
Create Geometry: 1.897473 s
Erase Geometry: 42.885206 s
----------------
Second start used: true
Create Geometry: 1.90262 s
Erase Geometry: 1.819289 s
for the record, this is even slower on my mac running SU v19…
Second start used: false
Create Geometry: 3.178602 s
Erase Geometry: 138.855325 s
----------------
Second start used: true
Create Geometry: 3.083154 s
Erase Geometry: 3.538786 s