Playing with Assembly4 vars in script

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
Jeanmimi0168
Posts: 15
Joined: Thu Apr 13, 2017 7:03 am

Playing with Assembly4 vars in script

Post by Jeanmimi0168 »

Hello,
I'm capturing distance variation between a vertex and a planar face on an Assembly4 document by varying the "Angle" variable.

This version of my code will not update the variation of distance and keep the initial distance, even if I calculate it inside the loop, "FacesJM" and "Sommets" initialization is outside the loop on variable.

Code: Select all

import FreeCAD
App.setActiveDocument('Test')
D403=[]
Sommets=[]
FacesJM=[]
FacesJM.append(App.ActiveDocument.Body.getSubObject('Pocket014.Face38'))
Sommets.append(App.ActiveDocument.Part.getSubObject('Body002.BaseFeature.Vertex403'))

with open("CoordinatesTest.csv", "w") as f:
	sys.stdout=f
	print("Angle,D403")
	for i in range(3600):
		FreeCAD.getDocument('TestAlignement').getObject('Variables').Angle = i/10
		App.activeDocument().recompute(None,True,True)
		D403.append(Sommets[0].Point.distanceToPlane(FacesJM[0].CenterOfMass, FacesJM[0].normalAt(0,0)))
		print("%3d,%8.10f" % (i,D403[i]))

This version of my code will update correctly the distance.

Code: Select all

import FreeCAD
App.setActiveDocument('Test')
D403=[]
with open("CoordinatesTest.csv", "w") as f:
	sys.stdout=f
	print("Angle,D403")
	for i in range(3600):
		Sommets=[]
		FacesJM=[]
		FacesJM.append(App.ActiveDocument.Body.getSubObject('Pocket014.Face38'))
		Sommets.append(App.ActiveDocument.Part.getSubObject('Body002.BaseFeature.Vertex403'))
		FreeCAD.getDocument('TestAlignement').getObject('Variables').Angle = i/10
		App.activeDocument().recompute(None,True,True)
		D403.append(Sommets[0].Point.distanceToPlane(FacesJM[0].CenterOfMass, FacesJM[0].normalAt(0,0)))
		print("%3d,%8.10f" % (i,D403[i]))
My question is: Is this the expected behaviour?

Thanks!
Edit: Added proper version of FreeCAD and precision on version.

Code: Select all

OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.20.1.29410 (Git)
Build type: Release
Branch: releases/FreeCAD-0-20
Hash: f5d13554ecc7a456fb6e970568ae5c74ba727563
Python 3.8.10, Qt 5.15.2, Coin 4.0.1, Vtk 8.2.0, OCC 7.6.3
Locale: French/France (fr_FR)
Installed mods: 
  * Assembly4 0.12.4
  * boltsfc 2022.11.5
  * ExtremeProDark 2.5.3
  * fasteners 0.4.23
  * freecad.gears 1.0.0
  * Render 2022.2.0
Last edited by Jeanmimi0168 on Thu Dec 08, 2022 6:25 am, edited 2 times in total.
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Playing with Assembly4 vars in script

Post by onekk »

Jeanmimi0168 wrote: Wed Dec 07, 2022 5:17 pm Hello,
I'm capturing distance variation between a vertex and a planar face on an Assembly4 document by varying the "Angle" variable.
...
Thanks!
It is not clear when you speak about version, what you intend.

FreeCAD version, Assembly4 version or "your code version"?

A proper version info is better than the version in the signature, this is the expected way to supply a "proper full FreeCAD info", more readable, see:

https://forum.freecadweb.org/viewtopic.php?f=3&t=2264

Code: Select all

OS: Artix Linux (openbox)
Word size of FreeCAD: 64-bit
Version: 0.20.1.29410 (Git) AppImage
Build type: Release
Branch: (HEAD detached at 0.20.1)
Hash: f5d13554ecc7a456fb6e970568ae5c74ba727563
Python 3.10.5, Qt 5.15.4, Coin 4.0.0, Vtk 9.1.0, OCC 7.6.2
Locale: Italian/Italy (it_IT)
Installed mods: 
  * QuickMeasure 2022.10.28
  * Assembly4 0.12.4
  * fasteners 0.4.24
  * toSketch 1.0.1
  * Curves 0.6.1
  * Help 1.0.3
Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Jeanmimi0168
Posts: 15
Joined: Thu Apr 13, 2017 7:03 am

Re: Playing with Assembly4 vars in script

Post by Jeanmimi0168 »

Edited initial message, Thanks Carlo.
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Playing with Assembly4 vars in script

Post by onekk »

Jeanmimi0168 wrote: Thu Dec 08, 2022 6:28 am Edited initial message, Thanks Carlo.
No problem but second version has horrible code as example why empty a list on every loop?

why not use objects instances?

If you have posted a proper minimal working example, in other words some test files that reproduce the problem, maybe someone will try to see what is wrong.


Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Jeanmimi0168
Posts: 15
Joined: Thu Apr 13, 2017 7:03 am

Re: Playing with Assembly4 vars in script

Post by Jeanmimi0168 »

onekk wrote: Thu Dec 08, 2022 7:33 am
No problem but second version has horrible code as example why empty a list on every loop?
Trying to make things, not skilled in Python neither in coding, my job is CAD engineer.
why not use objects instances?
I'll look how to do that...

Thanks!
Jeanmimi0168
Posts: 15
Joined: Thu Apr 13, 2017 7:03 am

Re: Playing with Assembly4 vars in script

Post by Jeanmimi0168 »

Hello,
Sample file included and two versions of the macro.
Variable distance is driving Pointe LCS_2 against Cube001 LCS_1

Macro Test1.FCMacro result doesn't follow variable change

Code: Select all

Distance,D1
  0,0.0000000000
  1,0.0000000000
  2,0.0000000000
  3,0.0000000000
  4,0.0000000000
  5,0.0000000000
  6,0.0000000000
  7,0.0000000000
  8,0.0000000000
  9,0.0000000000
Macro Test2 results follow variable change

Code: Select all

Distance,D1
  0,0.0000000000
  1,1.0000000000
  2,2.0000000000
  3,3.0000000000
  4,4.0000000000
  5,5.0000000000
  6,6.0000000000
  7,7.0000000000
  8,8.0000000000
  9,9.0000000000
Attachments
Test2.FCMacro
Working version
(530 Bytes) Downloaded 11 times
Test1.FCMacro
Non working version
(527 Bytes) Downloaded 12 times
CubePointe.FCStd
Simple example
(20.61 KiB) Downloaded 11 times
Post Reply