Which Python command to use to extract the location of a point

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
FlyingaHull
Posts: 16
Joined: Sun Jan 29, 2017 10:26 pm

Which Python command to use to extract the location of a point

Post by FlyingaHull »

I have a project that studies the motion of a parametrized 4-bar linkage, animated with a Python script (I'm new at Python programming). I would like to have the script publish the motion of certain points into a file that I can eventually load into EXCEL. My question is what Python command can be used to extract the x-y-z location of any point in my model into a variable.

Thanks in advance


OS: Windows 10 Version 2009
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.24291 (Git)
Build type: Release
Branch: releases/FreeCAD-0-19
Hash: 7b5e18a0759de778b74d3a5c17eba9cb815035ac
Python version: 3.8.6+
Qt version: 5.15.2
Coin version: 4.0.1
OCC version: 7.5.0
Locale: English/Canada (en_CA)
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Which Python command to use to extract the location of a point

Post by onekk »

It is not clear, from what data you have to start.

There is no such command, from what I know you would have to:

- scan the document for objects
- for each objects you have to extract the infos and put into some external file.

or you have to select objects and work on selections.

Without some example data it is hard to guess how much work has to be done, it depends also on the complexity of the input data.

But also how much experience in Python programming you have, and how much time you want to spend on learning FreeCAD "internals".

As I'm used to script in Python, it is not an impossible work, but it is not also a small work, if the "macro" has to be "generalized" you have to spend some time on filtering data.


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/
FlyingaHull
Posts: 16
Joined: Sun Jan 29, 2017 10:26 pm

Re: Which Python command to use to extract the location of a point

Post by FlyingaHull »

Thank you Carlos for your reply -
I probably wasn't clear enough with my question.

The animation of the linkage is being conducted by a Python script utilizing QtCore.QTimer.
For each increment of the animation, I wish to capture and record (via the script) the x-y-z position of various points in the linkage.

I note in the Python console the commands that are created for each action in the Sketcher.
However, I've not found in Sketcher a function that reports out the position of points - but that could just be because I'm not versed enough with all the FreeCad capabilities.

So my question is whether there would be a similar command that simply reports out the location of a selected point in my model.
Or perhaps there's some sort of a 'model report' that lists out every aspect of the model, which I can later go in and extract out the coordinate information that I'm looking for.

Otherwise, if it's more complicated than a relatively simple command and as you mention would involve having to go deep into the 'internals', I'll just skip the idea.

Regards & Thanks again, Richard
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: Which Python command to use to extract the location of a point

Post by heda »

of course you can extract the coordinates of points/vertexes, or anything else you have on the screen.
in order to get specific help - you have to be specific enough on what you have to begin with (like a file, or at least mention what is in your model).

suppose you are after the global coordinates, and depending on what you have you likely need to care for that in the code.

if you managed to make an animation, this thread https://forum.freecadweb.org/viewtopic.php?f=22&t=25821 should be enough to get you going.

good luck, I'm sure you will make it!
FlyingaHull
Posts: 16
Joined: Sun Jan 29, 2017 10:26 pm

Re: Which Python command to use to extract the location of a point

Post by FlyingaHull »

Thank you heda -
I think I should be able to get what I need from this reference, which extracts the needed coordinates that are an attribute of lines attached to the point, and is a good workaround vs getting the point coordinates directly.

Regards, Richard
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Which Python command to use to extract the location of a point

Post by onekk »

If the animation is driven by a script, i think that it would be possible to modify the script to record and output positions.

As you have been told, point and positions could be different, as there are many point and poistions.

Simply FreeCAD has two "engines":

a modelling engine (model coordinates)
a visualization engine (visualization window coordinates)

so there are many ways to obtain things, based also on what you want to record.

Such questions should be done with some example files and maybe with some code to ease the work of the good fellow that maybe could help you.
In some other forum requests without a MWE (Minimal Working Example) and/or a test file, simply remain unanswered or get a simple "Please post a MWE and test_code" (how much politely depends on the moderator) :D.


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/
FlyingaHull
Posts: 16
Joined: Sun Jan 29, 2017 10:26 pm

Re: Which Python command to use to extract the location of a point

Post by FlyingaHull »

Thanks again Carlos for your response.

Per your suggestion, I went ahead and created and attached a very simple model (MWE) of a rotating point (driven by a Python Macro) to help illustrate my question.
My desire is to identify the necessary Python commands that query the location of the point, and ultimately write them to a file.

'heda' pointed me to some links where the user (script) outputs the motion of the end-points of a vector, which seems like a good enough workaround, but any simpler approach would very much welcome.

Regards,
Richard
Attachments
rotate.FCMacro
Macro used to rotate point with placeholder for potential code that could be used to extract displacements
(377 Bytes) Downloaded 23 times
MWE_for_question_on_how_to_output_displacments.FCStd
MWE file of rotating point
(4.81 KiB) Downloaded 25 times
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Which Python command to use to extract the location of a point

Post by onekk »

Some code refactoring to make more readable:

Code: Select all

from PySide import QtCore
irot = 0

DOC = FreeCAD.getDocument('MWE_for_question_on_how_to_output_displacments')

def update():
    global irot, cycles
    sketch = DOC.getObjectsByLabel('Sketch')
    # it returns a list as multiple names could match
    sketch[0].setDatum(2, App.Units.Quantity(str(irot) + ' deg'))

    point = sketch[0].Geometry[0].EndPoint
    FreeCAD.Console.PrintMessage("{} > {} \n".format(irot, point))
    DOC.recompute()
    irot += 1.0

    if irot >360:
        irot = irot -360

for idx in range(100):
    update()
Please note that:

Code: Select all

Geometry[0]
is a cheat, as it takes the first element of the list Geometry of the Sketch.

and I've modified some code to make the routine to not clutter FreeCAD, if not the macro will run forever, maybe to use some other different from Qtimer used in that way.

Hope it helps.

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/
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: Which Python command to use to extract the location of a point

Post by mario52 »

Hi

other idea for create one animation Macro_Rotate_To_Point Image (in 3D view not in the sketcher)

but you must rotate, translate step by step and save the coordinate if needed after save the final work in one macro with different options

mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
FlyingaHull
Posts: 16
Joined: Sun Jan 29, 2017 10:26 pm

Re: Which Python command to use to extract the location of a point

Post by FlyingaHull »

Thanks again Carlo,
Using the commands in your script, I was able to report out the coordinates as they changed during the animation.

Best regards,
Richard
Post Reply