Konsola Phyton

From FreeCAD Documentation
Revision as of 20:37, 12 December 2019 by Kaktus (talk | contribs) (Created page with "Konsola Pythona potrafi wykonać uzupełnienie podstawowego kodu. Gdy po obiekcie zostanie zapisana kropka, pokaże na przykład publiczne metody i atrybuty ''(zmienne)'' bie...")
(styczeń 2020) FreeCAD został pierwotnie zaprojektowany do współpracy z Pythonem 2. Ponieważ Python 2 osiągnął koniec życia w 2020 roku, dalszy rozwój FreeCAD będzie się odbywał wyłącznie z Pythonem 3, a kompatybilność wsteczna nie będzie utrzymywana.

Wprowadzenie

Konsola Pythona to panel, który uruchamia instancję interpretera Python, który może być używany do sterowania procesami FreeCAD oraz tworzenia i modyfikowania obiektów i ich właściwości.

Konsola Pythona w FreeCAD obsługuje podstawowe kolorowanie składni, potrafi rozróżnić różne style i kolory, komentarze, łańcuchy, wartości liczbowe, wbudowane funkcje, wydrukowany tekst wyjściowy i separatory, takie jak nawiasy i przecinki. Te właściwości konsoli można skonfigurować w Edytor preferencji.

Konsola Pythona pokazująca komunikaty, gdy FreeCAD właśnie się uruchamia.

Tworzenie skryptów

Dla absolutnie początkujących, zobacz: Wprowadzenie do Pythona, oraz Samouczek tworzenia skryptów Python.

Zobacz również: Podstawy tworzenia skryptów FreeCAD, oraz Obiekty skryptowe.

Konsola Pythona potrafi wykonać uzupełnienie podstawowego kodu. Gdy po obiekcie zostanie zapisana kropka, pokaże na przykład publiczne metody i atrybuty (zmienne) bieżącego obiektu (klasy), obj.

The console is also able to show the documentation string of a particular function when the opening parenthesis is written, for example, function(

Example Python code that produces objects in the 3D view.

The FreeCAD initialization scripts automatically load some modules, and define some aliases. Therefore, in the Python console these are available

App = FreeCAD
Gui = FreeCADGui

Therefore these are equal

App.newDocument()
FreeCAD.newDocument()

Note: these pre-loaded modules and aliases are only available from the Python console embedded inside the FreeCAD program. If you use FreeCAD as a library in an external program, you must remember to load the FreeCAD and FreeCADGui modules and define the necessary aliases if you wish.

Actions

Right click on the Python console shows some commands:

  • Copy: stores the selected text in the clipboard for later pasting; it is disabled if nothing is selected.
  • Copy command: stores the selected command in the clipboard for later pasting; it is disabled if nothing is selected.
  • Copy history: copy the entire history of Python commands entered in this session.
  • Save history as: save the entire history of Python commands entered in this session to a text file.
  • Paste: paste previously copied text in the clipboard to the Python console.
  • Select all: selects all text in the Python console.
  • Clear console: erases all commands entered into the Python console. This is useful if the Python console is full of messages and previously entered commands that may be distracting when testing a new function. This is merely aesthetic, as this command doesn't delete existing variables nor clears the imported modules in the session.
  • Insert file name: opens a dialog to search for a file in the system, then it inserts the full path of the file. This is useful to test functions that process an input file, without having to write the entire name in the console, which is error prone. This command does not run the file, and does not import it as a Python module, it just returns the full path of that file.
  • Word wrap: wrap very long lines that exceed the horizontal dimension of the Python console.

Template:Interface