Part-WB: How can I add a property expression by Python

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
Post Reply
lutz_dd
Posts: 92
Joined: Thu Jul 31, 2014 5:10 am

Part-WB: How can I add a property expression by Python

Post by lutz_dd »

In order to follow one object a reference object all Placement parameters are an expression from the follower to the reference (see screenshot).
In case the Cylinder is selected ...

Code: Select all

Gui.Selection.getSeletion()[0].Placement.x = 0 ... and not ... <<Cube>>.Placement.Base.x
How can I create this parameter expressions in a python macro?
.
freecad_paramter_expression.png
freecad_paramter_expression.png (18.5 KiB) Viewed 844 times
---
OS: Windows 7 SP 1 (6.1)
Word size of FreeCAD: 64-bit
Version: 0.20.25645 (Git)
Build type: Release
Branch: master
Hash: 37d9757399b4c2ec30318eb88d7cd7c508246345
Python version: 3.8.10
Qt version: 5.12.9
Coin version: 4.0.0
OCC version: 7.5.2
Locale: German/Germany (de_DE)
TheMarkster
Veteran
Posts: 5512
Joined: Thu Apr 05, 2018 1:53 am

Re: Part-WB: How can I add a property expression by Python

Post by TheMarkster »

object.setExpression("propertyname","expressionstring")

Example:

Code: Select all

App.ActiveDocument.Cylinder.setExpression("Height","Cube.Height")
App.ActiveDocument.Cylinder.setExpression("Placement.Base.x","Cube.Placement.Base.x")
lutz_dd
Posts: 92
Joined: Thu Jul 31, 2014 5:10 am

Re: Part-WB: How can I add a property expression by Python

Post by lutz_dd »

Thanks @TheMarkster.

... and here is the macro.

Context:
A2plus with a Wheel and its Cutout-Cube referenced individual and Cutout-Cube tracks the wheel.
Note:
If it is applied to A2plus-objects the Transform-tool modifies the tree after the movement.
It seems both become a fusion with the 1st object the parent and the 2nd object the child.
Regular Solids of the Part-WB do not show this tree behavior AFAICT.

Code: Select all

# -*- coding: utf-8 -*-
#........1.........2.........3.........4.........5.........6.........7.........8
# copyright (c) 2021 lutz_dd
#
# Copy all Placement parameters from 1st selected object to the second selected
# object to get 2nd object tracks 1st object (eg. wheel and cutout-cube)

import traceback
import FreeCAD, FreeCADGui

def make_dependency():
  try:
    selset=Gui.Selection.getSelection()
    src=selset[0]
    dest=selset[1]
    
    dest.setExpression("Placement.Rotation.Angle",
                       src.Name+".Placement.Rotation.Angle")
    for hdr in ["Placement.Base", "Placement.Rotation.Axis"]:
      for item in ["x","y","z"]: 
        dest.setExpression(hdr+"."+item, src.Label+"."+hdr+"."+item)
    
    # set recompute flag on the object
    dest.recompute()
    # recompute the document
    App.ActiveDocument.recompute()
  except:
    App.Console.PrintError("ERROR: make_dependency() has failed."+os.linesep) 
    traceback.print_exc()

make_dependency()
Bance
Veteran
Posts: 4250
Joined: Wed Feb 11, 2015 3:00 pm
Location: London

Re: Part-WB: How can I add a property expression by Python

Post by Bance »

Did you try Attachment?
lutz_dd
Posts: 92
Joined: Thu Jul 31, 2014 5:10 am

Re: Part-WB: How can I add a property expression by Python

Post by lutz_dd »

Obviously not ... but good point.
Post Reply