## Chain generator [an ugly and buggy script]

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

### Chain generator [an ugly and buggy script]

Update: I uploaded a bit more reliable script

1. Open the chain_blank.fcstd file.

3. Paste below script ant hit enter:

Code: Select all

``````#an very buggy and ugly chain generator script
import math, Part, Draft, DraftGeomUtils
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
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)))
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
Attachments
chain_blank.fcstd
Last edited by kwahoo on Tue Jun 10, 2014 4:02 pm, edited 1 time in total.
yorik
Posts: 11845
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

### Re: Chain generator [an ugly and buggy script]

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

### Re: Chain generator [an ugly and buggy script]

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

1. Open the chain_blank.fcstd file.

3. Paste below script ant hit enter:

Code: Select all

``````#an very buggy and ugly chain generator script
import math, Part, Draft, DraftGeomUtils
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
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)))
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

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