首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >CATIA二次开发VBA入门——一些代码合集

CATIA二次开发VBA入门——一些代码合集

原创
作者头像
Arya
发布2025-01-21 23:05:45
发布2025-01-21 23:05:45
8140
举报
文章被收录于专栏:后端开发专栏后端开发专栏

引出

简介:CATIA二次开发VBA入门——一些代码合集

本篇博客文章分享一些CATIA vba基础相关的代码,包括定义工作对象,文档结构树操作,判断某个对象是否存在,图层操作,密码输入,工作模块切换等内容,希望对你有帮助~


一些代码集合

激活第一个窗口,切换窗口

代码语言:java
复制
Attribute VB_Name = "Module1"
Sub dd()
CATIA.Windows.Item(1).Activate
End Sub

进行截图

代码语言:java
复制
Sub dd()
Dim MyViewer As Viewer3D
MsgBox CATIA.ActiveWindow.Name
Set MyViewer = CATIA.ActiveWindow.ActiveViewer
MyViewer.CaptureToFile catCaptureFormatBMP, "H:\MyImage.bmp"
End Sub

添加零件

代码语言:java
复制
Sub dd()
Set documents1 = CATIA.Documents
Set odoc = documents1.Add("Product")
End Sub
截图相关

隐藏罗盘

StartCommand后面可以跟很多方法

代码语言:java
复制
Sub jk()
 CATIA.StartCommand "CompassDisplayOff"
 CATIA.StartCommand "CompassDisplayOn"
End Sub

罗盘复位

只显示零件

可以用在截图的时候

代码语言:java
复制
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 Sub
代码语言:java
复制
Sub CATMain()
Dim dd As Window

Set dd = CATIA.ActiveWindow
dd.Layout = 1
    
End Sub
定义工作对象
代码语言:java
复制
Sub 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
新建自现有文件
代码语言:java
复制
Sub CATMain()
Dim FileToRead As String
FileToRead = "H:\test\Part1.CATPart"
 Dim Doc As Document
 Set Doc = CATIA.Documents.NewFrom(FileToRead)
End Sub
密码输入
代码语言:java
复制
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
文件类型
不提醒,直接保存
代码语言:java
复制
CATIA.Application.DisplayFileAlerts = False
WorkbenchId模块的切换,比如GSD曲面
代码语言:java
复制
Sub dd()
ActDocType = TypeName(CATIA.ActiveDocument)
MsgBox CATIA.GetWorkbenchId
CATIA.StartWorkbench "PrtCfg"
End Sub
图层操作等

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

代码语言:java
复制
Sub CATMain()
Set Doc = CATIA.ActiveDocument
    
Doc.CurrentLayer = "General"
jkj = Doc.ReadOnly
Doc.SeeHiddenElements = True
    
End Sub
update另外一种方式

给某个对象更新

代码语言:java
复制
 CATIA.ActiveDocument.part.UpdateObject TestInt
导出文档结构树
代码语言:java
复制
Sub CATMain()

Dim productDocument1 As Document
Set productDocument1 = CATIA.ActiveDocument

productDocument1.ExportData "C:\temp\XXX.txt", "txt"
End Sub
关闭当前所有文件
代码语言:java
复制
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
树操作的命令
代码语言:java
复制
Attribute VB_Name = "Module9"

Sub jk()
catia.StartCommand ("SpecificationsLevel1") '————展开第一层
catia.StartCommand ("SpecificationsLevel2") '————展开第二层
catia.StartCommand ("SpecificationsLevel3") '————展开第三层

catia.StartCommand ("SpecificationsLevelAll") '————展开所有层
End Sub
进行换行
几何图形集中是否存在某个对象
代码语言:java
复制
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
startcommand执行
代码语言:java
复制
Sub js()
CATIA.StartCommand "复制"
CATIA.StartCommand "粘贴"
End Sub
平行模式和透视模式
代码语言:java
复制
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

代码语言:java
复制
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 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 引出
    • 一些代码集合
  • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档