CATIA V5 Automation

A comparison of two methods for deleting objects

Advertisements

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
objSel.Clear

'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
objSel.Delete

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

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


'Delete all points in a specific HybridBody (geometrical set)
objSel.Clear
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.Clear
objSel.Add objPart
objSel.Search “Name=*Trim*,sel”
If objSel.Count2 > 0 Then objSel.Delete

Tips for deleting with Selection

When to use DeleteObjectForDatum

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.

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

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

Advertisements