BSplines with X number of points from txt file

Need help, or want to share a macro? Post here!
DZ_FC
Posts: 7
Joined: Mon Aug 08, 2022 8:15 am

BSplines with X number of points from txt file

Post by DZ_FC »

1.FreeCAD Info

Code: Select all

OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.20.29177 (Git)
Build type: Release
Branch: releases/FreeCAD-0-20
Hash: 68e337670e227889217652ddac593c93b5e8dc94
Python 3.8.10, Qt 5.15.2, Coin 4.0.1, Vtk 8.2.0, OCC 7.6.2
Locale: English/United States (en_US)
Installed mods: 
  * Curves 0.5.1
  * sheetmetal 0.2.50

2. Problem Description
I need to create a couple of BSplines on a plate. The coordinates of the B-Splies are imported from txt file. First, to keep it simple, I decided that every B-Spline would have only three points with X, Y, and Z coordinates. My code looks like that:

Code: Select all

##### Start reading from a file 
if (fname[0] != ""): 
			f = open(fname[0], "r") 
			for line in f : 
				IsData = line.split(",")
				x_start1 =float(IsData[0]) 
				y_start1 = float(IsData[1]) 
				z_start1 =float(IsData[2]) 
				x_start2 =float(IsData[3]) 
				y_start2 = float(IsData[4]) 
				z_start2 =float(IsData[5]) 
				x_start3 =float(IsData[6]) 
				y_start3 = float(IsData[7]) 
				z_start3 =float(IsData[8]) 

				### Begin command Std_Workbench
				Gui.activateWorkbench("DraftWorkbench")
				### End command Std_Workbench
####### Creating Bspline
				import Draft
				points = [FreeCAD.Vector(x_start1, y_start1, z_start1), FreeCAD.Vector(x_start2, y_start2, z_start2), FreeCAD.Vector(x_start3, y_start3, z_start3)]
				spline = Draft.make_bspline(points, closed=False, face=True, support=None)
				Draft.autogroup(spline) 
			App.getDocument('Unnamed').recompute()  
######## End Code 
And it works fine! Now I want to expand the code. The B-splines should include a couple (X number) of points. I know this should be done with some for loop, but I have no idea, how to put it inside the FreeCAD.Vector constructor. Any suggestions would be appreciated.

Ps. I have attached the whole working macro(with the B-Splines with 3 points each) The FCstd file, which the macro generates, and an example of the Input File. The Input file comes from an Abaqus Simulation and could contain a big number of points for the B-Spline. For this reason, my solution with x_start1,2,3 is not very practical.
Attachments
inputBspline.txt
(218 Bytes) Downloaded 5 times
Bspline08.08.FCStd
(14.86 KiB) Downloaded 3 times
B-Splines.FCMacro
(7.75 KiB) Downloaded 5 times
Last edited by DZ_FC on Mon Aug 08, 2022 3:44 pm, edited 6 times in total.
chrisb
Veteran
Posts: 44415
Joined: Tue Mar 17, 2015 9:14 am

Re: BSplines with X number of points from txt file

Post by chrisb »

Hi and welcome to the forum!

Please add the missing information.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
DZ_FC
Posts: 7
Joined: Mon Aug 08, 2022 8:15 am

Re: BSplines with X number of points from txt file

Post by DZ_FC »

Sorry. :roll: I believe I hit enter, instead Shift+Enter after the first line(Or just click of submit?) and I post it without wanting to do so. Now the whole situation should be more clear.
User avatar
onekk
Veteran
Posts: 3215
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: BSplines with X number of points from txt file

Post by onekk »

use the </> icon to add code tags, plus the information required in the post IMPORTANT: near the top of this forum section.

Post message are here to stay so it is not a sin to edit a message.

Subsequent post have a limited "edit time" "delete time" but first post is always editable by OP (Original Poster).

Regards

Carlo D.
Last edited by onekk on Mon Aug 08, 2022 1:24 pm, edited 1 time in total.
My English and Italian Scripting Guide https://github.com/onekk/freecad-doc.
My Blog: https://onekk-maker.blogspot.com/

