I thought I would test out ChatGPT and see if could help me find some basic algorithms using the Ruby SketchUp API.
I asked it to help me find an edge in the root of the model, its response:
*✅ Here's the Fix:*
*Before selecting or highlighting an edge, check that its parent is the model’s entities collection, not a group or component instance.*
*Update your picking logic like this:*
def onMouseMove(flags, x, y, view)
@ip.pick(view, x, y)
entity = @ip.edge
if entity && entity.is_a?(Sketchup::Edge) && entity.parent == Sketchup.active_model.entities
@hover_edge = entity
else
@hover_edge = nil
end
view.invalidate
end
I then pointed out it was incorrect, it then responded:
So I’ll admit — you’re correct, and my previous suggestion that .parent == entities was misleading. That’s only true for container-like things (like faces inside a group), not for standalone geometry like Edge`.
It gets quite a few things wrong but it can help, just a word to the wise.
It can help cobble together some basic stuff, but I am finding even on simple syntax it will occasionally get some things wrong. I a less likely to trust it now, give it a few more months I guess and it will be smarter than me.
For those who are savvy with creating tools and use the draw method I do have a question which after all this time programming extensions I still do not have a good handle on.
In my draw method below I am essentially highlighting a bunch of edges in the root of the model which I have previously selected however I don’t understand why it does not display or draw them unless I am hovering over a valid edge. I’m a bit stumped by this, my very simple code is below:
def draw(view)
puts "Draw called"
@drawn = false
# Show the current input point
if( @ip.valid? && @ip.display? )
@ip.draw(view)
@drawn = true
end
# Highlight hover edge in green if not selected or red if already selected
if @hover_edge
if @edges.include?(@hover_edge)
view.drawing_color = 'red'
else
view.drawing_color = 'green'
end
view.line_width = 5
view.draw(GL_LINE_STRIP, edge_to_points(@hover_edge))
end
# Highlight selected edges in blue
view.drawing_color = 'blue'
@edges.each do |edge|
view.draw(GL_LINE_STRIP, edge_to_points(edge))
end
end
The related methods in the tool are the following:
def onMouseMove(flags, x, y, view)
self.set_current_point(x, y, view)
end
def set_current_point(x, y, view)
if (!@ip.pick(view, x, y, @ip1))
return false
end
need_draw = true
# Set the tooltip that will be displayed
view.tooltip = @ip.tooltip
# Compute points
case @state
when STATE_PICK
@ip.pick(view, x, y)
entity = @ip.edge
if entity && entity.is_a?(Sketchup::Edge)
if entity.parent == Sketchup.active_model
@hover_edge = entity
else
puts "edge parent is: #{entity.parent}, not in the root of the model"
@hover_edge = nil
end
else
@hover_edge = nil
end
need_draw = @ip.display? || @drawn
end
view.invalidate if need_draw
end
def onLButtonDown(flags, x, y, view)
@ip.pick(view, x, y)
edge = @ip.edge
return unless edge && edge.is_a?(Sketchup::Edge) && edge.parent == Sketchup.active_model
if @edges.include?(edge)
@edges.delete(edge)
else
@edges << edge
end
# view.invalidate
end
def edge_to_points(edge)
[edge.start.position, edge.end.position]
end
I would like the selected edges to still be drawn (ie. thick blue line) even when I am not mousing over a specific edge. However as it currently stands as soon as I move my mouse off of an edge the selected edges do not get drawn. I must be missing something obvious here.
Whenever I go to chatgpt to whip up a random tool, I always preface the convo with the link to the sketchup api website. Does help a bit. I’ve made like six random tools specific to my needs, a lot of trial and error lol.
I’ve spent all afternoon trying to make this work and now I think I’ve even stumped ChatGPT. I will say though I’ve learned quite a bit from ChatGPT as it tries to debug my code, this thing is really quite good overall.
Now that you mention it, there was an issue where SketchUp was not resetting draw color, line widths and stipple which was affecting things outside the tool’s scope. I believe SU was updated to always reset to black, 1 pixel width, solid stipple when the draw() method returns.
Looking at your first snippet the line width setting is within a if conditional that only sets for red or green.
I’ve found that MS Copilot AI is better at finding errors than it is at creating solutions.
I can’t believe I missed this and ChatGPT couldn’t seem to suss it out either, but it did help me tighten up other parts of my code and give me a number of ideas that I probably would not of had.
When I’m coding I find that it is easy to get going down one path and its like I have blinders on. So using ChatGPT or some other AI to throw different ideas at me is actually quite helpful.
When I really get stuck though there is nothing better than some really knowledgeable eyes looking at it, for that reason I am always grateful to this forum and the individuals who are gracious enough to help out.