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.
BSplines with X number of points from txt file
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
Re: BSplines with X number of points from txt file
Some more code using only "Part",
Probably more flexible as you could state a degree and more things, maybe see:
Hope it helps
Regards
Carlo D.
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}")
Code: Select all
import Part
help(Part.BSplineCurve)
# or
# help(Part.BSplineCurve.buildFromPoles)
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/
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/
Re: BSplines with X number of points from txt file
First, thank you all for the help! Second I have a question regarding the line
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.
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.
-
- Veteran
- Posts: 3192
- Joined: Thu Sep 24, 2020 10:31 pm
- Location: Hawaii
- Contact:
Re: BSplines with X number of points from txt file
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.