From 8096f6ad6f99ed84455815b3e265831c94ee4844 Mon Sep 17 00:00:00 2001 From: theo-vt Date: Tue, 16 Sep 2025 11:14:51 -0400 Subject: [PATCH] Open and close transactions on command activation --- OpticalObject.py | 10 ++++++++++ Plot.py | 5 +++-- Ray.py | 12 ++++++++++++ SunRay.py | 2 ++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/OpticalObject.py b/OpticalObject.py index 0cefbf6..267cab1 100644 --- a/OpticalObject.py +++ b/OpticalObject.py @@ -333,6 +333,7 @@ class OpticalMirror(): '''This class will be loaded when the workbench is activated in FreeCAD. You must restart FreeCAD to apply changes in this class''' def Activated(self): + FreeCAD.ActiveDocument.openTransaction("Make mirror") selection = Gui.Selection.getSelectionEx() Gui.doCommand('import OpticsWorkbench') Gui.doCommand('objects = []') @@ -341,6 +342,7 @@ def Activated(self): 'objects.append(FreeCAD.ActiveDocument.getObject("%s"))' % (sel.ObjectName)) Gui.doCommand('OpticsWorkbench.makeMirror(objects)') + FreeCAD.ActiveDocument.commitTransaction() def IsActive(self): '''Here you can define if the command must be active or not (greyed) if certain conditions @@ -362,6 +364,7 @@ class OpticalAbsorber(): '''This class will be loaded when the workbench is activated in FreeCAD. You must restart FreeCAD to apply changes in this class''' def Activated(self): + FreeCAD.ActiveDocument.openTransaction("Make absorber") selection = Gui.Selection.getSelectionEx() Gui.doCommand('import OpticsWorkbench') Gui.doCommand('objects = []') @@ -370,6 +373,7 @@ def Activated(self): 'objects.append(FreeCAD.ActiveDocument.getObject("%s"))' % (sel.ObjectName)) Gui.doCommand('OpticsWorkbench.makeAbsorber(objects)') + FreeCAD.ActiveDocument.commitTransaction() def IsActive(self): '''Here you can define if the command must be active or not (greyed) if certain conditions @@ -391,6 +395,7 @@ class OpticalLens(): '''This class will be loaded when the workbench is activated in FreeCAD. You must restart FreeCAD to apply changes in this class''' def Activated(self): + FreeCAD.ActiveDocument.openTransaction("Make lens") selection = Gui.Selection.getSelectionEx() Gui.doCommand('import OpticsWorkbench') Gui.doCommand('objects = []') @@ -399,6 +404,7 @@ def Activated(self): 'objects.append(FreeCAD.ActiveDocument.getObject("%s"))' % (sel.ObjectName)) Gui.doCommand('OpticsWorkbench.makeLens(objects, material="Quartz")') + FreeCAD.ActiveDocument.commitTransaction() def IsActive(self): '''Here you can define if the command must be active or not (greyed) if certain conditions @@ -420,6 +426,7 @@ class OpticalGrating(): '''This class will be loaded when the workbench is activated in FreeCAD. You must restart FreeCAD to apply changes in this class''' def Activated(self): + FreeCAD.ActiveDocument.openTransaction("Make grating") selection = Gui.Selection.getSelectionEx() Gui.doCommand('import OpticsWorkbench') Gui.doCommand('objects = []') @@ -428,6 +435,7 @@ def Activated(self): 'objects.append(FreeCAD.ActiveDocument.getObject("%s"))' % (sel.ObjectName)) Gui.doCommand('OpticsWorkbench.makeGrating(objects)') + FreeCAD.ActiveDocument.commitTransaction() def IsActive(self): '''Here you can define if the command must be active or not (greyed) if certain conditions @@ -449,6 +457,7 @@ class OpticalEmitter(): '''This class will be loaded when the workbench is activated in FreeCAD. You must restart FreeCAD to apply changes in this class''' def Activated(self): + FreeCAD.ActiveDocument.openTransaction("Make ray") selection = Gui.Selection.getSelectionEx() Gui.doCommand('import OpticsWorkbench') if len(selection) > 0: @@ -460,6 +469,7 @@ def Activated(self): Gui.doCommand( 'OpticsWorkbench.makeRay(baseShape=[obj, %s])' % faces) + FreeCAD.ActiveDocument.commitTransaction() def IsActive(self): '''Here you can define if the command must be active or not (greyed) if certain conditions diff --git a/Plot.py b/Plot.py index 592cb98..67331aa 100644 --- a/Plot.py +++ b/Plot.py @@ -106,9 +106,11 @@ class RayHits2CSV(): def Activated(self): '''Will be called when the feature is executed.''' - # Generate commands in the FreeCAD python console to plot ray hits for selected absorber + # Generate commands in the FreeCAD python console to write ray hits for selected absorber as csv + activeDocument().openTransaction("Ray hit to csv") Gui.doCommand('import OpticsWorkbench') Gui.doCommand('OpticsWorkbench.Hits2CSV()') + activeDocument().commitTransaction() def IsActive(self): @@ -129,4 +131,3 @@ def GetResources(self): Gui.addCommand('RayHits', PlotRayHits()) Gui.addCommand('Hits2CSV', RayHits2CSV()) - \ No newline at end of file diff --git a/Ray.py b/Ray.py index 8d9f2d5..7bf1bce 100644 --- a/Ray.py +++ b/Ray.py @@ -911,9 +911,11 @@ class Ray(): def Activated(self): '''Will be called when the feature is executed.''' + FreeCAD.ActiveDocument.openTransaction("Make ray") # Generate commands in the FreeCAD python console to create Ray Gui.doCommand('import OpticsWorkbench') Gui.doCommand('OpticsWorkbench.makeRay()') + FreeCAD.ActiveDocument.commitTransaction() def IsActive(self): '''Here you can define if the command must be active or not (greyed) if certain conditions @@ -940,8 +942,10 @@ class RaySun(): def Activated(self): '''Will be called when the feature is executed.''' # Generate commands in the FreeCAD python console to create Ray + FreeCAD.ActiveDocument.openTransaction("Make sun ray") Gui.doCommand('import OpticsWorkbench') Gui.doCommand('OpticsWorkbench.makeSunRay()') + FreeCAD.ActiveDocument.commitTransaction() def IsActive(self): '''Here you can define if the command must be active or not (greyed) if certain conditions @@ -973,9 +977,11 @@ class Beam2D(): def Activated(self): '''Will be called when the feature is executed.''' # Generate commands in the FreeCAD python console to create Ray + FreeCAD.ActiveDocument.openTransaction("Make beam 2d") Gui.doCommand('import OpticsWorkbench') Gui.doCommand( 'OpticsWorkbench.makeRay(beamNrColumns=50, beamDistance=0.1, rayBundleType="parallel")') + FreeCAD.ActiveDocument.commitTransaction() def IsActive(self): '''Here you can define if the command must be active or not (greyed) if certain conditions @@ -1005,9 +1011,11 @@ class RadialBeam2D(): def Activated(self): '''Will be called when the feature is executed.''' # Generate commands in the FreeCAD python console to create Ray + FreeCAD.ActiveDocument.openTransaction("Make radial beam 2d") Gui.doCommand('import OpticsWorkbench') Gui.doCommand( 'OpticsWorkbench.makeRay(beamNrColumns=64, rayBundleType="spherical")') + FreeCAD.ActiveDocument.commitTransaction() def IsActive(self): '''Here you can define if the command must be active or not (greyed) if certain conditions @@ -1040,10 +1048,12 @@ class SphericalBeam(): def Activated(self): '''Will be called when the feature is executed.''' # Generate commands in the FreeCAD python console to create Ray + FreeCAD.ActiveDocument.openTransaction("Make spherical beam") Gui.doCommand('import OpticsWorkbench') Gui.doCommand( 'OpticsWorkbench.makeRay(beamNrColumns=8, beamNrRows=8, rayBundleType="spherical")' ) + FreeCAD.ActiveDocument.commitTransaction() def IsActive(self): '''Here you can define if the command must be active or not (greyed) if certain conditions @@ -1127,8 +1137,10 @@ class GridToFocalBeam(): '''A grid of rays converging toward a focal point''' def Activated(self): + FreeCAD.ActiveDocument.openTransaction("Make grid to focal beam") Gui.doCommand('import OpticsWorkbench') Gui.doCommand('r = OpticsWorkbench.makeRay(beamNrColumns=10, beamNrRows=3, beamDistance=1.0, rayBundleType="focal", focalPoint=FreeCAD.Vector(0, 0, 100))') + FreeCAD.ActiveDocument.commitTransaction def IsActive(self): return activeDocument() is not None diff --git a/SunRay.py b/SunRay.py index 2b71881..365698a 100644 --- a/SunRay.py +++ b/SunRay.py @@ -88,8 +88,10 @@ class RaySun(): '''This class will be loaded when the workbench is activated in FreeCAD. You must restart FreeCAD to apply changes in this class''' def Activated(self): + FreeCAD.ActiveDocument.openTransaction("Make sun ray") Gui.doCommand('import OpticsWorkbench') Gui.doCommand('OpticsWorkbench.makeSunRay()') + FreeCAD.ActiveDocument.commitTransaction() def IsActive(self): '''Here you can define if the command must be active or not (greyed) if certain conditions