Thanks for your explanations!
I was able to understand how commands are created and committed in PAF.
Unfortunately I still haven't figured out how to undo a command in PAF.
However, I discovered that TDocStd_Document provides functionality for
undo/redo as well and I managed to kind of figure out how that works.
My first attempt at this is based on a script by Bryan Cole which I
found in the archive. I am posting it here in case anybody is
interested. I have to say that I don't understand some of the commands,
but it seems to work.
# based on
# http://www.mail-archive.com/pythonocc-users@gna.org/msg00255.html
from OCC import XCAFApp, TDocStd, TCollection, XCAFDoc, BRepPrimAPI, \
TopLoc, gp, TPrsStd, XCAFPrs
from OCC.Display.SimpleGui import init_display
display, start_display, add_menu, add_function_to_menu = init_display()
h_doc = TDocStd.Handle_TDocStd_Document()
app = XCAFApp.GetApplication().GetObject()
app.NewDocument(TCollection.TCollection_ExtendedString('MDTV-CAF'),
h_doc)
doc = h_doc.GetObject()
doc.SetUndoLimit(10)
def undo(self):
doc.Undo()
# update display
aisPres.Display(True)
def redo(self):
doc.Redo()
# update display
aisPres.Display(True)
add_menu('Edit')
add_function_to_menu('Edit', undo)
add_function_to_menu('Edit', redo)
shape_tool = XCAFDoc.XCAFDoc_DocumentTool().ShapeTool(doc.Main()).\
GetObject()
top_label = shape_tool.NewShape()
tr = gp.gp_Trsf()
tr.SetTranslation(gp.gp_Vec(0,0,0))
loc = TopLoc.TopLoc_Location(tr)
# create box
box = BRepPrimAPI.BRepPrimAPI_MakeBox(100,50, 50).Shape()
box_label = shape_tool.AddShape(box, False)
# add box
doc.OpenCommand()
box_comp = shape_tool.AddComponent(top_label, box_label, loc)
doc.CommitCommand()
# create cylinder
cyl = BRepPrimAPI.BRepPrimAPI_MakeCylinder(50,50).Shape()
cyl_label = shape_tool.AddShape(cyl, False)
# add cylinder
doc.OpenCommand()
cyl_comp = shape_tool.AddComponent(top_label, cyl_label, loc)
doc.CommitCommand()
context_handle = display.Context.GetHandle()
aisView = TPrsStd.TPrsStd_AISViewer().New(top_label, context_handle)
aisPres = TPrsStd.TPrsStd_AISPresentation().Set(top_label,
XCAFPrs.XCAFPrs_Driver().GetID()).GetObject()
aisPres.Display(True)
start_display()
Am 05.04.2012 18:46, schrieb jelle feringa:
hi Marko,
Fair enough, I had to delve a little in the back of my mind to recall
how it works.
Due to python's highly dynamic nature its easy to radically change
the
behaviour of modules
So around line 209 [1] in "_initialize_operation" you see that
we're looping through all the attributes that have operations ( like
"measure_operations" )
To make the methods in these attributes respect the undo
functionality
we need to wrap 'em.
So here, all the methods in *_operations get decorated
If undo is enabled, the method will be executed in the context [ with
statement ] of "operation" [2]
So:
StartOperation
the method is called
FinishOperation
Than OCC has collected all the data needed to undo / redo the
operation performed...
I agree its a little hysteric code ( a little bit too much magic
perhaps ), but does the job.
-jelle
Hi, jelle!
I have looked through the examples that come with pythonOCC and I
know how
parametric modeling works in pythonOCC.
I also tried to go through the tutorial you mention, but it fails
because the
newly created instance of ParametricModelingContext has no
attribute
'init_display'. I guess the tutorial is outdated as it was written
shortly
after the release of pythonOCC 0.4.
Anyway I have read through the tutorial and it looks like it
doesn't provide
any information on undo/redo. Understanding how parametric modeling
works is
not the problem for me - there is enough documentation available
for that. I'm
just trying to find out how undo/redo works.
Marko
Links:
------
[1]
https://github.com/tpaviot/pythonocc/blob/master/src/addons/PAF/Context.py#L209
[2]
https://github.com/tpaviot/pythonocc/blob/master/src/addons/PAF/Context.py#L30
_______________________________________________
Pythonocc-users mailing list
Pythonocc-users@gna.org
https://mail.gna.org/listinfo/pythonocc-users