Creating a Custom Line Tool
-
Hi,
I just had an idea. I think it should be possible, but there is no official documentation:
class Custom_Line_Tool < Sketchup::SketchTool
def initialize
super
enddef onLButtonUP(flags, x, y, view)
do whatever special stuff you want...
end
end
This would get us to a place where we can use the native sketchup tool stuff (inference locking, line tool image, use of crosshairs if that is what the user wanted, native use of the VCB...) We could then have the tool do custom things, and treat it as a custom tool. However, 'Sketchup::SketchTool' doesn't return anything useful. Any other ideas of things to try?
We'd also need to know how native sketchup names the input point, and probably a few other things. I do have a custom line tool working, but the original is just more elegant.
--
Karen -
That would have been great - but unfortunately not possible. They are implemented in C and there is no documentation nor externally exposed.
But the line tool isn't that complicated. It's mostly just the InputPoints and inference locking you need.
-
Ah, too bad. Of course I want a custom move tool and rectangle tool as well. I do have a line tool working with inference locking and and the VCB activated.
well, for future, I'll post my line tool:
class Custom_Line
def initialize
@cursor = UI.create_cursor("Cursor.tiff",1,1)
end # def initializedef activate
$ip = Sketchup::InputPoint.new
Sketchup.active_model.selection.clear
@ip1 = nil
enddef onSetCursor
UI.set_cursor(@cursor)
enddef onMouseMove(flags, x, y, view)
$ip.pick(view, x, y)
view.invalidate
enddef draw(view)
$ip.draw(view)
if(@pt1 == nil)
view = view.draw_line $ip.position, $ip.position
else
Sketchup.vcb_label = @pt1.distance($ip.position).to_s
view = view.draw_line @pt1, $ip.position #draws line from last point to current position
view.invalidate
end
enddef onKeyUp(key, repeat, flags, view)
if(key==16)
view.lock_inference
end
enddef enableVCB?
return true
enddef onUserText(text, view)
begin
if(@pt1 != nil)
value = text.to_l # convert the value to a length
point2 = [$ip.position]
dist = @pt1.distance(point2)
w2 = value/dist
w1 = 1-w2
@pt2 = Geom::Point3d.linear_combination w1, @pt1, w2, point2
line = Sketchup.active_model.entities.add_edges @pt1, @pt2 # RIGHT NOW, JUST ADDS TO THE BASE LEVEL
line[0].set_attribute "EPdict", "Tool", "Node/Branch"
@pt1 = @pt2
Sketchup.vcb_value = ''
end
rescue
# Error parsing the text
UI.beep
value = nil
end
enddef onKeyDown(key, repeat, flags, view)
if(key==16&&@pt1!=nil)
ip1 = Sketchup::InputPoint.new(@pt1)
@pt2 = $ip.position
ip2 = Sketchup::InputPoint.new(@pt2)
view.lock_inference(ip1,ip2)
end
enddef onLButtonUp(flags, x, y, view)
if(@pt1 == nil)
@pt1 = [$ip.position]
else
@pt2 = [$ip.position]
line = Sketchup.active_model.entities.add_edges @pt1, @pt2 # RIGHT NOW, JUST ADDS TO THE BASE LEVEL
@pt1 = @pt2
Sketchup.active_model.active_view.invalidate
end # if state
end
end -
Can you repost that in the [code] tag - preserver the formatting and much easier to read.
I'll have a look when I get home. -
Cool. Here it is formatted. As of right now, the line is always added to Sketchup.model.entities. I will need to have it add to the active path... just haven't gotten there yet. Otherwise, it seems to work.
--
Karenclass Custom_Line def initialize @cursor = UI.create_cursor("Cursor.tiff",1,1) end # def initialize def activate $ip = Sketchup;;InputPoint.new Sketchup.active_model.selection.clear @ip1 = nil end def onSetCursor UI.set_cursor(@cursor) end def onMouseMove(flags, x, y, view) $ip.pick(view, x, y) view.invalidate end def draw(view) $ip.draw(view) if(@pt1 == nil) view = view.draw_line $ip.position, $ip.position else Sketchup.vcb_label = @pt1.distance($ip.position).to_s view = view.draw_line @pt1, $ip.position #draws line from last point to current position view.invalidate end end def onKeyUp(key, repeat, flags, view) if(key==16) view.lock_inference end end def enableVCB? return true end def onUserText(text, view) begin if(@pt1 != nil) value = text.to_l # convert the value to a length point2 = [$ip.position] dist = @pt1.distance(point2) w2 = value/dist w1 = 1-w2 @pt2 = Geom;;Point3d.linear_combination w1, @pt1, w2, point2 line = Sketchup.active_model.entities.add_edges @pt1, @pt2 # RIGHT NOW, JUST ADDS TO THE BASE LEVEL @pt1 = @pt2 Sketchup.vcb_value = '' end rescue # Error parsing the text UI.beep value = nil end end def onKeyDown(key, repeat, flags, view) if(key==16&&@pt1!=nil) ip1 = Sketchup;;InputPoint.new(@pt1) @pt2 = $ip.position ip2 = Sketchup;;InputPoint.new(@pt2) view.lock_inference(ip1,ip2) end end def onLButtonUp(flags, x, y, view) if(@pt1 == nil) @pt1 = [$ip.position] else @pt2 = [$ip.position] line = Sketchup.active_model.entities.add_edges @pt1, @pt2 # RIGHT NOW, JUST ADDS TO THE BASE LEVEL @pt1 = @pt2 Sketchup.active_model.active_view.invalidate end end end
-
Karen, what's different about your edition, from the Google supplied sample: Plugins/Examples/linetool.rb ??
And Sketchup can use TIFFs as button images?? ..any advantage in doing that rather than PNG ?
-
Hmm... somehow my 'linetool.rb' file got put in the plugins folder and I didn't know that I had it... There are many differences (other than the obvious ones that theirs draws a construction line not an edge, and doesn't start a new line where the old one leaves off). They also work primarily with input points, where I convert earlier to point3d, which means that to do inferencing, I have to convert back.
Also, in order to get the tool to write into the active group, $entities = Sketchup.active_model.acitve_entities (not Sketchup.active_model.entities) or (as in 'linetool.rb' view.model.entities).
--
Karen -
The built-in Line tool seems very simple until you try to recreate it. It has a lot more states (and ways to transition) than I realized at first glance.
-
I agree. I tried to use the linetool to learn how to make a tool, as the API suggests. I got so confused. It is a pretty complex tool, at least more complex than it should be if it is being pointed to as a good script for beginners to learn from.
Chris
Advertisement