python secondary development Solidworks: gear generator

Table of Contents

1. Parameters

2. Manual modeling

2.1 Equation drives the curve to draw an involute

2.2 Draw the base circle, tooth root circle, index circle and tooth tip circle

2.3 Draw a single tooth profile

2.4 Extrude the base circle sketch based on the tooth thickness

2.5 Extrude the single tooth profile sketch by tooth thickness

2.6 array teeth, several single teeth

3. Python automated modeling

4. Summary


1, parameter

Modulus “m” = 2

Number of teeth “z” = 50

Pressure angle “a”= 20

Diameter of graduation circle “d”= “m” * “z”

Tip circle diameter “da”= ( “z” + 2 ) * “m”

Root circle diameter “df”= ( “z” – 2.5 ) * “m”

Base circle diameter “db”= “m” * “z” * cos ( “a” )

Base circle radius “rb”= “db” / 2

Indexing scallop thickness “s”= pi * “m” / 2

Tooth thickness “B”= 0.3 * “d”

Root fillet “r”= 0.38 * “m”

2. Manual modeling

2.1 Equation drives the curve to draw an involute line

Construct sketch 1 on the top datum plane and draw the involute tooth profile

xt=”rb”*cos(t) + “rb”*t*sin(t)

yt=”rb”*sin(t)-“rb”*t*cos(t)

t1=0

t2=pi/4

2.2 Draw base circle, tooth root circle, indexing circle and tooth addendum circle

Construct sketch 2 on the top datum plane, and use construction lines to draw the base circle, tooth root circle, index circle and tooth top circle.

2.3 Draw a single tooth profile

Construct sketch 3 on the top datum plane, convert the entity references to the involute line of sketch 1, convert the entity references to the base circle, tooth root circle, index circle and tooth tip circle of sketch 2, and draw the tooth width center construction line for constraints , then cut off part of the involute outside the tooth top circle, then fillet the tooth root circle and involute, and finally use the tooth width center construction line as the symmetry axis to symmetry the other side and draw the tooth bottom circle.

2.4 Extrude base circle sketch by tooth thickness

Construct sketch 4 on the top datum plane, draw a circle with the size of the tooth root circle, exit the sketch after constraint, and stretch the boss with the tooth thickness as the size.

2.5 Extrude single tooth profile sketch by tooth thickness

Select sketch 3 and extrude the boss using the tooth thickness as the dimension

2.6 array teeth per single tooth

3. python automated modeling

