Unexpected - Reference to deleted Group

Hi,

I am getting the error ‘Reference to deleted group’ when I run the following code (this is just a simplified extract). In a nutshell: I need to calculate the area of several polygons; to do this I create a face within a group, use the Face.area method and then delete the group. As you can see the group never gets deleted, nevertheless after a few successful iterations I receive the error.

nusselt = mod.entities.add_group
(1..nTilt).each do |i|
	(1..nAzimuth).each do |j|
		theta = Math::PI / 2.0 - (tiltMin + (i * 1.0 - 1.0) * dTilt)
		phi = azimuthMin + (j * 1.0 - 1.0) * dAzimuth
		pt1 = pt1(theta, phi)
		pt2 = pt2(theta, phi)
		pt3 = pt3(theta, phi)
		pt4 = pt4(theta, phi)
		projection =  nusselt.entities.add_face(pt1,pt2,pt3,pt4)
		if (projection != nil)
			view_factor[i-1][j-1] = projection.area / Math::PI
		else
			view_factor[i-1][j-1] = 0.0
		end
	end
end

I have tried several cases and i can’t find a pattern: sometimes it happens at the first iteration, sometimes at an apparently random point in the loop.

Any help or suggestion?

Thanks

If you’re group is empty, Sketchup cleans it up by deleting the empty group.
Alway check if the group is still valid and if it’s not create a new one.

nusselt = mod.entities.add_group
(1..nTilt).each do |i|
	(1..nAzimuth).each do |j|
		theta = Math::PI / 2.0 - (tiltMin + (i * 1.0 - 1.0) * dTilt)
		phi = azimuthMin + (j * 1.0 - 1.0) * dAzimuth
		pt1 = pt1(theta, phi)
		pt2 = pt2(theta, phi)
		pt3 = pt3(theta, phi)
		pt4 = pt4(theta, phi)
                nusselt = mod.entities.add_group if nusselt.deleted?
		projection =  nusselt.entities.add_face(pt1,pt2,pt3,pt4)
		if (projection != nil)
			view_factor[i-1][j-1] = projection.area / Math::PI
		else
			view_factor[i-1][j-1] = 0.0
		end
	end
end

Hi Thanks. Will check now. I just wonder how can a group ‘become’ empty after a few iterations. In my example some of the points will not generate a face and that is why I have the control.

It does work. Thanks

In my opinion this is a bug, most likely due to over-aggressive clean up inside Entities#add_face. nusselt holds an active reference to the Group, so this can’t be the Ruby garbage collector in action, it has to be the SketchUp API, and that method is the only API call you make. The fact that your code has not yet successfully added anything to the nusselt Group’s Entities collection should not make it a candidate for deletion. How does the API know that a later iteration of your loop will not succeed in adding a valid Face?

Do you have a sample snippet that is complete and ready to be run in the console?

Another thing: are you wrapping your code in model.start/commit_operation? If you are not then that could be the issue as you might be triggering SU’s cleanup functions by not marking the start and end of your operation.

Thanks for the replies. I have changed my code so that I don’t need to create a group. All the polygons I generate have 4 vertices. To calculate the area - my original goal - I simply use the classical formula from computational geometry. A bit slower maybe, but less headaches :smile:

That part of the code is not between model.start/commit_operation.

Could you quickly try that to see if it makes a difference?

Actually, I have just realized that I had a mod.start_operation at the beginning

Sketchup.active_model.start_operation("mesh",true

Hmm… then this is odd. Can you make a complete standalone example that reproduce this?

https://gist.github.com/Rojj/fbdfbbb2c416b9aabdc3

This piece of code works by itself. The fact is that now it is giving me a different error. I must have made some intermediate change and forgot to commit!

Thanks
Ruggiero

I get a duplicate points error when I paste that snippet into the console:

get_shadow_mask(30.0, 45.0)
Error: #<ArgumentError: Duplicate points in array>
<main>:47:in `add_face'
<main>:47:in `block (2 levels) in get_shadow_mask'
<main>:32:in `each'
<main>:32:in `block in get_shadow_mask'
<main>:31:in `each'
<main>:31:in `get_shadow_mask'
<main>:58:in `<main>'
SketchUp:1:in `eval'

Thanks for looking into this Thom. I have tried to recreate, but honestly I could not remember what was causing it. I think it was related to the order of the points. I realized that they were not sequential like the ones you see in the script that I posted.
From memory it was trying to create a face in this order and this caused the group to disappear because the face was invalid. But this is just from memory.

Thanks again