Home > Programming Technique & Theory > Generating code with Insert Object Resolution

Generating code with Insert Object Resolution

January 27, 2010 Leave a comment Go to comments

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:

  1. Edit an existing macro.
  2. Place the cursor where you want the generated code to be inserted
  3. Start the IOR command.  You will be prompted to select an object in CATIA.
  4. 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.

  1. 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.
  2. 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.
  3. 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: del.icio.us del.icio.us, StumbleUpon StumbleUpon, Digg Digg, Google Google

Advertisements
  1. Phil
    July 8, 2010 at 9:44 am

    Hi

    You mentioned in this article about using IOR to then be able to help assess parent child relationships of an object and i was wondering how you might do this?

    cheers

    • July 8, 2010 at 8:21 pm

      I think you are talking about the following statement when I was discussing the code that is generated by IOR…

      “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.”

      So, first off let me just clarify that when I refer above to parent/child relationships, I am referring to the parents/children in the programming object model, not the notion of parents/children like you would see when you display the parent/child graph in CATIA. They are not at all related.

      With that said, the idea here is that IOR always starts at the active document object and step by step it walks its way down the object model until it gets to the object you selected. This essentially shows you those parent/child relations in the object model. For example in the drafting workbench, this really helps me to visualize how the objects are structured and where I should look in the help documentation for more information.

      Hope that is more clear…

  2. Josh
    August 11, 2010 at 1:13 pm

    Regards,

    I have one question, I have a text in a .catpart file and I want to make a program that respects the current content of the text and then adds another text string to it.

    I tried to use “Insert Object Resolution” on the specific note itself but it doesn’t do anything. When I use “Insert Object Resolution” on the item “Notes”, the parent of all notes, it does return a code but, it the problem is, it does not point to the specific note I want. Here is the code:

    ‘—- Begin resolution script for object : Annotation Set.1

    Dim partDocument1 As PartDocument
    Set partDocument1 = CATIA.ActiveDocument

    Dim part1 As Part
    Set part1 = partDocument1.Part

    Dim annotationSets1 As AnnotationSets
    Set annotationSets1 = part1.AnnotationSets

    Dim annotationSet1 As AnnotationSet
    Set annotationSet1 = annotationSets1.Item(“Annotation Set.1”)

    Dim anyObject1 As AnyObject
    Set anyObject1 = annotationSet1.GetItem(“AnnotationFactory”)

    ‘—- End resolution script

    Then I tried recording a Macro and it gave me the following code:

    Sub CATMain()

    Dim partDocument1 As PartDocument
    Set partDocument1 = CATIA.ActiveDocument

    Dim part1 As Part
    Set part1 = partDocument1.Part

    Dim annotationSets1 As AnnotationSets
    Set annotationSets1 = part1.AnnotationSets

    Dim annotationSet1 As AnnotationSet
    Set annotationSet1 = annotationSets1.Item(“Annotation Set.1”)

    Dim text1 As Text
    ‘ No resolution found for the object text1…

    part1.UpdateObject text1

    End Sub

    So what I am afraid is that Catia VBA doesn’t recognize the Text as an object, and therefore I won’t be able to make the program I want, sort of backing up present text and adding something to it.

    Could you please help me?
    Tnanks in advance.

    • August 12, 2010 at 7:33 am

      So basically, there is no one method that works every time that allows you to figure out how to develop code for a specific object. Sometimes recording works, sometimes IOR works and sometimes you just have to poke around in the VBA object browser or the CATIA automation help docs to find what you need. In this case, I looked up the AnnotationSet class in the object browser and quickly figured out how to access the text of a note once you have a reference to the AnnotationSet object…you will need to extend this so you cycle thru all annotations and process only the notes.

      ‘Assume 1st annotation is a note
      Dim objAnnotation As Annotation
      Set objAnnotation = annotationset1.annotations.item(1)
      Msgbox objAnnotation.Text.Text

      Good luck

  3. Josh
    August 12, 2010 at 10:01 am

    Thanks a lot, it worked great. Regards.

  4. Josh
    August 12, 2010 at 2:01 pm

    Hi, thanks again.
    I got another question. I’d like to add, sort of an error proof for the previous question. You know, if there is currently no annotation set from where to look for the first item and then export the text, to receive a warning in a Msgbox. But I still couldn’t succeed in it, maybe something like annotationset Isempty or I don’t know, could you help me again.
    Thanks again, best regards.

    • August 12, 2010 at 7:56 pm

      Hey Josh,

      So the key concept here is that most objects are usually accessed through a collection object. The type of the collection object is usually the plural name of the type of objects it holds. So in this case, you are looking for an annotation set (or whether there are none) so you can see in the code that we get at the AnnotationSets collection object. All collection objects throughout the entire CATIA object model support a few common properties such as Item and Count. So before when I wanted to get the first item in the collection, I just requested Item(1). Well, to find out how many items there are, just query the Count property. If it is zero then none exist, otherwise you can iterate though each one and do whatever you want. This is a fundamental concept to understand because you will use this exact same technique when you work with selections (a collection holding selection objects), products (a collection holding product objects), shapes (a collection holding shape objects), parameters (a collection holding parameter objects) and any other collection. Here is the basic idea to add to your previous code,

      If part1.AnnotationSets.Count = 0 Then
      Msgbox “No annotation sets exist in this part!”
      Else
      For intIndex = 1 to part1.AnnotationSets.Count
      Set objAnnotationSet = part1.AnnotationSets.Item(intIndex)
      If objAnnotationSet.Annotations.Count > 0 Then
      For intIndex2 = 1 to objAnnotationSet.Annotations.Count
      Set objAnnotation = objAnnotationSet.Annotations.Item(intIndex2)
      ‘Do something with the annotation…
      Next
      End If
      Next
      End If

  5. Josh
    August 13, 2010 at 8:51 am

    Wow, it worked! Thanks again, I was trying with “if not (is nothing)” or is empty, but it didn’t work. I have to study more the object properties of CATIA objects. This is the best blog to learn and develop vba scripts for CATIA, keep up the good work. Thanks again and best regards.

  6. CR
    April 4, 2014 at 3:49 am

    The IOR tool is missing in my CATIA R21 64 bits version. Do you have an issue ?

    Thank you

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: