简介:CATIA二次开发VBA入门——语法、名词等的解释
本篇博客文章分享一些CATIA vba基础相关的代码,包括On Error错误控制,执行字符串代码,执行宏,CATIA选择文件并打开,轴系的设定选择,if语句的简写,参数可有可无等
采用On Error 包住可能有错误的代码,然后输出错误的number属性,就可以找出哪一行代码出的错
Sub errtest()
On Error Resume Next
Dim A As String
A = "STRING"
'MsgBox "我是中国人"
MsgBox Err.Number
End Sub
CAITA的函数,可以用来执行字符串定义的代码段
Sub JKJ()
Set AA = CATIA.ActiveDocument
Set serv = CATIA.SystemService
Dim params()
Dim codeToEvaluate
codeToEvaluate = "Sub CATMain()" & vbNewLine & _
"MsgBox " & Chr(34) & "Hello World" & Chr(34) & vbNewLine & _
"End Sub"
serv.Evaluate codeToEvaluate, 0, "CATMain", params
End Sub
执行宏文件,宏文件的函数为CATMain,通过CATIA.SystemService进行脚本的执行
Attribute VB_Name = "Module1"
Sub JKJ()
Set AA = CATIA.ActiveDocument
Set serv = CATIA.SystemService
Dim params()
serv.ExecuteScript "D:\dd", 1, "macro1.catvbs", "CATMain", params '这里的参数1是枚举类型 意思是目录类型
End Sub
CATIA自带的函数选择某个文件,并打开
Function BrowseForFile() '(FileTypeName As String) As String
FileTypeName = "CATPart"
FilePath = CATIA.FileSelectionBox("Select a text file", "*." & FileTypeName, CatFileSelectionModeOpen)
BrowseForFile = FilePath
End Function
Sub CATMain()
mon = Format(Now(), "m")
MsgBox mon
MyStr = Format(Time, "h:m:s") ' Returns "17:4:23".
MsgBox MyStr
MyStr = Format(Time, "hh:mm:ss AMPM") ' Returns "05:04:23 PM".
MsgBox MyStr
MyStr = Format(Date, "dddd, mmm d yyyy") ' Returns "Wednesday,
MsgBox MyStr
End Sub
CATIA多个窗口的时候,通过输入数字进行激活对应的窗口
caita装配体的操作
Sub CATMain()
MsgBox "Select what part you want to load. You can check with DESK command"
Set objSel = CATIA.ActiveDocument.selection
CATIA.StartCommand "Load"
objSel.Clear
End Sub
startcommand的使用,从多个角度显示零件
'Any command is accessible through CATScript command with following trick:
'- Command can be written to "powerinput" as c:Multi-View
'- All commands that can be written there, can be included to catvbs with following syntax:
'=> This toggles multi-view on and off through script, even when there is no actual VB-command for multi-view!
Sub CATMain()
CATIA.StartCommand ("Multi-View")
End Sub
窗体变量是全局的,可以保存下来
Private Function fact(n) As Double
If n > 0 Then
fact = n * fact(n - 1)
Else
fact = 1
End If
End Function
Sub SplitUpSendKeys()
InputStr = "AsString"
Dim WC As Integer
WC = 1
While WC < Len(InputStr)
Dim SubStr As String
SubStr = Mid(InputStr, WC, 1)
SendKeys SubStr, True
CATIA.RefreshDisplay = True
WC = WC + 1
Wend
Enum枚举类型
作为标识
关于CATIA的轴系,可以定义多个轴系,把轴系设置为当前的轴系
Sub mis()
Dim opart As Part
Set opartdoc = CATIA.ActiveDocument
Set opart = opartdoc.Part
Set axises = opart.AxisSystems
Set axisSystem1 = axises.Item(1)
axisSystem1.IsCurrent = True
End Sub
可选择参数,Optional
Object的方法有返回值的时候 方法后面需要加括号
当 我把view1 声明为drawingview时候,size命令出现错误
注释掉就ok
Sub jk()
Dim view1 'As DrawingView
Set view1 = CATIA.ActiveDocument.Sheets.ActiveSheet.Views.ActiveView
MsgBox view1.Name
Dim factory2d1 As Factory2D
Set factory2d1 = view1.Factory2D
Set texts1 = view1.Texts
Set gb = view1.GenerativeBehavior
Dim arr(3)
view1.Size arr
Dim xmin, xmax, ymin, ymax
xmin = arr(0) - view1.xAxisData
xmax = arr(1) - view1.xAxisData
ymin = arr(2) - view1.yAxisData
ymax = arr(3) - view1.yAxisData
End Sub
CATIA二次开发VBA入门——一些代码合集
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。