[Martin Rinehart's Donut Tutorial] TypeError with Deleted Entity

Hi there,
i have a problem with an Square Tutorial donut, what not will work, becouse of an reference to deleted error
who can help me to solve this problem…?
hope anyone can help me…
i am user of Sketchup pro 2017 and my OS is windows 10
René Remmelzwaal
The Netherlands

donut._.rb (5.9 KB)

@rene.remmelzwaal Because of the length of the code, it would be better if you attach the script instead of pasting it into a message.

Use this button to attach a file:

thanks for the tip, already done!

This is not the error I get:

donut(100,200,25,25)

results in this output:

Error: #<ArgumentError: Cannot convert argument to Sketchup::Point3d>
<main>:104:in `add_face'
<main>:104:in `add_face_group'
<main>:176:in `draw'
<main>:18:in `draw'
<main>:210:in `donut'
<main>:in `<main>'
SketchUp:1:in `eval'

Rene, you should tell us what YOU DID to get the error.

And what was the error’s line number ?
See that the backtrace list in the report I give, tells us that the error occurred in line 104, making a call to add_face().


Where did this example come from?
The indentation in this script is terrible! (It is very hard to read as a result.)

here you see some screen shots from the results of the donut function.
this result you get, when you disable line nr. 29
the error is in that line!!
the problem is that the inner_face will not deleted, so the result from the square donut is oké as you can see at the images i made. but the square hole has a bottom… because this was not erased.
the donut function start as followed : donut [0,0,0],[0,50,50],20,5

the error you get , when you enable line 29 !

the tutorial is not an tutorial, it is an part of an complete basic lesson from Edges to Rubies - The Complete SketchUp Tutorial
i love this lessons, because i am an beginner of Sketchup in combination with Ruby programing .
the attached script i made all the definitions and classes needed for the function “donut” together.

hope you can fix the TypeError "reference to deleted Drawingelement
thanks

hope its clear for you.

donut._.rb (6.1 KB)

The explode of groups was changed in SU2017.
You can NO LONGER hold references AFTER an explode.

So, move line 29 before the previous line where the group is exploded, ie like:

		inner_rec.face().erase!
		inner_rec.group().explode()

After the explode, inner_rec will be invalid.

same problem my friend…
i turned on the line 29 as you can see in the script.
the square donut is there , but the hole is not thorough ! see the images for the results of the square donut.
no square hole the inner_face is still there., as i told you before!
it has to do with older version of Sketchup, where this script work properly.
in this case with Sketchup pro 2017… the script works , but with NO hole (inner_face is still there!)

hope you have an other idea for this problem…?
thanks

Your code seems far more convoluted than it needs to be.
The normal way to draw a ‘donut’ is:

1 in a new group.entities add the inner ‘hole’ face from specified points
2 in that group then add the outer ring face as a outer loop only - its points need to be on the same plane as the hole, it will then form a proper boundary with the hole face
3 erase the inner hole with hole.erase! - this will leave just the ring face
4 then use face = group.entities.grep(Sketchup::Face)[0] - there’s only one face in the group
5 check the face.normal vector suits the direction you want to extrude it - if not then face.reverse!
6 finally face.pushpull(@thickness)

looks great your solution, but for me its difficult to change the script,
can you fix this for me…?
see the script as attachment
thanks
we are tolking about an existing script you can load in Sketchup program
with the command as example donut [0,0,0],[0,50,50],20,8 can you creat your square donut.

donut._.rb (6.2 KB)

Try this - there’s no reverse, but it’s not needed unless the rect.normal==Z_AXIS.reverse && rpts[0].z==0

=begin
Usage: Type in Ruby Console:
Remmelzwaal::Donut.new(near, far, pushpull, thickness)
e.g. Remmelzwaal::Donut.new([0,0,0], [50,0,50], 20, 10)
=end
module Remmelzwaal
	module Donut
		def self.new(near, far, pushpull, thickness)
			r = 0
			g = 1
			b = 2
			if near[r] == far[r]
				plane = 'gb'
			elsif near[g] == far[g]
				plane = 'rb'
			elsif near[b] == far[b]
				plane = 'rg'
			else
				plane = 'no'
			end
			rpts = []
			rpts[0] = near
			rpts[2] = far
			if plane == 'rg' || plane == 'no'
				rpts[1] = [near[r], far[g], far[b]]
				rpts[3] = [far[r], near[g], near[b]]
			elsif plane == 'gb'
				rpts[1] = [near[r], near[g], far[b]]
				rpts[3] = [near[r], far[g], near[b]]
			else # plane == 'rb'
				rpts[1] = [near[r], near[g], far[b]]
				rpts[3] = [far[r], near[g], near[b]]
			end
			vecs = []
			vecs[0] = rpts[0].vector_to(rpts[2])
			vecs[1] = rpts[1].vector_to(rpts[3])
			vecs[0].length = thickness * Math.sqrt(2)
			vecs[1].length = thickness * Math.sqrt(2)
			vecs[2] = vecs[0].reverse
			vecs[3] = vecs[1].reverse
			###
			hpts = []
			vecs.each_with_index{|v, i| hpts[i] = rpts[i].offset(v) }
			###
			model = Sketchup.active_model
			group = model.active_entities.add_group()
			ents = group.entities
			###
			model.start_operation(self.to_s, true)
			###
			hole = ents.add_face(hpts) #hole face
			rect = ents.add_face(rpts) #rectangle face
			###
			hole.erase!
			###
			rect.pushpull(pushpull)
			###
			model.commit_operation
			###
		end
	end #module
end #module

Apparently this is code from Martin Rinehart’s “Edges to Rubies” series.

It is quite old and doesn’t use operations.
It also lacks the commentary that you’d expect in a tutorial script.

It has annoying outdented comments, and strange indentation. (As I recall he was a proponent of forcing everyone at SCF and those writing code for SketchUp, to use the same indentation in all languages. Ie, Python, Java, JavaScript, HTML, XML, Ruby, etc. There was a big long argument topic at SCF over this. So he basically choose his own indentation, rejecting the Ruby norm, and posted his tutorials at his own website.)

If there are going to be these kinds of basic errors in his tutorials, it is tempting me to remove the link to them from the Ruby Learning Resources wikilist.

So you can not help me , do you discourage the use of Martin Rinehart’s “Edges to Rubies” …?
not all the tutorials are bad.
i am looking for a good book about Sketchup in combination with Ruby. what book do you recommend.
i shall looking the side’s through"Ruby Learning Resources".
thanks anyway for your support.::thumbsup:

1 Like

i don’t know if i try to use your recommondation…? i think i start at the beginning.
thanks anyway.

[quote=“rene.remmelzwaal, post:13, topic:40499”]
i don’t know if i try to use your recommondation…?
[/quote] My working example was simply offered to shos how you could write a simple module and method to do what you wanted, with far less convolution.
I also think it shows the logical steps needed to get the 4 corners and inset 4 corners needed, then make a group and add the hole-face and the main-face [making them in that order is important, then erase the hole-face to leave a ‘ring’ and then pushpull it into a 3d donut…
PS: In my example code I have added an operation ‘block’ so it’s one step undo-able…

1 Like

yes i am busy to try to understand the way you made that script
for me all is new… so i need help from you.
I assume your script separate from that uploaded script…?

see image… near…??
my Ruby editor says : uninitialized constant AS_RubyEditor::RubyEditor::Remmelzwaal
do i miss something…?
i hear from you, meanwhile i study your script

The “e.g.” part shows how to use it, passing four arguments
The line before that is simply explaining what each of those means…
You only need the e.g. part !
Unless you have previously defined some references for near, far, pushpull, thickness - then it will fail when you try to use the first line !

And of course you do need to load the code that creates Remmelzwaal::Donut etc BEFORE trying to run it.

This error is from the Ruby Editor you are using. I don’t recommend using it because of errors like this. It appears friendly at first but causes problems when things become even slightly complex. These errors that are not directly due to your own mistakes are not helping you learn Ruby for SketchUp.

Write your code in Notepad++ then switch to SketchUp and load to test.

oh i see what you mean… first i load the script in Sketchup, then i type in Ruby console Remmelzwaal::Donut.new(near, far, pushpull, thickness), then i type in the function donut… and yes i got error see image
oops i forget to defined near, far and the rest…
i do my best… but after several attempts… I can not do define near or far…?
how do you do that…?
i thought defining near is … near = and far = . but noway…?!

I’m sorry, but I just do not have the time to fix his (or other people’s) tutorials. (It is something the author should do.)

As TIG said, they are convoluted for new programmers. They don’t teach basic Ruby, they teach advanced library class programming.

1 Like

i understand i go further to study the basic’s of Ruby’s library.
thanks