Starting the CylTool in the sample linetool.rb script
-
I'm very new to Ruby programming for SketchUp (first started using it only two weeks ago), but have downloaded many of the generous selection of free sample tools, and modified some to do more closely what I want - for example, I've added a Sphere to the Shapes tool, and also added a couple of lines of code for each Shape to remember the previously used parameters. (I'd be happy to post this somewhere suitable for others, if that would be useful.)
I'd like to be able to use the example CylTool in the linetool.rb script (which illustrates inheritance from the LineTool), but can't see how to call it or put it on a menu.
I successfully added the 'standard' lines of code at the end of the LineTool to put the entry 'Construction line' on to the Draw menu, as below.
Add LineTool to Draw menu
if( not file_loaded?("linetool.rb") )
UI.menu("Draw").add_item("Construction line") { linetool }
endfile_loaded("linetool.rb")
But I can't see how to do the equivalent to start the CylTool - I've tried substituting CylTool for LineTool and changing "Construction line" to "Cylinder - pick axis" instead of the above lines at the end, but it doesn't work.
And if I just type 'cyltool' in the Ruby console, with the linetool.rb script loaded and working, I get
Error: #<NameError: (eval):298: undefined local variable or method `cyltool' for main:Object>
(eval):298Any pointer to how to do this would be welcome, as I'd really like to use this script as the basis for a range of other tools, and have got stuck on using the 'inheritance' part. Is there something missing from the code to draw a cylinder this way?
I'd also like to adapt the code at the start of the LineTool to modify the Shapes tool to allow you to pick a point to start the shape, not always have it at the origin. I can see in principle how to do that.
Further, I'd like very much to be able not only to place the component or shape, but also to be able to place and rotate it about its origin (around the z-axis) in one go, without having manually to choose the Rotate tool, pick the rotation plane and origin, etc. I wrote something similar years ago in AutoLisp for AutoCad, but can't see how to go beyond the place_component method to include the rotation step. I guess I'd have to generate a selection set in code for the script-created group or component, then use the PickHelper or InputPoint class to pick the point at which to place the entry, then a second pick for the direction of the newly drawn group or component's x-axis.
And a final 'wish' that I'd like to be able to code is a script 'Draw new component', to start a 'draw' operation, draw an object perhaps with several elements to it, then automatically select all its elements to make it into a component when the object is complete. Maybe I'm missing something obvious, but I have to go through contortions to select only the things I've just drawn in a complex scene, as they usually aren't still highlighted after I've finished drawing them.
As a simple common example, take drawing a box with the Rectangle tool then PushPull. Easy to window select the result in a clear field, but not if it is surrounded by other previously draw objects.
(Though on reflection, perhaps I could use instead the context menu 'Select->All connected' for this particular simple construction - I've only just discovered this. However, that wouldn't work for some more complex objects.)
I've read what I can find on the Internet in forums like this one, and am quite prepared to work at it, and have learned a lot just from looking at a lot of sample scripts - but I just can't see quite how to get started on the CylTool.
Thanks if anyone can offer a few pointers.
John McC
-
Answer to part 1:
'cyltool' doesn't exists because it's not defined anywhere... like the function linetool is.
Look again at the linetool.rb - linetool is a function which tells SketctUp to switch to a newly created Linetool.
cyltool is not defined... nor is it needed. the linetool function is so simple, it's contents could go right inside menu.add_item(...) { Sketchup.active_model.select_tool Linetool.new } .
I think when you look at it in the morning, it will be clear. Right now your're making it harder than it really is. I know, I do it all the time.
-
Well, it wouldn't have 'been clear in the morning' without your clarification, Jim, but it now is - THANKS.
I now see that although the Class CylTool is defined, the function to create the tool is NOT 'def'ined.
So my slightly modified code now works to provide Draw menu entries for 'Construction line' and 'Cylinder - on axis' with the addition of just these lines at the end of the linetool.rb script where only the first four lines were originally.
These functions provide shortcuts for selecting the new tools
def linetool
Sketchup.active_model.select_tool LineTool.new
enddef cyltool
Sketchup.active_model.select_tool CylTool.new
endAdd tools to Draw menu
if( not file_loaded?("linetool.rb") )
UI.menu("Draw").add_item("Construction line") { linetool }
UI.menu("Draw").add_item("Cylinder - on axis") { cyltool }
endfile_loaded("linetool.rb")
I now just want to add a few refinements, which I can see how to do (following the excellent examples elsewhere in this forum and in the script repositories), to:
- specify the radius or diameter of the cylinder
- make the drawn cylinder into a group or component automatically
- design PNG graphics to indicate which tool is in use, instead of the generic white arrow.
Thanks again for such a prompt and helpful response - much appreciated.
John McC
Advertisement