[solved] Py basics question. reformatting text

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
llll
Posts: 173
Joined: Fri Nov 12, 2021 1:56 am

[solved] Py basics question. reformatting text

Post by llll »

Task:
get some data (1st block)
if there's Vector tuples, replace "Vector" with "App.Vector" (2nd block)

Text is printed to console (unintended) with corrected formatting, but new list is not being remembered

Code: Select all

place = []
length = []
objects = [o.Label for o in App.ActiveDocument.Objects if hasattr(o,"getGlobalPlacement") and o.Shape.Length]
num = 0
for i in objects:
    loc = eval(f'doc.{i}.Placement.Base')
    place.append(loc)
    len = eval(f'doc.{i}.Shape.Length')
    length.append(len)

print(place)


length_ = []
place_ = []
newL = [place_, length_]
Lists = [place,length]
num = 0
for i in Lists:
    if 'Vector ' in str(i):
        str(i).replace('Vector ', 'App.Vector')
        for j in i:
            newL[num].append(j)     
    num +=1

print(place_)
Thanks, feel free to point out any less than ideal practices as you see them

edit:
solution

Code: Select all

import FreeCAD

def format(v):
    if 'Vector ' in str(v):
        a = str(v).replace('Vector ', 'App.Vector')
        return a
    else:
        return v

place = []
length = []
doc = FreeCAD.getDocument("just_spline")

objects = [o.Name for o in doc.Objects if hasattr(o,"getGlobalPlacement") and o.Shape.Length]

for i in objects:
    loc = doc.getObject(i).Placement.Base
    var = format(loc)
    place.append(var)
    len = doc.getObject(i).Shape.Length
    var = format(len)
    length.append(var)
Attachments
just spline.FCStd
(13.77 KiB) Downloaded 15 times
Last edited by llll on Thu Sep 29, 2022 1:50 am, edited 4 times in total.
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Py basics question. reformatting text

Post by openBrain »

Check variables.
In 2nd 'for' loop, you use 'lol' which doesn't exist.
At the end you print 'place_', which is initialized to empty list then never touch again.

Other things :
Don't use 'Label', use 'Name'.
Check for 'Placement' attribute, not 'getGlobalPlacement'.
'eval' is pretty dangerous, you should use FC function (can't remember if it's 'doCommand' or 'runCommand')

You should better tell what this code is aiming at (functionnally) because it looks very weird.
llll
Posts: 173
Joined: Fri Nov 12, 2021 1:56 am

Re: Py basics question. reformatting text

Post by llll »

openBrain wrote: Wed Sep 28, 2022 5:22 am Check variables.
In 2nd 'for' loop, you use 'lol' which doesn't exist.
At the end you print 'place_', which is initialized to empty list then never touch again.
A residual error made when posting, srry, was using "lol" as list of lists.

newL[num].append(j) == place_.append(j) == same error
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Py basics question. reformatting text

Post by openBrain »

Please post a tested code. There still are obvious problems.
llll
Posts: 173
Joined: Fri Nov 12, 2021 1:56 am

Re: Py basics question. reformatting text

Post by llll »

openBrain wrote: Wed Sep 28, 2022 6:13 am .
That's all I have + some draft squiggles in file... It shows list problem I wanted help with

The eval is used only since idk how to do this:

Code: Select all

for i in objects:
    loc = doc.i.Placement?
edit: Does it help to know I was playing around with console: first block is only to show second block failure.
Attachments
just spline.FCStd
(13.77 KiB) Downloaded 15 times
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Py basics question. reformatting text

Post by onekk »

Probably you are missing "the point".

You should not post partial code, but a "working" code, that loaded in the FC Macro editor will run and show the problem.

So if the code has to run with some related file you should post the file, or a hardcoded data source that mimic what the code is expecting.

This is the concept of "Minimal Working Example".

An helper must not guess thing, it has to solve (if possible) problems.

If not it will be hard for an helper to supply help. And when volunteer helpers will be tired there will be simply "no help at all".

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/
llll
Posts: 173
Joined: Fri Nov 12, 2021 1:56 am

Re: Py basics question. reformatting text

Post by llll »

onekk wrote: Wed Sep 28, 2022 8:28 am Probably you are missing "the point".
This is the concept of "Minimal Working Example".
An helper must not guess thing, it has to solve (if possible) problems.
Agreed. it took me 90 minutes to get the point. Still after a solution should someone have the motivation.

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

Re: Py basics question. reformatting text

Post by onekk »

Sadly it's still not clear the question.

see next post for some code that may be a correct guess or not.

Regards

Carlo D.
Last edited by onekk on Wed Sep 28, 2022 2:02 pm, edited 1 time in total.
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/
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Py basics question. reformatting text

Post by onekk »

llll wrote: Wed Sep 28, 2022 12:19 pm ...

with "just_splin.FCStd open as document this code will show information you need in a more Pythonic Way.

Code: Select all

import FreeCAD

place = []
length = []
doc = FreeCAD.getDocument("just_spline")

objects = [o.Name for o in doc.Objects if hasattr(o,"getGlobalPlacement") and o.Shape.Length]

for i in objects:
    loc = doc.getObject(i).Placement.Base
    place.append(loc)
    len = doc.getObject(i).Shape.Length
    length.append(len)
    print(f"{i} loc: {loc}  len: {len}")


Note the use of Name that is unique and identify the object and the absence of eval.

But the point is not clear on what you are trying to achieve.

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/
Post Reply