Testing my first script

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
Post Reply
mr-manuel
Posts: 2
Joined: Wed Aug 26, 2009 5:03 pm

Testing my first script

Post by mr-manuel »

Hi.

I'm new in FreeCAD and Python.
I've some initial considerations and/or questions.
First, in Edit/Preferences/Display , options of 3D Mouse Model are 'Inventor' or 'CAD' navigation.
What's the difference exactly?

Now I'm testing Line Drawing Function script, but not working.
With first part, when script defines the main function body, I test it writing one of two forms above in Python Console of Report view:
>>> import exercise
And now (1):
>>> exercise.line
appears: <class exercise.line at 0x01478C90>

as well (2):
>>> exercise.line()
appears: <exercise.line instance at 0x0A322F30>

In two cases, I click two points in 3D-view, but the unique results are (Now in Output Report View):
<type 'exceptions.AttributeError'>
<type 'exceptions.AttributeError'>

And no line draws on the scrceen.
What happens?
What's wrong?

I write the file text script 'exercise.py' (+ notes indent) as:

The import FreeCADGui,Part
from pivy.coin import *
class line:
++"this class will create a line after the user clicked 2 points on the screen"
++def __init__(self):
++++self.view = FreeCADGui.ActiveDocument.ActiveView
++++self.stack = []
++++self.callback = self.view.addEventCallbackPivy(SoMouseButtonEvent.getClassTypeId(),self.getpoint)
++def getpoint(self,event_cb):
++++event = event_cb.getEvent()
++++if event.getState() == SoMouseButtonEvent.DOWN:
++++++pos = event.getPosition()
++++++point = self.view.getPoint(pos[0],pos[1])
++++++self.stack.append(point)
++++++if len(self.stack) == 2:
++++++++l = Part.Line(self.stack[0],self.stack[1])
++++++++shape = l.toShape()
++++++++Part.show(shape)
++++++++self.view.removeEventCallbackPivy(SoMouseButtonEvent.getClassTypeId(),self.callback)

Thanks.
manuel
User avatar
jriegel
Founder
Posts: 3369
Joined: Sun Feb 15, 2009 5:29 pm
Location: Ulm, Germany
Contact:

Re: Testing my first script

Post by jriegel »

Hi,
CAD mouse model is the complexer one. Its basically like Catia V5. Middle mouse button for movement in different flavors. And left one for selection.

with
exercise.line
you get the class type of your python class
and
exercise.line()
is the constructor and returns an object.

Dont get me wrong, but without some basics in python
is hard to program. You shut first do the turorials on pyhton.org.

For Line drawing in python Yorik make a excellent module called "Draft" .
Its a good example how to do such things.

And if you get exceptions we need the stack trace, otherwise we donts see
where it come from.

Let me know if you need more help!

Jürgen
Stop whining - start coding!
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Testing my first script

Post by wmayer »

There was a little change in the interface of the registered callback function. Instead of SoEvent it expects an SoEventCallback.

For your version you should try this:

Code: Select all

import FreeCADGui, Part

from pivy.coin import *
class line:
	"this class will create a line after the user clicked 2 points on the screen"
	def __init__(self):
		self.view = FreeCADGui.ActiveDocument.ActiveView
		self.stack = []
		self.callback = self.view.addEventCallbackPivy(SoMouseButtonEvent.getClassTypeId(),self.getpoint)
	def getpoint(self,event):
		if event.getState() == SoMouseButtonEvent.DOWN:
			pos = event.getPosition()
			point = self.view.getPoint(pos[0],pos[1])
			self.stack.append(point)
			if len(self.stack) == 2:
				l = Part.Line(self.stack[0],self.stack[1])
				shape = l.toShape()
				Part.show(shape)
				self.view.removeEventCallbackPivy(SoMouseButtonEvent.getClassTypeId(),self.callback)

Werner
Post Reply