macro make b-spline shape extrude fails

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
Leatherman
Posts: 155
Joined: Thu Feb 02, 2017 2:49 pm

macro make b-spline shape extrude fails

Post by Leatherman »

Hello,
I'm using a macro for defining about 23 points to create a shape. It does the job, but later the extrude fails. It's my first macro and it's certainly not up to the mark, kindly somebody could advice me. I'm also feeling the way how I collect all these points is not really comfortable, is there a better way like "nesting" them together, so I could save lot's of writing? I'm not particular on using the Part Bench either, I simply don't know how to use the draft bench. The idea is later to create more layers and sweep or loft them together (this macro is obviously only for one layer).
Thanks for your help and advice
Andre

Code: Select all

App.newDocument("Unnamed")
App.setActiveDocument("Unnamed")
App.ActiveDocument=App.getDocument("Unnamed")
Gui.ActiveDocument=Gui.getDocument("Unnamed")
import Part

p1 = FreeCAD.Vector(0,0,0)
p2 = FreeCAD.Vector(27,22,0)
p3 = FreeCAD.Vector(27,44,0)
p4 = FreeCAD.Vector(20,88,0)
p5 = FreeCAD.Vector(22.5,110,0)
p6 = FreeCAD.Vector(28,132,0)
p7 = FreeCAD.Vector(34,154,0)
p8 = FreeCAD.Vector(43,174,0)
p9 = FreeCAD.Vector(44,196,0)
p10 = FreeCAD.Vector(43,218,0)
p11 = FreeCAD.Vector(37,238,0)
p12 = FreeCAD.Vector(0,260,0)
p13 = FreeCAD.Vector(-27,238,0)
p14 = FreeCAD.Vector(-36,218,0)
p15 = FreeCAD.Vector(-41,196,0)
p16 = FreeCAD.Vector(-46.5,174,0)
p17 = FreeCAD.Vector(-44.5,163,0)
p18 = FreeCAD.Vector(-42.5,154,0)
p19 = FreeCAD.Vector(-39,132,0)
p20 = FreeCAD.Vector(-34,110,0)
p21 = FreeCAD.Vector(-34,88,0)
p22 = FreeCAD.Vector(-27,44,0)
p23 = FreeCAD.Vector(-27,22,0)
bs = Part.BSplineCurve()
t = FreeCAD.Vector(0.01,0,0)
bs.interpolate(Points = [p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16,p17,p18,p19,p20,p21,p22,p23,p1-t-p1])
Part.show(bs.toShape())
Attachments
Insoletest.py
(701 Bytes) Downloaded 86 times
Leatherman
Posts: 155
Joined: Thu Feb 02, 2017 2:49 pm

Re: macro make b-spline shape extrude fails

Post by Leatherman »

Hello,
most important: I've found the error and changed the macro

Code: Select all

App.newDocument("Unnamed")
App.setActiveDocument("Unnamed")
App.ActiveDocument=App.getDocument("Unnamed")
Gui.ActiveDocument=Gui.getDocument("Unnamed")
import Part

p1 = FreeCAD.Vector(0,0,0)
p2 = FreeCAD.Vector(27,22,0)
p3 = FreeCAD.Vector(27,44,0)
p4 = FreeCAD.Vector(20,88,0)
p5 = FreeCAD.Vector(22.5,110,0)
p6 = FreeCAD.Vector(28,132,0)
p7 = FreeCAD.Vector(34,154,0)
p8 = FreeCAD.Vector(43,174,0)
p9 = FreeCAD.Vector(44,196,0)
p10 = FreeCAD.Vector(43,218,0)
p11 = FreeCAD.Vector(37,238,0)
p12 = FreeCAD.Vector(0,260,0)
p13 = FreeCAD.Vector(-27,238,0)
p14 = FreeCAD.Vector(-36,218,0)
p15 = FreeCAD.Vector(-41,196,0)
p16 = FreeCAD.Vector(-46.5,174,0)
p17 = FreeCAD.Vector(-44.5,163,0)
p18 = FreeCAD.Vector(-42.5,154,0)
p19 = FreeCAD.Vector(-39,132,0)
p20 = FreeCAD.Vector(-34,110,0)
p21 = FreeCAD.Vector(-34,88,0)
p22 = FreeCAD.Vector(-27,44,0)
p23 = FreeCAD.Vector(-27,22,0)
bs = Part.BSplineCurve()
#t = FreeCAD.Vector(0.01,0,0)
#bs.interpolate(Points = [p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16,p17,p18,p19,p20,p21,p22,p23,p1-t-p1])
bs.interpolate(Points = [p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16,p17,p18,p19,p20,p21,p22,p23,p1])
Part.show(bs.toShape())
Now the extrude works. Still I would appreciate any suggestions, if this could not be done easier?
Regards
Andre
User avatar
kkremitzki
Veteran
Posts: 2515
Joined: Thu Mar 03, 2016 9:52 pm
Location: Illinois

