Command/ro: Difference between revisions

From FreeCAD Documentation
(Updating to match new version of source page)
(Updating to match new version of source page)
 
(5 intermediate revisions by the same user not shown)
Line 12: Line 12:
</div>
</div>


{{Code|code=
FreeCADGui.runCommand("my_Command_Name")
FreeCADGui.runCommand("my_Command_Name")
}}


== Background ==
== Background ==
Line 27: Line 29:
Comenzile pot fi definite sau în C++ ori în Python.
Comenzile pot fi definite sau în C++ ori în Python.


== Example ==
== Commands defined in C++ ==


<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
Exemplu de definiție a comenzii în C++ (usually defined in /Mod/ModuleName/Gui/Command.cpp):
Exemplu de definiție a comenzii în C++ (usually defined in /Mod/ModuleName/Gui/Command.cpp):
</div>
</div>

{{Code|code=
{{Code|lang=cpp|code=
DEF_STD_CMD_A(StdCmdMyCommand);
DEF_STD_CMD_A(StdCmdMyCommand);


Line 62: Line 65:
}}
}}


== Commands defined in Python ==

<div class="mw-translate-fuzzy">
și comanda similară în python (no rule for where it must be done, each python workbench does as it sees fit...)
și comanda similară în python (no rule for where it must be done, each python workbench does as it sees fit...)
</div>
{{Code|code=
{{Code|code=
from PySide.QtCore import QT_TRANSLATE_NOOP


class MyCommand:
class MyCommand:
"""Explanation of the command."""


def __init__(self):
def __init__(self):
# you can add things here like defining some variables that must exist at all times
"""Initialize variables for the command that must exist at all times."""
pass


def GetResources(self):
def GetResources(self):
"""Return a dictionary with data that will be used by the button or menu item."""
return {'Pixmap' : 'MyCommand.svg',
'Accel' : "Ctrl+A",
return {'Pixmap': 'MyCommand.svg',
'MenuText': QtCore.QT_TRANSLATE_NOOP("My_Command", "My Command"),
'Accel': "Ctrl+A",
'ToolTip': QtCore.QT_TRANSLATE_NOOP("My_Command", "Runs my command in the active document")}
'MenuText': QT_TRANSLATE_NOOP("My_Command", "My Command"),
'ToolTip': QT_TRANSLATE_NOOP("My_Command", "Runs my command in the active document")}


def Activated(self):
def Activated(self):
# place here the code to be executed when the command is ran
"""Run the following code when the command is activated (button press)."""
print("Activated")


def isActive(self):
def IsActive(self):
# here you have a chance to return True or False depending if your command must be shown as active or inactive (greyed).
"""Return True when the command should be active or False when it should be disabled (greyed)."""
return True


# the command must be "registered" in FreeCAD's command system
# The command must be "registered" with a unique name by calling its class.
FreeCADGui.addCommand('My_Command',MyCommand())
FreeCADGui.addCommand('My_Command', MyCommand())
}}
}}


