Why this error and how to correct it?

Dear friends,
I have following code…

grp.entities.grep(Sketchup::Edge) do |e|
    e.erase! if e.faces.first.normal == e.faces.last.normal
end

I receive this error for it…
Error: #<NoMethodError: undefined method normal' for nil:NilClass> C:/Users/Majid PC/AppData/Roaming/SketchUp/SketchUp 2020/SketchUp/Plugins/MAJ_Stair/MAJ_Stair_Data.rb:593:in block in make_stair’
Logically edge has faces or not if yes faces have normal and this error should not ever happen. Please help me to know why it happen. Also I wish to use this error. So I want whenever this error happen I do something and codes continue working. Your help will be highly appreciated.

Error when the edge has no face or only one face

1 Like

For example you can check if there is a valid face. If not: jump to next iteration

grp.entities.grep(Sketchup::Edge) do |e|
    next unless e.faces.first.valid?
    next unless e.faces.last.valid?
    e.erase! if e.faces.first.normal == e.faces.last.normal
end

or

grp.entities.grep(Sketchup::Edge) do |e|
    if e.faces.first.valid? && e.faces.last.valid?
       e.erase! if e.faces.first.normal == e.faces.last.normal
    end
end
2 Likes

Just a little warning:
Take a simple example, draw a rectangle and run this code from console:
image

grp = Sketchup.active_model
grp.entities.grep(Sketchup::Edge) do |e|
  p e.faces
    e.erase! if e.faces.first.normal == e.faces.last.normal
end

image

You can see the e.faces give you an Array with a faces.
In the first iteration there will be one face, so the first element and the last element of array will be same. Therefore the normal’s will be the same, so the edge will be deleted. The face will be deleted too!!

In the next iteration you will get an empty array: []
The first elemet in this array will be nil as well as the last. The nil class does not have a method normal’` as you see the error message.

You can see my complete code in following. First I delete edges with one face, then delete faces without edge and delete edges with 3 faces. So all of my faces have 2 faces but this error happen.

          grp.entities.grep(Sketchup::Edge) do |e|
            e.erase! if e.faces.size == 1
          end
          grp.entities.grep(Sketchup::Edge) do |e|
            e.erase! if e.faces.size == 0
          end
          grp.entities.grep(Sketchup::Edge) do |e|
            e.erase! if e.faces.size == 3
          end
          grp.entities.grep(Sketchup::Edge) do |e|
            e.erase! if e.faces.first.normal == e.faces.last.normal
          end

Now I have following error.
Error: #<NoMethodError: undefined method `valid?’ for nil:NilClass>

Following code works but I really don’t know why this problem happen.

if e.faces.first != nil && e.faces.last != nil
   if e.faces.first && e.faces.last && e.faces.first.valid? && e.faces.last.valid?
       e.erase! if e.faces.first.normal == e.faces.last.normal
    end
1 Like

Can be a situation like here. When you are deleting the edge where faces.size == 3
image

will result an orphan edges
image

1 Like

You are great. Thank you so much.

1 Like

I write following codes but problem happen again.

            grp.entities.grep(Sketchup::Edge) do |e|
              e.erase! if e.faces.size  == 1
            end
            grp.entities.grep(Sketchup::Edge) do |e|
              e.erase! if e.faces.size > 2
            end
            grp.entities.grep(Sketchup::Edge) do |e|
              e.erase! if e.faces.size == 0
            end
            grp.entities.grep(Sketchup::Edge) do |e|
              if e.faces.first != nil && e.faces.last != nil
#              if e.faces.first.valid? && e.faces.last.valid?
                e.erase! if e.faces.first.normal == e.faces.last.normal
              else
                t2 = 1
              end
            end

t2 = 1 at the end.

This means at least once there were no faces in the iteration.

Did you get the same error message? Or what do you mean by " problem happen again."?

My problem solved by using your last code. I just confuse why this problem happen. I deleted edges that have one face, then edges that have more than 2 faces then edges that have no faces so in remain edges both faces should be valid but it is not!!! again after delete edges with more than 2 edges I deleted faces with one edges but problem is same as before.

Are you sure if t2 were set to “1” by your last “trial” not the previous one?
What do you see in console if you change:
t2 = 1
to
puts " no faces"

Yes I am sure. Shape is complex I can send you shape if you are interest. My Code…

            grp.entities.grep(Sketchup::Edge) do |e|
              e.erase! if e.faces.size > 2
            end
            grp.entities.grep(Sketchup::Edge) do |e|
              e.erase! if e.faces.size == 1
            end
            grp.entities.grep(Sketchup::Edge) do |e|
              e.erase! if e.faces.size == 0
            end
            grp.entities.grep(Sketchup::Edge) do |e|
              if e.faces.first && e.faces.last
                e.erase! if e.faces.first.normal == e.faces.last.normal
              else
                t2 = 1
              end
            end
            grp.entities.grep(Sketchup::Edge) do |e|
              if e.faces.first && e.faces.last
                e.erase! if e.faces.first.normal == e.faces.last.normal.reverse
              else
                t2 = 1  
              end  
            end         

I use t2 to terminate extension forgive me not to change it.

Yes, I may be interested … but I don’t know exactly if I will have time for it today or tomorrow …to examine.
Just drop the group in question saved as separated .skp here in a forum (if not super secret :wink: )

Nothing secret for you. Let me send you PM and short video.


if I don’t delete faces shape will be like following picture.

Also SKP file.Test Villa2.skp (10.7 MB)

You told me above that “problem is same as before.” Which is an error message.

But I see now, you have an other problem. You do not have error message, but deleted geometry. It is not the same. Absolutely different one.

1 Like

Sorry for misunderstanding. As I told you by using your last code my problem solved (Error message) but I just confuse why this problem happen (deleted geometry). It is not my problem now and I really don’t want waste your time. Thanks again Dezmo.