Reload module/workbench

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
albertof
Posts: 53
Joined: Mon Apr 12, 2010 4:52 pm

Reload module/workbench

Post by albertof »

Forgive me if this is trivial.
Is it possible to reload a workbench from the console in a way that it loads its modules again? I would like to change code in the python files and not having to open and close freecad to test it.
I have tried importing the module file in the console but it does not seem to do anything
rockstar1707-1

Re: Reload module/workbench

Post by rockstar1707-1 »

In Python you can reload modules like:

Code: Select all

import mymodule
reload(mymodule)
But note that reload function does not work if you do something like:

Code: Select all

from mymodule import *
reload(mymodule)
I would assume that this would work in FreeCAD as well, but it is true that I never tried.
albertof
Posts: 53
Joined: Mon Apr 12, 2010 4:52 pm

Re: Reload module/workbench

Post by albertof »

I thought so but it does not seem to work for me.
User avatar
jriegel
Founder
Posts: 3369
Joined: Sun Feb 15, 2009 5:29 pm
Location: Ulm, Germany
Contact:

Re: Reload module/workbench

Post by jriegel »

Mhh,
the way Werner descriped to reload modules actually work in FreeCAD to - with some exceptions.
e.g. the Workbench description class is transfered to FreeCAD while start. So if you change the
Workbench and reload the python module it will have no effect.

So the answer is - It depends.
So maybe you can tell us what you change so we can look into it....
Stop whining - start coding!
Guest

Re: Reload module/workbench

Post by Guest »

The main problem that I have is that since I'm just starting I make many mistakes. Also I do a lot of trial and error so I end up spending some time opening and closing Freecad. I know some things can be tested from the console but some others can not.
A little off topic, it would be great if the console would have command history.
Anyhow, I'll give you an example to see what I was doing:
In the Mod directory I have created a BoatDesign directory.
Inside that directory I have two files: BoatDesign.py and InitGui.py
InitGui.py

Code: Select all

class BoatWorkbench (Workbench):
    MenuText = "Boat Design"
    def Initialize(self):
        import BoatDesign # assuming Scripts.py is your module
        list = ["LoadHull","AddKeel","CalcDispl"] # That list must contain command names, that can be defined in BoatDesign.py
        self.appendToolbar("Boat Design",list)

Gui.addWorkbench(BoatWorkbench())
BoatDesign.py

Code: Select all

import FreeCAD as App
import FreeCADGui as Gui
import Part
import FreeCAD.Base as Base

class LoadHull:
   def Activated(self):
       '''Loads a hull in any fo the formats supported by FreeCAD'''
       if App.ActiveDocument is None:
           App.Console.PrintMessage('Creating Document')
           App.newDocument("Boat")
           App.ActiveDocument=App.getDocument("Boat")
           Gui.ActiveDocument=Gui.getDocument("Boat")
       App.Console.PrintMessage('Select hull file')
       Gui.runCommand("Std_Import")
       Gui.runCommand("Std_FitAll")
       hull = App.ActiveDocument.Objects[0]
       hull.Label='Hull'

   def GetResources(self):
       return {'Pixmap' : 'path_to_an_icon/myicon.png', 'MenuText': 'Load Hull', 'ToolTip': 'Load a hull for the boat'}
Ok, that works but originally I have written App.activeDocument, wrong case. This is just an example, but the reload method did not work this time. What I did was:
import BoatDesign
reload(BoatDesign)
no errors but when I call the function again the syntax error is still there.
User avatar
jriegel
Founder
Posts: 3369
Joined: Sun Feb 15, 2009 5:29 pm
Location: Ulm, Germany
Contact:

Re: Reload module/workbench

Post by jriegel »

Hi,
unfortunately GuiCommands (your LoadHull) is also created once and stored in FreeCAD. So reloading the the module will have no effect.
Thats because such basic objects like Commands and Workbenches are handled in the FreeCAD C++ code and are not handled by python.
So a good idea is to write first a script which does all you need and test it with e.g. FreeCADCmd.exe till its flawless. And then put it
in commands.

And the Console has a history (Ctrl + Up).
Yea I know, not enough documentation ;)
Stop whining - start coding!
albertof
Posts: 53
Joined: Mon Apr 12, 2010 4:52 pm

Re: Reload module/workbench

Post by albertof »

Yes!!! :D
Cut and paste was driving me nuts
User avatar
mangtronix
Posts: 44
Joined: Fri Apr 18, 2014 3:58 am
Location: Berlin, Germany
Contact:

Re: Reload module/workbench

Post by mangtronix »

Found this old thread since I was having the same problem. Here's a solution! The trick is to load and run your modules from the Python console, rather than through the GUI.

Here's an example for testing/developing the MakeConnectors functionality inside the Polycon module.

>>> import Polycon
>>> import PolyconGui
>>> PolyconGui.MakeConnectors().Activated() # create Polycon command and run it
>>> # make code changes
>>> reload(Polycon)
<module 'Polycon' from '/Users/mangtronix/Library/Preferences/FreeCAD/Mod/Polycon/Polycon.pyc'>
>>> PolyconGui.MakeConnectors().Activated() # run command with new code
http://michaelang.com - Art / Engineering
User avatar
mangtronix
Posts: 44
Joined: Fri Apr 18, 2014 3:58 am
Location: Berlin, Germany
Contact:

Re: Reload module/workbench

Post by mangtronix »

You can even make a nice little function in the FreeCAD console that puts it all together.

>>> def go(): reload(Polycon); PolyconGui.MakeConnectors().Activated()
>>> go()

Edit your code in your text editor, then it's as simple as

>>> go()
http://michaelang.com - Art / Engineering
User avatar
sliptonic
Veteran
Posts: 3457
Joined: Tue Oct 25, 2011 10:46 pm
Location: Columbia, Missouri
Contact:

Re: Reload module/workbench

Post by sliptonic »

Sorry to necrobump this topic but it is still the top result on google if you search for 'FreeCAD module reload' and it's wrong.

These days you have to do this:

Code: Select all

>>> from importlib import reload
>>> reload(<mymodule>)
Post Reply