BSplines with X number of points from txt file

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
DZ_FC
Posts: 9
Joined: Mon Aug 08, 2022 8:15 am

Re: BSplines with X number of points from txt file

Post by DZ_FC »

Thanks a lot @edwilliams16 and @heda . Really great and elegant solutions. I have tested them with a different number of points and lines and your both solutions work fine. Only in the code of @heda on line 8 at the end between the square brackets, one round bracket(parenthesis) was missing.
I believe we could close the topic. Except some other people want to post their solution too.
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: BSplines with X number of points from txt file

Post by onekk »

Some more code using only "Part",

Code: Select all

import FreeCAD
from FreeCAD import Placement, Rotation, Vector

import Part

# buildFromPoles(...)
#    Builds a B-Spline by a list of poles.
#    arguments: poles (sequence of Base.Vector), [periodic (default is False), degree (default is 3), interpolate (default is False)]


with open('/home/carlo-arch/inputBspline.txt', 'rt') as filehandle:
    lines = [line.strip() for line in filehandle.readlines() if line.strip()]

for l_idx, line in enumerate(lines):
    data = [float(field) for field in line.split(',')]
    print(data)
    num2d = [Vector(*xyz) for xyz in zip(data[0::3], data[1::3], data[2::3])]
    spline = Part.BSplineCurve()
    spline.buildFromPoles(num2d, False, 3, False)
    Part.show(spline.toShape(), "spline_{l_idx}")

Probably more flexible as you could state a degree and more things, maybe see:

Code: Select all

import Part
help(Part.BSplineCurve)
# or
# help(Part.BSplineCurve.buildFromPoles)
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/
DZ_FC
Posts: 9
Joined: Mon Aug 08, 2022 8:15 am

Re: BSplines with X number of points from txt file

Post by DZ_FC »

First, thank you all for the help! Second I have a question regarding the line

Code: Select all

numdata = [float(field) for field in data]

I realize I do not understand how it works. My code works now, which is great, but I want to gain a deeper understanding of python and scripting in FreeCAD. Therefore if someone explains in a couple of sentences how the line works, will contribute further to my development. Of course, some link/pdf, etc. to read about the topic will be appreciated too. I am a beginner in python(and in programming), therefore keeping it simple will be highly welcome.
edwilliams16
Veteran
Posts: 3106
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: BSplines with X number of points from txt file

Post by edwilliams16 »

It is a python list comprehension. https://www.geeksforgeeks.org/python-l ... rehension/ a compact way of transforming a list into another list without writing an explicit for loop.
Post Reply