设计背景:需要在零件和装配图中增加自定义属性每套数量。
利用正则表达式验证输入的数据是否为数字,如果不是数字弹出对话框要求用户输入数字。
如果不存在对应的自定义属性添加属性并赋值。
如果存在对应的自定义属性直接赋值。
代码来源:Inventor&Vault全国专业用户群 - LUCAS
Imports System.Text.RegularExpressions
doc=ThisDoc.Document
Dim summaryPropSet as PropertySet = doc.PropertySets.Item("Inventor User Defined Properties")
Dim PartQuantuty As Inventor.Property=Nothing
Try
PartQuantuty=summaryPropSet.Item("每套数量")
Catch ex As Exception
PartQuantuty=summaryPropSet.Add(1,"每套数量")
End Try
dim pattern as String = "^[1-9]\d*$"
Dim regex as New Regex(pattern)
Again:
Dim PropertyValue as String=InputBox("输入数量:","输入",PartQuantuty.Value)
If Not String.IsNullOrEmpty(PropertyValue) Then
If regex.IsMatch(PropertyValue) Then
PartQuantuty.Value=PropertyValue
Else
MsgBox("请输入一个整数",vbInformation + vbOKonly,"警告")
GoTo Again
End If
End If
网友评论