Viewprovider: Difference between revisions

From FreeCAD Documentation
(→‎Introduction: Like with data properties, view properties are accessible from the property editor.)
(→‎Python view providers: Added specific properties)
Line 17: Line 17:
{{Code|code=
{{Code|code=
# views/view_custom.py
# views/view_custom.py

class ViewProviderCustom:
class ViewProviderCustom:
"""Viewprovider of the custom object."""
"""Viewprovider of the custom object."""
Line 28: Line 27:


def _set_properties(self, vobj):
def _set_properties(self, vobj):
if not hasattr(vobj, "Pattern"):
...
_tip = "Defines a hatch pattern for this object."
vobj.addProperty("App::PropertyEnumeration",
"Pattern",
"Custom",
_tip)
vobj.Pattern = ["None", "diagonals", "cross", "brick"]

if not hasattr(vobj, "PatternSize"):
_tip = "Defines the size of the hatch pattern."
vobj.addProperty("App::PropertyFloat",
"PatternSize",
"Draft",
_tip)
vobj.PatternSize = 1
}}
}}
<translate>
<translate>

Revision as of 01:10, 12 May 2020

Introduction

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.

Python view providers

The viewproviders classes usually include ViewProvider in their name. They are assigned on the ViewObject attribute of the base object.

# 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"):
            _tip = "Defines a hatch pattern for this object."
            vobj.addProperty("App::PropertyEnumeration",
                             "Pattern",
                             "Custom",
                             _tip)
            vobj.Pattern = ["None", "diagonals", "cross", "brick"]

        if not hasattr(vobj, "PatternSize"):
            _tip = "Defines the size of the hatch pattern."
            vobj.addProperty("App::PropertyFloat",
                             "PatternSize",
                             "Draft",
                             _tip)
            vobj.PatternSize = 1

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
import views.view_custom

obj = doc.addObject("Part::FeaturePython", "Custom")

objects.custom.CustomObject(obj)

if App.GuiUp:
    views.view_custom.ViewProviderCustom(obj.ViewObject)