How to change obj properties

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
vitort
Posts: 12
Joined: Tue Feb 04, 2014 5:59 pm

How to change obj properties

Post by vitort »

Hi
I'm trying to create objects from the point and line FreeCAD's objects.
I want to change the label and add some properties.
I've started by scripting them has Yorik has done in the Draft workbench modules.
It was going fine but then I realised that that have already been done by Yorik , no need to repeat... I think.

Changing and adding properties to an object is not hard if one can have hold on the object. So in a script...
This works (case 1)...

Code: Select all

import FreeCAD
from Draft import makePoint

obj=None
doc=FreeCAD.ActiveDocument    

point=makePoint(1,1,0)
obj=doc.ActiveObject
print "obj.Label= ", obj.Label
but this won't (case 2)...

Code: Select all

import FreeCAD
from pivy import coin
from DraftTools import Point

obj=None
doc=FreeCAD.ActiveDocument

Point.Activated(Point())
obj=doc.ActiveObject
print "obj.Label= ", obj.Label
because I can't get hold on the object, the user can wait forever to choose a point.
My question is:
Is there a way to wait for an operation to be executed (point and line creation) or to know if the operation has been executed and then get the object? ... scan the document be scanned for changes? ... or, is there another way?

Thank you
User avatar
shoogen
Veteran
Posts: 2823
Joined: Thu Dec 01, 2011 5:24 pm

Re: How to change obj properties

Post by shoogen »

vitort wrote:Is there a way to wait for an operation to be executed (point and line creation) or to know if the operation has been executed and then get the object? ... scan the document be scanned for changes? ... or, is there another way?
What you are asking for sounds rather complicated. This can be achived using 'observer' objects. I would rather reimplement the point and line creation. And then do your stuff.
There are two concepts. The first one is the 'selection'. The users selects one or more objects in the tree or 3d view. After that your script gets executed. http://freecadweb.org/wiki/index.php?ti ... ection_API An example would be http://freecadweb.org/wiki/index.php?ti ... _SuperWire
The second concept is the 'observer'. This basically means that you register a callback that gets executed when the user goes something in the GUI. (I can't find the any example right now)
An finnaly you an register callbacks in coin3d. An example for this is http://freecadweb.org/wiki/index.php?ti ... o_makeCube
User avatar
yorik
Founder
Posts: 13665
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: How to change obj properties

Post by yorik »

Have a look at the Arch Window tool, it does something similar. If you press the Arch Window tool with a face selected, it runs another command from the Sketcher, so the user can create a sketch, then "watches" for any change in the selection. When a change occurs (the sketcher command has created a new sketch), the command runs again. Objects are added in order of creation to FreeCAD.ActiveDocument.Objects. So you can always get the last created object with:

Code: Select all

FreeCAD.ActiveDocument.Objects[-1]
vitort
Posts: 12
Joined: Tue Feb 04, 2014 5:59 pm

Re: How to change obj properties

Post by vitort »

Thank you Shoogen and Yorik I'll have a look at these approaches
Post Reply