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.
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
Just a little warning:
Take a simple example, draw a rectangle and run this code from console:
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
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
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
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.
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 )
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.