前言

其实 SolidWorks 软件本身就是根据 SolidWorks API 的基础上开发而成的,其 API 主要可以通过 VBA、C++ 和 C# 进行调用。
一般情况下,SolidWorks 自带 VBA 编译器,所以使用 VBA 进行开发是最容易的。但如果追求更高阶和复杂的二次开发,使用 C# 则较为轻松。

SolidWorks启动界面
SolidWorks 启动界面

“宏” 在哪里

打开 SolidWorks 后,点击图示位置。

SolidWorks主界面
SolidWorks 主界面

注:实际使用中如果找不到所需的函数方法,可以用录制宏后编辑看看。

思路

思路
思路

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Dim swApp As Object
Dim swModel As SldWorks.AssemblyDoc
Dim designer As String
Dim valout As String
Dim longstatus As Long, longwarnings As Long
Dim swAssy As SldWorks.AssemblyDoc

Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

'遍历装配体
Dim swAssy As Variant
swAssy = swModel.GetComponents(True)
tmp = GetAsmChild(swAssy)

swModel.Save

End Sub

Function GetAsmChild(swmodelComps As Variant)
Dim swmodelComp As SldWorks.Component2
Dim swmodel As ModelDoc2
Dim swAsm As SldWorks.AssemblyDoc
Dim text As String
Set myd = CreateObject("Scripting.Dictionary")

'遍历装配体所有东西
For i = 0 To UBound(swmodelComps)
Set swmodelComp = swmodelComps(i)
Set swmodel = swmodelComp.GetModelDoc2

'跳过重复零件
If myd.Exists(swmodel.GetTitle) Then
GoTo Continue
Else
myd.Add swmodel.GetTitle, i
End If

'递归改名
If swmodel.GetType = 1 Then
'do something here
ElseIf swmodel.GetType = 2 Then
swAsm = swmodel
t = GetAsmChild(swAsm.GetComponents(True), designer, no)
End If
Continue:
Next

m = MsgBox(text, 64, Title)

End Function


网站地图 | 状态监测 | 图片加密&解密 | File Server | 博友圈 | 博客说
Copyright 2022-2025 | Powered by Hexo 7.3.0 & Stellar 1.29.1
总访问量次 |