Scripting point to point tubing

Need help, or want to share a macro? Post here!
DavidCAD
Posts: 10
Joined: Fri Feb 06, 2015 10:34 pm

Scripting point to point tubing

Postby DavidCAD » Fri May 20, 2016 10:05 am

I am trying to create 3D model of spheres located at certain points in 3D space. The spheres would be connected together by square tubing. I am trying to 3D print the spheres with holes at the correct angles so they could be connected like Tinkertoys or K'Nex. I have figured out a lot on my own but is there an easy way to script making a square tube (box) go from point to point in 3D space. I have been making boxes and manually adjusting rotation and position to intersect the spheres, a tedious process to say the least. I know there must be a better way. An example line between points is Line (74,94,86) (53,-18,41) . This is center point to center point. The tubing might be 4mm square. I thought about making 8 points offset from the center point and creating lines from those and then turning that into a shape but maybe there is an easier way. Or maybe someone could help me with scripting that. I am somewhat familiar with Freecad and Python scripting but am still beginning. Once I get the tubing, I know how to make the cuts. Thank you in advance.

I'm a version behind but I don't think that will make a difference with this issue. Amazing program but so much to learn!!!

OS: Windows 7
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.15.4671 (Git)
Branch: releases/FreeCAD-0-15
Hash: 244b3aef360841646cbfe80a1b225c8b39c8380c
Python version: 2.7.8
Qt version: 4.8.6
Coin version: 4.0.0a
OCC version: 6.8.0.oce-0.17
User avatar
wandererfan
Posts: 4078
Joined: Tue Nov 06, 2012 5:42 pm

Re: Scripting point to point tubing

Postby wandererfan » Fri May 20, 2016 12:09 pm

Make a line connecting tube endpoints, then Sweep tube profile along line? The only tricky bit would be rotating the tube profile to be perpendicular to the sweep path every time.

Still have to make the end cuts, though.

wf
DavidCAD
Posts: 10
Joined: Fri Feb 06, 2015 10:34 pm

Re: Scripting point to point tubing

Postby DavidCAD » Sat May 21, 2016 4:52 pm

Thanks wandererfan. Any mathematical geniuses out there that could help me figure out how to make a square perpendicular to the line? This seemed like such an easy thing to do but it has taken me many hours trying to figure it out. Thanks.
User avatar
wandererfan
Posts: 4078
Joined: Tue Nov 06, 2012 5:42 pm

Re: Scripting point to point tubing

Postby wandererfan » Sat May 21, 2016 7:29 pm

DavidCAD wrote:Any mathematical geniuses out there that could help me figure out how to make a square perpendicular to the line?
Definitely no genius, but play with this:

Code: Select all

#line is an Edge
#start is a Vector (line.Vertexes[0].Point)

t = (line.tangentAt(0.0)).normalize()
n = (line.normalAt(0.0)).normalize()
b = (t.cross(n)).normalize()

corner1 = start + ((0.5 * tubeSize) * n) + ((0.5 * tubeSize) * b)
corner2 = start + ((0.5 * tubeSize) * n) - ((0.5 * tubeSize) * b)
corner3 = start - ((0.5 * tubeSize) * n) + ((0.5 * tubeSize) * b)
corner4 = start - ((0.5 * tubeSize) * n) - ((0.5 * tubeSize) * b)
Should give you a perpendicular square. May not be rotated the way you want. Code is not tested. Probably some gotchas.

wf
User avatar
DeepSOIC
Posts: 7832
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: Scripting point to point tubing

Postby DeepSOIC » Sat May 21, 2016 9:05 pm

Actually, I think this can be semi-automated in 0.16, with my plug-in workbench Lattice2. But explaining this is going to be a whopping tutorial that I'm not up to writing...

There is an easy way of aligning sketches to be perpendicular to edges in v0.17. Use Attachment Editor from Part workbench (in the menu).
User avatar
DeepSOIC
Posts: 7832
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: Scripting point to point tubing

Postby DeepSOIC » Sat May 21, 2016 9:07 pm

wandererfan wrote:n = (line.normalAt(0.0)).normalize()
I'm afraid this won't work for lines. Since lines have no curvature, their normal is undefined.
DavidCAD
Posts: 10
Joined: Fri Feb 06, 2015 10:34 pm

Re: Scripting point to point tubing

Postby DavidCAD » Sun May 22, 2016 3:31 pm

Thanks DeepSOIC and wandererfan I will look into the newer versions.
User avatar
wandererfan
Posts: 4078
Joined: Tue Nov 06, 2012 5:42 pm

Re: Scripting point to point tubing

Postby wandererfan » Sun May 22, 2016 5:07 pm

DeepSOIC wrote:I'm afraid this won't work for lines. Since lines have no curvature, their normal is undefined.
:oops: But actually they have an infinite number of normals, we just need to pick 2.

For lines:

Code: Select all

def isColinear(u,v,tolerance=0.0001)
'''Determine if 2 unit vectors are colinear'''
    if ((u+v).length() < tolerance):    # opposites
        return True
    if ((u-v).length() < tolerance):    # same
        return True;
     return False

#u is line unit vector
def getPerps(u)
'''return 2 unit vectors perpendicular to u'''
if (u.y != 0.0 || u.z != 0):
    v = (1.0, 0.0, 0.0)           #arbitrary choice
    if (isColinear(u,v):
        v = (1.5, 0.0, 0.0)
else:
    v = (0.0, 1.0, 0.0);
    if (isColinear(u,v):
       v = (0.0, 1.5, 0.0)
perp1 = (u.cross(v).normalize()               #was n
perp2 = (u.cross(perp1)).normalize()           #was b
return (perp1, perp2)
mario52
Posts: 3309
Joined: Wed May 16, 2012 2:13 pm

Re: Scripting point to point tubing

Postby mario52 » Sun May 22, 2016 5:26 pm

hi
one little macro for create one line between 2 objects center

Code: Select all

# create line to axis (BoundBox.Center) twoo objects
sel = FreeCADGui.Selection.getSelection()                                             # select objects with getSelection()
try:
    boundBox_0= sel[0].Shape.BoundBox.Center                                          # BoundBox of the object 1
    App.Console.PrintMessage("boundBoxCent   : "+str(boundBox_0)+"\n")
    
    boundBox_1= sel[1].Shape.BoundBox.Center                                          # BoundBox of the object 2
    App.Console.PrintMessage("boundBoxCent   : "+str(boundBox_1)+"\n")

    import Draft                                                                      # create line
    points=[FreeCAD.Vector(boundBox_0),FreeCAD.Vector(boundBox_1)]
    Draft.makeWire(points,closed=False,face=False,support=None)
except Exception:
    App.Console.PrintError("select twoo objects ("+str(len(sel))+" object selected)"+"\n")
mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting, Dialog creation. My macros on Gist.github.
DavidCAD
Posts: 10
Joined: Fri Feb 06, 2015 10:34 pm

Re: Scripting point to point tubing

Postby DavidCAD » Sun May 22, 2016 8:44 pm

Thanks guys. I have installed V16 but the Addons installer macro is hanging at trying to download the list. Don't think I have the GIT package. Working on it.

mario52, interesting macro but what I have is the two points and am trying to create an object, square tube, between them. Thanks.