I need to insert some geometry from a skp file.into the model
The API seems to have no way to do this except to
load the file as a component definition
Add an instance of the definition.
explode the instance.
This all works fine, but then I want to clean up and erase! the definition.
This produces the error "“Cannot determine parent of entity”.
Here’s some code:
mod = Sketchup.active_model # Open model
ent = mod.active_entities # All entities in model
dimfolder = "Plugins/2DXY_SiteSurvey/resources/"
dimfile = "dimft.skp"
filepath = Sketchup.find_support_file dimfile, dimfolder
#load the file as comp def
dimcompdef = mod.definitions.load filepath
# now add an instance
point = Geom::Point3d.new 0,0,0
transform = Geom::Transformation.new point
instance = ent.add_instance dimcompdef, transform
#now explode the instance
entarr = instance.explode
#need to delete the definition
status = dimcompdef.erase!
I actually don’t see a way to remove/delete/purge a specific definition! The erase! method is inherited from DrawingElement which is very odd for a definition! It doesn’t work at all, no matter whether the component is imported.
As John suggested, if you have full control over the model (= know that there are no unused definitions that the user wants to preserve), then you could just purge.
If you put your process inside a: mod.start_operation(... mod.commit_operation
block
AND within that you empty the definitions entities using: dimcompdef.entities.clear!
then it will erase just that one [unused] now ‘empty’ definition.
SketchUp’s Garbage-Collection tidies up for you - empty definitions are assumed ready for immediate deletion.
Using ‘mod.definitions.purge_all’ will remove ALL unused definitions - which is unfriendly to the user, since things totally unconnected to your process might also vanish, often unexpectedly…
No I should not purge unused definitions.
My understanding is that mod.entities.clear! will erase all entities?
Just to see what would happen, I tried it but got the same error message."
“cannot determine parent.”
TIG, I didn’t know this trick!
When you manually enter the last remaining component instance, select all entities and delete them and leave the instance, SketchUp will not only garbage-collect the empty instance, but also the definition.
However, when we do by script instance.entities.clear!, the definition will survive. It seems it depends on the start/commit operation.
mod.start_operation("Insert geometry from file")
definition = mod.definitions.load("some_file.skp")
instance_to_explode = mod.entities.add_instance(definition, IDENTITY)
instance_to_explode.explode
# In order to delete only this definition, we need to clear its geometry within an operation.
definition.entities.clear!
mod.commit_operation
instance_to_explode
>> #<Deleted Entity:0x10f800d8>
definition
>> #<Deleted Entity:0x10f800b0>
Don’t call erase! on a component definition, but on an instance. I don’t think the method is supposed to exist for definitions.
I think erasing all entities inside the component definition purges it automatically. However I know this crashes some older SU version so it may not be a good idea depending on what versions you are planning to support.