Failed to add_face from points in a group

I am importing a model from the data exported it previously. It encountered the issue:

TypeError: reference to deleted Group

Traced to the code:

face = group.entities.add_face(pts)

The face had not been created. Even the group had been deleted!

There were other faces created successfully in the group. I was wondering why the face failed to build?

And, I can create the face with the same points in Ruby console like this:

Sketchup.active_model.add_face(pts)

So, I think there may be conflict to create the face in the group. What possible reasons could due to the issue?

If you create the group too soon the garbage-collection sees it’s empty and throws it away !
If you have to make it early straight away add a temporary cpoint to it so it’s no longer empty, you can erase the cpoint after adding something else…

Otherwise make the group just before you need to add things to its entities…

1 Like

Thank you very much! How to add a temporary cpoint to a group? I am newbie to SU.

guide_pt = group.entities.add_cpoint(ORIGIN)

… and then later …

guide_pt.erase!

Thank you Dan. I have added add_cpoint. My code is below:

def createGroup(groupData, modelData, model, entities)
  group = entities.add_group
  guide_pt = group.entities.add_cpoint(ORIGIN)
  group.transformation = groupData.transformation.toSUTransformation
  definitionId = groupData.definition
  definition = getComponentDefinition(definitionId, modelData)
  definition.entities.each do |entData|
    if(entData != nil)
      createEntity(entData, modelData, model, group.entities);
    end
  end
  group.name = groupData.name
  return group
end

But the issue is still the same “TypeError: reference to deleted Group” for the line:

createEntity(entData, modelData, model, group.entities);

My plugin is ok for importing small models. The issue only occurs when importing complex models.

There are many things in your code sample that are not known to us without seeing more, so we cannot run it to test it and must guess what they are just to analyze the problem.

For example:

  • groupData, modelData, model, entities arguments all came from the caller - they could in principle be anything. It seems reasonable to assume that model is the active model of the SketchUp session and that entities is some Sketchup::Entities collection, but we have no clue what groupData or modelData might be.
  • Geom::Transformation has no method #toSUTransformation, so we must assume that groupData.transformation returns some other kind of object that provides this method to convert to a Geom::Transformation
  • Not knowing what groupData is, we can’t be sure what groupData.definition returns and why you called it definitionId, though that suggests it is something other than a Sketchup::ComponentDefinition. The next point reinforces this one.
  • getComponentDefinition must have been defined somewhere else. It seems to imply that modelData is some object in which you gathered object info using some form of Id.
  • createEntity must have been defined somewhere else. That leaves the possibility that it is raising the Exception rather than the Sketchup Ruby API. In any case, we don’t know what it does.

But, all that said, my stab-in-the-dark guess is that createEntity somehow manipulates its group argument in a way that can cause the Ruby API to delete it and then a later pass of the each loop tries to pass its entities again. You could add some periodic checks of group.valid? to try to detect this.

Just a quick note that Ruby statements do not need to end in semi-colon, nor do if statements need their boolean expression wrapped within parenthesis.

Thank you very much! I am debugging it. The group was deleted after group.entities.add_face(pts). I checked the code:

face = group.entities.add_face(pts)

Before the line, group.deleted? was false. After it for a specific pts (points), face was nil, and group.deleted? is true.

Then, I added code like this:

face = group.entities.add_face(pts)
if face == nil
  tmpEntities = Sketchup.active_model.entities
  tmpEntities.add_face(pts)
end

The face was created successfully outside the group. It was weird that add_face did not work in the group. How can I track down its reason? Thanks.

I assume the failing statement is inside createEntity, as it wasn’t in the code you shared?

Does it fail every time, or only for certain pts? I’d look closely to see what is in one that fails.