What to do when you get stuck
You’re trying to write your first CATIA macro and you’ve answered all the questions you needed to ask before you began but now you’re simply stuck and don’t know what to do next. Or maybe you’ve already written some code but it’s just not working the way you intended it to.
How in the world are we supposed to go on from this point? If you’re new to CATIA programming you’re likely going to be dead in the water at this point until you get some help. I’ve had many questions come in lately from beginner CATIA programmers who are stuck and don’t know where to go for help. That’s where I come in!
Before giving up, there are a few steps I recommend you take to try and figure the solution out for yourself. I strongly believe you learn more through struggling, overcoming obstacles, and doing it yourself. Here is my list of five things you should do if you consider yourself stuck writing a CATIA macro:
1. Use the CATIA Macro Recorder
The first step I recommend you to try if you’re stuck writing a CATIA macro is to use the macro recorder to record your actions of doing the process manually just to get an idea of what the code might look like. Start the macro recorder, perform the action, then look at the recorded code to get an idea of what the code looks like, then modify the program so it will work on other parts all the time. Remember, recorded macros do not contain any comments or explanations of what is happening in the code and input parameters are never recorded. There are no loops either.
I generally only use the macro recorder as a guide because it usually records a lot of extra lines of code that aren’t needed. This is based on the order of steps that you do as you record the macro. These unnecessary lines can be removed One way for you to learn what is needed is to comment out a line of code (by inserting an apostrophe at the beginning of the line) and then running the macro to see if it still works properly.
2. CATIA Documentation (Built-in features)
CATIA has two built in features that can potentially help you solve your problems. The first is the CATIA object browser and is a great tool to use when you’re stuck and don’t know what to do next. While in the CATScript editor go to View>Object Browser (or simply hit F2) and use the search bar.
The other feature is the CATIA Visual Basic Help Documentation (also known as CATIA Automation Documentation) that can be accessed using either the online help (if the complete CATIA Documentation is installed) or by opening the file:
<Catia_install_folder>\intel_a\code\biV5Automation.chm.
3. Look at Other Code Examples
A great way to learn is to see how other programmers solved similar problems. I’ve made a number of my CATIA macros available as a free download here. I often send sample macros in my email newsletter as well. Also, be sure to read all of my free articles as well as many of them have custom code with thorough explanations of how they work. Forums such as Eng Tips are also a create place to see CATIA macro examples.
4. Step Away From the Problem
This is actually my favorite tip here. There have been countless times where I’ve been banging my head against a wall, not able to figure out a problem. So, I would simply get up from my computer and walk away, maybe for a few hours or days, and not think about the problem at all. Then when I sit down in front of the screen again refreshed the answer hits me almost immediately. Seriously, this happens almost every time! It’s that whole not being able to see the forest through the trees type of thing. So take a break!
5. Use the Internet to Ask Questions
Google is your friend! If you don’t find a suitable answer on your first search try a new one with different words. Personally, there’s nothing that annoys me more than when someone asks me a question and I simply Google it and I find the solution right away. Don’t be lazy! Other people do not want to do your work for you.
If you’re still stuck and clueless about what to do next you can ask for help from another programmer or automation expert. Posting in the recommend forums listed on my Resources page is a great way to get feedback from multiple power users.
If all else fails you can contact me through email and I will attempt to get you going in the right direction (as my time and knowledge permits, please be patient). I hope you enjoyed reading about some of my CATIA macro troubleshooting techniques.
We are experiencing certain limitations generating the script file. It is difficult / impossible to record all the modelling steps in MACRO and generate the script file. Is it possible?
How to highlight misspelled word on drawing sheet using VBA programming
Thanks for all your posts.
I am a catia user mainly GSD workbench only.
Could you please make a detail article how to use Object browser with examples.
I know the command to use interactively in CATIA and also been able to find that command by using object browser but do not understand how to use that command in code. I know this would be basic from your point of view, may be a few example with detail explanation will work for me.
Anyways Thanks a lot to you as I have already started a bit by using recording macros.
You can see me use the object browser in some of my bonus videos here: http://www.vbpdf.website
Hello Emmett,
I am trying to get a screenshot of each sketch of the Catpart.
Question is : How to open the 2D sketch view that we get while creating or modifying the sketch? With all the constraints and lines..
Thank you for your Help
Best wishes
Hi,
I have created the below macro for exporting CATIA V5 2D drawing(dimensions, welding positions) to excel but i got the below error. Can you please help me.
Macros used:
Sub CATMain()
Dim oDwgDoc1 As DrawingDocument
On Error Resume Next
Set oDwgDoc1 = CATIA.ActiveDocument
If Err.Number 0 Then
MsgBox (“The active document must be a drawing.”), vbExclamation
End
End If
On Error GoTo 0
Dim oSel As Selection
Set oSel = oDwgDoc1.Selection
If oSel.Count < 1 Then
MsgBox ("No dimensions selected."), vbExclamation
End
End If
Dim oDwgDim As DrawingDimension
Dim oInt As Integer
Dim dblDims()
Dim oTolType As Long
Dim oTolName As String
Dim oUpTol As String
Dim oLowTol As String
Dim odUpTol As Double
Dim odLowTol As Double
Dim oDisplayMode As Long
For ctr = 1 To oSel.Count
On Error Resume Next
Set oDwgDim = oSel.Item(ctr).Value
If Err.Number 0 Then
MsgBox (“One of the selected elements is not a drawing dimension.”), vbExclamation
End
End If
On Error GoTo 0
ReDim Preserve dblDims(3, ctr – 1)
If oDwgDim.DimType = catDimAngle Or oDwgDim.DimType = catDimChamfer Then
dblDims(0, ctr – 1) = oDwgDim.GetValue.Value * 57.2957795
Else
dblDims(0, ctr – 1) = oDwgDim.GetValue.Value
End If
oDwgDim.GetTolerances oTolType, oTolName, oUpTol, oLowTol, odUpTol, odLowTol, oDisplayMode
dblDims(1, ctr – 1) = odUpTol
dblDims(2, ctr – 1) = odLowTol
dblDims(3, ctr – 1) = oDwgDim.Parent.Parent.Name
odUpTol = 0
odLowTol = 0
Next
Dim strFilePath As String
Dim objFSO As Object
Dim objStream As Object
strFilePath = “C:\Users\Drew\Desktop\dimensions.csv”
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objStream = objFSO.OpenTextFile(strFilePath, 8, True, 0)
For i = 1 To oSel.Count
objStream.WriteLine (dblDims(0, i – 1) & “,” & dblDims(1, i – 1) & “,” & dblDims(2, i – 1) & “,” & dblDims(3, i – 1))
Next
objStream.Close
End Sub
Error:
Source: Microsoft VB Script compilation error
Description: Expected ‘If’
Statement: End
Line: 9
Column: 15
I think you need End Sub instead of just End.
On Error Resume Next
Set oDwgDoc1 = CATIA.ActiveDocument
If Err.Number 0 Then
MsgBox (“The active document must be a drawing.”), vbExclamation
Ens Sub
End If
On Error GoTo 0
Hi Emmett,
I want to rename all the points created inside a Geomterical Set to 1,2,3 ….. instead of default numbering (Point.1, Point.2, Point.3…). Can you help me with the code to rename all the points? I tried a code which was available on the internet, but it throws up error “Expected end of statement” on third line.
Option Explicit
Sub CATMain()
Dim MyObj As Object, pd1 As PartDocument, a As String
Dim Result As String
ReDim InputObjectType(0) As Variant
Dim MySelection As Object
‘ Make sure active document is a CATPart
If TypeName(CATIA.ActiveDocument) “PartDocument” Then
MsgBox “This program only works on a CATPart.”
Exit Sub
End If
Set pd1 = CATIA.ActiveDocument
Set MySelection = pd1.Selection
ReDim InputObjectType(0)
InputObjectType(0) = “HybridBody”
Result = MySelection.SelectElement2(InputObjectType, “Select the open body with the inspection points.”, False)
If Result = “Normal” Then
Set MyObj = pd1.Selection.Item(1).Value
ElseIf Result = “Redo” Then
MsgBox “Redo is not an option for this program.”
End
Else
End
End If
Call Dumb_Renumber(MyObj)
End Sub
Sub Dumb_Renumber(x As HybridBody)
Dim n As Double, i As Integer
Dim PtString As String
Dim PtNumberOld As Integer, PtNumberNew As Integer
Dim ptName As String, NewPtName As String
Dim StartPos As Integer
n = InputBox(“What number would you like to start with?”, “Point Rename Input”, “100”)
For i = 0 To x.HybridShapes.Count – 1
x.HybridShapes.Item(i + 1).name = “Tooling Hole.” & n + i
Next
End Sub