Download CollabCAD and Jython Scripting Presentation

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CollabCAD Jython & Swing
TM
Embedding Jython
Interpreter … Scripting
Support
G. Prasad, CAD Group, NIC
www.collabcad.com
Agenda
Overview of Jython
Using Swing in Jython
Jython and CollabCAD
ScriptingInterface
www.collabcad.com
Python
A very high-level, object-oriented,
open-source programming language
“Pseudo-code that runs”
Tutorial and documentation at:
http://www.python.org
www.collabcad.com
Jython
Implementation of Python in Java
(originally Jpython)
http://www.jython.org
Ease of Python, plus seamless access to
every Java class libraries
www.collabcad.com
Overview of Jython
Interpreted (No compilation)
 Dynamic (run-time) strong typing

– No need to declare variables(no explicit
static typing), determined at runtime
Modules, classes, functions
 Built-in types: tuples, lists,
dictionaries

www.collabcad.com
Jython Syntax
ibm04:/home/gpr/jython-2.1:[32]jython
Jython 2.1 on java1.4.2 (JIT: null)
Type "copyright", "credits" or "license" for more
information.
>>>
Whitespace is used to delimit blocks
for i in range(5):
if i < 2:
print i
print “Done”
www.collabcad.com
Jython Conditional Logic
if expression:
block
elif expression:
block
else:
block
www.collabcad.com
Jython boolean logic
Generally….
Non-zero is true, 0 is false
Objects are true, None* is false
Empty lists are false
use: and, or, not
* None is the equivalent of Null
www.collabcad.com
Jython Loops
while expression:
block
else:
block
# exits once on normal
# loop end
Note: pass, break, continue
www.collabcad.com
Jython Loops
For variable in sequence:
block
else:
block
www.collabcad.com
Jython Loops
for x in (‘a’, 5, [1, 2]):
print x
for x in range(10):
print x
# prints 0, 1, 2, …, 9
for key, value in dict.items():
print key, “ : ” , value
www.collabcad.com
Jython Lists
Heterogeneous, mutable
list(sequence)
l = [1, ‘test’, [9,
len(l)
l[1] = ‘foo’
l.remove(5)
l.append(2)
newList = l[2:4]
10], 5]
#4
# [1, ‘foo’, [9, 10], 5]
# [1, ‘foo’, [9, 10]]
# [1, ‘foo’, [9, 10], 2]
# [[9, 10], 2]
www.collabcad.com
Jython Strings
s1 = ‘This is a string.’
s2 = “So is this.”
s3 = s1 + ‘ ‘ + s2
s2.split()
# [‘So’, ‘is’, ‘this.’]
s2.find(‘is’)
#3
www.collabcad.com
Jython Tuples
Heterogeneous, immutable: accepts one
argument that must be a sequence and returns a
tuple with the same members as those in the
argument
tuple(sequence)
t = (‘example’, 1, [1, 2])
len(t)
#3
t[1]
#1
t[1] = 3
# TypeError
www.collabcad.com
Jython Dictionaries
(Key, value) pairs, similar to hash tables
dict = {‘one’: 1, 2 : ‘two’ }
dict.keys()
# [‘one’, 2]
dict.values()
# [1, ‘two’]
dict[3] = 4
dict.items()
# [(‘one’, 1),
# (2, ‘two’), (3, 4)]
del dict[3]
www.collabcad.com
Jython Exception Handling
try:
block
except: / except exception-type:
block
else:
block # if no exceptions raised
www.collabcad.com
Jython Exception Handling
try:
block
finally:
block
# executed even if exception
www.collabcad.com
Jython Functions
def FuncName(args):
block
def AddNumbers(a , b):
return a + b
www.collabcad.com
Jython Functions
Keyword arguments and default values
def AddSome(a, b=9, c=10):
return a + b + c
AddSome(1, 2, 3)
AddSome(1, 2)
AddSome(1)
#6
# 13
# 20
www.collabcad.com
Jython Classes
class MyClass:
def __init__(self, aValue = 5):
self.aField = aValue
def addToField(self, aNumber)
self.aField += aNumber
def printField(self):
print self.aField
inst1 = MyClass()
inst2 = MyClass(10)
inst1.aField = 6
inst1.addToField(1)
inst1.printField()
MyClass.printField(inst2)
#7
# 10
www.collabcad.com
Jython Classes: Inheriting
class MyNewClass(MyClass):
def __init__(self):
MyClass.__init__(self, 8)
inst = MyNewClass()
inst.aField
#8
www.collabcad.com
Jython: Prototype-Instance
inst = MyClass()
inst.b = ‘test’
inst.b
# test
www.collabcad.com
Jython Modules
Basic unit for managing namespace
Usually a file full of code (.class, .jar, .py)
import javax.swing
import javax.swing as swing
from javax.swing import JFrame
from javax.swing import *
www.collabcad.com
Swing
Essentially a library of classes for
building GUIs
Part of the Java Foundation Classes
Online tutorial:
http://java.sun.com/docs/books/tutorial/uiswing/
www.collabcad.com
Useful Swing Classes
JFrame (essentially a window)
JLabel
JButton
JTextField
JTextArea
JMenuBar
JScrollPane
JPanel
JPasswordField
JTable
www.collabcad.com
Jython and Swing
Can work with Java classes from within Jython
import javax.swing as swing
frame = swing.JFrame(‘Hello World Swing’)
frame.setDefaultCloseOperation(
swing.JFrame.EXIT_ON_CLOSE)
label = swing.JLabel(“Hello World”)
frame.getContentPane().add(label)
frame.pack()
frame.setVisible(1)
www.collabcad.com
Jython, Swing, and Listeners
Jython also handles Listener wrapping for you
Instead of:
int count = 1;
JButton button = new JButton(“Increment”);
button.addActionListener(
new ActionListener{
public void actionPerformed(ActionEvent e) {
count++;
})
Use:
count = 1
def Increment(event):
count += 1
button = swing.JButton(“Increment”, actionPerformed = Increment)
www.collabcad.com
Learning More
Python and Jython websites
 Jython for Java Programmers

– Robert W Bill (NewRiders)

Jython Essentials
– S.Pedroni and N.Rappin(O’Reilly)
www.collabcad.com
CollabCAD and Jython
CollabCAD
- http://www.collabcad.com/
Jython Interpreter
- ScriptingInterface (see User’s
guide)
- Tools  Execute Script
www.collabcad.com
ScriptingInterface
Embed a Jython interpreter directly into
Java code. An instance of the interpreter
can be used within a Java program to
evaluate Jython code.
 Easy to use methods available to interact
with CollabCAD
 Allows Jython scripts to
create/edit/modify CollabCAD entities.

www.collabcad.com
Jython Console Frame in CollabCAD
Jython Console Frame
An
independent
Jython Console Frame is
invoked where a user can
interactively
enter
Jython expressions as
well
as
use
the
CollabCAD
scripting
methods.
www.collabcad.com
Jython Console Panel in CollabCAD
www.collabcad.com
Example Jython script for CollabCAD
TM
import incad.Incad as incad
theApp =
incad.getScriptingInstance()
import javax.vecmath.Point3d as
P3d
p1 = P3d(100,100,-50)
p2 = P3d(200,200,55)
box1 = theApp.addBox(p1,p2)
p4 =
0.0)
p5 =
0.0)
p6 =
22.0,
ar2 =
theApp.addPoint(104.0, 22.0,
theApp.addPoint(126.0, 0.0,
theApp.addPoint(104.0, 0.0)
theApp.addArc(p4, p5, p6)
Import Incad class and refer to it using
the identifier incad in the script
Create an instance of Incad to expose
its methods
Import Point3d class ( Java3D object)
Create two Point3d objects
Create a CollabCAD SolidBox entity by
calling the method using two corner
points
Create three points for creating a
CollabCAD CADArc
www.collabcad.com
Jython Interactive session
Starting Jython in an interactive mode
ibm04:/home/gpr/jython-2.1:[49]jython
Jython 2.1 on java1.4.2 (JIT: null)
Type "copyright", "credits" or "license" for more information.
>>>
You
>>>
>>>
>>>
>>>
>>>
can type any Jython code and see the result. Type in some Swing GUICode
import javax.swing as swing
win = swing.Jframe(“Welcome to Jython”)
// create a window object
win.size = (200,200)
// size of the window
win.show()
// get the window on the screen
field = swing.JTextField(preferredSize=(200,200))// add a textfield object to the
window
>>> win.contentPane.add(field)
>>> win.pack()
>>> win.show()
>>> field.text
‘It is Jython’
You can add buttons,etc and also behaviour to it.
Running this script from Jython ( executing jython filename at an ordinary command prompt) will
give the same window and behaviour.
www.collabcad.com
THANK YOU
Q&A
Thanks for your time
Any queries : [email protected]
www.collabcad.com