programming in sketchup. we can add programmed behaviour to sketchup the language used is ruby, an...
Post on 20-Dec-2015
245 views
TRANSCRIPT
Programming in Sketchup
Programming in Sketchup
• We can add programmed behaviour to Sketchup
• The language used is Ruby, an object-oriented programming language
• There are several ways of adding Ruby programs to Sketchup
• We will focus on plugins
Programming in Sketchup• Look at the Sketchup menu bar below• It contains the following items
Sketchup File Edit View Camera Draw Tools Window Help
• This will change when we add certain types of plugin to Sketchup
Programming in Sketchup• To write Ruby programs, we need a plain-text editor• We will use TextEdit, which is in the Applications folder
Programming in Sketchup• By default, TextEdit creates Rich Text files• We must change this in the TextEdit preferences
Programming in Sketchup• Below, we see the default situation
Programming in Sketchup• Below, we have changed this to plain text
Programming in Sketchup• A simple one-line Ruby program:
UI.messagebox('Hello World!')
Programming in Sketchup• We save the program in a filled called helloWorld.rb in this folder:
/Library/Application Support/Google SketchUp [n]/plugins • Now, when we restart Sketchup, ...
Programming in Sketchup• ... a message box pops up, containing the string we specified
A program which adds a menu item
module Say_Hello
def Say_Hello.main
UI.messagebox("Hello world!")
end
end
UI.menu("Plugins").add_item("Greet the world") { Say_Hello.main }
Programming in Sketchup• Let's overwrite the previous program with this new one
Programming in Sketchup• When we re-start Sketchup, we do not see the greeting we saw
before
Programming in Sketchup• But notice that there is a new item in the Sketchup menu bar• It now contains the following items
Sketchup File Edit View Camera Draw Tools Window Plugins Hello
Programming in Sketchup• When we click on the Plugins menu item, we see the option Greet the world
Programming in Sketchup• If we choose the option Greet the world, ...
Programming in Sketchup• ... we receive the Hello World greeting
Programming in Sketchup• If we click OK on the message box, we can start drawing
Programming in Sketchup• Let's create a box
Programming in Sketchup• We can then use the Greet the World option again and ...
Programming in Sketchup• ... we receive the greeting again
A program which draws geometry
module Draw_triangle
def Draw_triangle.main
point1 = [0,0,0]
point2 = [20,20,20]
point3 = [20,20,0]
Sketchup.active_model,entities.add_face point1,point2,point3
end
end
UI.menu("Plugins").add_item("Draw triangle") { Draw_triangle.main }
Programming in Sketchup• Saving the program in the plugins folder in a file called draw_triangle.rb
Programming in Sketchup• Re-starting Sketchup
Programming in Sketchup• Now, we have two of our own plugins
Programming in Sketchup• Let's choose the new one
Programming in Sketchup• A triangle is created
Programming in Sketchup• If we remove Sang, we can see the triangle more clearly
Programming in Sketchup• Let's move the triangle and ...
Programming in Sketchup• ... use the plugin again
Programming in Sketchup• As expected, we get another triangle
Exercise
• Tackle Exercise 34