Macro FCCamGroover: Difference between revisions

From FreeCAD Documentation
(update version)
 
(15 intermediate revisions by 4 users not shown)
Line 1: Line 1:
<languages/>
{{Macro|Icon=Text-x-python|Name=FCCamGroover|Description=Create grooved cylinder for cam|Author=TheMarkster|Version=2018.07.23|Date=2018-07-23}}
<translate>
<!--T:1-->
{{Macro
|Name=FCCamGroover
|Icon=Cam-groover-icon-32x32.png
|Description=This macro creates a groove in a cylinder for a cam to ride in.<br/>The object created is similar to the one shown in this [https://youtu.be/io1JL1U7kUs youtube video].
|Author=TheMarkster
|Version=2020.06.13
|Date=2020.06.13
|FCVersion=019 and above
|Download=[https://www.freecadweb.org/wiki/images/b/ba/Cam-groover-icon-32x32.png ToolBar Icon]
}}


==Description==
==Description== <!--T:2-->


<!--T:3-->
This macro creates a groove in a cylinder for a cam to ride in.
This macro creates a groove in a cylinder for a cam to ride in.


<!--T:4-->
The object created is similar to the one shown in this [https://youtu.be/io1JL1U7kUs youtube video].
The object created is similar to the one shown in this [https://youtu.be/io1JL1U7kUs youtube video].


<!--T:5-->
Screenshot:
[[File:Cam-groover-screenshot.png]]
[[Image:Cam-groover-screenshot.png|1000px]]


<!--T:6-->
==Script==
[[Image:Cam-groover-animated-opt.gif]]
# -*- coding: utf-8 -*-

import FreeCAD
==Script== <!--T:7-->
from FreeCAD import Base
</translate>
from PySide import QtGui,QtCore

import Part, Draft
ToolBar Icon [[Image:Cam-groover-icon-32x32.png]]
from math import *

import time
'''Macro_FCCamGroover.FCMacro'''
from DraftTools import msg as say

import math
{{MacroCode|code=
# -*- coding: utf-8 -*-
import FreeCAD
__title__ = "FC Cam Groover"
from FreeCAD import Base
__author__ = "TheMarkster"
from PySide import QtGui,QtCore
__url__ = <nowiki>"https://www.freecadweb.org/wiki/Macro_FCCamGroover"</nowiki>
import Part, Draft
__Wiki__ =<nowiki> "https://www.freecadweb.org/wiki/Macro_FCCamGroover"</nowiki>
from math import *
__date__ = "2018.07.23"
import time
__version__ = __date__
import math

#OS: Windows 10

#Word size of OS: 64-bit
__title__ = "FC Cam Groover"
#Word size of FreeCAD: 64-bit
__author__ = "TheMarkster"
#Version: 0.18.14061 (Git)
__url__ = "https://www.freecadweb.org/wiki/Macro_FCCamGroover"
#Build type: Release
__Wiki__ ="https://www.freecadweb.org/wiki/Macro_FCCamGroover"
#Branch: master
__date__ = "2020.06.13"
#Hash: c4fc02cbcfff975712e977dc08f859fba71ba0ad
__version__ = __date__
#Python version: 2.7.14

#Qt version: 4.8.7
msg = u"""FC Cam Groover v"""+__version__+""", a macro to create a cam groove in a cylinder, Copyright (c) 2018 by 'TheMarkster'<br/>
#Coin version: 4.0.0a
<br/>
#OCC version: 7.2.0
Credit to Gomez Lucio and Laurent Despeyroux for their macro, '3D Parametric Curve Macro', 2015, which provided inspiration
#Locale: English/UnitedStates (en_US)
for some of the code used herin. Thanks also to emills2 for his invaluable feedback. All errors are mine, not theirs.<br/>
<br/>
The grooved cylinder produced will be similar to the one depicted in this video: <a href = 'https://youtu.be/io1JL1U7kUs'>https://youtu.be/io1JL1U7kUs</a><br/>
msg = <nowiki>u"""FC Cam Groover v"""+__version__+""", a macro to create a cam groove in a cylinder, Copyright (c) 2018 by 'TheMarkster'<br/>
<br/>
<br/>
Parameters supported: cylinder height, cylinder radius, groove depth, groove width, strokes per revolution, stroke range, steps (precision) <br/>
Credit to Gomez Lucio and Laurent Despeyroux for their macro, '3D Parametric Curve Macro', 2015, which provided inspiration
<br/>
for some of the code used herin. Thanks also to emills2 for his invaluable feedback. All errors are mine, not theirs.<br/>
This file is a supplement to the FreeCAD CAx development system.
<br/>
<br/>
The grooved cylinder produced will be similar to the one depicted in this video: <a href = 'https://youtu.be/io1JL1U7kUs'>https://youtu.be/io1JL1U7kUs</a><br/>
This program is free software; you can redistribute it and/or modify
<br/>
it under the terms of the GNU Lesser General Public License (LGPL)
Parameters supported: <br/>
as published by the Free Software Foundation; either version 2 of
<br/>
the License, or (at your option) any later version.
cylinder height, <br/>
<br/>
cylinder radius, <br/>
groove depth, <br/>
<br/>
This software is distributed in the hope that it will be useful,
groove width, <br/>
but WITHOUT ANY WARRANTY; without even the implied warranty of
strokes per revolution, <br/>
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
stroke range, <br/>
GNU Library General Public License at <a href='http://www.gnu.org/licenses'>http://www.gnu.org/licenses</a>
steps (precision) <br/>
for more details.<br/>
<br/>
<br/>
This file is a supplement to the FreeCAD CAx development system.
For more information about the GNU Library General Public License
<br/>
write to the Free Software Foundation, Inc., 59 Temple Place,
This program is free software; you can redistribute it and/or modify
Suite 330, Boston, MA 02111-1307 USA
it under the terms of the GNU Lesser General Public License (LGPL)
<br/>
as published by the Free Software Foundation; either version 2 of
"""
the License, or (at your option) any later version.
title = u"""Welcome to FC Cam Groover Macro"""
<br/>
icon=QtGui.QMessageBox.Information
<br/>
diag = QtGui.QMessageBox(icon, title, msg)
This software is distributed in the hope that it will be useful,
diag.setWindowModality(QtCore.Qt.ApplicationModal)
but WITHOUT ANY WARRANTY; without even the implied warranty of
diag.setTextFormat(QtCore.Qt.RichText);
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
diag.exec_()
GNU Library General Public License at <a href='http://www.gnu.org/licenses'>http://www.gnu.org/licenses</a>

for more details.<br/>
window = QtGui.QMainWindow()
<br/>

For more information about the GNU Library General Public License
def say(msg):
write to the Free Software Foundation, Inc., 59 Temple Place,
FreeCAD.Console.PrintMessage(msg);
Suite 330, Boston, MA 02111-1307 USA

<br/> </nowiki>
def processEvents():
"""
time.sleep(0.001)
title = u"""Welcome to FC Cam Groover Macro"""
QtGui.QApplication.processEvents()
icon=QtGui.QMessageBox.Information

diag = QtGui.QMessageBox(icon, title, msg)
def getDouble(txt1,txt2,default,start=0,decimals=4):
diag.setWindowModality(QtCore.Qt.ApplicationModal)
inp,ok = QtGui.QInputDialog.getDouble(window, txt1,txt2,default, start,1000000, decimals)
diag.setTextFormat(QtCore.Qt.RichText);
if ok:
diag.exec_()
return inp
else:
window = QtGui.QMainWindow()
raise StandardError('User canceled')
def processEvents():

time.sleep(0.001)
def getInt(txt1,txt2,default):
QtGui.QApplication.processEvents()
inp, ok = QtGui.QInputDialog.getInt(window, txt1,txt2,default, 1, 100000, 1)
if ok:
def getDouble(txt1,txt2,default,start=0,decimals=4):
return inp
inp,ok = QtGui.QInputDialog.getDouble(window, txt1,txt2,default, start,1000000, decimals)
if ok:
else:
return inp
raise StandardError('User canceled')

else:

raise StandardError('User canceled')
def getDistance3d(x1, y1, z1, x2, y2, z2):
return math.sqrt((x1 - x2)**2 + (y1 - y2)**2 + (z1 - z2)**2)
def getInt(txt1,txt2,default):

inp, ok = QtGui.QInputDialog.getInt(window, txt1,txt2,default, 1, 100000, 1)
radius = getDouble(u'Radius of Cylinder',u'Enter radius of cylinder (in mm).\nMake sure it\'s big enough or the operation will fail:',10)
if ok:
cylinderHeight = getDouble(u'Height of Cylinder', u'Enter the height of the cylinder. (in mm)\n(Can be adjusted after the macro finishes.)',10)
return inp
depth = getDouble(u'Depth of Groove', u'Enter the depth of the groove to be cut into the cylinder (in mm):',2)
else:
slotHeight = getDouble(u'Height of Groove',u'Enter the height of the slot to be cut into the cylinder.\n\nNote: this is not the stroke length, which will be input below. (in mm) ',2)
raise StandardError('User canceled')
innerRadius = radius-depth
strokeFactor = getDouble(u'Stroke Length',u'Enter the axial length of the stroke: (in mm)',5)
strokeFactor /= radius
def getDistance3d(x1, y1, z1, x2, y2, z2):
strokesPerRevolution = getInt(u'Strokes per Revolution',u'Enter the number of strokes per revolution of the cylinder.\n1 or 2 seems to work okay, but anything beyond 3 could be troublesome:', 2)
return math.sqrt((x1 - x2)**2 + (y1 - y2)**2 + (z1 - z2)**2)
steps = getInt(u'Steps',u'Enter number of steps (points) in curve, the more points the more precise the curve,\nbut the longer to process. If it\'s too small the operation *will* fail.\nExperiment with this value if you are having trouble with a particular groove.\nThe default is 628:',628)

radius = getDouble(u'Radius of Cylinder',u'Enter radius of cylinder (in mm).\nMake sure it\'s big enough or the operation will fail:',10)
#inspiration for this makeBSpline function is from the 3d parametric curve macro
cylinderHeight = getDouble(u'Height of Cylinder', u'Enter the height of the cylinder. (in mm)\n(Can be adjusted after the macro finishes.)',10)
#it is not an exact copy/paste of the code, but rather is based on the same principles
depth = getDouble(u'Depth of Groove', u'Enter the depth of the groove to be cut into the cylinder (in mm):',2)
#but without overhead of the gui
slotHeight = getDouble(u'Height of Groove',u'Enter the height of the slot to be cut into the cylinder.\n\nNote: this is not the stroke length, which will be input below. (in mm) ',2)

innerRadius = radius-depth

strokeFactor = getDouble(u'Stroke Length',u'Enter the axial length of the stroke: (in mm)',5)
strokeFactor /= radius
def makeBSpline(strokesPerRevolution,strokeFactor,radius,steps):
a = strokesPerRevolution
strokesPerRevolution = getInt(u'Strokes per Revolution',u'Enter the number of strokes per revolution of the cylinder.\n1 or 2 seems to work okay, but anything beyond 3 could be troublesome:', 2)
b = strokeFactor
steps = getInt(u'Steps',u'Enter number of steps (points) in curve, the more points the more precise the curve,\nbut the longer to process. If it\'s too small the operation *will* fail.\nExperiment with this value if you are having trouble with a particular groove.\nThe default is 628:',628)
c = radius
interval = (2*math.pi)/float(steps)
#inspiration for this makeBSpline function is from the 3d parametric curve macro
points=[]
#it is not an exact copy/paste of the code, but rather is based on the same principles
t = 0.0
#but without overhead of the gui
maxT = 2*math.pi
counter=0
while t <= maxT:
def makeBSpline(strokesPerRevolution,strokeFactor,radius,steps):
say(u'Step '+str(counter)+u' of '+str(steps)+u'\n')
a = strokesPerRevolution
b = strokeFactor
counter +=1
c = radius
processEvents()
interval = (2*math.pi)/float(steps)
x = math.cos(t)*c
points=[]
y = math.sin(t)*c
t = 0.0
z = math.sin(t*a/2.0)*math.cos(t*a/2.0)*c*b
points.append(App.Vector(x,y,z))
maxT = 2*math.pi
counter=0
t += interval

while t <= maxT:
Draft.makeBSpline(points,closed=True,face=False)
say(u'Step '+str(counter)+u' of '+str(steps)+u'\n')
say(u'Done with BSpline\n')
counter +=1
processEvents()
processEvents()

x = math.cos(t)*c

y = math.sin(t)*c
if not App.ActiveDocument:
z = math.sin(t*a/2.0)*math.cos(t*a/2.0)*c*b
points.append(App.Vector(x,y,z))
App.newDocument()
toBeRemoved=[] #temporary objects to be removed to clean up tree
t += interval
say(u'Making BSpline...\n')
processEvents()
Draft.makeBSpline(points,closed=True,face=False)
makeBSpline(strokesPerRevolution,strokeFactor,radius,steps)
say(u'Done with BSpline\n')
App.ActiveDocument.recompute()
processEvents()
bs = App.ActiveDocument.ActiveObject


if not App.ActiveDocument:

App.newDocument()
#we use an array of circles aligned along the bspline to create a new
toBeRemoved=[] #temporary objects to be removed to clean up tree
#bspline that closely follows (but is not exact copy of) original bspline
say(u'Making BSpline...\n')
#each circle has a vertex, similar to the seam line of a cylinder
processEvents()
#these vertices are used to create a list of points from which the new
makeBSpline(strokesPerRevolution,strokeFactor,radius,steps)
#bspline is created
bs = App.ActiveDocument.ActiveObject
#simply moving the original bspline along the z axis does not work because
#the groove will narrow in places and widen in others. the circles ensure
#better (though probably still not perfect) uniformity of the groove

#we use an array of circles aligned along the bspline to create a new

#bspline that closely follows (but is not exact copy of) original bspline
Draft.makeCircle(slotHeight,face=False)
#each circle has a vertex, similar to the seam line of a cylinder
cir = App.ActiveDocument.getObject('Circle')
#these vertices are used to create a list of points from which the new
cir.Placement=App.Placement(App.Vector(0,0,0), App.Rotation(90,0,0), App.Vector(0,0,0))
#bspline is created
App.ActiveDocument.recompute()
#simply moving the original bspline along the z axis does not work because
say(u'Making a draft path array of '+str(steps)+' circles, waiting for FreeCAD...\n')
#the groove will narrow in places and widen in others. the circles ensure
processEvents()
#better (though probably still not perfect) uniformity of the groove
try:
Draft.make_path_array(cir,bs,count=steps,align=True,align_mode='Tangent',tan_vector=App.Vector(0,0,1),force_vertical=True)
except:
Draft.makeCircle(slotHeight,face=False)
title = u"""Potential incompatibility with this version of FreeCAD"""
cir = App.ActiveDocument.getObject('Circle')
msg='There is a potential incompatibility with this version of FreeCAD. If the groove does not appear correct after processing completes \
cir.Placement=App.Placement(App.Vector(0,0,0), App.Rotation(90,0,0), App.Vector(0,0,0))
update to version 0.19 or later and try again.'
say(u'Making a draft path array of '+str(steps)+' circles, waiting for FreeCAD...\n')
icon=QtGui.QMessageBox.Information
processEvents()
diag = QtGui.QMessageBox(icon, title, msg)
Draft.makePathArray(cir,bs,count=steps,xlate=None,align=True)
diag.setWindowModality(QtCore.Qt.ApplicationModal)
pa = App.ActiveDocument.getObject('PathArray')
diag.setTextFormat(QtCore.Qt.RichText);
points = []
diag.exec_()
for v in pa.Shape.Vertexes:
Draft.makePathArray(cir,bs,count=steps,xlate=None,align=True)
points.append(v.Point)
App.ActiveDocument.recompute()
Draft.makeBSpline(points,True,None)
bs1 = App.ActiveDocument.ActiveObject
pa = App.ActiveDocument.getObject('PathArray')
points = []
for v in pa.Shape.Vertexes:
points.append(v.Point)
say(u'Scaling bspline1...\n')
Draft.makeBSpline(points,True,None)
processEvents()
bs1 = App.ActiveDocument.ActiveObject
Draft.scale([bs],delta=FreeCAD.Vector(innerRadius/float(radius),innerRadius/float(radius),1.0),center=FreeCAD.Vector(0.0,0.0,0.0),copy=True,legacy=True)
bs2 = App.ActiveDocument.ActiveObject
App.ActiveDocument.recompute()

say(u'Scaling bspline2...\n')
say(u'Scaling bspline1...\n')
processEvents()
processEvents()
Draft.scale([bs1],delta=FreeCAD.Vector(innerRadius/float(radius),innerRadius/float(radius),1.0),center=FreeCAD.Vector(0.0,0.0,0.0),copy=True,legacy=True)
Draft.scale([bs],FreeCAD.Vector(innerRadius/float(radius),innerRadius/float(radius),1.0),center=FreeCAD.Vector(0.0,0.0,0.0),copy=True)
bs3 = App.ActiveDocument.ActiveObject
bs2 = App.ActiveDocument.ActiveObject
#now make the original bsplines bigger so they extend outside the cylinder edges (to prevent issues with boolean cut later on)
say(u'Scaling bspline2...\n')
Draft.scale([bs],delta=FreeCAD.Vector(1.05,1.05,1.0),center=FreeCAD.Vector(0.0,0.0,0.0),copy=False,legacy=True)
processEvents()
say(u'Scaling bspline2...\n')
Draft.scale([bs1],FreeCAD.Vector(innerRadius/float(radius),innerRadius/float(radius),1.0),center=FreeCAD.Vector(0.0,0.0,0.0),copy=True)
processEvents()
bs3 = App.ActiveDocument.ActiveObject
Draft.scale([bs1],delta=FreeCAD.Vector(1.05,1.05,1.0),center=FreeCAD.Vector(0.0,0.0,0.0),copy=False,legacy=True)
#now make the original bsplines bigger so they extend outside the cylinder edges (to prevent issues with boolean cut later on)
say(u'Making ruled surfaces.')
Draft.scale([bs],FreeCAD.Vector(1.05,1.05,1.0),center=FreeCAD.Vector(0.0,0.0,0.0),copy=False)
processEvents()
say(u'Scaling bspline2...\n')
FreeCAD.ActiveDocument.addObject('Part::RuledSurface', 'Ruled Surface')
processEvents()
FreeCAD.ActiveDocument.ActiveObject.Curve1=(bs1,['Edge1'])
Draft.scale([bs1],FreeCAD.Vector(1.05,1.05,1.0),center=FreeCAD.Vector(0.0,0.0,0.0),copy=False)
FreeCAD.ActiveDocument.ActiveObject.Curve2=(bs3,['Edge1'])
say(u'Making ruled surfaces.')
FreeCAD.ActiveDocument.ActiveObject.Orientation = u"Forward"
processEvents()
rs = FreeCAD.ActiveDocument.ActiveObject
FreeCAD.ActiveDocument.addObject('Part::RuledSurface', 'Ruled Surface')
say(u'.')
FreeCAD.ActiveDocument.ActiveObject.Curve1=(bs1,['Edge1'])
processEvents()
FreeCAD.ActiveDocument.addObject('Part::RuledSurface', 'Ruled Surface')
FreeCAD.ActiveDocument.ActiveObject.Curve2=(bs3,['Edge1'])
FreeCAD.ActiveDocument.ActiveObject.Curve1=(bs2,['Edge1'])
FreeCAD.ActiveDocument.ActiveObject.Orientation = u"Forward"
FreeCAD.ActiveDocument.ActiveObject.Curve2=(bs3,['Edge1'])
rs = FreeCAD.ActiveDocument.ActiveObject
say(u'.')
rs1 = FreeCAD.ActiveDocument.ActiveObject
processEvents()
say(u'.')
FreeCAD.ActiveDocument.addObject('Part::RuledSurface', 'Ruled Surface')
processEvents()
FreeCAD.ActiveDocument.addObject('Part::RuledSurface', 'Ruled Surface')
FreeCAD.ActiveDocument.ActiveObject.Curve1=(bs2,['Edge1'])
FreeCAD.ActiveDocument.ActiveObject.Curve1=(rs,['Edge1'])
FreeCAD.ActiveDocument.ActiveObject.Curve2=(bs3,['Edge1'])
FreeCAD.ActiveDocument.ActiveObject.Curve2=(bs,['Edge1'])
FreeCAD.ActiveDocument.ActiveObject.Orientation=u"Forward"
FreeCAD.ActiveDocument.ActiveObject.Orientation=u"Forward"
rs1 = FreeCAD.ActiveDocument.ActiveObject
say(u'.')
rs2 = FreeCAD.ActiveDocument.ActiveObject
processEvents()
say(u'.')
FreeCAD.ActiveDocument.addObject('Part::RuledSurface', 'Ruled Surface')
processEvents()
FreeCAD.ActiveDocument.addObject('Part::RuledSurface', 'Ruled Surface')
FreeCAD.ActiveDocument.ActiveObject.Curve1=(bs1,['Edge1'])
FreeCAD.ActiveDocument.ActiveObject.Curve1=(rs2,['Edge3'])
FreeCAD.ActiveDocument.ActiveObject.Curve2=(bs,['Edge1'])
FreeCAD.ActiveDocument.ActiveObject.Curve2=(rs1,['Edge1'])
FreeCAD.ActiveDocument.ActiveObject.Orientation=u"Forward"
FreeCAD.ActiveDocument.ActiveObject.Orientation = u"Reversed"
rs2 = FreeCAD.ActiveDocument.ActiveObject
say(u'.')
rs3 = FreeCAD.ActiveDocument.ActiveObject
processEvents()
say(u'. Done.\n')
FreeCAD.ActiveDocument.addObject('Part::RuledSurface', 'Ruled Surface')
processEvents()
FreeCAD.ActiveDocument.ActiveObject.Curve1=(bs2,['Edge1'])
FreeCAD.ActiveDocument.ActiveObject.Curve2=(bs,['Edge1'])
toBeRemoved.extend([bs.Name,bs1.Name,bs2.Name,bs3.Name,rs.Name,rs1.Name,rs2.Name,rs3.Name,pa.Name,cir.Name])
FreeCAD.ActiveDocument.ActiveObject.Orientation = u"Forward"
App.ActiveDocument.recompute()
rs3 = FreeCAD.ActiveDocument.ActiveObject
say(u'. Done.\n')
processEvents()
say(u'Building shell...\n')

processEvents()
toBeRemoved.extend([bs.Name,bs1.Name,bs2.Name,bs3.Name,rs.Name,rs1.Name,rs2.Name,rs3.Name,pa.Name,cir.Name])
try:

_=Part.Shell([App.ActiveDocument.getObject(rs3.Name).Shape.Faces[0], App.ActiveDocument.getObject(rs2.Name).Shape.Faces[0], App.ActiveDocument.getObject(rs.Name).Shape.Faces[0], App.ActiveDocument.getObject(rs1.Name).Shape.Faces[0], ])
App.ActiveDocument.recompute()
except:

say('Exception making shell. You might try increasing the number of steps, which sometimes helps.\n')
say(u'Building shell...\n')
raise StandardError('Exception creating shell object.')
processEvents()
if _.isNull(): raise RuntimeError('Failed to create shell')
try:
App.ActiveDocument.addObject('Part::Feature','Shell').Shape=_.removeSplitter()
_=Part.Shell([App.ActiveDocument.getObject(rs3.Name).Shape.Faces[0], App.ActiveDocument.getObject(rs2.Name).Shape.Faces[0], App.ActiveDocument.getObject(rs.Name).Shape.Faces[0], App.ActiveDocument.getObject(rs1.Name).Shape.Faces[0], ])
del _
except:
shell = App.ActiveDocument.ActiveObject
say('Exception making shell. You might try increasing the number of steps, which sometimes helps.\n')
toBeRemoved.extend([shell.Name])
raise StandardError('Exception creating shell object.')
if _.isNull(): raise RuntimeError('Failed to create shell')
App.ActiveDocument.recompute()
App.ActiveDocument.addObject('Part::Feature','Shell').Shape=_.removeSplitter()
del _
shell = App.ActiveDocument.ActiveObject
say(u'Making shell into a solid...\n')
toBeRemoved.extend([shell.Name])
processEvents()


__s__=App.ActiveDocument.Shell.Shape
App.ActiveDocument.recompute()
__s__=Part.Solid(__s__)

__o__=App.ActiveDocument.addObject("Part::Feature","GrooveFeature")
say(u'Making shell into a solid...\n')
__o__.Label="GrooveFeature"
processEvents()
__o__.Shape=__s__

del __s__, __o__
shellSolid = App.ActiveDocument.ActiveObject
__s__=App.ActiveDocument.Shell.Shape
__s__=Part.Solid(__s__)
App.ActiveDocument.recompute()
__o__=App.ActiveDocument.addObject("Part::Feature","GrooveFeature")
__o__.Label="GrooveFeature"
say('Building cylinder...\n')
__o__.Shape=__s__
processEvents()
del __s__, __o__
App.ActiveDocument.addObject("Part::Cylinder","Cylinder")
App.ActiveDocument.ActiveObject.Label = "Cylinder"
shellSolid = App.ActiveDocument.ActiveObject
App.ActiveDocument.getObject("Cylinder").Radius = str(radius)
App.ActiveDocument.recompute()

App.ActiveDocument.getObject("Cylinder").Height = str(cylinderHeight)
say('Building cylinder...\n')
cylinder = App.ActiveDocument.ActiveObject
processEvents()
App.ActiveDocument.addObject("Part::Cylinder","Cylinder")
App.ActiveDocument.ActiveObject.Label = "Cylinder"
shellSolid.Placement=App.Placement(App.Vector(0,0,-slotHeight/2.0), App.Rotation(0,0,0), App.Vector(0,0,0)) #center groove in cylinder
App.ActiveDocument.getObject("Cylinder").Radius = str(radius)
cylinder.Placement=App.Placement(App.Vector(0,0,-cylinderHeight/2.0), App.Rotation(10,0,0), App.Vector(0,0,0)) #rotate slightly to move the edge lines apart
App.ActiveDocument.getObject("Cylinder").Height = str(cylinderHeight)
App.ActiveDocument.recompute()
cylinder = App.ActiveDocument.ActiveObject


say(u'Cleaning up temporary objects.')
shellSolid.Placement=App.Placement(App.Vector(0,0,-slotHeight/2.0), App.Rotation(0,0,0), App.Vector(0,0,0)) #center groove in cylinder
processEvents()
cylinder.Placement=App.Placement(App.Vector(0,0,-cylinderHeight/2.0), App.Rotation(10,0,0), App.Vector(0,0,0)) #rotate slightly to move the edge lines apart
for tbr in toBeRemoved:

App.ActiveDocument.removeObject(tbr)
App.ActiveDocument.recompute()
say(u'.')

processEvents()
say(u'. Done.\n')
say(u'Cleaning up temporary objects.')
processEvents()
for tbr in toBeRemoved:
App.ActiveDocument.recompute()
App.ActiveDocument.removeObject(tbr)
Gui.SendMsgToActiveView("ViewFit")
say(u'.')
processEvents()
msg=u"""
say(u'. Done.\n')
Done. Last thing you need to do is the boolean cut from the Part workbench, cutting Groove Feature from the cylinder,

if all looks good. It is recommended to check the geometry via the Part workbench menu with RunBoPCheck = True enabled
App.ActiveDocument.recompute()
in Tools -> Edit Parameters -> Preferences -> Mod -> Part -> CheckGeometry. If the check fails, go back and do it again
Gui.SendMsgToActiveView("ViewFit")
with a different number of steps.

"""
msg=u"""
title=u'Thanks for using FC Cam Groover'
Done. Last thing you need to do is the boolean cut from the Part workbench, cutting Groove Feature from the cylinder,
icon=QtGui.QMessageBox.Information
if all looks good. It is recommended to check the geometry via the Part workbench menu with RunBoPCheck = True enabled
diag = QtGui.QMessageBox(icon, title, msg)
in Tools -> Edit Parameters -> Preferences -> Mod -> Part -> CheckGeometry. If the check fails, go back and do it again
diag.setWindowModality(QtCore.Qt.ApplicationModal)
with a different number of steps. Sometimes it might be necessary to adjust the z-position Placement of the Groove Feature.
diag.setTextFormat(QtCore.Qt.RichText);
"""
diag.exec_()
title=u'Thanks for using FC Cam Groover'
icon=QtGui.QMessageBox.Information
diag = QtGui.QMessageBox(icon, title, msg)
diag.setWindowModality(QtCore.Qt.ApplicationModal)
diag.setTextFormat(QtCore.Qt.RichText);
diag.exec_()
}}
<translate>

==Link== <!--T:8-->

<!--T:9-->
The forum [https://forum.freecadweb.org/viewtopic.php?f=22&t=29816&sid=f6f9b946f015bc5072bf112d97b50256 FC Cam Groover Macro]
</translate>

Latest revision as of 18:09, 13 June 2020

Other languages:

FCCamGroover

Description
This macro creates a groove in a cylinder for a cam to ride in.
The object created is similar to the one shown in this youtube video.

Macro version: 2020.06.13
Last modified: 2020.06.13
FreeCAD version: 019 and above
Download: ToolBar Icon
Author: TheMarkster
Author
TheMarkster
Download
ToolBar Icon
Links
Macro Version
2020.06.13
Date last modified
2020.06.13
FreeCAD Version(s)
019 and above
Default shortcut
None
See also
None

Description

This macro creates a groove in a cylinder for a cam to ride in.

The object created is similar to the one shown in this youtube video.

Script

ToolBar Icon

Macro_FCCamGroover.FCMacro

# -*- coding: utf-8 -*-
import FreeCAD
from FreeCAD import Base
from PySide import QtGui,QtCore
import Part, Draft
from math import *
import time
import math


__title__ = "FC Cam Groover"
__author__ = "TheMarkster"
__url__ = "https://www.freecadweb.org/wiki/Macro_FCCamGroover"
__Wiki__ ="https://www.freecadweb.org/wiki/Macro_FCCamGroover"
__date__ = "2020.06.13" 
__version__ = __date__

msg = u"""FC Cam Groover v"""+__version__+""", a macro to create a cam groove in a cylinder, Copyright (c) 2018 by 'TheMarkster'<br/>
<br/> 
Credit to Gomez Lucio and Laurent Despeyroux for their macro, '3D Parametric Curve Macro', 2015, which provided inspiration
for some of the code used herin.  Thanks also to emills2 for his invaluable feedback. All errors are mine, not theirs.<br/>
<br/>
The grooved cylinder produced will be similar to the one depicted in this video: <a href = 'https://youtu.be/io1JL1U7kUs'>https://youtu.be/io1JL1U7kUs</a><br/>
<br/>
Parameters supported: cylinder height, cylinder radius, groove depth, groove width, strokes per revolution, stroke range, steps (precision) <br/>
<br/>
This file is a supplement to the FreeCAD CAx development system.                                                                            
<br/>
This program is free software; you can redistribute it and/or modify  
it under the terms of the GNU Lesser General Public License (LGPL)    
as published by the Free Software Foundation; either version 2 of     
the License, or (at your option) any later version.                   
<br/>                                                             
<br/>
This software is distributed in the hope that it will be useful,      
but WITHOUT ANY WARRANTY; without even the implied warranty of        
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         
GNU Library General Public License at <a href='http://www.gnu.org/licenses'>http://www.gnu.org/licenses</a> 
for more details.<br/>                                                               
<br/>
For more information about the GNU Library General Public License     
write to the Free Software Foundation, Inc., 59 Temple Place,         
Suite 330, Boston, MA  02111-1307 USA                                 
<br/>
"""
title = u"""Welcome to FC Cam Groover Macro"""
icon=QtGui.QMessageBox.Information
diag = QtGui.QMessageBox(icon, title, msg)
diag.setWindowModality(QtCore.Qt.ApplicationModal)
diag.setTextFormat(QtCore.Qt.RichText); 
diag.exec_()

window = QtGui.QMainWindow()

def say(msg):
    FreeCAD.Console.PrintMessage(msg);

def processEvents():
     time.sleep(0.001)
     QtGui.QApplication.processEvents()

def getDouble(txt1,txt2,default,start=0,decimals=4):
    inp,ok = QtGui.QInputDialog.getDouble(window, txt1,txt2,default, start,1000000, decimals)
    if ok:
        return inp
    else:
        raise StandardError('User canceled')

def getInt(txt1,txt2,default):
    inp, ok = QtGui.QInputDialog.getInt(window, txt1,txt2,default, 1, 100000, 1)
    if ok:
        return inp
    else:
        raise StandardError('User canceled')


def getDistance3d(x1, y1, z1, x2, y2, z2):
    return math.sqrt((x1 - x2)**2 + (y1 - y2)**2 + (z1 - z2)**2)

radius = getDouble(u'Radius of Cylinder',u'Enter radius of cylinder (in mm).\nMake sure it\'s big enough or the operation will fail:',10)
cylinderHeight = getDouble(u'Height of Cylinder', u'Enter the height of the cylinder. (in mm)\n(Can be adjusted after the macro finishes.)',10)
depth = getDouble(u'Depth of Groove', u'Enter the depth of the groove to be cut into the cylinder (in mm):',2)
slotHeight = getDouble(u'Height of Groove',u'Enter the height of the slot to be cut into the cylinder.\n\nNote: this is not the stroke length, which will be input below. (in mm) ',2)
innerRadius = radius-depth
strokeFactor = getDouble(u'Stroke Length',u'Enter the axial length of the stroke: (in mm)',5)
strokeFactor /= radius
strokesPerRevolution = getInt(u'Strokes per Revolution',u'Enter the number of strokes per revolution of the cylinder.\n1 or 2 seems to work okay, but anything beyond 3 could be troublesome:', 2)
steps = getInt(u'Steps',u'Enter number of steps (points) in curve, the more points the more precise the curve,\nbut the longer to process.  If it\'s too small the operation *will* fail.\nExperiment with this value if you are having trouble with a particular groove.\nThe default is 628:',628)

#inspiration for this makeBSpline function is from the 3d parametric curve macro
#it is not an exact copy/paste of the code, but rather is based on the same principles
#but without overhead of the gui


def makeBSpline(strokesPerRevolution,strokeFactor,radius,steps):
    a = strokesPerRevolution
    b = strokeFactor
    c = radius
    interval = (2*math.pi)/float(steps)
    points=[]
    t = 0.0
    maxT = 2*math.pi
    counter=0
    while t <= maxT:
        say(u'Step '+str(counter)+u' of '+str(steps)+u'\n')
        counter +=1
        processEvents()
        x = math.cos(t)*c
        y = math.sin(t)*c
        z = math.sin(t*a/2.0)*math.cos(t*a/2.0)*c*b
        points.append(App.Vector(x,y,z))
        t += interval

    Draft.makeBSpline(points,closed=True,face=False)
    say(u'Done with BSpline\n')
    processEvents()


if not App.ActiveDocument:
    App.newDocument()
toBeRemoved=[] #temporary objects to be removed to clean up tree
say(u'Making BSpline...\n')
processEvents()
makeBSpline(strokesPerRevolution,strokeFactor,radius,steps)
App.ActiveDocument.recompute()
bs = App.ActiveDocument.ActiveObject



#we use an array of circles aligned along the bspline to create a new
#bspline that closely follows (but is not exact copy of) original bspline
#each circle has a vertex, similar to the seam line of a cylinder
#these vertices are used to create a list of points from which the new
#bspline is created
#simply moving the original bspline along the z axis does not work because
#the groove will narrow in places and widen in others.  the circles ensure
#better (though probably still not perfect) uniformity of the groove


Draft.makeCircle(slotHeight,face=False)
cir = App.ActiveDocument.getObject('Circle')
cir.Placement=App.Placement(App.Vector(0,0,0), App.Rotation(90,0,0), App.Vector(0,0,0))
App.ActiveDocument.recompute()
say(u'Making a draft path array of '+str(steps)+' circles, waiting for FreeCAD...\n')
processEvents()
try:
    Draft.make_path_array(cir,bs,count=steps,align=True,align_mode='Tangent',tan_vector=App.Vector(0,0,1),force_vertical=True)
except:
    title = u"""Potential incompatibility with this version of FreeCAD"""
    msg='There is a potential incompatibility with this version of FreeCAD.  If the groove does not appear correct after processing completes \
update to version 0.19 or later and try again.'
    icon=QtGui.QMessageBox.Information
    diag = QtGui.QMessageBox(icon, title, msg)
    diag.setWindowModality(QtCore.Qt.ApplicationModal)
    diag.setTextFormat(QtCore.Qt.RichText); 
    diag.exec_()
    Draft.makePathArray(cir,bs,count=steps,xlate=None,align=True)
App.ActiveDocument.recompute()
pa = App.ActiveDocument.getObject('PathArray')
points = []
for v in pa.Shape.Vertexes:
    points.append(v.Point)
Draft.makeBSpline(points,True,None)
bs1 = App.ActiveDocument.ActiveObject
App.ActiveDocument.recompute()

say(u'Scaling bspline1...\n')
processEvents()
Draft.scale([bs],FreeCAD.Vector(innerRadius/float(radius),innerRadius/float(radius),1.0),center=FreeCAD.Vector(0.0,0.0,0.0),copy=True)
bs2 = App.ActiveDocument.ActiveObject
say(u'Scaling bspline2...\n')
processEvents()
Draft.scale([bs1],FreeCAD.Vector(innerRadius/float(radius),innerRadius/float(radius),1.0),center=FreeCAD.Vector(0.0,0.0,0.0),copy=True)
bs3 = App.ActiveDocument.ActiveObject
#now make the original bsplines bigger so they extend outside the cylinder edges (to prevent issues with boolean cut later on)
Draft.scale([bs],FreeCAD.Vector(1.05,1.05,1.0),center=FreeCAD.Vector(0.0,0.0,0.0),copy=False)
say(u'Scaling bspline2...\n')
processEvents()
Draft.scale([bs1],FreeCAD.Vector(1.05,1.05,1.0),center=FreeCAD.Vector(0.0,0.0,0.0),copy=False)
say(u'Making ruled surfaces.')
processEvents()
FreeCAD.ActiveDocument.addObject('Part::RuledSurface', 'Ruled Surface')
FreeCAD.ActiveDocument.ActiveObject.Curve1=(bs1,['Edge1'])
FreeCAD.ActiveDocument.ActiveObject.Curve2=(bs3,['Edge1'])
FreeCAD.ActiveDocument.ActiveObject.Orientation = u"Forward"
rs = FreeCAD.ActiveDocument.ActiveObject
say(u'.')
processEvents()
FreeCAD.ActiveDocument.addObject('Part::RuledSurface', 'Ruled Surface')
FreeCAD.ActiveDocument.ActiveObject.Curve1=(bs2,['Edge1'])
FreeCAD.ActiveDocument.ActiveObject.Curve2=(bs3,['Edge1'])
FreeCAD.ActiveDocument.ActiveObject.Orientation=u"Forward"
rs1 = FreeCAD.ActiveDocument.ActiveObject
say(u'.')
processEvents()
FreeCAD.ActiveDocument.addObject('Part::RuledSurface', 'Ruled Surface')
FreeCAD.ActiveDocument.ActiveObject.Curve1=(bs1,['Edge1'])
FreeCAD.ActiveDocument.ActiveObject.Curve2=(bs,['Edge1'])
FreeCAD.ActiveDocument.ActiveObject.Orientation=u"Forward"
rs2 = FreeCAD.ActiveDocument.ActiveObject
say(u'.')
processEvents()
FreeCAD.ActiveDocument.addObject('Part::RuledSurface', 'Ruled Surface')
FreeCAD.ActiveDocument.ActiveObject.Curve1=(bs2,['Edge1'])
FreeCAD.ActiveDocument.ActiveObject.Curve2=(bs,['Edge1'])
FreeCAD.ActiveDocument.ActiveObject.Orientation = u"Forward"
rs3 = FreeCAD.ActiveDocument.ActiveObject
say(u'. Done.\n')
processEvents()

toBeRemoved.extend([bs.Name,bs1.Name,bs2.Name,bs3.Name,rs.Name,rs1.Name,rs2.Name,rs3.Name,pa.Name,cir.Name])

App.ActiveDocument.recompute()

say(u'Building shell...\n')
processEvents()
try:
    _=Part.Shell([App.ActiveDocument.getObject(rs3.Name).Shape.Faces[0], App.ActiveDocument.getObject(rs2.Name).Shape.Faces[0], App.ActiveDocument.getObject(rs.Name).Shape.Faces[0], App.ActiveDocument.getObject(rs1.Name).Shape.Faces[0], ])
except:
    say('Exception making shell.  You might try increasing the number of steps, which sometimes helps.\n')
    raise StandardError('Exception creating shell object.')
if _.isNull(): raise RuntimeError('Failed to create shell')
App.ActiveDocument.addObject('Part::Feature','Shell').Shape=_.removeSplitter()
del _
shell = App.ActiveDocument.ActiveObject
toBeRemoved.extend([shell.Name])


App.ActiveDocument.recompute()

say(u'Making shell into a solid...\n')
processEvents()

__s__=App.ActiveDocument.Shell.Shape
__s__=Part.Solid(__s__)
__o__=App.ActiveDocument.addObject("Part::Feature","GrooveFeature")
__o__.Label="GrooveFeature"
__o__.Shape=__s__
del __s__, __o__
shellSolid = App.ActiveDocument.ActiveObject
App.ActiveDocument.recompute()

say('Building cylinder...\n')
processEvents()
App.ActiveDocument.addObject("Part::Cylinder","Cylinder")
App.ActiveDocument.ActiveObject.Label = "Cylinder"
App.ActiveDocument.getObject("Cylinder").Radius = str(radius)
App.ActiveDocument.getObject("Cylinder").Height = str(cylinderHeight)
cylinder = App.ActiveDocument.ActiveObject


shellSolid.Placement=App.Placement(App.Vector(0,0,-slotHeight/2.0), App.Rotation(0,0,0), App.Vector(0,0,0)) #center groove in cylinder
cylinder.Placement=App.Placement(App.Vector(0,0,-cylinderHeight/2.0), App.Rotation(10,0,0), App.Vector(0,0,0)) #rotate slightly to move the edge lines apart

App.ActiveDocument.recompute()

say(u'Cleaning up temporary objects.')
processEvents()
for tbr in toBeRemoved:
    App.ActiveDocument.removeObject(tbr)
    say(u'.')
    processEvents()
say(u'. Done.\n')

App.ActiveDocument.recompute()
Gui.SendMsgToActiveView("ViewFit")

msg=u"""
Done.  Last thing you need to do is the boolean cut from the Part workbench, cutting Groove Feature from the cylinder, 
if all looks good.  It is recommended to check the geometry via the Part workbench menu with RunBoPCheck = True enabled 
in Tools -> Edit Parameters -> Preferences -> Mod -> Part -> CheckGeometry.  If the check fails, go back and do it again 
with a different number of steps.  Sometimes it might be necessary to adjust the z-position Placement of the Groove Feature.
"""
title=u'Thanks for using FC Cam Groover'
icon=QtGui.QMessageBox.Information
diag = QtGui.QMessageBox(icon, title, msg)
diag.setWindowModality(QtCore.Qt.ApplicationModal)
diag.setTextFormat(QtCore.Qt.RichText); 
diag.exec_()

Link

The forum FC Cam Groover Macro