DeepSeek自动生成的脚本可以在CAD软件中自动建模,类似的功能可以在Solidworks中实现吗?
那么我们就一起来试试看。
1. DeepSeek生成一段脚本
2. 打开solidworks(本文以2024版本为例)
点击新增,保存swp宏文件后,弹出以下画面
3. 运行以下稍微复杂的建模代码
Dim swApp As Object
Dim swDoc As Object
Dim swSketchMgr As Object
Dim swFeatureMgr As Object
Sub CreateHexBolt()
' Get SolidWorks application object
Set swApp = Application.SldWorks
' Create a new part document
Set swDoc = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2022\templates\Part.prtdot", 0, 0, 0)
' Ensure the part is created successfully
If swDoc Is Nothing Then
MsgBox "Failed to create part. Please check the template path!", vbCritical, "Error"
Exit Sub
End If
' Get FeatureManager and SketchManager
Set swFeatureMgr = swDoc.FeatureManager
Set swSketchMgr = swDoc.SketchManager
' Create the hexagonal bolt head
' Select the Front Plane for the hexagon sketch
swDoc.ClearSelection2 True
Dim boolStatus As Boolean
boolStatus = swDoc.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
' Start a new sketch on the front plane
swSketchMgr.InsertSketch True
' Draw a hexagon (Center: 0,0 and radius: 0.02)
Dim angle As Double
angle = 60 ' Each side of hexagon is 60 degrees
Dim i As Integer
Dim x(5) As Double, y(5) As Double
For i = 0 To 5
x(i) = 0.02 * Cos(i * angle * 3.14159 / 180)
y(i) = 0.02 * Sin(i * angle * 3.14159 / 180)
Next i
' Create hexagon vertices
For i = 0 To 5
If i = 0 Then
swSketchMgr.CreateLine x(i), y(i), 0, x(5), y(5), 0
Else
swSketchMgr.CreateLine x(i), y(i), 0, x(i - 1), y(i - 1), 0
End If
Next i
' End the sketch for the hexagonal head
swSketchMgr.InsertSketch False
' Select the hexagon sketch for extrusion
swDoc.ClearSelection2 True
boolStatus = swDoc.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
' Extrude the hexagon to create the head of the bolt (10mm height)
Dim swFeat As Object
Set swFeat = swFeatureMgr.FeatureExtrusion2(True, False, False, 0, 0, 0.01, 0.0, False, False, False, False, 0, 0, False, False, False, False, True, True, True, 0, 0, False)
' Ensure the extrusion is successful
If swFeat Is Nothing Then
MsgBox "Extrusion failed!", vbCritical, "Error"
Exit Sub
End If
' Create the cylindrical shaft of the bolt
' Select the top face of the hexagonal head
swDoc.ClearSelection2 True
boolStatus = swDoc.Extension.SelectByID2("", "FACE", 0, 0, 0.01, False, 0, Nothing, 0)
' Start a new sketch for the cylindrical shaft
swSketchMgr.InsertSketch True
' Draw a circle for the shaft with diameter 10mm (radius 0.005m)
swSketchMgr.CreateCircle 0, 0, 0, 0.005, 0, 0 ' Center at origin, 10mm diameter (0.005m radius)
' End the sketch for the cylindrical shaft
swSketchMgr.InsertSketch False
' Select the circle sketch for extrusion
swDoc.ClearSelection2 True
boolStatus = swDoc.Extension.SelectByID2("Sketch2", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
' Extrude the shaft to a length of 30mm
Set swFeat = swFeatureMgr.FeatureExtrusion2(True, False, False, 0, 0, 0.03, 0.0, False, False, False, False, 0, 0, False, False, False, False, True, True, True, 0, 0, False)
' Ensure the extrusion is successful
If swFeat Is Nothing Then
MsgBox "Extrusion of the shaft failed!", vbCritical, "Error"
Exit Sub
End If
' Save the bolt part to the desktop
Dim docName As String
docName = swDoc.GetTitle()
Dim filePath As String
filePath = Environ("USERPROFILE") & "\Desktop\" & docName & ".SLDPRT"
swDoc.SaveAs filePath
' Notify completion
MsgBox "Hexagonal bolt created and saved to: " & filePath, vbInformation, "Completed"
' Release objects
Set swApp = Nothing
Set swDoc = Nothing
Set swSketchMgr = Nothing
Set swFeatureMgr = Nothing
End Sub
这段代码通过SolidWorks自动化编程创建了一个六角螺栓模型,它在工程中的意义不仅是简单的几何创建,还展示了自动化设计的优势。它节省了设计时间,提供了一个标准化且可调整的零件模型,有助于在机械设计和制造过程中提高效率。更多AI用法等你来发现。
节选自微信公众号 Momy MENG DS3DE