Dostawca widoku
Wprowadzenie
Viewproviders are classes that define the way objects will look like in the tree view and the 3D view, and how they will interact with certain graphical actions such as selection.
They complement the scripted objects. While the base class of the scripted object defines its data properties, the viewprovider defines it view properties. These view properties are not essential information of the object, as they only indicate superficial information like line width, line color, face color, etc. In a terminal only session, the viewprovider is not loaded because there will be no interface to manipulate those visible properties.
Like with data properties, view properties are accessible from the property editor.
Dostawcy widoków Python
Klasy viewprovider zazwyczaj zawierają w nazwie ciąg ViewProvider
. Są one przypisywane do atrybutu ViewObject
obiektu bazowego.
W tym przykładzie definiujemy dwie właściwości dla Dostawcy widoku, tylko jeśli właściwości jeszcze nie istnieją, i przypisujemy im wartości domyślne. Definiujemy również metodę onChanged
, która jest uruchamiana za każdym razem, gdy zmienia się właściwość. Musimy przetestować właściwość według nazwy, a następnie wywołamy jedną z dwóch metod, które wykonają rzeczywistą pracę polegającą na aktualizacji wzorca lub ustawieniu jego rozmiaru.
# views/view_custom.py
class ViewProviderCustom:
"""Viewprovider of the custom object."""
def __init__(self, vobj):
self.Object = vobj.Object
self._set_properties(vobj)
vobj.Proxy = self
def _set_properties(self, vobj):
if not hasattr(vobj, "Pattern"):
vobj.addProperty("App::PropertyEnumeration",
"Pattern",
"Custom",
"Defines a hatch pattern for this object.")
vobj.Pattern = ["None", "diagonals", "cross", "brick"]
if not hasattr(vobj, "PatternSize"):
vobj.addProperty("App::PropertyFloat",
"PatternSize",
"Custom",
"Defines the size of the hatch pattern.")
vobj.PatternSize = 1
def onChanged(self, vobj, prop):
if prop in "Pattern":
self._set_pattern(vobj.Pattern)
if prop in "PatternSize":
self._set_size(vobj.PatternSize)
def _set_pattern(self, pattern):
...
def _set_size(self, size):
...
The normal workflow is to first add the object proxy class, for example, CustomObject
, and then the viewprovider, for example, ViewProviderCustom
. The viewprovider can only be assigned when we have verified that the graphical interface is available, as otherwise the ViewObject
attribute doesn't exist, and it will be an error to use this element as input for our class.
import FreeCAD as App
import objects.custom as custom
import views.view_custom as view_custom
doc = App.newDocument()
obj = doc.addObject("Part::FeaturePython", "Custom")
custom.CustomObject(obj)
if App.GuiUp:
view_custom.ViewProviderCustom(obj.ViewObject)
Ikonki użytkownika
Implementując metodę getIcon
, można zdefiniować ikonkę, która będzie wyświetlana w widok drzewa w górnej części widoku połączonego.
Wartością zwrotną może być pełna ścieżka do ikony.
import os
some_path = "/home/user/.FreeCAD/custom_icons"
class ViewProviderCustom:
...
def getIcon(self):
return os.path.join(some_path, "my_icon.svg")
Względna ścieżka do ikony w skompilowanym pliku zasobów.
import MyModule_rc.py
class ViewProviderCustom:
...
def getIcon(self):
return ":/icons/my_icon.svg"
Nieobrobiona Ikona XPM, która jest zasadniczo grafiką ASCII.
import MyModule_rc.py
class ViewProviderCustom:
...
def getIcon(self):
return """
/* XPM */
static char *Some_icon_xpm[] = {
/* columns rows colors chars-per-pixel */
"16 16 3 1 ",
" c None",
". c #D71414",
"+ c #AA1919",
/* pixels */
" ",
" + + ",
" +.+ +.+ ",
" +.+ +.+ ",
" + + ",
" ++++ ",
" +....+ ",
" +...++ ",
" +..+++ ",
" +.++.+ ",
" ++++ ",
" + + ",
" +.+ +.+ ",
" +.+ +.+ ",
" + + ",
" "
};
"""
Zobacz różne przykłady na stronie Własna ikona w widoku drzewa.
- Tworzenie skryptów FreeCAD: Python, Wprowadzenie do środowiska Python, Poradnik: Tworzenie skryptów Python, Podstawy tworzenia skryptów FreeCAD
- Moduły: Moduły wbudowane, Jednostki miar, Ilość
- Środowiska pracy: Tworzenie Środowiska pracy, Polecenia Gui, Polecenia, Instalacja większej liczby Środowisk pracy
- Siatki i elementy: Skrytpy w Środowisku Siatek, v, Konwerska Mesh na Part, PythonOCC
- Obiekty parametryczne: Obiekty tworzone skryptami, Obsługa obrazu (Ikonka niestandardowa w widoku drzewa)
- Scenegraph: Coin (Inventor) scenegraph, Pivy
- Interfejs graficzny: Stworzenie interfejsu, Kompletne stworzenie interfejsu w środowisku Python (1, 2, 3, 4, 5), PySide, PySide examples początkujący, średniozaawansowany, zaawansowany
- Makrodefinicje: Makrodefinicje, Instalacja makrodefinicji
- Osadzanie programu: Osadzanie programu FreeCAD, Osadzanie GUI FreeCAD
- Pozostałe: Wyrażenia, Wycinki kodu, Funkcja kreślenia linii, Biblioteka matematyczna FreeCAD dla wektorów (deprecated)
- Węzły użytkowników: Centrum użytkownika, Centrum Power użytkowników, Centrum programisty