Home > The CATIA Object Model > A comparison of two methods for deleting objects

A comparison of two methods for deleting objects

December 7, 2009 Leave a comment Go to comments

In this article, I am going to discuss two different methods for deleting objects using automation.  The first method is provided by the selection object.  This process is very similar to the way you delete things when you use CATIA interactively – you select the items to delete then click delete.  The other method I will show is provided by the HybridShapeFactory object and is a bit more specialized but can be very handy (and even required) in some situations.

Method 1: Deleting with the Selection object

First, let’s take a minute and look at how you delete items using the Selection object.  It can be summarized in four easy steps,

  1. Get at the Selection object
  2. Clear the current selection
  3. Select (add) each item to be deleted
  4. Call the delete method

Selection example

Here is an example that deletes a solid body, a geometrical set and a parameter from a CATPart that is open in its own window…

'Assume a single part is open in its own window.

'Declare variables
Dim objSel As Selection
Dim objPart As Part

'Get at the selection object and the part
Set objSel = CATIA.ActiveDocument.Selection
Set objPart = CATIA.ActiveDocument.Part

'Clear the selection to prevent any currently
'selected items from getting deleted

'Select the first geometrical set in the part
objSel.Add objPart.HybridBodies.Item(1)

'Select the body named “Body.2”
objSel.Add objPart.Bodies.Item(“Body.2”)

'Select the parameter named “Length.1”
objSel.Add objPart.Parameters.Item(“Length.1”)

'Delete the currently selected items

Method 2: DeleteObjectForDatum

This technique is somewhat limited because it can only be used to delete items inside of a CATPart, but is still very useful.  To use this technique you need to first access the HybridShapeFactory object in the part that contains the items you want to delete.

While the primary purpose for the HybridShapeFactory object is to create new wireframe and surface geometry, it also has the ability to delete other objects as well.   Deleting this way is a simple two step process.

  1. Create a special reference to the object to be deleted using a reference object
  2. (The easiest way to accomplish this is to use the CreateReferenceFromObject method provided by the part object like in the example below)

  3. Call the DeleteObjectForDatum method and pass the reference as an argument

This is a little different than the selection method.  With the Selection object, you add all the items to delete then call the delete method once.  With DeleteObjectForDatum you need to call the method once for each object that is to be deleted.

DeleteObjectForDatum example

In this example, I will replicate the exact same deletion scenario shown in the earlier example.  Again, a solid body, a geometrical set and a parameter will be deleted from a CATPart that is open in it’s own window…

'Assume a single part is open in its own window.

'Declare variables
Dim objPart As Part
Dim objHSF As HybridShapeFactory
Dim objRef As Reference

'Get at the part and its HybridShapeFactory object
Set objPart = CATIA.Activedocument.Part
Set objHSF = objPart.HybridShapeFactory

'Delete the first geometrical set in the part
Set objRef = objPart.CreateReferenceFromObject(objPart.HybridBodies.Item(1))
objHSF.DeleteObjectForDatum objRef

'Delete the body named “Body.2”
Set objRef = objPart.CreateReferenceFromObject(objPart.Bodies.Item(“Body.2”))
objHSF.DeleteObjectForDatum objRef

'Delete the parameter named “Length.1”
Set objRef = objPart.CreateReferenceFromObject(objPart.Parameters.Item(“Length.1”))
objHSF.DeleteObjectForDatum objRef

The DeleteObjectForDatum method can delete virtually anything that exists in the part.  It works for solids, parameters, points, lines, sketches, really almost anything.  In general, if you can create a reference to it you can delete it.

When to use Selection

  • Anytime you need to delete something that is not inside of a CATPart, you must use selection because the DeleteObjectForDatum method only works inside a part.  So definitely use Selection for products and drawings and the data within them.  But make no mistake – Selection is also a fine solution for deleting objects in a part as well but you have the other option as well.
  • The Selection object is very powerful when combined with search.  In the above examples, I wanted to delete specific items, but in some cases you want to delete a broad group of objects.  For example, maybe you want to delete all points in a geometrical set but leave all other features in that set untouched.  In another case, you may need to delete any kind of feature that contains specific characters in its name.  In both of these cases, the Selection.Search method will find all the objects and add them to the selection in one line of code.  While this can be accomplished without search, it takes a fair amount of code, will certainly execute slower and is more prone to potential errors.

