Scripted objects with attachment/fr: Difference between revisions
No edit summary |
(Updating to match new version of source page) |
||
Line 2: | Line 2: | ||
{{TOCright}} |
{{TOCright}} |
||
<span id="Introduction"></span> |
|||
== Introduction == |
== Introduction == |
||
Line 15: | Line 16: | ||
'''REMARQUE:''' La boîte est notre objet personnalisé crée par script et le cylindre est un objet FreeCAD standard généré à partir de l'atelier Part. |
'''REMARQUE:''' La boîte est notre objet personnalisé crée par script et le cylindre est un objet FreeCAD standard généré à partir de l'atelier Part. |
||
<span id="Making_Scripted_Objects_Attachable"></span> |
|||
== Rendre les objets créés par script attachables == |
== Rendre les objets créés par script attachables == |
||
<span id="Add_Attach_Extension"></span> |
|||
=== Ajouter une extension attachée === |
=== Ajouter une extension attachée === |
||
Line 32: | Line 35: | ||
# Needed to make this object "attachable" |
# Needed to make this object "attachable" |
||
obj.addExtension('Part::AttachExtensionPython' |
obj.addExtension('Part::AttachExtensionPython') |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
Sans ce code, nous verrions la boîte de dialogue d'avertissement suivante lors de la connexion de notre objet personnalisé créé par script à un autre objet. |
Sans ce code, nous verrions la boîte de dialogue d'avertissement suivante lors de la connexion de notre objet personnalisé créé par script à un autre objet. |
||
Line 38: | Line 41: | ||
[[File:Part-attachment-warning-dialog.png]] |
[[File:Part-attachment-warning-dialog.png]] |
||
<span id="Update_Position_Based_on_Attached_Object"></span> |
|||
===Mis à jour de la position en fonction de l'objet attaché=== |
===Mis à jour de la position en fonction de l'objet attaché=== |
||
Line 54: | Line 58: | ||
Si <code>positionBySupport</code> n'est pas appelé, notre objet personnalisé crée par script ne mettra pas à jour sa position lorsque la position de l'objet attaché change. |
Si <code>positionBySupport</code> n'est pas appelé, notre objet personnalisé crée par script ne mettra pas à jour sa position lorsque la position de l'objet attaché change. |
||
<span id="Full_and_Complete_Minimal_Example"></span> |
|||
==Exemple minimal complet== |
==Exemple minimal complet== |
||
<syntaxhighlight lang="python" line='line'> |
<syntaxhighlight lang="python" line='line'> |
||
Line 87: | Line 92: | ||
# Needed to make this object "attachable", |
# Needed to make this object "attachable", |
||
# or able to attach parameterically to other objects |
# or able to attach parameterically to other objects |
||
obj.addExtension('Part::AttachExtensionPython' |
obj.addExtension('Part::AttachExtensionPython') |
||
def execute(self, obj): |
def execute(self, obj): |
||
Line 118: | Line 123: | ||
document.recompute() |
document.recompute() |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
<span id="References"></span> |
|||
== Références == |
== Références == |
||
Line 125: | Line 132: | ||
* [https://github.com/gbroques/freecad-part-attachment-python-example GitHub - freecad-part-attachment-python-example] |
* [https://github.com/gbroques/freecad-part-attachment-python-example GitHub - freecad-part-attachment-python-example] |
||
<span id="Tested_With_the_Following_FreeCAD_Version"></span> |
|||
== Testé avec la version FreeCAD == |
== Testé avec la version FreeCAD == |
||
Latest revision as of 09:52, 26 March 2024
Introduction
Le but de cette page est de montrer un exemple minimal de fonctionnalité de Part Ancrage utilisant des Objets créés par script en Python.
Voir l'Exemple minimal complet ci-dessous.
Le GIF suivant montre comment attacher notre boîte personnalisée à un cylindre et mettre à jour automatiquement sa position lorsque la position du cylindre change.
REMARQUE: La boîte est notre objet personnalisé crée par script et le cylindre est un objet FreeCAD standard généré à partir de l'atelier Part.
Rendre les objets créés par script attachables
Ajouter une extension attachée
Tout d'abord, nous devons ajouter l'extension Part::AttachExtensionPython
à notre objet Part::FeaturePython
dans le constructeur, ou la méthode __init__
de notre objet créé par script personnalisé.
class Box():
"""Custom Scripted Box Object"""
def __init__(self, obj):
self.Type = 'Box'
obj.Proxy = self
... custom properties
# Needed to make this object "attachable"
obj.addExtension('Part::AttachExtensionPython')
Sans ce code, nous verrions la boîte de dialogue d'avertissement suivante lors de la connexion de notre objet personnalisé créé par script à un autre objet.
Mis à jour de la position en fonction de l'objet attaché
Ensuite, dans la méthode execute de notre script personnalisé, nous devons appeler le positionBySupport sur notre objet Part::FeaturePython.
class Box:
...
def execute(self, obj):
obj.positionBySupport()
# Assign a Shape to obj
obj.Shape = Part.makeBox(...)
Si positionBySupport
n'est pas appelé, notre objet personnalisé crée par script ne mettra pas à jour sa position lorsque la position de l'objet attaché change.
Exemple minimal complet
import FreeCAD as App
import Part
class Box():
"""
Simple Custom Box Object
See Also:
https://wiki.freecadweb.org/FeaturePython_Objects
"""
def __init__(self, obj):
"""
Constructor
Arguments
---------
- obj: an existing document object or an object created with FreeCAD.Document.addObject('Part::FeaturePython', '{name}').
"""
self.Type = 'Box'
obj.Proxy = self
obj.addProperty('App::PropertyLength', 'Length',
'Dimensions', 'Box length').Length = 10.0
obj.addProperty('App::PropertyLength', 'Width',
'Dimensions', 'Box width').Width = 10.0
obj.addProperty('App::PropertyLength', 'Height',
'Dimensions', 'Box height').Height = 10.0
# Needed to make this object "attachable",
# or able to attach parameterically to other objects
obj.addExtension('Part::AttachExtensionPython')
def execute(self, obj):
"""
Called on document recompute
"""
# Needed to update position when attached-to object changes position.
# Reposition object based on Support, MapMode and MapPathParameter properties.
# Returns True if attachment calculation was successful, False if object is not attached and Placement wasn't updated,
obj.positionBySupport()
obj.Shape = Part.makeBox(obj.Length, obj.Width, obj.Height)
def create_box(obj_name, document):
"""
Create a Box.
"""
obj = document.addObject('Part::FeaturePython', obj_name)
Box(obj)
obj.ViewObject.Proxy = 0 # Mandatory unless ViewProvider is coded
return obj
document = App.ActiveDocument
if document is None:
document = App.newDocument('Part Attachment Example')
box = create_box('CustomBox', document)
document.recompute()
Références
- Part Ancrage
- FreeCAD Forum - Parametric attachment of scripted object
- FreeCAD Forum - Port attachment to be an extension
- GitHub - freecad-part-attachment-python-example
Testé avec la version FreeCAD
Testé avec la version suivante de FreeCAD:
OS: Ubuntu 18.04.3 LTS Word size of OS: 64-bit Word size of FreeCAD: 64-bit Version: 0.18.16146 (Git) AppImage Build type: Release Branch: (HEAD detached at 0.18.4) Hash: 980bf9060e28555fecd9e3462f68ca74007b70f8 Python version: 3.6.7 Qt version: 5.6.2 Coin version: 4.0.0a OCC version: 7.3.0 Locale: English/UnitedStates (en_US)
Remarque : pour FreeCAD 0.19, ce tutoriel nécessite une mise à jour mineure :
- Le deuxième argument de la méthode `addExtension` est devenu obsolète.
- Pour plus de détails, voir https://forum.freecadweb.org/viewtopic.php?f=10&t=54370
- Scripts FreeCAD : Python, Introduction à Python, Tutoriel sur les scripts Python, Débuter avec les scripts
- Modules : Modules intégrés, Unités, Quantity
- Ateliers : Création d'atelier, Commands Gui, Les commandes, Installer des ateliers supplémentaires
- Maillages et objets Parts : Scripts Mesh, Script de données topologiques, Conversion objet Mesh en Part, PythonOCC
- Objets paramétriques : Objets créés par script, Viewproviders (Icône personnalisée dans l'arborescence)
- Scénographie : Graphe de scène Coin (Inventor), Pivy
- Interface graphique : Création d'interface, Création d'une boite de dialogue (1, 2, 3, 4, 5), PySide, Exemples PySide débutant, intermédiaire, expérimenté
- Macros : Macros, Comment installer des macros
- Intégration : Intégrer FreeCAD, Intégration de FreeCADGui
- Autre : Expressions, Extraits de codes, Fonction - tracer une ligne, Bibliothèque mathématique vectorielle de FreeCAD (déprécié)