To help development see on GitHub Page

For enquiry about customized FC Scripting job PM me.
chrisb
Veteran
Posts: 44415
Joined: Tue Mar 17, 2015 9:14 am

Re: BSplines with X number of points from txt file

Post by chrisb »

onekk wrote: Mon Aug 08, 2022 12:23 pm Post message are here to stay so it is not a sin to edit a message.
Very well said.
Subsequent post have a limited "edit time" but first post is always editable by OP (Original Poster).
You can always edit your own posts. If there is no subsequent post this will pass unrecognized, other users just see the edited post. If a subsequent ost exists, you will see a comment that the post was edited.

Using the code tags will keep the spaces, which is mandatory to understand Python code.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
onekk
Veteran
Posts: 3215
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: BSplines with X number of points from txt file

Post by onekk »

chrisb wrote: Mon Aug 08, 2022 12:51 pm ...
You can always edit your own posts. If there is no subsequent post this will pass unrecognized, other users just see the edited post. If a subsequent ost exists, you will see a comment that the post was edited.
Sorry I have mistaken the delete X that is available for a limited time.

Regards

Carlo D.
My English and Italian Scripting Guide https://github.com/onekk/freecad-doc.
My Blog: https://onekk-maker.blogspot.com/

To help development see on GitHub Page

For enquiry about customized FC Scripting job PM me.
DZ_FC
Posts: 7
Joined: Mon Aug 08, 2022 8:15 am

Re: BSplines with X number of points from txt file

Post by DZ_FC »

I believe that my post matches all requirements from "IMPORTANT: Please...". I try to explain as clearly as possible the problem and give some context. If is it still unclear, please let me know and I will try to explain it further and/or in a different way.
Thanks for the bits of advice about formulating a clear and structured post!
User avatar
onekk
Veteran
Posts: 3215
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: BSplines with X number of points from txt file

Post by onekk »

DZ_FC wrote: Mon Aug 08, 2022 3:23 pm I believe that my post matches all requirements from "IMPORTANT: Please...".

...
Quite correct.

First part is slightly wrong.

the first part is missing so no indication of the os you used.

Regards

Carlo D.
My English and Italian Scripting Guide https://github.com/onekk/freecad-doc.
My Blog: https://onekk-maker.blogspot.com/

To help development see on GitHub Page

For enquiry about customized FC Scripting job PM me.
edwilliams16
Veteran
Posts: 1447
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: BSplines with X number of points from txt file

Post by edwilliams16 »

I assume you are looking for something like this

Code: Select all

import Draft
f = open('/Users/ed/Downloads/inputBspline.txt', 'r')
#each line is one bspline x1, y1, z1, x2, y2, z2 ... xn, yn, zn
# n can vary, but there must be 3n floats in the line
for line in f:
    data = line.split(',')
    numdata = [float(field) for field in data]
    num2d = [App.Vector(numdata[i], numdata[i+1], numdata[i+2]) for i in range(0, len(numdata), 3)]
    spline = Draft.make_bspline(num2d, closed=False, face=False, support=None)
heda
Veteran
Posts: 1024
Joined: Sat Dec 12, 2015 5:49 pm

Re: BSplines with X number of points from txt file

Post by heda »

should be exactly same as edwilliams, just a bit different python constructs
not better, just putting the use of alternative code-constructs out there :-)
using zip in loops gives great code readability imho

Code: Select all

import Draft

with open('/Users/ed/Downloads/inputBspline.txt', 'rt') as filehandle:
    lines = [line.strip() for line in filehandle.readlines() if line.strip()]

for line in lines:
    data = [float(field) for field in line.split(',')]
    num2d = [App.Vector(*xyz) for xyz in zip(data[0::3], data[1::3], data[2::3])]
    spline = Draft.make_bspline(num2d, closed=False, face=False, support=None)
Last edited by heda on Sun Aug 21, 2022 10:31 pm, edited 1 time in total.
Post Reply