Okay, then...I tried using .extend, but my overall problem that prompted my restructuring of the class/subclass/module, etc., is the misbehaving of onEraseEntity() in the observer.
Here's what my class looks like now (note that my "all_gcode_edges" workaround is still there, will change it to a class variable if I can fix my onEraseEntity problem):
module Gcode
@@all_gcode_edges = []
def gcode?
return @@all_gcode_edges.include?(self)
end
def to_gcode!
@@all_gcode_edges << self unless @@all_gcode_edges.include?(self)
@observer = self.add_observer(GcodeEdgeObserver.new)
#Other stuff
end
# lots of other methods
end
And currently, this is how my unit tests are building edges, although I'll find a way to make this a single line later:
p1 = Geom;;Point3d.new(0,0,0)
p2 = Geom;;Point3d.new(0,1,0)
e1 = Sketchup.active_model.entities.add_edges(p1,p2)[0]
e1.extend(Gcode)
e1.to_gcode!
Say I have 3 gcode edges in a row, coupled together in doubly linked list form (each edge has a @previous_edge and a @next_edge class variable that stores a reference to the edge preceding or following it). The middle one, which is being observed, is erased by the user.
In onEraseEntity(), I need to tell the other two, non-deleted edges "The edge that you were paired with is gone, decouple from it". But, I can't; when the entity is in onEraseEntity, it has lost access its module class functions.
module Gcode
class GcodeEdgeObserver < Sketchup;;EntityObserver
def onEraseEntity(entity)
entity.vertices.each do |v|
c = entity.coupled_edge[v]
c.decouple_vertex(v)
end
end
end
end
On deletion, this throws:
Error; #<NoMethodError; undefined method `coupled_edge' for #<Sketchup;;Edge;0xa16f364>>
C;/Users/.../Plugins/Toolpath_Builder/gcode_edge_observer.rb;5;in `onEraseEntity'
The coupled_edge method normally returns the edge coupled at a given vertex, and it works perfectly right up to the point of erasure, then when erasure occurs, the erased Gcode edge no longer has any Gcode functions; it has lost the Gcode mixin at some point prior to being passed into onEraseEntity(). Is there a way for me to trigger a cleanup method as it's being erased?
If not, my only real option is to trigger a brute force validation for ALL gcode edges by storing references to all of them in a class variable Array. One of them gets deleted, all of them check and validate their coupled edges. Needless to say, this isn't exactly optimal, especially later, when I do things like break down b-splines into low-error arcs that could number in the hundreds or thousands.
(For clarity, the reason I'm using linked list style references rather than just using the vertex is because multiple toolpaths might converge on the same vertex; A cnc bit will descend to a certain Z-plane, cut around the part to the same point, then descend again. The toolpath must have an unambiguous path and can't get confused if it gets to a four-way intersection.)