SolidWorks机械工程师网——最大的SolidWorks学习平台

标题: VB添加的自定义属性为什么没有类型 [打印本页]

作者: belibe    时间: 2021-11-10 14:18
标题: VB添加的自定义属性为什么没有类型
初学者,遇到个问题,需要大佬指点一下

1.png

2.png

作者: belibe    时间: 2021-11-10 14:19
因为没有类型,所以后面的评估值也没有,导致工程图里这些数值显示不出来
作者: 鲁东秀才    时间: 2021-11-10 14:42
AddCustomInfo3(“”, “名称”,30, "值"),这样试试?
作者: belibe    时间: 2021-11-10 15:02
鲁东秀才 发表于 2021-11-10 14:42
AddCustomInfo3(“”, “名称”,30, "值"),这样试试?

这个字段报错

3.png

作者: 鲁东秀才    时间: 2021-11-10 15:18
“值”就是需要你填写的图号

作者: belibe    时间: 2021-11-10 15:26
鲁东秀才 发表于 2021-11-10 15:18
“值”就是需要你填写的图号

我还没运行,写完这一行,就报错了,表示这个语法应该有问题。
作者: 鲁东秀才    时间: 2021-11-10 15:26
solidworks API 帮助例子
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swFeat As SldWorks.Feature
    Dim swCustPropMgr As SldWorks.CustomPropertyManager
    Dim nRetVal As Long
    Dim vNameArr As Variant
    Dim vName As Variant
    Dim bRet As Boolean   
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    Set swFeat = swSelMgr.GetSelectedObject6(1, 0)
    Set swCustPropMgr = swFeat.CustomPropertyManager
    Debug.Print "File = " & swModel.GetPathName
    Debug.Print "  " & swFeat.Name & " [" & swFeat.GetTypeName & "]"
    ' Add custom property
    bRet = swCustPropMgr.Add3("Date added", swCustomInfoType_e.swCustomInfoDate, "17-Apr-2005", swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)
    ' Get all custom properties
    vNameArr = swCustPropMgr.GetNames: If IsEmpty(vNameArr) Then Exit Sub
    For Each vName In vNameArr
        Debug.Print "    " & vName & " [" & swCustPropMgr.GetType(vName) & "] = " & swCustPropMgr.Get(vName)
    Next vName
End Sub
作者: 鲁东秀才    时间: 2021-11-10 15:28
我一般用c#,你去掉括号试试

作者: 鲁东秀才    时间: 2021-11-10 15:31
这是solidworks API 帮助,自定义属性可以参考这个

Add and Get Custom Properties Example (VBA)
This example shows how to add and get a custom property assigned to a weldment feature.

'--------------------------------------------------------------
' Preconditions:
' 1. Open public_documents\samples\tutorial\api\weldment_box3.sldprt.
' 2. Select the Weldment feature in the FeatureManager design
'    tree.
' 3. Open the Immediate window.
'
' Postconditions:
' 1. Adds a custom property named Date added to the selected
'    weldment feature.
' 2. To verify:
'    * Examine the Immediate window.
'    * Right-click the Weldment feature in the FeatureManager
'      design and click Properties.
'
' NOTE: Because the part is used elsewhere, do not save
' changes.
'-------------------------------------------------------------
Option Explicit
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swFeat As SldWorks.Feature
    Dim swCustPropMgr As SldWorks.CustomPropertyManager
    Dim nRetVal As Long
    Dim vNameArr As Variant
    Dim vName As Variant
    Dim bRet As Boolean   
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    Set swFeat = swSelMgr.GetSelectedObject6(1, 0)
    Set swCustPropMgr = swFeat.CustomPropertyManager
    Debug.Print "File = " & swModel.GetPathName
    Debug.Print "  " & swFeat.Name & " [" & swFeat.GetTypeName & "]"
    ' Add custom property
    bRet = swCustPropMgr.Add3("Date added", swCustomInfoType_e.swCustomInfoDate, "17-Apr-2005", swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)
    ' Get all custom properties
    vNameArr = swCustPropMgr.GetNames: If IsEmpty(vNameArr) Then Exit Sub
    For Each vName In vNameArr
        Debug.Print "    " & vName & " [" & swCustPropMgr.GetType(vName) & "] = " & swCustPropMgr.Get(vName)
    Next vName
End Sub
作者: belibe    时间: 2021-11-10 15:41
鲁东秀才 发表于 2021-11-10 15:28
我一般用c#,你去掉括号试试

去掉括号,可以用了,类型出现了,感谢大佬,
作者: qqfly332211    时间: 2021-11-10 23:06
活到老学到老!
作者: zp1220    时间: 2021-11-13 15:59
SolidWorks机械工程师网,顶一下。




欢迎光临 SolidWorks机械工程师网——最大的SolidWorks学习平台 (https://www.swbbsc.com/) Powered by Discuz! X3.2