Re: macro make b-spline shape extrude fails

Post by kkremitzki »

Note that FreeCAD.Vector(0) == FreeCAD.Vector(0, 0, 0), and FreeCAD.Vector(1, 1) == FreeCAD.Vector(1, 1, 0).

You can use a neat Python feature called (tuple, list, dict) unpacking. For iterable things like tuples and lists, you add an asterisk in front of the variable before referring to it, and you add two for a dict. Anyway, here's a way to make it shorter.

Code: Select all

App.newDocument("Unnamed")
App.setActiveDocument("Unnamed")
App.ActiveDocument=App.getDocument("Unnamed")
Gui.ActiveDocument=Gui.getDocument("Unnamed")
import Part

points_list = [ (0,), (27,22), (27,44), (20,88), (22.5,110), (28,132),
                (34,154), (43,174), (44,196), (43,218), (37,238), (0,260),
                (-27,238), (-36,218), (-41,196), (-46.5,174), (-44.5,163),
                (-42.5,154), (-39,132), (-34,110), (-34,88), (-27,44), (-27,22)]

vec_list = [FreeCAD.Vector(*p) for p in points_list + [points_list[0]]]

bs = Part.BSplineCurve()
bs.interpolate(Points=vec_list)
Part.show(bs.toShape())
One easy thing to miss here is that in order to create a tuple with one element like points_list[0], you have to do (0,), because (0) == 0.
Like my FreeCAD work? I'd appreciate any level of support via Patreon, Liberapay, or PayPal! Read more about what I do at my blog.
Leatherman
Posts: 155
Joined: Thu Feb 02, 2017 2:49 pm

Re: macro make b-spline shape extrude fails

Post by Leatherman »

Hello,
thanks a lot and wow! That's hardcore coding! Thanks a lot, but I have to admit that I don't everything.
By any change, if you have just a second, could you explain the last line to me please:

vec_list = [FreeCAD.Vector(*p) for p in points_list + [points_list[0]]]

I believe here is where the music is! I will certainly check on my own as well.
Thank you
Andre
mario52
Veteran
Posts: 4690
Joined: Wed May 16, 2012 2:13 pm

Re: macro make b-spline shape extrude fails

Post by mario52 »

hi
create file coordinates and load it here little macro example for download a file coordinates Macro_WireXYZ

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.
Leatherman
Posts: 155
Joined: Thu Feb 02, 2017 2:49 pm

Re: macro make b-spline shape extrude fails

Post by Leatherman »

Hi,
have a problem with this macro. Error

>>> fichier = "C:\users\andre\documents\points.asc"
File "<input>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \uXXXX escape
>>>
I understand that there is problem with the reading of my file

0 0 0
27 22 0
27 44 0
20 88 0
22.5 110 0
28 132 0
34 154 0
43 174 0
44 196 0
43 218 0
37 238 0
0 260 0
-27 238 0
-36 218 0
-41 196 0
-46.5 174 0
-44.5 163 0
-42.5 154 0
-39 132 0
-34 110 0
-34 88 0
-27 44 0
-27 22 0

This are my points. Not correct? File location is correct and checked.
Thanks for your advice
Regards
Andre
mario52
Veteran
Posts: 4690
Joined: Wed May 16, 2012 2:13 pm

Re: macro make b-spline shape extrude fails

Post by mario52 »

hi
i have not problem ni macro ni your file
wireTest00.png
wireTest00.png (2.28 KiB) Viewed 1683 times
Leatherman wrote: >>> fichier = "C:\users\andre\documents\points.asc"
File "<input>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \uXXXX escape
>>>
You have copied the entire macro ? line 1 is not "fichier = "C:\users\andre\documents\points.asc"

other try "C:/users/andre/documents/points.asc" (the backslash is command Python ex: here for "\uXXXX escape" and here)

or "C:\\users\\andre\\documents\\points.asc" (for ignore the first \)

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.
Leatherman
Posts: 155
Joined: Thu Feb 02, 2017 2:49 pm

Re: macro make b-spline shape extrude fails

Post by Leatherman »

Thank you it works wonderful and is really helpful to me. But you should might change something in the macro site this is how it looks (and get copied): fichier = "C:\yourPath\cloud.asc" :o
Again, thanks great macro, the sad part is like this I will never properly learn Phython.
Regards
Andre
Post Reply