重建

官方文档 EditRebuild3 Method (IModelDoc2) - 2020 - SOLIDWORKS API Help

1
2
3
4
5
6
7
8
Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2

Set swApp = Application.SldWorks
swModel.EditRebuild3

保存

官方文档 EditRebuild3 Method (IModelDoc2) - 2020 - SOLIDWORKS API Help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim bool As Boolean '是否保存成功
Dim Options As Integer '保存选项,一般为1
Dim Errors As Long '报错代码
Dim Warnings As Long '警告代码

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

bool = swModel.Save3(Options, Errors, Warnings)

另存为

官方文档 SaveAs3 Method (IModelDocExtension) - 2020 - SOLIDWORKS API Help,可参考【SW VB 二次开发】【API 秘籍】SaveAs3 - 另存为(另存为副本并继续 / 打开) - 哔哩哔哩,这里就不赘述了。

关闭文件

可参考【SW VB 二次开发】【API 秘籍】CloseDoc - 关闭文件 - 哔哩哔哩SolidWorks 最高阶层 API 物件:SldWorks (2),这里就不赘述了。

获取属性

官方文档 Get4 Method (ICustomPropertyManager) - 2020 - SOLIDWORKS API Help,可以结合 ICustomPropertyManager Interface_int lretval = cuspropmgr.getall3 (ref vpropnames, r-CSDN 博客来看,虽然是 C#,但差不多也能知道 VBA 的代码。获取的值推荐用 ResolvedValOut

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As ModelDocExtension
Dim swCustProp As CustomPropertyManager
Dim FieldName As String '属性名
Dim UseCached As Boolean '是否用缓存,一般为False
Dim ValOut As String '属性值,如果为公式,则是公式
Dim ResolvedValOut As String '属性值,如果为公式,则是公式计算出的值
Dim bool As Boolean '该方法返回是否成功

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swModelDocExt = swModel.Extension

'为空表示自定义属性,其他则是对应的配置名下的属性
Set swCustProp = swModelDocExt.CustomPropertyManager("")

bool = swCustProp.Get4(FieldName, UseCached, ValOut, ResolvedValOut)

获取 / 设定压缩

包含轻化状态,官方文档 SetSuppression2 Method (IComponent2) - 2021 - SOLIDWORKS API Help,可参考 IComponent2 Interface 学习_swapp.activedoc-CSDN 博客。下面省略了获取子组件的代码,可见 SolidWorks 新・遍历装配体(VBA) - Malvern’s Blog

1
2
3
4
5
6
7
8
9
10
11
12
13
Option Explicit

Dim swChildComp As SldWorks.Component2 '子组件

'获取组件状态
Dim value As System.Integer '返回值,压缩0,轻化1,还原2
value = swChildComp.GetSuppression

'设定组件状态
Dim State As Integer '状态值,压缩0,轻化1,还原2
Dim Errors As Integer '报错代码
Errors = swChildComp.SetSuppression2(State)

打开文件

官方文档 OpenDoc6 Method (ISldWorks) - 2017 - SOLIDWORKS API HelpActivateDoc2 Method (ISldWorks) - 2020 - SOLIDWORKS API Help,可以参考【SW VB 二次开发】【API 秘籍】OpenDoc6 - 打开文件 - 哔哩哔哩

1
2
3
4
5
6
7
8
9
10
11
12
13
Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2 '返回的对象
Dim FieldName As String '文件地址
Dim Type As Integer '文件类型,零件为1,装配体为2,工程图为3
Dim Options As Integer '一般为0
Dim Configuration As String '配置名,无特殊要求为""
Dim Errors As Integer '报错代码
Dim Warnings As Integer '警告代码

Set swModel = swApp.OpenDoc6(FileName, Type, Options, Configuration, Errors, Warnings)

这里需要注意警告代码,因为如果一个文件已经在内存中存在了,那么就无法打开它了(SolidWorks 底层同名文件限制)。此时 Warnings 会返回 128,但是再注意,如果一个文件既是已打开状态,又是只读文件,那么此时返回的是 130。因为只读返回 2,已打开返回 128,根据 SolidWorks API Bit 掩码进位的规则,返回值为所有状态之和 130。所以判断文件是否已打开,最好不要使用 Warnings=128,而是用十六进制判断倒数第二位是否为 8。

如果一个文件处于已打开状态,那么要想 “打开” 它,需要改用以下 ActivateDoc2 方法。

1
2
3
4
5
6
7
8
9
10
Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2 '返回的对象
Dim Name As String '文件名,建议加上后缀名以区分工程图
Dim Silent As Boolean '是否静默打开,一般为False
Dim Errors As Integer '报错代码

Set swModel = swApp.ActivateDoc2(Name, Silent, Errors)

弹窗

官方文档 SendMsgToUser Method (ISldWorks) - 2020 - SOLIDWORKS API HelpSendMsgToUser2 Method (ISldWorks) - 2020 - SOLIDWORKS API Help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Option Explicit

Dim swApp As SldWorks.SldWorks
Dim Message As String

'普通版,Message为消息内容
swApp.SendMsgToUse Message

'进阶版,能显示按钮并有返回值
Dim Icon As Integer '图标
Dim Buttons As Integer '按钮
Dim value As Integer '返回值

value = swApp.SendMsgToUser2(Message, Icon, Buttons)

新建 / 删除 / 激活配置

官方文档 AddConfiguration2 Method (IConfigurationManager) - 2020 - SOLIDWORKS API Help

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
Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim ConfigMgr As ConfigurationManager

Dim Name As String '配置名
Dim Comment As String '评论,一般为""
Dim AlternateName As String '替代名,一般为""
Dim Options As Integer '选项,一般为False
Dim ParentConfigName As String '父配置名,一般为""
Dim Description As String '描述,一般为""
Dim Rebuild As Boolean '是否重建,建议为True
Dim swDerivConf As SldWorks.Configuration '返回的对象

Set ConfigMgr = swModel.ConfigurationManager
'新建配置
Set swDerivConf = ConfigMgr.AddConfiguration2(Name, Comment, AlternateName, Options, ParentConfigName, Description, Rebuild)

Dim bool As Boolean '返回值
'激活指定配置
bool = instance.ShowConfiguration2(Name)
'删除指定配置
bool = instance.SDeleteConfiguration2(Name)


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