Scripted Parts: Ball Bearing - Part 1: Difference between revisions
No edit summary |
(Added categories.) |
||
(5 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
⚫ | |||
<translate> |
<translate> |
||
<!--T:1--> |
<!--T:1--> |
||
Line 10: | Line 11: | ||
}} |
}} |
||
<!--T:2--> |
|||
===Introduction=== |
===Introduction=== <!--T:2--> |
||
This tutorial is meant as a beginner's introduction to creating parts with python scripts within FreeCAD.<br /> |
This tutorial is meant as a beginner's introduction to creating parts with python scripts within FreeCAD.<br /> |
||
This tutorial will cover how to build a ball bearing with a CSG-workflow.<br /> |
This tutorial will cover how to build a ball bearing with a CSG-workflow.<br /> |
||
The code will produce a new FreeCAD document with |
The code will produce a new FreeCAD document with 12 shapes (Inner Ring, Outer Ring and 10 balls/spheres).<br /> |
||
It will look like this:<br /> |
It will look like this:<br /> |
||
<br /> |
|||
[[Image:Tutorial_BallBearing01.jpg|400px]] |
[[Image:Tutorial_BallBearing01.jpg|400px]] |
||
<!--T:3--> |
|||
===Workflow=== |
===Workflow=== <!--T:3--> |
||
The workflow is more or less identical how you would create the part in part workbench.<br /> |
The workflow is more or less identical how you would create the part in part workbench.<br /> |
||
Just some small differences<br /> |
Just some small differences.<br /> |
||
*Create a new empty document and make it the active document |
*Create a new empty document and make it the active document |
||
*Insert Cylinder |
*Insert Cylinder |
||
Line 36: | Line 37: | ||
*Zoom to fit all |
*Zoom to fit all |
||
<!--T:4--> |
|||
===Filleting edges=== |
===Filleting edges=== <!--T:4--> |
||
In part workbench using the GUI, you would select chamfers in the 3D view or in the menu for fillets and then apply the fillets.<br /> |
In part workbench using the GUI, you would select chamfers in the 3D view or in the menu for fillets and then apply the fillets.<br /> |
||
Here we select all edges of a shape and apply fillets.<br /> |
Here we select all edges of a shape and apply fillets.<br /> |
||
Therefore we need to select the edges BEFORE creating the groove. |
Therefore we need to select the edges BEFORE creating the groove. |
||
<!--T:5--> |
|||
===Notes=== |
===Notes=== <!--T:5--> |
||
This tutorial is using part primitives and boolean operations, which can be performance consuming.<br /> |
This tutorial is using part primitives and boolean operations, which can be performance consuming.<br /> |
||
For doing a scripted part with revolved sketches have a look at the tutorial [[Scripted Parts: Ball Bearing - Part 2]]. |
For doing a scripted part with revolved sketches have a look at the tutorial [[Scripted Parts: Ball Bearing - Part 2]]. |
||
<!--T:6--> |
|||
===Links=== |
===Links=== <!--T:6--> |
||
[[Scripted objects]]: The wiki page explaing the basics of scripting<br /> |
[[Scripted objects]]: The wiki page explaing the basics of scripting<br /> |
||
[[Topological data scripting]]: A tutorial for covering basics of scripting<br /> |
[[Topological data scripting]]: A tutorial for covering basics of scripting<br /> |
||
Line 54: | Line 55: | ||
[http://linuxforanengineer.blogspot.de/2013/08/free-cad-bearing-script.html Bearing Script 1]: Base for this tutorial, thanks to JMG ...<br /> |
[http://linuxforanengineer.blogspot.de/2013/08/free-cad-bearing-script.html Bearing Script 1]: Base for this tutorial, thanks to JMG ...<br /> |
||
<!--T:7--> |
|||
===Code=== |
===Code=== <!--T:7--> |
||
{{Code|code= |
{{Code|code= |
||
## Ball-bearing script |
## Ball-bearing script |
||
Line 61: | Line 62: | ||
## based on ball bearing script by JMG |
## based on ball bearing script by JMG |
||
## (http://linuxforanengineer.blogspot.de/2013/08/free-cad-bearing-script.html) |
## (http://linuxforanengineer.blogspot.de/2013/08/free-cad-bearing-script.html) |
||
# |
|||
#needed for inserting primitives |
#needed for inserting primitives |
||
import Part |
import Part |
||
Line 68: | Line 69: | ||
#needed for translation of torus |
#needed for translation of torus |
||
from FreeCAD import Base |
from FreeCAD import Base |
||
# |
|||
#VALUES# |
#VALUES# |
||
#(radius of shaft/inner radius of inner ring) |
#(radius of shaft/inner radius of inner ring) |
||
Line 90: | Line 91: | ||
#second coordinate of center of ball |
#second coordinate of center of ball |
||
PBall=TH/2 |
PBall=TH/2 |
||
# |
|||
#Create new document |
#Create new document |
||
App.newDocument("Unnamed") |
App.newDocument("Unnamed") |
||
Line 96: | Line 97: | ||
App.ActiveDocument=App.getDocument("Unnamed") |
App.ActiveDocument=App.getDocument("Unnamed") |
||
Gui.ActiveDocument=Gui.getDocument("Unnamed") |
Gui.ActiveDocument=Gui.getDocument("Unnamed") |
||
# |
|||
#Inner Ring# |
#Inner Ring# |
||
B1=Part.makeCylinder(R1,TH) |
B1=Part.makeCylinder(R1,TH) |
||
Line 109: | Line 110: | ||
InnerRing=IRF.cut(T1) |
InnerRing=IRF.cut(T1) |
||
Part.show(InnerRing) |
Part.show(InnerRing) |
||
# |
|||
#Outer Ring# |
#Outer Ring# |
||
B3=Part.makeCylinder(R3,TH) |
B3=Part.makeCylinder(R3,TH) |
||
Line 122: | Line 123: | ||
OuterRing=ORF.cut(T2) |
OuterRing=ORF.cut(T2) |
||
Part.show(OuterRing) |
Part.show(OuterRing) |
||
# |
|||
#Balls# |
#Balls# |
||
for i in range(NBall): |
for i in range(NBall): |
||
Line 130: | Line 131: | ||
Ball.translate(BV) |
Ball.translate(BV) |
||
Part.show(Ball) |
Part.show(Ball) |
||
# |
|||
#Make it pretty# |
#Make it pretty# |
||
App.activeDocument().recompute() |
App.activeDocument().recompute() |
||
Line 139: | Line 140: | ||
</translate> |
</translate> |
||
{{Powerdocnavi{{#translation:}}}} |
|||
[[Category:Developer Documentation{{#translation:}}]] |
|||
[[Category:Python Code{{#translation:}}]] |
|||
{{clear}} |
{{clear}} |
||
⚫ |
Latest revision as of 13:30, 22 August 2020
Tutorial |
Topic |
---|
Part Scripting - Ball Bearing #1 |
Level |
Beginner |
Time to complete |
30 min |
Authors |
r-frank |
FreeCAD version |
0.16.6706 |
Example files |
See also |
None |
Introduction
This tutorial is meant as a beginner's introduction to creating parts with python scripts within FreeCAD.
This tutorial will cover how to build a ball bearing with a CSG-workflow.
The code will produce a new FreeCAD document with 12 shapes (Inner Ring, Outer Ring and 10 balls/spheres).
It will look like this:
Workflow
The workflow is more or less identical how you would create the part in part workbench.
Just some small differences.
- Create a new empty document and make it the active document
- Insert Cylinder
- Insert Cylinder
- Do boolean cut to get basic shape of inner ring
- Select all edges and apply a fillet
- Insert torus
- Move torus into position and do a boolean cut to create the groove for the balls
- Repeat all steps for getting the shape for the outer ring
- Insert first ball
- Insert other balls using math to calculate the position of the balls
- Set view to axometric
- Zoom to fit all
Filleting edges
In part workbench using the GUI, you would select chamfers in the 3D view or in the menu for fillets and then apply the fillets.
Here we select all edges of a shape and apply fillets.
Therefore we need to select the edges BEFORE creating the groove.
Notes
This tutorial is using part primitives and boolean operations, which can be performance consuming.
For doing a scripted part with revolved sketches have a look at the tutorial Scripted Parts: Ball Bearing - Part 2.
Links
Scripted objects: The wiki page explaing the basics of scripting
Topological data scripting: A tutorial for covering basics of scripting
Scripted Parts: Ball Bearing - Part 2: Doing it with sketches
Bearing Script 1: Base for this tutorial, thanks to JMG ...
Code
## Ball-bearing script
## 11.08.2016 by r-frank (BPLRFE/LearnFreeCAD on Youtube)
## based on ball bearing script by JMG
## (http://linuxforanengineer.blogspot.de/2013/08/free-cad-bearing-script.html)
#
#needed for inserting primitives
import Part
#needed for calculating the positions of the balls
import math
#needed for translation of torus
from FreeCAD import Base
#
#VALUES#
#(radius of shaft/inner radius of inner ring)
R1=15.0
#(outer radius of inner ring)
R2=25.0
#(inner radius of outer ring)
R3=30.0
#(outer radius of outer ring)
R4=40.0
#(thickness of bearing)
TH=15.0
#(number of balls)
NBall=10
#(radius of ball)
RBall=5.0
#(rounding radius for fillets)
RR=1
#first coordinate of center of ball
CBall=((R3-R2)/2)+R2
#second coordinate of center of ball
PBall=TH/2
#
#Create new document
App.newDocument("Unnamed")
App.setActiveDocument("Unnamed")
App.ActiveDocument=App.getDocument("Unnamed")
Gui.ActiveDocument=Gui.getDocument("Unnamed")
#
#Inner Ring#
B1=Part.makeCylinder(R1,TH)
B2=Part.makeCylinder(R2,TH)
IR=B2.cut(B1)
#get edges and apply fillets
Bedges=IR.Edges
IRF=IR.makeFillet(RR,Bedges)
#create groove and show shape
T1=Part.makeTorus(CBall,RBall)
T1.translate(Base.Vector(0,0,TH/2))
InnerRing=IRF.cut(T1)
Part.show(InnerRing)
#
#Outer Ring#
B3=Part.makeCylinder(R3,TH)
B4=Part.makeCylinder(R4,TH)
OR=B4.cut(B3)
#get edges and apply fillets
Bedges=OR.Edges
ORF=OR.makeFillet(RR,Bedges)
#create groove and show shape
T2=Part.makeTorus(CBall,RBall)
T2.translate(Base.Vector(0,0,TH/2))
OuterRing=ORF.cut(T2)
Part.show(OuterRing)
#
#Balls#
for i in range(NBall):
Ball=Part.makeSphere(RBall)
Alpha=(i*2*math.pi)/NBall
BV=(CBall*math.cos(Alpha),CBall*math.sin(Alpha),TH/2)
Ball.translate(BV)
Part.show(Ball)
#
#Make it pretty#
App.activeDocument().recompute()
Gui.activeDocument().activeView().viewAxometric()
Gui.SendMsgToActiveView("ViewFit")
- FreeCAD scripting: Python, Introduction to Python, Python scripting tutorial, FreeCAD Scripting Basics
- Modules: Builtin modules, Units, Quantity
- Workbenches: Workbench creation, Gui Commands, Commands, Installing more workbenches
- Meshes and Parts: Mesh Scripting, Topological data scripting, Mesh to Part, PythonOCC
- Parametric objects: Scripted objects, Viewproviders (Custom icon in tree view)
- Scenegraph: Coin (Inventor) scenegraph, Pivy
- Graphical interface: Interface creation, Interface creation completely in Python (1, 2, 3, 4, 5), PySide, PySide examples beginner, intermediate, advanced
- Macros: Macros, How to install macros
- Embedding: Embedding FreeCAD, Embedding FreeCADGui
- Other: Expressions, Code snippets, Line drawing function, FreeCAD vector math library (deprecated)
- Hubs: User hub, Power users hub, Developer hub