CATIA V5 Macros Programming
CATIA V5 Macros Programming
Emmett Ross
The following programs are the snippets of code I find myself using more than any others.
While each piece of code by itself may not be the final solution to your problem, they can
quickly and easily be placed into larger, more complicated programs. You can increase your
programming speed by reusing snippets of code youve already written that you know work.
Feel free to use these in any way that will benefit you.
http://www.vbpdf.website
Next
oSel.Clear
End Sub
9. Search by Name
Do you have an enormous specification tree with thousands of pieces of geometry? Do you
need to find one specific element within the tree? Rid yourself hours of endless scrolling by
searching for those components directly by name. The search is not case sensitive.
Sub CATMain()
Dim oSelection as Selection
Set oSelection = CATIA.ActiveDocument.Selection
Dim iCount
Dim geoName As String
geoName = Inputbox("Please enter EXACT name of geometry to
search for.")
oSelection.Search "Name=" & geoName & "*,all"
iCount = oSelection.Count
msgbox "Number of shapes found: "&icount
For i=1 to iCount
CATIA.StartCommand "Center Graph"
Next
End Sub
7. Screen capture
Automate the process of taking and saving images of your CATParts and CATProducts by using a
screen capture macro. The images can be saved to a file or inserted directly into PowerPoint.
This macro takes a screen shot with a white background and saves it in a folder.
Sub CATMain()
Dim ObjViewer3D As Viewer3D
Set objViewer3D = CATIA.ActiveWindow.ActiveViewer
Dim objCamera3D As Camera3D
Set objCamera3D = CATIA.ActiveDocument.Cameras.Item(1)
'Input box to name the screen capture image file
Dim partname As String
partName = Inputbox ("Please name the image.")
If partName="" Then
MsgBox "No name was entered. Operation aborted.", vbExclamation,
"Cancel"
Else
'turn off the spec tree
Dim objSpecWindow As SpecsAndGeomWindow
Set objSpecWindow = CATIA.ActiveWindow
objSpecWindow.Layout = catWindowGeomOnly
'Toggle Compass
CATIA.StartCommand("Compass")
'change background color to white
Dim DBLBackArray(2)
objViewer3D.GetBackgroundColor(dblBackArray)
Dim dblWhiteArray(2)
dblWhiteArray(0) = 1
dblWhiteArray(1) = 1
dblWhiteArray(2) = 1
objViewer3D.PutBackgroundColor(dblWhiteArray)
Option Explicit
Sub CATMain()
Dim oDrwDoc As DrawingDocument
Set oDrwDoc = CATIA.ActiveDocument
'memorize which View was active before the macro
Dim oViewerActive As DrawingView
Set oViewerActive =
oDrwDoc.DrawingRoot.ActiveSheet.Views.ActiveView
'get the background View
Dim oView As DrawingView
Set oView =
oDrwDoc.DrawingRoot.ActiveSheet.Views.Item("Background View")
oView.Activate
'create a Frame
Dim oFact As Factory2D
Set oFact = oView.Factory2D
Dim dW As Double
dW = oDrwDoc.DrawingRoot.ActiveSheet.GetPaperWidth
Dim dH As Double
dH = oDrwDoc.DrawingRoot.ActiveSheet.GetPaperHeight
Dim oLine As Line2D
draw the frame 10 units in from the edges of the paper
Set oLine = oFact.CreateLine(10#, 10#, dW - 10#, 10#)
Set oLine = oFact.CreateLine(dW - 10#, 10#, dW - 10#, dH 10#)
Set oLine = oFact.CreateLine(dW - 10#, dH - 10#, 10#, dH 10#)
Set oLine = oFact.CreateLine(10#, dH - 10#, 10#, 10#)
text objects are used to write notes on the drawing
Dim oNote As DrawingText
Set oNote = oView.Texts.Add(" ", 100#, 100#)
oNote.Text = "GENERAL NOTES - UNLESS OTHERWISE SPECIFIED"
oNote.Text = oNote.Text & vbCrLf & "1. Dimensions are mm"
oNote.Text = oNote.Text & vbCrLf & "2. Edges to be deburred"
oNote.AnchorPosition = catTopLeft
oNote.SetFontSize 0, 0, 3.5
oNote.WrappingWidth = 200#
Sub CatMain()
Dim fileSys
Set fileSys = CATIA.FileSystem
Dim FolderPath
FolderPath = InputBox( "Enter a folder path:", "Folder path to
convert the drawings" ,sDocPath & "P:\DrawingtoPDF")
Dim filefolder
Set filefolder = FileSys.GetFolder(FolderPath)
Dim i as Integer
'loop through all files in the folder
For i = 1 To filefolder.Files.Count
Dim IFile
Set IFile = filefolder.Files.Item(i)
'if the file is a CATDrawing, then open it in CATIA
If InStr(IFile.Name, ".CATDrawing") <> 0 Then
Dim Doc
Set Doc = CATIA.Documents.Open(IFile.Path)
Set partDocument1 = CATIA.ActiveDocument
Dim drawingName as String
drawingName =len(CATIA.ActiveDocument.Name)
pdfName =left( CATIA.ActiveDocument.Name,drawingName-11)
'msgbox partt
PartDocument1.ExportData FolderPath &"\"& pdfName, "pdf"
'close the open drawing document
CATIA.ActiveDocument.Close()
End IF
Next 'go to the next drawing in the folder
End Sub
Sub CATMain()
'Get the current CATIA assembly
Dim oProdDoc As ProductDocument
Set oProdDoc = CATIA.ActiveDocument
Dim oRootProd As Product
Set oRootProd = oProdDoc.Product
'Begin scroll down the specification tree
Call WalkDownTree(oRootProd)
End Sub
'--------------------------------------------------------------' WalkDownTree is a recursive function to scroll down the spec
tree and output names of each item
Sub WalkDownTree(oInProduct As Product)
Dim oInstances As Products
Set oInstances = oInProduct.Products
'-----No instances found then this is CATPart
If oInstances.Count = 0 Then
Msgbox "This is a CATPart with part number " &
oInProduct.PartNumber
Exit Sub
End If
'-----Found an instance therefore it is a CATProduct
Msgbox "This is a CATProduct with part number " &
oInProduct.ReferenceProduct.PartNumber
Dim k As Integer
For k = 1 To oInstances.Count
Dim oInst As Product
Set oInst = oInstances.Item(k)
'apply design mode
oInstances.Item(k).ApplyWorkMode DESIGN_MODE
Call WalkDownTree(oInst)
Next
End Sub
###
Thanks for reading!
Questions or comments? Id love to hear how you are able to use this macros in your own work
and projects. Email me: [email protected]
To learn how to setup your Macro Libraries in order to implement these macros, check out VB
Scripting for CATIA V5. Ill teach you how to program your own macros so you will be able to
customize these codes for your specific needs.
http://www.vbpdf.website