Skip to content

The purpose of this repository is to show a minimal example of FreeCAD's Part Attachment feature in Python using scripted object in Python.

License

Notifications You must be signed in to change notification settings

gbroques/freecad-part-attachment-python-example

Repository files navigation

FreeCAD Part Attachment Python Example

The purpose of this repository is to show a minimal example of FreeCAD's Part Attachment Feature using scripted objects in Python.

See part_attachment_example.py for the full and complete example.

The following GIF demonstrates attaching our custom box to a cylinder, and automatically updating it's position when the cylinder's position changes.

Custom Box Attached To Cylinder Example

NOTE: The box is our custom scripted object, and the cylinder is a regular FreeCAD object generated from the Part workbench.

Making Scripted Objects Attachable

Add Attach Extension

First, we need to add the Part::AttachExtensionPython extension to our Part::FeaturePython object in the constructor, or __init__ method, of our custom scripted object.

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', obj)

Without adding this code, we'll see the following warning dialog when attaching our custom scripted object to another object.

attachment warning dialog

Update Position Based on Attached Object

Then, in the execute method of our custom scripted object, we need to call the positionBySupport on our Part::FeaturePython object.

class Box:
    
    ...

    def execute(self, obj):
        obj.positionBySupport()
        
        # Assign a Shape to obj
        obj.Shape = Part.makeBox(...)

Without calling positionBySupport, our custom scripted object won't update it's position when the position of the attached-to object changes.

References

Tested With the Following FreeCAD Version

Tested with the following FreeCAD version information:

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)

About

The purpose of this repository is to show a minimal example of FreeCAD's Part Attachment feature in Python using scripted object in Python.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published