#Import the win32com.client library and abbreviate it to win32 for subsequent use
import win32com.client as win32
import pythoncom
import numpy as np
m=2/1000
z=50
a=20*np.pi/180
d=m*z
da= (z + 2)* m
df= (z - 2.5) * m
db= m*z* np.cos(a)
rb=db/2
s= np.pi * m / 2
B= 0.3*d
r=0.38*m
#Use the Dispatch function to create an instance of the SolidWorks application. Here, "sldworks.application" is the COM object identifier of the SolidWorks application
swApp = win32.Dispatch("sldworks.application")
#Make the SolidWorks application visible.
swApp.Visible=True
Nothing = win32.VARIANT(pythoncom.VT_DISPATCH, None)
swSheetWidth = 0
swSheetHeight = 0
Part = swApp.NewDocument(r"C:\ProgramData\SolidWorks\SOLIDWORKS 2018\templates\gb_part.prtdot", 0, swSheetWidth, swSheetHeight)
#sketch1 spline
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("top plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.SketchManager.InsertSketch(True)
Part.ClearSelection2(True)
equationDriveCurve = Part.SketchManager.CreateEquationSpline2(f"{1000*rb}*cos(t) + {1000*rb}*t*sin(t)", f"{1000*rb}*sin(t)-{1000 *rb}*t*cos(t)", "", "0", "pi/4", False, 0, 0, 0, True, True)
Part.SketchManager.InsertSketch(True)
#sketch2 4circle
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("top plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.SketchManager.InsertSketch(True)
skSegment = Part.SketchManager.CreateCircleByRadius(0, 0, 0, 10*db)
skSegment = Part.SketchManager.CreateCircleByRadius(0, 0, 0, 10*df)
skSegment = Part.SketchManager.CreateCircleByRadius(0, 0, 0, 10*d)
skSegment = Part.SketchManager.CreateCircleByRadius(0, 0, 0, 10*da)
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", -4.17913430963818E-02, 4.06570904526804E-03, 0, False, 0, Nothing, 0)
myDisplayDim = Part.AddDimension2(-0.129688100551224, 0, 4.06570904526804E-03)
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Arc2", "SKETCHSEGMENT", -7.00577012206262E-02, 5.61455058632253E-03, 0, False, 0, Nothing, 0)
myDisplayDim = Part.AddDimension2(-0.145176515961769, 0, 5.2273402010589E-03)
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Arc3", "SKETCHSEGMENT", -8.74821685574891E-02, 4.84012981579529E-03, 0, False, 0, Nothing, 0)
myDisplayDim = Part.AddDimension2(-0.160664931372314, 0, 9.09944405369512E-03)
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Arc4", "SKETCHSEGMENT", -0.108778739746988, 1.35523634842269E-03, 0, False, 0, Nothing, 0)
myDisplayDim = Part.AddDimension2(-0.183123133717604, 0, 2.12965711894994E-03)
Part.ClearSelection2(True)
myDimension = Part.Parameter("D1@sketch2")
myDimension.SystemValue = db
myDimension = Part.Parameter("D3@sketch2")
myDimension.SystemValue = df
myDimension = Part.Parameter("D2@sketch2")
myDimension.SystemValue = d
myDimension = Part.Parameter("D4@sketch2")
myDimension.SystemValue = da
Part.SketchManager.InsertSketch(True)
#sketch3
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("top plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.SketchManager.InsertSketch(True)
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Spline2@sketch1", "EXTSKETCHSEGMENT", 5.68109938451896E-02, 4.85306098373878E-03, 0, False, 0, Nothing, 0)
boolstatus = Part.SketchManager.SketchUseEdge3(False, False)
Part.ClearSelection2(True)
skSegment = Part.SketchManager.CreateCenterLine(0, 0, 0, 0.076976, 0.012754, 0)
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Arc2@sketch2", "EXTSKETCHSEGMENT", 4.98812101498907E-02, -3.44454263762836E-03, 0, False, 0, Nothing, 0)
boolstatus = Part.SketchManager.SketchUseEdge3(False, False)
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Arc2", "SKETCHSEGMENT", 4.96566221826677E-02, 0, 5.84977550063237E-03, False, 2, Nothing, 0)
boolstatus = Part.SketchManager.SketchTrim(4, 0, 0, 0)
boolstatus = Part.Extension.SelectByID2("Point9", "SKETCHPOINT", 4.93275253639167E-02, 8.17283558333082E-03, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Point10", "SKETCHPOINT", 4.99944465783859E-02, 7.45192136917484E-04, 0, True, 0, Nothing, 0)
myDisplayDim = Part.AddDimension2(7.32063894288394E-02, 0, -2.05367474294104E-03)
Part.ClearSelection2(True)
myDimension = Part.Parameter("D1@sketch3")
myDimension.SystemValue = s/2
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Arc1@sketch2", "EXTSKETCHSEGMENT", 4.69732428651685E-02, -1.05103745408897E-03, 0, False, 0, Nothing, 0)
boolstatus = Part.SketchManager.SketchUseEdge3(False, False)
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Arc4", "SKETCHSEGMENT", 4.69719073506605E-02, 0, -1.09337721747326E-03, False, 2, Nothing, 0)
boolstatus = Part.SketchManager.SketchTrim(4, 0, 0, 0)
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Spline2", "SKETCHSEGMENT", 4.79822046708633E-02, 3.17508241647045E-03, -6.01947737600295E-05, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Arc4", "SKETCHSEGMENT", 4.68002780288081E-02, 3.17508241647046E-03, 8.7549715120034E-04, True, 0, Nothing, 0)
skSegment = Part.SketchManager.CreateFillet(r, 1)
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Arc4@sketch2", "EXTSKETCHSEGMENT", 5.19985313273173E-02, -3.90819398187038E-04, 0, False, 0, Nothing, 0)
boolstatus = Part.SketchManager.SketchUseEdge3(False, False)
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Arc7", "SKETCHSEGMENT", 5.19429524769286E-02, 0, 2.43509506581086E-03, False, 2, Nothing, 0)
boolstatus = Part.SketchManager.SketchTrim(4, 0, 0, 0)
boolstatus = Part.Extension.SelectByID2("Spline2", "SKETCHSEGMENT", 5.65326491634615E-02, 0, -4.6272000856053E-03, False, 2, Nothing, 0)
boolstatus = Part.SketchManager.SketchTrim(4, 0, 0, 0)
boolstatus = Part.Extension.SelectByID2("Spline2", "SKETCHSEGMENT", 5.27616245715102E-02, 0, -2.05666338249153E-03, False, 2, Nothing, 0)
boolstatus = Part.SketchManager.SketchTrim(4, 0, 0, 0)
boolstatus = Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 5.57072998941891E-02, 0.011, -2.62243109097623E-03, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Point22", "SKETCHPOINT", 5.19442248953657E-02, 2.40779982965117E-03, 0, True, 0, Nothing, 0)
Part.SketchAddConstraints("sgFIXED")
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Arc5", "SKETCHSEGMENT", 4.70597163100455E-02, 0.011, 3.38711166624456E-04, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Spline2", "SKETCHSEGMENT", 4.86320042344352E-02, 0.011, -3.16408801871271E-04, True, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Arc7", "SKETCHSEGMENT", 5.20648328693528E-02, 0.011, -1.83628712878136E-03, True, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 4.81079082596386E-02, 0.011, -2.25556390861863E-03, True, 0, Nothing, 0)
Part.SketchMirror()
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Arc4", "SKETCHSEGMENT", 4.66277826249411E-02, 0, -5.7797440583439E-03, False, 2, Nothing, 0)
boolstatus = Part.SketchManager.SketchTrim(4, 0, 0, 0)
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Arc4", "SKETCHSEGMENT", 4.68500779201268E-02, 0.011, -3.43477985191093E-03, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 4.87106186306547E-02, 0.011, -2.30797350609828E-03, True, 0, Nothing, 0)
Part.SketchMirror()
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Arc2", "SKETCHSEGMENT", 4.99783574598799E-02, 0, -1.47098117339316E-03, False, 2, Nothing, 0)
boolstatus = Part.SketchManager.SketchTrim(4, 0, 0, 0)
Part.SketchManager.InsertSketch(True)
#sketch4
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("top plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.SketchManager.InsertSketch(True)
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Arc1@sketch2", "EXTSKETCHSEGMENT", 4.69092540638955E-02, -2.66685360845074E-03, 0, False, 0, Nothing, 0)
boolstatus = Part.SketchManager.SketchUseEdge3(False, False)
Part.ClearSelection2(True)
Part.SketchManager.InsertSketch(True)

boolstatus = Part.Extension.SelectByID2("Sketch 4", "SKETCH", 0, 0, 0, False, 4, Nothing, 0)
myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, False, 0, 0,B, 0.01, False, False, False, False, 1.74532925199433E-02, 1.74532925199433E-02, False, False, False, False, True, True, True, 0, 0, False)
Part.SelectionManager.EnableContourSelection = False
Part.ClearSelection2(True)

boolstatus = Part.Extension.SelectByID2("Sketch3", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, False, 0, 0, B, 0.01, False, False, False, False, 1.74532925199433E-02, 1.74532925199433E-02, False, False, False, False, True, True, True, 0, 0, False)
Part.SelectionManager.EnableContourSelection = False
Part.ClearSelection2(True)
boolstatus = Part.Extension.SelectByID2("Boss-Extension 2", "BODYFEATURE", 0, 0, 0, False, 4, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("", "FACE", 1.00069959505049E-02, 2.39622182303378E-02, 4.59065963226521E-02, True, 1, Nothing, 0)
myFeature = Part.FeatureManager.FeatureCircularPattern4(z, 6.2831853071796, False, "NULL", False, True, False)
Part.ClearSelection2(True)
Part.ShowNamedView2("*Upper and lower isometric", 8)
Part.ViewZoomtofit2()

4. Summary

Automated modeling only requires two parameters, namely the module m and the number of teeth z. However, changing these two parameters cannot be successful because some APIs are affected by size and fail. This problem needs to be solved to achieve arbitrary parameter changes.