how to modify built in cpp-classes with my own python-code

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

how to modify built in cpp-classes with my own python-code

Post by microelly2 »

I want to bring the allignment and placement tools into my own tollbar an menu
without changing the cpp-code

I want to have my own symbol and shortcut,

something like this:

Code: Select all

tdCmdAlignment::StdCmdAlignment() 
  : Command("Std_Alignment")
{
// #+  hack 
    sGroup        = QT_TR_NOOP("School");
// #-
    sMenuText     = QT_TR_NOOP("Alignment...");
    sToolTipText  = QT_TR_NOOP("Align the selected objects");
    sStatusTip    = QT_TR_NOOP("Align the selected objects");
    sWhatsThis    = "Std_Alignment";
// #+  hack 
    sPixmap       = "irgendwo/Allign.svg";
    sAccel        = "Ctrl+A";
// #-
}
Is it possible to modify the tdCmdAlignment::StdCmdAlignment() from inside a python script to add the sPixmap and sAccel?
Or is it possible to derive a python class from this class and overwrite with my own attributes?

Code: Select all

class _CommandMyAllignment (tdCmdAlignment::StdCmdAlignment) :
    "the School Alligment command definition"
    def GetResources(self): 
	return {'Pixmap' :  App.getHomePath() +'/Mod/School/icons/allign.svg', 
                         'MenuText': 'Ausrichten',
                         'ToolTip': 'Richtet zwei Koerper aneinander aus ',
                         'Accel': 'Ctrl+A'

} 
thank you for a clue
thomas.
wmayer
Founder
Posts: 20319
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: how to modify built in cpp-classes with my own python-co

Post by wmayer »

Making any changes on the command class is not possible. However, you can access the corresponding QAction instance and change this instead. But note, the changes you make there are only temporary. E.g. if you change the language at runtime your changes are lost.

Code: Select all

from PySide import QtGui
mw=Gui.getMainWindow()
a=mw.findChild(QtGui.QAction,"Std_Alignment")
a.setText("Custom text...")
a.setShortcut("CTRL+J")
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: how to modify built in cpp-classes with my own python-co

Post by microelly2 »

Thank you,
it works fine
I have designed my school-workbench with re aranged ikons,
https://github.com/microelly2/freecad_mod_school

one problem remains open:
when I configure freecad to start with my workbench
the properties cannot be changed because the qt-action instance seems to be not yet available.


the next question for me
can I hide/unhide the toolbar with the sketcher constraints when I leave/enter the sjḱetcher edit mode within python - there is no command displayed when I toggle the visibility of a toolbar ? :o

thanks for a hint
wmayer
Founder
Posts: 20319
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: how to modify built in cpp-classes with my own python-co

Post by wmayer »

one problem remains open:
when I configure freecad to start with my workbench
the properties cannot be changed because the qt-action instance seems to be not yet available.
You can add the method Activated to your workbench class and check if you can do it there

Code: Select all

class SchoolWorkbench (Workbench):
...
    def Activated(self):
        # change QAction there
but no guarantee that this will work.
can I hide/unhide the toolbar with the sketcher constraints when I leave/enter the sjḱetcher edit mode within python - there is no command displayed when I toggle the visibility of a toolbar ?
There is no easy way to achieve this.
Post Reply