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,
- Get at the Selection object
- Clear the current selection
- Select (add) each item to be deleted
- Call the delete method
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.
- Create a special reference to the object to be deleted using a reference object
- Call the DeleteObjectForDatum method and pass the reference as an argument
(The easiest way to accomplish this is to use the CreateReferenceFromObject method provided by the part object like in the example below)
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.
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.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
- 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.