You are probably already aware that you can record macros in CATIA V5 much like you can in Microsoft Office and other softwares. Recording macros is very simple, so I’m not going to discuss in detail how to do it in this article other than to direct you to the “Tools-Macros” pull down menu. What I will discuss in this article is another related tool called Insert Object Resolution (I will refer to it as IOR) that you can also use to generate code. I will discuss how to use IOR and show how it complements the recording capability.
How does it work?
Using this command is very simple. Here is a step by step overview:
- Edit an existing macro.
- Place the cursor where you want the generated code to be inserted
- Start the IOR command. You will be prompted to select an object in CATIA.
- The IOR command will then generate a block of code where your cursor was.
To avoid confusing this new code with your other code, starting and ending comments are automatically inserted as shown below,
'---- Begin resolution script for object : Object name ' < The generated code will be here > '---- End resolution script
It’s as simple as that – just a couple clicks and you have some new code to work with.
How is this different from recording a macro?
When you record a macro, you manually perform some operations and code is automatically generated that when executed will hopefully mimic the exact same operations. This method of code generation has many uses such as:
- Creating a step by step sequence of operations
- Capturing the creation of new objects
- Deleting objects
- Editing many types of existing objects
On the other hand, IOR just asks you to select an object and it generates code showing how to access that object. The produced code will start at the active document object and it will go step by step down the CATIA object model until it reaches the object you selected. This method of code generation has some unique benefits:
- It inserts new code inline with your existing code rather than in a separate macro
- There is no need to record yourself performing a series of actions
- It can generate code for some objects that recording cannot
Above, I introduced a few benefits of using IOR. Below, are some more detailed scenarios showing how this command is useful.
- When you already have some existing code but you need to access some other object that is not yet referenced in your program. Sure, you could record a separate macro of yourself modifying that object then edit the new macro and copy that code back into your original macro. However, I find IOR to be faster and more convenient because it simply puts the snippet of code I need right where I need it. Just be aware that in this situation, the generated code will ONLY get you a reference to the selected object so you should either have a good idea of what properties you intend to access and how to write that code or you should understand how to look up such information in the API documentation. If not, recording may still be the best option.
- Sometimes when I am writing code that deals with objects that I have never coded before, I like to just play around with IOR to understand how those objects are structured relative to other objects in the CATIA object model. I might not even use any of the generated code, but it allows me to get an idea of things like what collection an object is referenced in, or maybe its parent / child relationships with other objects. I find it really helps me write code if I can build a picture in my head of that object hierarchy.
- As mentioned earlier, macro recording will not generate any code for some objects. This is especially true in the Generative Drafting workbench. When writing code in these areas, you have few choices besides using IOR on some previously created data to generate the code you need. Once you see how to access the needed objects, then you can refer to the API documentation to understand how to use their properties and methods.
Using Insert Object Resolution
Now that I discussed when IOR is useful, I’ll show how to use it and run through an actual scenario. To start, you need to have one of the CATIA V5 macro editors open. This means you should either be in the CATIA VBA editor (ALT-F11) or be in the CATIA macro editor window (Tools-Macros-Macros-Edit). In VBA, you can find this command under the Insert drop down menu. If you are in the CATIA macro editor, it is the second button in from the right at the top of the window.
Now let’s look at a hypothetical example. Let’s assume I already have some code written that opens a drawing and gets at the first view on the first sheet. Now I need to check if any of the text objects in that view have a text value of “Front View”. So basically, I want to understand how to access a text object in a view. I will use IOR to generate some code to get at one text object then I will extend that code a bit to work in a loop so that we can check all text objects in the view.
Step 1 – Open the drawing and the code
Open or create a drawing that has one sheet with at least one view. In that view there should be at least one text. Open the existing code (listed below) in one of the CATIA V5 code editors. This code gets at the view object but does not yet access the text objects in that view.
Sub CheckTextValue() 'This code assumes a drawing is already open! Dim objDwgDoc As Drawing Dim objSheet As DrawingSheet Dim objView As DrawingView Dim objText As DrawingText Dim intCount As Integer Dim intIndex As Integer ‘Get at the active drawing Set objDwgDoc = CATIA.ActiveDocument 'Get at the first sheet or notify user if none are found intCount = objDwgDoc.Sheets.Count If intCount = 0 Then Set objSheet = objDwgDoc.Sheets.Item(1) Else MsgBox "This drawing has no sheets!", 16, "Error" Exit Sub End If 'Get at the first view or notify user if none are found intCount = objSheet.Views.Count If intCount = 0 Then Set objView = objSheet.Views.Item(1) Else MsgBox "The first sheet has no views!", 16, "Error" Exit Sub End If '...We need to add some code here to see if there '...is a drawing text with a value of "Front View" End Sub
Step 2 – Using IOR to get a single text object
Position the cursor on line 35 then start the IOR command. Next, select one of the drawing texts in that view. New code will be generated something like this,
'...Need to add some code here to see if there '...is a drawing text with a value of "Front View" '---- Begin resolution script for object : Text.1 Dim drawingDocument1 As DrawingDocument Set drawingDocument1 = CATIA.ActiveDocument Dim drawingSheets1 As DrawingSheets Set drawingSheets1 = drawingDocument1.Sheets Dim drawingSheet1 As DrawingSheet Set drawingSheet1 = drawingSheets1.Item("Sheet.1") Dim drawingViews1 As DrawingViews Set drawingViews1 = drawingSheet1.Views Dim drawingView1 As DrawingView Set drawingView1 = drawingViews1.Item("Front view") Dim drawingTexts1 As DrawingTexts Set drawingTexts1 = drawingView1.Texts Dim drawingText1 As DrawingText Set drawingText1 = drawingTexts1.Item("Text.1") '---- End resolution script End Sub
Step 3 – Using the generated code
In the code generated by IOR I am really just looking to see how to get at each text object in a view. I can see that the text objects exist in a collection called Texts and each one can be retrieved by the Item method. I will clearly need a loop to check each text object in that collection. At this point, I have all the information I need so I will just delete most of the generated code and tweak the rest to look something like this,
'...Need to add some code here to see if there '...is a drawing text with a value of "Front View" 'Get at the collection of text objects intCount = objView.Texts.Count If intCount = 0 Then For intIndex = 1 to intCount Set objText = objView.Texts.Item(intIndex) If objText.Text = "Front View" Then Msgbox "Text ""Front View"" was found." , 16, "Result" Exit Sub End If Next Else MsgBox "The view has no text objects!", 16, "Error" Exit Sub End If ‘Notify user the text was not found Msgbox "Text ""Front View"" was not found." , 16, "Text Not Found" End Sub
The goal in this scenario was to check multiple text objects. Because this required a loop to access each text in the collection, neither recording nor IOR can perfectly produce the code we need. However, I hope you can see how IOR can be a quicker alternative to directly inject some rough code in the editor which can then be tweaked to your needs.
Please take a moment to rate this article…Just click the stars up near the title.
Add to: