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.
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.
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
That part of the code is not between model.start/commit_operation.
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 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.