Create a FeaturePython object part II/fr: Difference between revisions

From FreeCAD Documentation
(Updating to match new version of source page)
(Updating to match new version of source page)
 
(101 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<languages/>
<languages/>


{{Docnav/fr
<div class="mw-translate-fuzzy">
|[[Create_a_FeaturePython_object_part_I/fr|Créer un objet FeaturePython partie I]]
{{docnav/fr|[[FeaturePython Objects/fr|Objects FeaturePython]]|[[Scripting examples/fr|Exemples de Scripts]]}}
|
</div>
|IconL=
|IconR=
}}


{{TOCright}}
=== App::FeaturePython vs. Part::FeaturePython ===


<span id="Introduction"></span>
To this point, we've focused on the internal aspects of a Python class built around a FeaturePython object - specifically, an App::FeaturePython object.<br>
== Introduction ==
We've created the object, defined some properties, and added some document-level event callbacks that allow our object to respond to a document recompute with the {{incode|execute()}}> method.


Sur la page [[Create_a_FeaturePython_object_part_I/fr|Créer un objet FeaturePython partie I]], nous nous sommes concentrés sur les aspects internes d'une classe Python construite autour d'un objet FeaturePython, en particulier un objet {{incode|App::FeaturePython}}. Nous avons créé l'objet, défini certaines propriétés et ajouté un rappel d'événement au niveau du document qui permet à notre objet de répondre à un recalcul de document avec la méthode {{incode|execute()}}. Mais notre objet n'a toujours pas de présence dans la [[3D_view/fr|Vue 3D]]. Remédions à cela en ajoutant une boîte.
'''But we still don't have a box.'''


<span id="Adding_a_box"></span>
We can easily create one, however, by adding just two lines.
==Ajout d'une boîte==


Tout d'abord, en haut du fichier {{FileName|box.py}}, sous l'importation existante, ajoutez:
First, add a new import to the top of the file: {{incode|import Part}}

Then, in {{incode|execute()}}, add the following line (you can delete the {{incode|print()}} statement):


{{Code|code=
{{Code|code=
import Part
def execute():
"""
Called on document recompute
"""

Part.show(Part.makeBox(obj.Length, obj.Width, obj.Height)
}}
}}


Puis dans {{incode|execute()}} supprimez l'instruction {{incode|print()}} et ajoutez la ligne suivante à sa place:
These commands execute python scripts that come with FreeCAD as a default.
*The {{incode|makeBox()}} method generates a new box Shape.
*The enclosing call to {{incode|Part.show()}} adds the Shape to the document tree and makes it visible.


{{Code|code=
If you have FreeCAD open, reload the box module and create a new box object using {{incode|box.create()}} (delete any existing box objects, just to keep things clean).
Part.show(Part.makeBox(obj.Length, obj.Width, obj.Height))
}}


[[Image:App_featurepython_box.png | right]]
Notice how a box immediately appears on the screen. that's because the execute method is called immediately after the box is created, because we force the document to recompute at the end of {{incode|box.create()}}


Ces commandes exécutent des méthodes Python fournies avec FreeCAD par défaut.
* La méthode {{incode|Part.makeBox()}} génère une nouvelle forme de boîte.
* L'appel englobant à {{incode|Part.show()}} ajoute la forme à l'arborescence du document et la rend visible.


Supprimez tous les objets existants, rechargez le module boîte et créez un nouvel objet boîte en utilisant {{incode|box.create()}}. Remarquez comment une boîte apparaît immédiatement à l'écran. C'est parce que nous forçons le document à recalculer à la fin de {{incode|box.create()}} qui à son tour déclenche la méthode {{incode|execute()}} de notre classe {{incode|box}}.
[[File:App_featurepython_box.png]]


À première vue, le résultat peut sembler correct mais il y a quelques problèmes. Le plus évident est que la boîte est représentée par un objet entièrement différent de notre objet FeaturePython. {{incode|Part.show()}} crée simplement un objet boîte séparé et l'ajoute au document. Pire encore, si vous modifiez les dimensions de l'objet FeaturePython, une autre forme de boîte est créée et l'ancienne est laissée en place. Et si vous avez ouvert la [[Report_view/fr|Vue rapport]], vous avez peut-être remarqué une erreur indiquant "Appel récursif de recalcul pour le document Sans nom". Cela a à voir avec l'utilisation de la méthode {{incode|Part.show()}} dans un objet FeaturePython.
'''But there's a problem.'''
{{clear}}


[[#top|En haut]]
It should be pretty obvious. The box itself is represented by an entirely different object than our FeaturePython object. The reason is because {{incode|Part.show()}} creates a separate box object and adds it to the document. In fact, if you go to your FeaturePython object and change the dimensions, you'll see another box shape gets created and the old one is left in place. That's not good! Additionally, if you have the Report View open, you may notice an error stating 'Nested recomputes of a document are not allowed". This has to do with using the Part.show() method inside a FeaturePython object. We want to avoid doing that.


<span id="Fixing_the_code"></span>
'''Tip'''
==Correction du code==


Pour résoudre ces problèmes, nous devons apporter un certain nombre de changements. Jusqu'à présent, nous avons utilisé un objet {{incode|App::FeaturePython}} qui n'est en fait pas destiné à avoir une représentation visuelle dans la vue 3D. Nous devons utiliser un objet {{incode|Part::FeaturePython}} à la place. Dans {{incode|create()}} modifiez la ligne suivante:
The problem is, we're relying on the {{incode|Part.make*()}} methods, which only generate non-parametric Part::Feature objects (simple, dumb shapes), just as you'd get if you copied a parametric object using [[Part SimpleCopy|Part Simple Copy]].


{{Code|code=
What we want, of course, is a '''parametric''' box object that resizes the existing box as we change it's properties. We could delete the previous Part::Feature object and regenerate it every time we change a property, but we still have two objects to manage - our custom FeaturePython object and the Part::Feature object it generates.
obj = App.ActiveDocument.addObject('App::FeaturePython', obj_name)
}}


par:
'''So how do we solve this problem?'''


{{Code|code=
First, we need to use the right type of object.
obj = App.ActiveDocument.addObject('Part::FeaturePython', obj_name)
}}


Pour se débarrasser de l'objet box séparé, nous devons affecter le résultat de la méthode {{incode|makeBox()}} à la propriété {{incode|Shape}} de notre objet {{incode|Part::FeaturePython}}. Modifiez cette ligne dans {{incode|execute()}}:
To this point we've been using {{incode|App::FeaturePython}} objects. They're great, but they're not intended for use as geometry objects. Rather, they are better used as document objects which do not require a visual representation in the 3D view. So we need to use a {{incode|Part::FeaturePython}} object ibstead.


{{Code|code=
Part.show(Part.makeBox(obj.Length, obj.Width, obj.Height))
}}


en:
In {{incode|create()}}, change the following line:


{{Code|code=
obj = App.ActiveDocument.addObject('App::FeaturePython', obj_name)
obj.Shape = Part.makeBox(obj.Length, obj.Width, obj.Height)

}}
to read:

obj = App.ActiveDocument.addObject('{{ColoredText||red|Part::FeaturePython}}', obj_name)

To finish making the changes we need, the following line in the {{incode|execute()}} method needs to be changed:

Part.show(Part.makeBox(obj.Length, obj.Width, obj.Height))

to:

obj.Shape = Part.makeBox(obj.Length, obj.Width, obj.Height)

Your code should look like this:

import FreeCAD as App
import Part
def create(obj_name):
"""
Object creation method
"""
obj = App.ActiveDocument.addObject('Part::FeaturePython', obj_name)
fpo = box(obj)
App.ActiveDocument.recompute()
return fpo
class box():
def __init__(self, obj):
"""
Default Constructor
"""
self.Type = 'box'
obj.addProperty('App::PropertyString', 'Description', 'Base', 'Box description').Description = ""
obj.addProperty('App::PropertyLength', 'Length', 'Dimensions', 'Box length').Length = 10.0
obj.addProperty('App::PropertyLength', 'Width', 'Dimensions', 'Box width').Width = '10 mm'
obj.addProperty('App::PropertyLength', 'Height', 'Dimensions', 'Box height').Height = '1 cm'
obj.Proxy = self
self.Object = obj
def execute(self, obj):
"""
Called on document recompute
"""
obj.Shape = Part.makeBox(obj.Length, obj.Width, obj.Height)


[[File:Part_featurepython_no_vp.png|right]]
[[File:Part_featurepython_no_vp.png|right]]


Enregistrez vos modifications, revenez à FreeCAD, supprimez tous les objets existants, rechargez le module de boîte et créez un nouvel objet boîte. Le nouveau résultat est quelque peu décevant. Il n'y a plus d'objet supplémentaire dans l'arborescence, et l'icône dans l'arborescence a changé, mais notre boîte dans la vue 3D a également disparu (c'est pourquoi l'icône est grise). Qu'est ce qui c'est passé? Bien que nous ayons correctement créé notre forme de boîte et l'avons assignée à un objet {{incode|Part::FeaturePython}}, pour le faire apparaître réellement dans la vue 3D, nous devons attribuer un [[Viewprovider/fr|ViewProvider]].
Now, save your changes and switch back to FreeCAD. Delete any existing objects, reload the box module, and create a new box object.
{{clear}}


[[#top|En haut]]
The results may seem a bit mixed. The icon in the treeview is different - it's a box, now. But there's no cube. And the icon is still gray!


<span id="Writing_a_ViewProvider"></span>
What happened? Although we've properly created our box shape and assigned it to a {{incode|Part::FeaturePython}} object, before we can make it show up in our 3D view, we need to assign a '''ViewProvider.'''
==Écrire un ViewProvider==


Un fournisseur de vue (View Provider) est le composant d'un objet qui lui permet d'avoir une représentation visuelle dans la vue 3D. FreeCAD utilise une structure d'application conçue pour séparer les données (le "modèle") de sa représentation visuelle (la "vue"). Si vous avez passé du temps à travailler avec FreeCAD en Python, vous en êtes probablement déjà conscient grâce à l'utilisation des deux modules Python de base: {{incode|FreeCAD}} et {{incode|FreeCADGui}} (souvent sous les alias { {incode|App}} et {{incode|Gui}} respectivement).
=== Writing a ViewProvider ===


Notre objet FeaturePython nécessite également ces éléments. Jusqu'à présent, nous nous sommes concentrés uniquement sur la partie "modèle" du code, il est maintenant temps d'écrire la partie "vue". Heureusement, la plupart des ViewProviders sont simples et nécessitent peu d'efforts pour écrire, du moins pour commencer. Voici un exemple de ViewProvider emprunté et légèrement modifié à [https://github.com/FreeCAD/FreeCAD/blob/master/src/Mod/TemplatePyMod/FeaturePython.py]:
A View Provider is the component of an object which allows it to have visual representation in the GUI - specifically in the 3D view. FreeCAD uses an application structure known as 'model-view', which is designed to separate the data (the 'model') from it's visual representation (the 'view'). If you've spent any time working with FreeCAD in Python, you'll likely already be aware of this through the use of two core Python modules: FreeCAD and FreeCADGui (often aliased as 'App' and 'Gui' repectively).


{{Code|code=
Thus, our FeaturePython Box implementation also requires these elements. Thus far, we've focused purely on the 'model' portion, so now it's time to write the 'view'. Fortunately, most view implementations are simple and require little effort to write, at least to get started. Here's an example ViewProvider, borrowed and slightly modified from [https://github.com/FreeCAD/FreeCAD/blob/master/src/Mod/TemplatePyMod/FeaturePython.py]
class ViewProviderBox:


class ViewProviderBox:
def __init__(self, obj):
def __init__(self, obj):
"""
"""
Set this object to the proxy object of the actual view provider
Set this object to the proxy object of the actual view provider
"""
"""

obj.Proxy = self
obj.Proxy = self

def attach(self, obj):
def attach(self, obj):
"""
"""
Line 139: Line 99:
"""
"""
return
return

def updateData(self, fp, prop):
def updateData(self, fp, prop):
"""
"""
Line 145: Line 105:
"""
"""
return
return

def getDisplayModes(self,obj):
def getDisplayModes(self,obj):
"""
"""
Return a list of display modes.
Return a list of display modes.
"""
"""
return None
return []

def getDefaultDisplayMode(self):
def getDefaultDisplayMode(self):
"""
"""
Line 157: Line 117:
"""
"""
return "Shaded"
return "Shaded"

def setDisplayMode(self,mode):
def setDisplayMode(self,mode):
"""
"""
Line 165: Line 125:
"""
"""
return mode
return mode

def onChanged(self, vp, prop):
def onChanged(self, vp, prop):
"""
"""
Print the name of the property that has changed
Print the name of the property that has changed
"""
"""

App.Console.PrintMessage("Change property: " + str(prop) + "\n")
App.Console.PrintMessage("Change property: " + str(prop) + "\n")

def getIcon(self):
def getIcon(self):
"""
"""
Return the icon in XMP format which will appear in the tree view. This method is optional and if not defined a default icon is shown.
Return the icon in XMP format which will appear in the tree view. This method is optional and if not defined a default icon is shown.
"""
"""

return """
return """
/* XPM */
/* XPM */
static const char * ViewProviderBox_xpm[] = {
static const char * ViewProviderBox_xpm[] = {
"16 16 6 1",
"16 16 6 1",
" c None",
" c None",
". c #141010",
". c #141010",
"+ c #615BD2",
"+ c #615BD2",
"@ c #C39D55",
"@ c #C39D55",
"# c #000000",
"# c #000000",
"$ c #57C355",
"$ c #57C355",
" ........",
" ........",
" ......++..+..",
" ......++..+..",
Line 205: Line 165:
" ####### "};
" ####### "};
"""
"""

def __getstate__(self):
def dumps(self):
"""
"""
Called during document saving.
Called during document saving.
"""
"""
return None
return None

def __setstate__(self,state):
def loads(self,state):
""""
"""
Called during document restore.
Called during document restore.
""""
"""
return None
return None
}}


Note in the above code, we also define an XMP icon for this object. Icon design is beyond the scope of this tutorial, but basic icon designs can be managed using open source tools like [https://www.gimp.org GIMP], [https://krita.org/en/ Krita], and [https://inkscape.org/ Inkscape]. The getIcon method is optional, as well. If it is not provided, FreeCAD will provide a default icon.
Dans le code ci-dessus, nous définissons une icône XMP pour cet objet. La conception des icônes dépasse le cadre de ce tutoriel mais la conception de base peut être gérée à l'aide d'outils open source tels que [https://www.gimp.org GIMP], [https://krita.org/en/ Krita] et [ https://inkscape.org/ Inkscape]. La méthode {{incode|getIcon()}} est facultative, FreeCAD utilisera une icône par défaut si cette méthode n'est pas fournie.


Ajoutez le code ViewProvider à la fin de {{FileName|box.py}} et dans la méthode {{incode|create()}} insérez la ligne suivante au-dessus de l'instruction {{incode|recompute()}}:


{{Code|code=
With out ViewProvider defined, we now need to put it to use to give our object the gift of visualization.

Return to the {{incode|create()}} method in your code and add the following near the end:

ViewProviderBox(obj.ViewObject)
ViewProviderBox(obj.ViewObject)
}}


This instances the custom ViewProvider class and passes the FeaturePython's built-in ViewObject to it. The ViewObject won't do anything without our custom class implementation, so when the ViewProvider class initializes, it saves a reference to itself in the FeaturePython's ViewObject.Proxy attribute. This way, when FreeCAD needs to render our Box visually, it can find the ViewProvider class to do that.
Cela instancie la classe ViewProvider personnalisée et lui transmet le ViewObject intégré de FeaturePython. Lorsque la classe ViewProvider s'initialise, elle enregistre une référence à elle-même dans l'attribut {{incode|ViewObject.Proxy}} de FeaturePython. De cette façon, lorsque FreeCAD a besoin de rendre notre boîte visuellement, il peut trouver la classe ViewProvider pour le faire.


Now, save the changes and return to FreeCAD. Import or reload the Box module and call {{incode|box.create()}}.
Maintenant, enregistrez les modifications et revenez à FreeCAD. Importez ou rechargez le module box et appelez {{incode|box.create()}}. Vous devriez maintenant voir deux choses:
* L'icône de l'objet boîte a changé.
* Et, plus important encore, il y a une boîte dans la vue 3D. Si vous ne le voyez pas, appuyez sur le bouton {{Button|[[Image:Std_ViewFitAll.svg|16px]] [[Std_ViewFitAll/fr|Std Tout afficher]]}}. Vous pouvez même modifier les dimensions de la boîte en modifiant les valeurs dans l'[[Property_editor/fr|Éditeur de propriétés]]. Essayez!


[[#top|En haut]]
We still don't see anything, but notice what happened to the icon next to the box object. It's in color! And it has a shape! That's a clue that our ViewProvider is working as expected.


<span id="Trapping_events"></span>
So now, it's time to actually *add* a box.
==Piégeage d'événements==


Nous avons déjà discuté du piégeage d'événements. Presque toutes les méthodes d'une classe FeaturePython servent de rappel accessible à l'objet FeaturePython (qui obtient l'accès à notre instance de classe via l'attribut {{incode|Proxy}}, si vous vous en souvenez).
Return to the code and add the following line to the execute() method:


Voici une liste des rappels pouvant être implémentés dans l'objet FeaturePython de base:
obj.Shape = Part.makeBox(obj.Length, obj.Width, obj.Height)


{| class="wikitable" cellpadding="5px" width="100%"
Now save, reload the box module in FreeCAD and create a new box.
|+ FeaturePython basic callbacks

|style="width:25%" | {{incode|execute(self, obj)}}
You should see a box appear on screen! If it's too big (or maybe doesn't show up at all), click one of the 'ViewFit' buttons to fit it to the 3D view.
|style="width:25%" | Appelé pendant le recalcul du document

|style="width:50%" | N'appelez pas {{incode|recompute()}} depuis cette méthode (ou toute méthode appelée depuis {{incode|execute()}}) car cela provoque un recalcul imbriqué.
Note what we did here that differs from the way it was implemented for App::FeaturePython above. In the {{incode|execute()}} method, we called the {{incode|Part.makeBox()}} macro and passed it the box properties as before. But rather than call {{incode|Part.show()}} on the resulting object (which created a new, separate box object), we simply assigned it to the {{incode|Shape}} property of our Part::FeaturePython object instead.

You can even alter the box dimensions by changing the values in the FreeCAD property panel. Give it a try!

=== Trapping Events ===

To this point, we haven't explicitly addressed event trapping. Nearly every method of a FeaturePython class serves as a callback accessible to the FeaturePython object (which gets access to our class instance through the {{incode|Proxy}} attribute, if you recall).

Below is a list of the callbacks that may be implemented in the basic FeaturePython object:

{| class="wikitable"

|+style="caption-side:bottom; | FeaturePython basic callbacks
|{{incode|execute(self, obj)}} || Called during document recomputes || Do not call {{incode|recompute()}} from this method (or any method called from {{incode|execute()}}) as this causes a nested recompute.
|-
|-
| {{incode|onBeforeChange(self, obj, prop)}}
|{{incode|onBeforeChanged(self, obj, prop)}} || Called before a property value is changed || {{incode|prop}} is the name of the property to be changed, not the property object itself. Property changes cannot be cancelled. Previous / next property values are not simultaneously available for comparison.
| Appelé avant la modification d'une valeur de propriété
| {{incode|prop}} est le nom de la propriété à modifier, pas l'objet de propriété lui-même. Les modifications de propriété ne peuvent pas être annulées. Les valeurs de propriétés précédentes/suivantes ne sont pas disponibles simultanément pour la comparaison.
|-
|-
|{{incode|onChanged(self, obj, prop)}} || Called after a property is changed || {{incode|prop}} is the name of the property to be changed, not the property object itself.
| {{incode|onChanged(self, obj, prop)}}
| Appelé après la modification d'une propriété
| {{incode|prop}} est le nom de la propriété à modifier, pas l'objet de propriété lui-même.
|-
|-
| {{Incode|onDocumentRestored(self, obj)}}
|{{Incode|onDocumentRestored(self, obj)}} || Called after a document is restored or aFeaturePython object is copied / duplicated. || Occasionally, references to the FeaturePython object from the class, or the class from the FeaturePython object may be broken, as the class {{incode|__init__()}} method is not called when the object is reconstructed. Adding {{incode|self.Object = obj}} or {{incode|obj.Proxy = self}} often solves these issues.
| Appelé après la restauration d'un document ou la copie d'un objet FeaturePython.

| Parfois, les références à l'objet FeaturePython de la classe ou à la classe de l'objet FeaturePython peuvent être interrompues, car la méthode class {{incode|__init __()}} n'est pas appelée lors de la reconstruction de l'objet. L'ajout de {{incode|self.Object <nowiki>=</nowiki> obj}} ou {{incode|obj.Proxy <nowiki>=</nowiki> self}} résout souvent ces problèmes.
|}
|}


Pour une référence complète des méthodes FeaturePython disponibles, voir [[FeaturePython_methods/fr|Méthodes FeaturePython]].
In addition, there are two callbacks in the ViewProvider class that may occasionally prove useful:


En outre, il existe deux rappels dans la classe ViewProvider qui peuvent parfois s'avérer utiles:
{| class="wikitable"


{| class="wikitable" cellpadding="5px" width="100%"
|+style="caption-side:bottom; | ViewProvider basic callbacks
|+ ViewProvider basic callbacks
|-
|-
|style="width:25%" | {{incode|updateData(self, obj, prop)}}
|{{incode|updateData(self, obj, prop)}} || Called after a data (model) property is changed || {{incode|obj}} is a reference to the FeaturePython class instance, not the ViewProvider instance. {{incode|prop}} is the name of the property to be changed, not the property object itself.
|style="width:25%" | Appelé après la modification d'une propriété de données (modèle)
|style="width:50%" | {{incode|obj}} est une référence à l'instance de classe FeaturePython et non à l'instance ViewProvider. {{incode|prop}} est le nom de la propriété à modifier, pas l'objet de propriété lui-même.
|-
|-
| {{incode|onChanged(self, vobj, prop)}}
|{{incode|onChanged(self, vobj, prop)}} || Called after a view property is changed || {{incode|vobj}} is a reference to the ViewProvider instance. {{incode|prop}} is the name of the view property which was changed.
| Appelé après la modification d'une propriété de vue
| {{incode|vobj}} est une référence à l'instance ViewProvider. {{incode|prop}} est le nom de la propriété de vue qui a été modifiée.
|}
|}


Il n'est pas rare de rencontrer une situation où les rappels Python ne sont pas déclenchés comme ils le devraient. Les débutants dans ce domaine peuvent être assurés que le système de rappel FeaturePython n'est pas fragile ou cassé. Invariablement, lorsque les rappels ne s'exécutent pas, c'est parce qu'une référence est perdue ou indéfinie dans le code sous-jacent. Si, cependant, les rappels semblent interrompre sans explication, fournir des références d'objet/proxy dans le rappel {{incode|onDocumentRestored()}} (comme indiqué dans le premier tableau ci-dessus) peut atténuer ces problèmes. Tant que vous n'êtes pas à l'aise avec le système de rappel, il peut être utile d'ajouter des instructions d'impression dans chaque rappel pour imprimer des messages sur la console pendant le développement.


[[#top|En haut]]
{| class="wikitable"
!{{ColoredParagraph|Tip}}


<span id="Complete_code"></span>
|-
==Le code terminé==
|It is not uncommon to encounter a situation where the Python callbacks are not being triggered as they should. Beginners in this area need to rest assured that the FeaturePython callback system is not fragile or broken. Invariably, when callbacks fail to run, it is because a reference is lost or undefined in the underlying code. If, however, callbacks appear to be breaking with no explanation, providing object / proxy references in the {{incode|onDocumentRestored()}} callback (as noted in the first table above) may alleviate these problems. Until you are comfortable with the callback system, it may be useful to add print statements in each callback to print messages to the console as a diagnostic during development.


{{Code|code=
|}
import FreeCAD as App

import Part
=== The Code ===

The complete code for this example:


def create(obj_name):
import FreeCAD as App
import Part
def create(obj_name):
"""
"""
Object creation method
Object creation method
"""
"""

obj = App.ActiveDocument.addObject('Part::FeaturePython', obj_name)
obj = App.ActiveDocument.addObject('Part::FeaturePython', obj_name)

fpo = box(obj)
box(obj)

ViewProviderBox(obj.ViewObject)
ViewProviderBox(obj.ViewObject)

App.ActiveDocument.recompute()
App.ActiveDocument.recompute()

return fpo
return obj

class box():

class box():
def __init__(self, obj):
def __init__(self, obj):
"""
"""
Default Constructor
Default constructor
"""
"""

self.Type = 'box'
self.Type = 'box'

self.ratios = None
obj.addProperty('App::PropertyString', 'Description', 'Base', 'Box description').Description = 'Hello World!'
obj.addProperty('App::PropertyLength', 'Length', 'Dimensions', 'Box length').Length = 10.0
obj.addProperty('App::PropertyLength', 'Width', 'Dimensions', 'Box width'). Width = '10 mm'
obj.addProperty('App::PropertyLength', 'Height', 'Dimensions', 'Box Height').Height = '1 cm'
obj.addProperty('App::PropertyBool', 'Aspect Ratio', 'Dimensions', 'Lock the box aspect ratio').Aspect_Ratio = False
obj.Proxy = self
obj.Proxy = self

self.Object = obj
obj.addProperty('App::PropertyString', 'Description', 'Base', 'Box description').Description = ""
obj.addProperty('App::PropertyLength', 'Length', 'Dimensions', 'Box length').Length = 10.0
def __getstate__(self):
obj.addProperty('App::PropertyLength', 'Width', 'Dimensions', 'Box width').Width = '10 mm'
return self.Type
obj.addProperty('App::PropertyLength', 'Height', 'Dimensions', 'Box height').Height = '1 cm'

def __setstate__(self, state):
if state:
self.Type = state
def execute(self, obj):
def execute(self, obj):
"""
"""
Called on document recompute
Called on document recompute
"""
"""

obj.Shape = Part.makeBox(obj.Length, obj.Width, obj.Height)
obj.Shape = Part.makeBox(obj.Length, obj.Width, obj.Height)

class ViewProviderBox:

class ViewProviderBox:
def __init__(self, obj):
def __init__(self, obj):
"""
"""
Set this object to the proxy object of the actual view provider
Set this object to the proxy object of the actual view provider
"""
"""

obj.Proxy = self
obj.Proxy = self

def attach(self, obj):
def attach(self, obj):
"""
"""
Line 356: Line 300:
"""
"""
return
return

def updateData(self, fp, prop):
def updateData(self, fp, prop):
"""
"""
If a property of the handled feature has changed we have the chance to handle this here
If a property of the handled feature has changed we have the chance to handle this here
"""
"""
return
return

def getDisplayModes(self,obj):
def getDisplayModes(self,obj):
"""
"""
Return a list of display modes.
Return a list of display modes.
"""
"""
return None
return []

def getDefaultDisplayMode(self):
def getDefaultDisplayMode(self):
"""
"""
Line 375: Line 318:
"""
"""
return "Shaded"
return "Shaded"

def setDisplayMode(self,mode):
def setDisplayMode(self,mode):
"""
"""
Line 383: Line 326:
"""
"""
return mode
return mode

def onChanged(self, vobj, prop):
def onChanged(self, vp, prop):
"""
"""
Print the name of the property that has changed
Print the name of the property that has changed
"""
"""

App.Console.PrintMessage("Change property: " + str(prop) + "\n")
App.Console.PrintMessage("Change property: " + str(prop) + "\n")

def getIcon(self):
def getIcon(self):
"""
"""
Return the icon in XMP format which will appear in the tree view. This method is optional and if not defined a default icon is shown.
Return the icon in XMP format which will appear in the tree view. This method is optional and if not defined a default icon is shown.
"""
"""

return """
return """
/* XPM */
/* XPM */
static const char * ViewProviderBox_xpm[] = {
static const char * ViewProviderBox_xpm[] = {
"16 16 6 1",
"16 16 6 1",
" c None",
" c None",
". c #141010",
". c #141010",
"+ c #615BD2",
"+ c #615BD2",
"@ c #C39D55",
"@ c #C39D55",
"# c #000000",
"# c #000000",
"$ c #57C355",
"$ c #57C355",
" ........",
" ........",
" ......++..+..",
" ......++..+..",
Line 423: Line 366:
" ####### "};
" ####### "};
"""
"""

def __getstate__(self):
def dumps(self):
"""
Called during document saving.
"""
return None
return None

def __setstate__(self,state):
def loads(self,state):
"""
Called during document restore.
"""
return None
return None
}}


[[#top|En haut]]
<div class="mw-translate-fuzzy">
{{docnav/fr|[[FeaturePython Objects/fr|Objects FeaturePython]]|[[Scripting examples/fr|Exemples de Scripts]]}}
</div>


{{Userdocnavi/fr}}
{{Docnav/fr
|[[Create_a_FeaturePython_object_part_I/fr|Créer un objet FeaturePython partie I]]

|
[[Category:Tutorials/fr]]
|IconL=

|IconR=
[[Category:Python Code/fr]]
}}


{{Powerdocnavi{{#translation:}}}}
[[Category:Poweruser Documentation/fr]]
[[Category:Developer Documentation{{#translation:}}]]
[[Category:Python Code{{#translation:}}]]
{{clear}}

Latest revision as of 09:00, 27 January 2024

Other languages:

Introduction

Sur la page Créer un objet FeaturePython partie I, nous nous sommes concentrés sur les aspects internes d'une classe Python construite autour d'un objet FeaturePython, en particulier un objet App::FeaturePython. Nous avons créé l'objet, défini certaines propriétés et ajouté un rappel d'événement au niveau du document qui permet à notre objet de répondre à un recalcul de document avec la méthode execute(). Mais notre objet n'a toujours pas de présence dans la Vue 3D. Remédions à cela en ajoutant une boîte.

Ajout d'une boîte

Tout d'abord, en haut du fichier box.py, sous l'importation existante, ajoutez:

import Part

Puis dans execute() supprimez l'instruction print() et ajoutez la ligne suivante à sa place:

Part.show(Part.makeBox(obj.Length, obj.Width, obj.Height))

Ces commandes exécutent des méthodes Python fournies avec FreeCAD par défaut.

  • La méthode Part.makeBox() génère une nouvelle forme de boîte.
  • L'appel englobant à Part.show() ajoute la forme à l'arborescence du document et la rend visible.

Supprimez tous les objets existants, rechargez le module boîte et créez un nouvel objet boîte en utilisant box.create(). Remarquez comment une boîte apparaît immédiatement à l'écran. C'est parce que nous forçons le document à recalculer à la fin de box.create() qui à son tour déclenche la méthode execute() de notre classe box.

À première vue, le résultat peut sembler correct mais il y a quelques problèmes. Le plus évident est que la boîte est représentée par un objet entièrement différent de notre objet FeaturePython. Part.show() crée simplement un objet boîte séparé et l'ajoute au document. Pire encore, si vous modifiez les dimensions de l'objet FeaturePython, une autre forme de boîte est créée et l'ancienne est laissée en place. Et si vous avez ouvert la Vue rapport, vous avez peut-être remarqué une erreur indiquant "Appel récursif de recalcul pour le document Sans nom". Cela a à voir avec l'utilisation de la méthode Part.show() dans un objet FeaturePython.

En haut

Correction du code

Pour résoudre ces problèmes, nous devons apporter un certain nombre de changements. Jusqu'à présent, nous avons utilisé un objet App::FeaturePython qui n'est en fait pas destiné à avoir une représentation visuelle dans la vue 3D. Nous devons utiliser un objet Part::FeaturePython à la place. Dans create() modifiez la ligne suivante:

obj = App.ActiveDocument.addObject('App::FeaturePython', obj_name)

par:

obj = App.ActiveDocument.addObject('Part::FeaturePython', obj_name)

Pour se débarrasser de l'objet box séparé, nous devons affecter le résultat de la méthode makeBox() à la propriété Shape de notre objet Part::FeaturePython. Modifiez cette ligne dans execute():

Part.show(Part.makeBox(obj.Length, obj.Width, obj.Height))

en:

obj.Shape = Part.makeBox(obj.Length, obj.Width, obj.Height)

Enregistrez vos modifications, revenez à FreeCAD, supprimez tous les objets existants, rechargez le module de boîte et créez un nouvel objet boîte. Le nouveau résultat est quelque peu décevant. Il n'y a plus d'objet supplémentaire dans l'arborescence, et l'icône dans l'arborescence a changé, mais notre boîte dans la vue 3D a également disparu (c'est pourquoi l'icône est grise). Qu'est ce qui c'est passé? Bien que nous ayons correctement créé notre forme de boîte et l'avons assignée à un objet Part::FeaturePython, pour le faire apparaître réellement dans la vue 3D, nous devons attribuer un ViewProvider.

En haut

Écrire un ViewProvider

Un fournisseur de vue (View Provider) est le composant d'un objet qui lui permet d'avoir une représentation visuelle dans la vue 3D. FreeCAD utilise une structure d'application conçue pour séparer les données (le "modèle") de sa représentation visuelle (la "vue"). Si vous avez passé du temps à travailler avec FreeCAD en Python, vous en êtes probablement déjà conscient grâce à l'utilisation des deux modules Python de base: FreeCAD et FreeCADGui (souvent sous les alias { {incode|App}} et Gui respectivement).

Notre objet FeaturePython nécessite également ces éléments. Jusqu'à présent, nous nous sommes concentrés uniquement sur la partie "modèle" du code, il est maintenant temps d'écrire la partie "vue". Heureusement, la plupart des ViewProviders sont simples et nécessitent peu d'efforts pour écrire, du moins pour commencer. Voici un exemple de ViewProvider emprunté et légèrement modifié à [1]:

class ViewProviderBox:

    def __init__(self, obj):
        """
        Set this object to the proxy object of the actual view provider
        """

        obj.Proxy = self

    def attach(self, obj):
        """
        Setup the scene sub-graph of the view provider, this method is mandatory
        """
        return

    def updateData(self, fp, prop):
        """
        If a property of the handled feature has changed we have the chance to handle this here
        """
        return

    def getDisplayModes(self,obj):
        """
        Return a list of display modes.
        """
        return []

    def getDefaultDisplayMode(self):
        """
        Return the name of the default display mode. It must be defined in getDisplayModes.
        """
        return "Shaded"

    def setDisplayMode(self,mode):
        """
        Map the display mode defined in attach with those defined in getDisplayModes.
        Since they have the same names nothing needs to be done.
        This method is optional.
        """
        return mode

    def onChanged(self, vp, prop):
        """
        Print the name of the property that has changed
        """

        App.Console.PrintMessage("Change property: " + str(prop) + "\n")

    def getIcon(self):
        """
        Return the icon in XMP format which will appear in the tree view. This method is optional and if not defined a default icon is shown.
        """

        return """
            /* XPM */
            static const char * ViewProviderBox_xpm[] = {
            "16 16 6 1",
            "    c None",
            ".   c #141010",
            "+   c #615BD2",
            "@   c #C39D55",
            "#   c #000000",
            "$   c #57C355",
            "        ........",
            "   ......++..+..",
            "   .@@@@.++..++.",
            "   .@@@@.++..++.",
            "   .@@  .++++++.",
            "  ..@@  .++..++.",
            "###@@@@ .++..++.",
            "##$.@@$#.++++++.",
            "#$#$.$$$........",
            "#$$#######      ",
            "#$$#$$$$$#      ",
            "#$$#$$$$$#      ",
            "#$$#$$$$$#      ",
            " #$#$$$$$#      ",
            "  ##$$$$$#      ",
            "   #######      "};
            """

    def dumps(self):
        """
        Called during document saving.
        """
        return None

    def loads(self,state):
        """
        Called during document restore.
        """
        return None

Dans le code ci-dessus, nous définissons une icône XMP pour cet objet. La conception des icônes dépasse le cadre de ce tutoriel mais la conception de base peut être gérée à l'aide d'outils open source tels que GIMP, Krita et [ https://inkscape.org/ Inkscape]. La méthode getIcon() est facultative, FreeCAD utilisera une icône par défaut si cette méthode n'est pas fournie.

Ajoutez le code ViewProvider à la fin de box.py et dans la méthode create() insérez la ligne suivante au-dessus de l'instruction recompute():

ViewProviderBox(obj.ViewObject)

Cela instancie la classe ViewProvider personnalisée et lui transmet le ViewObject intégré de FeaturePython. Lorsque la classe ViewProvider s'initialise, elle enregistre une référence à elle-même dans l'attribut ViewObject.Proxy de FeaturePython. De cette façon, lorsque FreeCAD a besoin de rendre notre boîte visuellement, il peut trouver la classe ViewProvider pour le faire.

Maintenant, enregistrez les modifications et revenez à FreeCAD. Importez ou rechargez le module box et appelez box.create(). Vous devriez maintenant voir deux choses:

  • L'icône de l'objet boîte a changé.
  • Et, plus important encore, il y a une boîte dans la vue 3D. Si vous ne le voyez pas, appuyez sur le bouton Std Tout afficher. Vous pouvez même modifier les dimensions de la boîte en modifiant les valeurs dans l'Éditeur de propriétés. Essayez!

En haut

Piégeage d'événements

Nous avons déjà discuté du piégeage d'événements. Presque toutes les méthodes d'une classe FeaturePython servent de rappel accessible à l'objet FeaturePython (qui obtient l'accès à notre instance de classe via l'attribut Proxy, si vous vous en souvenez).

Voici une liste des rappels pouvant être implémentés dans l'objet FeaturePython de base:

FeaturePython basic callbacks
execute(self, obj) Appelé pendant le recalcul du document N'appelez pas recompute() depuis cette méthode (ou toute méthode appelée depuis execute()) car cela provoque un recalcul imbriqué.
onBeforeChange(self, obj, prop) Appelé avant la modification d'une valeur de propriété prop est le nom de la propriété à modifier, pas l'objet de propriété lui-même. Les modifications de propriété ne peuvent pas être annulées. Les valeurs de propriétés précédentes/suivantes ne sont pas disponibles simultanément pour la comparaison.
onChanged(self, obj, prop) Appelé après la modification d'une propriété prop est le nom de la propriété à modifier, pas l'objet de propriété lui-même.
onDocumentRestored(self, obj) Appelé après la restauration d'un document ou la copie d'un objet FeaturePython. Parfois, les références à l'objet FeaturePython de la classe ou à la classe de l'objet FeaturePython peuvent être interrompues, car la méthode class __init __() n'est pas appelée lors de la reconstruction de l'objet. L'ajout de self.Object = obj ou obj.Proxy = self résout souvent ces problèmes.

Pour une référence complète des méthodes FeaturePython disponibles, voir Méthodes FeaturePython.

En outre, il existe deux rappels dans la classe ViewProvider qui peuvent parfois s'avérer utiles:

ViewProvider basic callbacks
updateData(self, obj, prop) Appelé après la modification d'une propriété de données (modèle) obj est une référence à l'instance de classe FeaturePython et non à l'instance ViewProvider. prop est le nom de la propriété à modifier, pas l'objet de propriété lui-même.
onChanged(self, vobj, prop) Appelé après la modification d'une propriété de vue vobj est une référence à l'instance ViewProvider. prop est le nom de la propriété de vue qui a été modifiée.

Il n'est pas rare de rencontrer une situation où les rappels Python ne sont pas déclenchés comme ils le devraient. Les débutants dans ce domaine peuvent être assurés que le système de rappel FeaturePython n'est pas fragile ou cassé. Invariablement, lorsque les rappels ne s'exécutent pas, c'est parce qu'une référence est perdue ou indéfinie dans le code sous-jacent. Si, cependant, les rappels semblent interrompre sans explication, fournir des références d'objet/proxy dans le rappel onDocumentRestored() (comme indiqué dans le premier tableau ci-dessus) peut atténuer ces problèmes. Tant que vous n'êtes pas à l'aise avec le système de rappel, il peut être utile d'ajouter des instructions d'impression dans chaque rappel pour imprimer des messages sur la console pendant le développement.

En haut

Le code terminé

import FreeCAD as App
import Part

def create(obj_name):
    """
    Object creation method
    """

    obj = App.ActiveDocument.addObject('Part::FeaturePython', obj_name)

    box(obj)

    ViewProviderBox(obj.ViewObject)

    App.ActiveDocument.recompute()

    return obj

class box():

    def __init__(self, obj):
        """
        Default constructor
        """

        self.Type = 'box'

        obj.Proxy = self

        obj.addProperty('App::PropertyString', 'Description', 'Base', 'Box description').Description = ""
        obj.addProperty('App::PropertyLength', 'Length', 'Dimensions', 'Box length').Length = 10.0
        obj.addProperty('App::PropertyLength', 'Width', 'Dimensions', 'Box width').Width = '10 mm'
        obj.addProperty('App::PropertyLength', 'Height', 'Dimensions', 'Box height').Height = '1 cm'

    def execute(self, obj):
        """
        Called on document recompute
        """

        obj.Shape = Part.makeBox(obj.Length, obj.Width, obj.Height)

class ViewProviderBox:

    def __init__(self, obj):
        """
        Set this object to the proxy object of the actual view provider
        """

        obj.Proxy = self

    def attach(self, obj):
        """
        Setup the scene sub-graph of the view provider, this method is mandatory
        """
        return

    def updateData(self, fp, prop):
        """
        If a property of the handled feature has changed we have the chance to handle this here
        """
        return

    def getDisplayModes(self,obj):
        """
        Return a list of display modes.
        """
        return []

    def getDefaultDisplayMode(self):
        """
        Return the name of the default display mode. It must be defined in getDisplayModes.
        """
        return "Shaded"

    def setDisplayMode(self,mode):
        """
        Map the display mode defined in attach with those defined in getDisplayModes.
        Since they have the same names nothing needs to be done.
        This method is optional.
        """
        return mode

    def onChanged(self, vp, prop):
        """
        Print the name of the property that has changed
        """

        App.Console.PrintMessage("Change property: " + str(prop) + "\n")

    def getIcon(self):
        """
        Return the icon in XMP format which will appear in the tree view. This method is optional and if not defined a default icon is shown.
        """

        return """
            /* XPM */
            static const char * ViewProviderBox_xpm[] = {
            "16 16 6 1",
            "    c None",
            ".   c #141010",
            "+   c #615BD2",
            "@   c #C39D55",
            "#   c #000000",
            "$   c #57C355",
            "        ........",
            "   ......++..+..",
            "   .@@@@.++..++.",
            "   .@@@@.++..++.",
            "   .@@  .++++++.",
            "  ..@@  .++..++.",
            "###@@@@ .++..++.",
            "##$.@@$#.++++++.",
            "#$#$.$$$........",
            "#$$#######      ",
            "#$$#$$$$$#      ",
            "#$$#$$$$$#      ",
            "#$$#$$$$$#      ",
            " #$#$$$$$#      ",
            "  ##$$$$$#      ",
            "   #######      "};
            """

    def dumps(self):
        """
        Called during document saving.
        """
        return None

    def loads(self,state):
        """
        Called during document restore.
        """
        return None

En haut