Here is some example code using search to build on our earlier example.

    'Delete all points in a specific HybridBody (geometrical set)
    objSel.Add objPart.HybridBodies.Item(1)
    objSel.Search “Type=Point,sel”
    If objSel.Count2 > 0 Then objSel.Delete
    'Delete everything in the part that contains the characters “Trim”
    objSel.Add objPart
    objSel.Search “Name=*Trim*,sel”
    If objSel.Count2 > 0 Then objSel.Delete

Tips for deleting with Selection

  • Always clear the selection first to prevent unwanted items that might be already selected from getting deleted.
  • If using Search to find items, perform the search manually first to confirm that it works as expected.  Then copy-paste the search criteria into your code.

When to use DeleteObjectForDatum

  • Only for deleting objects that exist in a CATPart
  • When you need to delete geometry that has not been appended in the tree.  Actually, this is what this method was originally intended for.  When you create geometry using the HybridShapeFactory, it is created in the part but it is not appended to the tree until you call the AppendHybridShape method.  So in some cases, you might create some temporary geometry used to make some final isolated result element and you only want to keep the isolated result.  In this situation, you probably won’t append that temporary geometry to the tree so you must delete those temporary elements using DeleteObjectForDatum before the code execution ends.  After the program ends, search won’t work because search can only find geometry that has been appended to the tree.

Also, don’t be fooled when creating temp geometry like this!  Just because you don’t delete unappended geometry it still exists in the part.  Do some tests and you will see the file size increase even though visually nothing new can be seen – it’s still stored in the CATPart.  If you don’t use DeleteObjectForDatum while your code is still running, the only way to delete that data out of the part is to use CATDUA afterwards.  You will see these as LIF_1 errors.

  • When you want to delete some items but there are already other items selected for some other purpose.  In this case, you could store the currently selected items in a collection or an array, but sometimes it is easier to use DeleteObjectForDatum and leave the selection untouched.  Especially if there are just one or two items to delete, I would not write a lot of extra code to work around the selection.
  • As a general rule, I try not to delete using selection inside small reusable subs and functions that perform tasks within parts.  When I write code I try to separate common tasks into separate subs and functions stored in a code library so I can reuse that code in other projects.  When I reuse one of those functions at some later time, I don’t want to wonder whether that code is going to modify or clear the items I currently have selected in my program, so I often like to use DeleteObjectForDatum instead.

Please take a second to rate this article – just click the stars at the top by the title.

Add to: del.icio.us del.icio.us, StumbleUpon StumbleUpon, Digg Digg, Google Google

  1. Satish Kolekar
    May 5, 2011 at 9:48 pm


    I read your posts and they are really helpful.


  2. June 12, 2011 at 9:17 am

    I agree with Satish,

    Good job.
    Not many of us have the skill to present technical stuff in a compelling way so that
    I can also understand it :-D
    E.g with MicroSoft, they will simply will never learn!
    A lot of bla bla and no straightforward stuff that is helpfull for me.

    What a delight in that respect to be at this blog.

    Kind regards, JJ_Delft

  3. rajesh
    November 19, 2012 at 7:20 am

    Nice and thank you for all the information provided above….With correct selection i could select product but i want to reorder instances of the product…i tried to reorder using method available in catiascript manual…but not working please guide.
    I am using below command
    oselection1—contains product selected by user

    I am tryign to reorder 1 item in collection products to item no 2.

    i used below command also but not working..

    Many other forums are telling about copy paste but it breaks links..How to do it with catia reorder..

    I tried to record macro but it is also not working.

    Please help…I am trying to get it solved from many days but failed…please help

  4. Michael Laursen
    February 26, 2014 at 8:26 am

    Very nice Mike Berry,
    It works fine when the script is activated in the part alone(part opened in separate window. But when you try to run the script in the contexts of a product it won’t work due to the active document is the product. How did you solve this..? Or maybe you know how to delete a geometrical.Set/body with the KWA Language ” Feature –> Delete() ”

    Please let me know.. have struggled with this for a month now.. and no one seems to be able to solve the problem..

    BR. Michael

    • February 27, 2014 at 9:11 pm

      Hi Michael…If the active document is a product, you just need to get a reference to the part you are interested in from the assembly. For example, lets say the part you want is the 2nd part instance under the root product…

      Set objRootProd = CATIA.ActiveDocument.Product
      Set objPartInst = objRootProd.Products.Item(2)
      Set objPart = objPartInst.ReferenceProduct.Parent.Part <–This replaces the line in my sample

  1. No trackbacks yet.

Leave a Reply to catia4inventorusersforumJJ Cancel 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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

%d bloggers like this: