"Cannot determine parent of entity"

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

  1. load the file as a component definition
  2. Add an instance of the definition.
  3. 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!

mod.definitions.purge_unused

is what I use…

john

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…

2 Likes

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.

Sorry, I corrected a typo in my earlier post - it is [assuming the definition reference is definition]:

definition.entities.clear!

that’s needed to remove the definition within an operation block…

Seems to work. One of those eternal mysteries.
Thanks TIG. Only you seem to know where the bodies are buried.

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.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.