== Examples ==
{{Userdocnavi{{#translation:}}}} <!-- Anything below this line must come after a closing translate tag -->

See [[Line_drawing_function|Line drawing function]].

<!-- Anything below this line must come after a closing translate tag -->
{{Powerdocnavi{{#translation:}}}}
[[Category:Developer Documentation{{#translation:}}]]
[[Category:Developer Documentation{{#translation:}}]]
[[Category:Command Reference{{#translation:}}]]
[[Category:Python Code{{#translation:}}]]
{{Category:Glossary{{#translation:}}}}
[[Category:Glossary{{#translation:}}]]

Latest revision as of 09:12, 18 April 2022

Introduction

O comandă în FreeCAD este o aplicație care se execută atunci când apăsați un buton din bara de instrumente sau apăsați o scurtătură din tastatură. Poate fi o acțiune foarte simplă, cum ar fi schimbarea nivelului de zoom al vizualizării 3D sau rotirea punctului de vedere sau un sistem complex care va deschide casetele de dialog și va aștepta ca utilizatorul să îndeplinească anumite sarcini.

Fiecare comandă FreeCAD are un nume unic care apare în lista tuturor comenzilor Category:Command_Reference. Comenzile pot fi invocate prin intermediul unui buton al barei de instrumente, al unei intrări în meniu, al unui script Python sau al consolei Python rulând:

FreeCADGui.runCommand("my_Command_Name")

Background

Comenzile FreeCAD sunt definite prin Atelier/workbench. Atelierele de lucru vor adăuga, de regulă, definițiile comenzilor lor la momentul de pornire al programului FreeCAD, astfel încât comanda să existe și să fie disponibilă imediat ce începe FreeCAD, indiferent dacă Atelierul de lucru este activat sau nu. Cu toate acestea, în unele cazuri, autorul Atelierului de lucru s-ar fi putut decide să nu supraîncărcați procesul de boot FreeCAD și, prin urmare, să încărcați definițiile comenzii numai la inițializarea spațiului de lucru. În acele cazuri, comanda nu va fi disponibilă până când nu ați activat Atelierul de lucru (ați fost comutat cel puțin o dată acolo cu selectorul de Ateliere).

Deoarece majoritatea dintre ele necesită interacțiune cu utilizatorul, comenzile FreeCAD sunt disponibile numai în modul GUI și nu în modul consola. Pentru simplificare, majoritatea comenzilor FreeCAD au o funcție Python corespunzătoare (cum ar fi Part.makeBox sau Draft.makeLine) sau execută un cod care poate fi ușor replicat într-un script Python.

Comenzile pot fi definite sau în C++ ori în Python.

Commands defined in C++

Exemplu de definiție a comenzii în C++ (usually defined in /Mod/ModuleName/Gui/Command.cpp):

DEF_STD_CMD_A(StdCmdMyCommand);

StdCmdMyCommand::StdCmdMyCommand()
  : Command("Std_My_Command")
{
    sGroup        = QT_TR_NOOP("File");
    sMenuText     = QT_TR_NOOP("My Command");
    sToolTipText  = QT_TR_NOOP("Runs my command in the active document");
    sWhatsThis    = "Std_MyCommand";
    sStatusTip    = QT_TR_NOOP("Runs my command in the active document");
    sPixmap       = "MyCommand.svg";
    sAccel        = "Ctrl+A";
}

void StdCmdExport::activated(int iMsg)
{
    // place here the code to be executed when the command is ran
}

bool StdCmdMyCommand::isActive(void)
{
    // here you have a chance to return true or false depending if your command must be shown as active or inactive (greyed).
}

// the command must be "registered" in FreeCAD's command system
CommandManager &rcCmdMgr = Application::Instance->commandManager();
rcCmdMgr.addCommand(new StdCmdMyCommand());

Commands defined in Python

și comanda similară în python (no rule for where it must be done, each python workbench does as it sees fit...)

from PySide.QtCore import QT_TRANSLATE_NOOP


class MyCommand:
    """Explanation of the command."""

    def __init__(self):
        """Initialize variables for the command that must exist at all times."""
        pass

    def GetResources(self):
        """Return a dictionary with data that will be used by the button or menu item."""
        return {'Pixmap': 'MyCommand.svg',
                'Accel': "Ctrl+A",
                'MenuText': QT_TRANSLATE_NOOP("My_Command", "My Command"),
                'ToolTip': QT_TRANSLATE_NOOP("My_Command", "Runs my command in the active document")}

    def Activated(self):
        """Run the following code when the command is activated (button press)."""
        print("Activated")

    def IsActive(self):
        """Return True when the command should be active or False when it should be disabled (greyed)."""
        return True

# The command must be "registered" with a unique name by calling its class.
FreeCADGui.addCommand('My_Command', MyCommand())

Examples

See Line drawing function.