Chain generator [an ugly and buggy script]

Need help, or want to share a macro? Post here!
User avatar
kwahoo
Posts: 218
Joined: Fri Nov 29, 2013 3:09 pm
Contact:

Chain generator [an ugly and buggy script]

Postby kwahoo » Thu Jun 05, 2014 5:58 pm

Update: I uploaded a bit more reliable script

1. Open the chain_blank.fcstd file.
2. Select [Ctrl] Sketch008, Link2 and Link1.
Image
3. Paste below script ant hit enter:

Code: Select all

#an very buggy and ugly chain generator script
from FreeCAD import Vector
import math, Part, Draft, DraftGeomUtils
s = FreeCADGui.Selection.getSelection()
try:
     edges = s[0].Shape.Edges #select chain driving sketch/wire
     shape = s[1].Shape #select first chain link
     shape2 = s[2].Shape #select select chain link
except:
     print "Wrong selection"

is_intersect = True
r = 25.4 #link length
edge = edges[0]
p1 = edge.Vertexes[0].Point
p2 = edge.Vertexes[1].Point
angle = math.degrees(Draft.DraftVecUtils.angle(Vector(1,0,0),(p2-p1)))
sc1 = 1

for e in range(len(edges)):
    edge = edges[e]
    is_intersect = True
    pl = FreeCAD.Placement(p1, Vector(0,0,1), angle) #base, rotation axis, angle
    while is_intersect == True:
        arc = Draft.makeCircle(radius=r,placement=pl,face=False,startangle=-90,endangle=90,support=None) #draw helper half-circle
        intersect = DraftGeomUtils.findIntersection(arc.Shape.Edges[0], edge)
        p2 = intersect[0]
        is_intersect = DraftGeomUtils.isPtOnEdge(p2, edge) #check if intersection point is on edge     
        angle = math.degrees(Draft.DraftVecUtils.angle(Vector(1,0,0),(p2-p1)))
        pl = FreeCAD.Placement(p2, Vector(0,0,1), angle)
        if sc1 == 1:
             newshape = shape.copy()
             sc1 = 0
        else:
             newshape = shape2.copy()
             sc1 = 1

        newshape.rotate(Vector(0,0,0),Vector(0,0,1),angle)
        newshape.translate(p1)
        p1 = p2
        Part.show(newshape)
4. A chain should be generated
Image
Attachments
chain_blank.fcstd
(38.78 KiB) Downloaded 112 times
Last edited by kwahoo on Tue Jun 10, 2014 4:02 pm, edited 1 time in total.
User avatar
yorik
Site Admin
Posts: 11163
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: Chain generator [an ugly and buggy script]

Postby yorik » Fri Jun 06, 2014 10:25 pm

nice! thanks for sharing!
Sirios.77
Posts: 14
Joined: Wed Jun 11, 2014 6:21 pm

Re: Chain generator [an ugly and buggy script]

Postby Sirios.77 » Mon Jun 30, 2014 6:34 pm

kwahoo wrote:Update: I uploaded a bit more reliable script

1. Open the chain_blank.fcstd file.
2. Select [Ctrl] Sketch008, Link2 and Link1.
Image
3. Paste below script ant hit enter:

Code: Select all

#an very buggy and ugly chain generator script
from FreeCAD import Vector
import math, Part, Draft, DraftGeomUtils
s = FreeCADGui.Selection.getSelection()
try:
     edges = s[0].Shape.Edges #select chain driving sketch/wire
     shape = s[1].Shape #select first chain link
     shape2 = s[2].Shape #select select chain link
except:
     print "Wrong selection"

is_intersect = True
r = 25.4 #link length
edge = edges[0]
p1 = edge.Vertexes[0].Point
p2 = edge.Vertexes[1].Point
angle = math.degrees(Draft.DraftVecUtils.angle(Vector(1,0,0),(p2-p1)))
sc1 = 1

for e in range(len(edges)):
    edge = edges[e]
    is_intersect = True
    pl = FreeCAD.Placement(p1, Vector(0,0,1), angle) #base, rotation axis, angle
    while is_intersect == True:
        arc = Draft.makeCircle(radius=r,placement=pl,face=False,startangle=-90,endangle=90,support=None) #draw helper half-circle
        intersect = DraftGeomUtils.findIntersection(arc.Shape.Edges[0], edge)
        p2 = intersect[0]
        is_intersect = DraftGeomUtils.isPtOnEdge(p2, edge) #check if intersection point is on edge     
        angle = math.degrees(Draft.DraftVecUtils.angle(Vector(1,0,0),(p2-p1)))
        pl = FreeCAD.Placement(p2, Vector(0,0,1), angle)
        if sc1 == 1:
             newshape = shape.copy()
             sc1 = 0
        else:
             newshape = shape2.copy()
             sc1 = 1

        newshape.rotate(Vector(0,0,0),Vector(0,0,1),angle)
        newshape.translate(p1)
        p1 = p2
        Part.show(newshape)
4. A chain should be generated
Image

Kwahoo are Sirios.77 hello, I saw your work and I thought I'd turn your script into a macro. It is used as you describe, except that after selecting the profile and the two solids, you activate the macro function at the top, you select the button Chain_blank.FCMacro and run, and you're done.
I hope you do not mind it! ;)
Hello
Chain_blank.zip
(846 Bytes) Downloaded 102 times