Home > The CATIA Object Model > How to get the Part object from virtually any object within it

How to get the Part object from virtually any object within it

December 15, 2009 Leave a comment Go to comments

Sometimes I run into a situation where I have a variable that refers to some object inside a part and somehow I need to need to get at the part that the object exists in.  Here, I will show an easy way to get the part object from virtually any other child object in that part.

Most of the time I run into this scenario when I am using some of the CATIA knowledgeware tools together with VB scripts inside of a part.  For example, I might create a reaction on a parameter in the part which will execute some Visual Basic code anytime the value of that parameter changes.  The problem with the reaction feature is it only provides the parameter object to your code when it executes, so it is very useful to be able to then get at the part from that parameter.

There are other similar situations you might come across like this outside of the knowledgeware tools when working solely in a VB project.  An understanding of how to navigate up the object hierarchy like this is a useful skill.

Why is getting at the part object so important?

I can think of many reasons why you might want to get at the part object.  Some examples might be to access parameters, relations, geometrical sets, or bodies as well as the HybridShapeFactory to create new wireframe and surface geometry or the ShapeFactory to create new solid geometry.  Also, the parent of the part is the CATPart document which holds information about the file path and allows you to save the CATPart.

Using an object’s Parent property

Nearly every object in the CATIA object model has a property called Parent which returns its parent object.  The CATIA object model follows a strict hierarchy and each object’s parent is usually another object one level up that hierarchy.  These parent objects are different from the parents you would see if you displayed a parent/child graph for some geometry interactively in CATIA.  Those parents are the parent features that were used to create the feature being analyzed.  However, in the automation world parents are closely related to the tree structure.  For example, the parent of a surface is usually a HybridShapes collection object representing all the wireframe and surface objects contained in that geometrical set and the parent of that HybridShapes object is the geometrical set.  Once you understand the concept of this hierarchy, you can then use it to repetitively access the parent property of each object and “walk” up the object model and get at nearly any higher level object.

The code

Before we dig into the code, I want to encourage you to gradually develop a reusable code library over time.  With each project you tackle, you should strive to identify any commonly used chunks of code and develop these as individual subroutines or functions that you can add to your library.  This way, they are very easy to use in the next project you take on.  Since this code is probably something I will use again and again in a number of projects, I have written it as an easily reusable function.

Function GetPartFromObject(ByRef iObject As Object) As Part

   'This function calls itself recursively to access the
   'parent of the object passed in until a Part object
   'is reached.

   Dim strType As String

   'Get the type of the object passed into the function
   strType = TypeName(iObject)

   If strType = “Part” Then
      'If the object passed in is a part, return it and exit
      Set GetPartFromObject = iObject
      Exit Function

   ElseIf strType = TypeName(iObject.Parent) Then
      'If the type of this object is the same as the parent object
      'then return nothing.  The reason is the Parent property of
      'some objects simply returns the same object.  This will never
      'get us to the part and will result in an infinite loop.
      'Example object types are Reference and Resource among others
      Set GetPartFromObject = Nothing
      Exit Function

      'Call the function again and pass it the object’s parent
      Set GetPartFromObject = GetPartFromObject(iObject.Parent)

   End If

End Function

In case you new to writing functions, don’t worry they are very easy.  A function simply executes some code (just like a subroutine) but it can also return a value as a result.  To return the value, you just set the name of the function equal to the value or the object to be returned.  You can see this on line numbers 14, 23 and 28 above.

If line number 28  stopped you in your tracks, let me quickly introduce a concept called recursion.  Recursion is a very easy but powerful concept that simply allows a procedure (subroutine or function) to call itself again and again.  The computer keeps track of each call to the procedure in what is referred to as a call stack and when the last call of the procedure exits, it returns the value back to the previous call in the stack again and again until it reaches the original call.  If you want to read more about recursion, check out the wikipedia entry.

I used recursion in the example above because it simplifies the code and it allows us to call the function an unknown number of times to finally get the part object.  This is important because the object originally passed into the function could exist nearly anywhere in the part object hierarchy.

Using the function

To use this function, just declare a new variable as a Part object.  Then set the variable equal to this function and pass in the starting object.  As with any function, you should always check the returned data before carrying on with the rest of your code in case a problem occurred.  Below are a few example usages.

Dim objPart As Part

'Assume the variable objParam refers to some parameter in the part
Set objPart = GetPartFromObject(objParam)
If objPart Is Nothing Then
   Msgbox “The Part object could not be accessed.”, 64, “Error”
   Exit Sub
   'Do something with the part object
End If

'Assume the variable objSurf refers to any surface in the part
Set objPart = GetPartFromObject(objSurf)
If objPart Is Nothing Then
   Msgbox “The Part object could not be accessed.”, 64, “Error”
   Exit Sub
   'Do something with the part object
End If

'Assume the variable objGeoSet refers to a geometrical set in the part
Set objPart = GetPartFromObject(objGeoSet)
If objPart Is Nothing Then
   Msgbox “The Part object could not be accessed.”, 64, “Error”
   Exit Sub
   'Do something with the part object
End If

Now you not only know how to easily get at the part object, but also about working with the parent property and recursion which comes in very handy in many other situations.  If anything in this article still doesn’t make sense, just post a comment and I’ll try to clear things up.

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

  1. catfox
    February 18, 2010 at 7:23 pm


    all here is fine. but what if I want to analize if my selected hybridshape object has children. Is any method to check it??

    Thanks in advance


    • February 18, 2010 at 8:15 pm

      Sorry, but unfortunately I don’t know of a method to check for children using the automation API.

  2. catfox
    February 19, 2010 at 3:19 am

    it’s OK. As soon as I get info about this method I will give feedback here.

    Your blog is what many pepople were trying to find.

  3. June 12, 2012 at 2:07 am

    This Blog is Great !

  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: