简介:CATIA二次开发VBA入门——一些代码合集
本篇博客文章分享一些CATIA vba基础相关的代码,包括定义工作对象,文档结构树操作,判断某个对象是否存在,图层操作,密码输入,工作模块切换等内容,希望对你有帮助~
激活第一个窗口,切换窗口
Attribute VB_Name = "Module1"
Sub dd()
CATIA.Windows.Item(1).Activate
End Sub进行截图
Sub dd()
Dim MyViewer As Viewer3D
MsgBox CATIA.ActiveWindow.Name
Set MyViewer = CATIA.ActiveWindow.ActiveViewer
MyViewer.CaptureToFile catCaptureFormatBMP, "H:\MyImage.bmp"
End Sub添加零件
Sub dd()
Set documents1 = CATIA.Documents
Set odoc = documents1.Add("Product")
End Sub隐藏罗盘

StartCommand后面可以跟很多方法
Sub jk()
 CATIA.StartCommand "CompassDisplayOff"
 CATIA.StartCommand "CompassDisplayOn"
End Sub罗盘复位

只显示零件


可以用在截图的时候
Sub CATMain()
    Dim MyWindow As SpecsAndGeomWindow
    Dim MyViewer As Viewer3D
    Set MyWindow = CATIA.ActiveWindow
   
   MsgBox "See how it looks a Window in CATIA if you write in a line  MyWindow.Layout = catWindowGeomOnly  (only geometry)"
   MyWindow.Layout = catWindowGeomOnly
   
     MsgBox "See how it looks a Window in CATIA if you write in a line MyWindow.Layout = catWindowSpecsOnly  (only specification tree)"
     MyWindow.Layout = catWindowSpecsOnly
    
       MsgBox "See how it looks a Window in CATIA if you write in a line  MyWindow.Layout = catWindowSpecsAndGeom  (geometry and spec tree)"
       MyWindow.Layout = catWindowSpecsAndGeom
End SubSub CATMain()
Dim dd As Window
Set dd = CATIA.ActiveWindow
dd.Layout = 1
    
End SubSub dd()
Set document1 = CATIA.ActiveDocument
Set opart = document1.Part
Set obodies = opart.Bodies
Set obody = obodies.Item(obodies.Count)
Dim selection1 As Selection
Set selection1 = document1.Selection
'selection1.Add obody
Set pad1 = opart.FindObjectByName("Pad.1")
Set pad11 = opart.FindObjectByName("Sketch.1")
selection1.Add pad1
opart.InWorkObject = pad1
End Sub



Sub CATMain()
Dim FileToRead As String
FileToRead = "H:\test\Part1.CATPart"
 Dim Doc As Document
 Set Doc = CATIA.Documents.NewFrom(FileToRead)
End Sub

Public n As Integer
Private Sub Command1_Click()
bj = Val(TextBox1.Text)
If bj = "123456" Then
Unload Me
MsgBox "恭喜你 登录成功"
Else
MsgBox "您输入的密码错误,输入错误次数超过三次将退出CATIA"
n = n + 1
If n = 3 Then
CATIA.Quit
MsgBox "哈哈  大傻瓜"
End If
End If
End Sub




CATIA.Application.DisplayFileAlerts = False

Sub dd()
ActDocType = TypeName(CATIA.ActiveDocument)
MsgBox CATIA.GetWorkbenchId
CATIA.StartWorkbench "PrtCfg"
End Sub

绘图区域切换到不可视区域


Sub CATMain()
Set Doc = CATIA.ActiveDocument
    
Doc.CurrentLayer = "General"
jkj = Doc.ReadOnly
Doc.SeeHiddenElements = True
    
End Sub给某个对象更新
 CATIA.ActiveDocument.part.UpdateObject TestInt





Sub CATMain()
Dim productDocument1 As Document
Set productDocument1 = CATIA.ActiveDocument
productDocument1.ExportData "C:\temp\XXX.txt", "txt"
End Sub

Attribute VB_Name = "Module2"
   Sub CATMain()
     Exe = MsgBox("This CATScript will close all files in the session without saving them" & Chr(10) & "Would you like to continue?", vbOKCancel, "Kill'em All")
     If Exe = vbOK Then
        AllDocs = CATIA.Documents.Count ' Extraemos cuantos documentos hay abiertos (para mostrarlo al final, queda elegante)
        If AllDocs > 0 Then ' Si hay al menos uno abierto, vamos al bucle.
           For Each CATIA_Document In CATIA.Documents ' Por cada documento abierto...
               CATIA_Document.Close 
           Next
        End If
        MsgBox AllDocs & " Documents Closed" ' Mensaje de salida mostrando cuantos documentos se han cerrado.
     End If
   End Sub
Attribute VB_Name = "Module9"
Sub jk()
catia.StartCommand ("SpecificationsLevel1") '————展开第一层
catia.StartCommand ("SpecificationsLevel2") '————展开第二层
catia.StartCommand ("SpecificationsLevel3") '————展开第三层
catia.StartCommand ("SpecificationsLevelAll") '————展开所有层
End Sub



Function HybridShapeExists(InputStr As String, curset As HybridBody) As Boolean
On Error GoTo blast
Set HHH = curset.HybridShapes.Item(InputStr)
HybridShapeExists = True
Exit Function
blast:
HybridShapeExists = False
End Function
Sub f()
Set opartdoc = CATIA.ActiveDocument
Dim ohybridbody As HybridBody
Set ohybridbody = opartdoc.Part.HybridBodies.Item(1)
MsgBox HybridShapeExists("Point.3", ohybridbody)
End Sub
Sub js()
CATIA.StartCommand "复制"
CATIA.StartCommand "粘贴"
End Sub

Sub CATMain()
    Dim specsAndGeomWindow1 As SpecsAndGeomWindow
    Dim viewer3D1 As Viewer3D
    Dim viewpoint3D1 As Viewpoint3D
    Set specsAndGeomWindow1 = CATIA.ActiveWindow
    Set viewer3D1 = specsAndGeomWindow1.ActiveViewer
    Set viewpoint3D1 = viewer3D1.Viewpoint3D
    If viewpoint3D1.ProjectionMode = 1 Then
    
        viewpoint3D1.ProjectionMode = 0
    ElseIf viewpoint3D1.ProjectionMode = 0 Then
        viewpoint3D1.ProjectionMode = 1
    End If
End Sub对象的parent

Sub CATMain()
Dim oSel
Set oSel = CATIA.ActiveDocument.selection
Dim oSelElem
Set oSelElem = oSel.Item(1).Value
Dim oGS
Set oGS = oSelElem.Parent.Parent
MsgBox "oGS .Name =" & oGS.Name
End Sub后台添加按钮

CATIA二次开发VBA入门——一些代码合集
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。