SelectionSets object in CATIA VBA

Selection sets are nice CATIA feature to group and store selected elements of any type. They are pretty well described in V5Automation reference. The purpose of the methods of a SelectionSets object like CreateSelectionSet, DeleteSelectionSet or RenameSelectionSet is quite obvious, other methods like AddCSOIntoSelectionSet or PutSelectionSetIntoCSO are a bit less obvious, but at the first glance, we have enough to start with. However, there is no real-world example or use case which could help us at the beginning.

What's worse, there is no information about how to get or create this object. Or at least I could not find it in official VBA reference. But after a bit of googling, I found an answer. The missing piece of the puzzle. A trick to get a SelectionSets object of a product. And although it looks quite weird and a bit scary, it works.

Set selSets = doc.Product.GetItem("CATIAVBSelectionSetsImpl")

With this information, there is no problem to write a simple script to test the functionality of selection sets in VBA.

How things work

  • Open product
  • Select some parts or assemblies and launch a macro

Sub Main()
    Dim doc, sel, setName
    setName = "Example"
    Set doc = CATIA.ActiveDocument
    Set sel = doc.Selection
    Dim selSets
    ' get SelectionSets object (a tricky part)
    Set selSets = doc.Product.GetItem("CATIAVBSelectionSetsImpl")
    ' create new selection set
    selSets.CreateSelectionSet setName
    ' add selected elements to selection set
    selSets.AddCSOIntoSelectionSet setName

    ' activate the content of selection set
    selSets.PutSelectionSetIntoCSO setName

    'Dim a()
    'selSets.GetListOfSelectionSet a
End Sub

A new selection set named "Example" is created and highlighted and it contains selection content. Press Ctrl+G to open Selection Sets Selection dialog window.

However, I have a problem to get a list of selection sets. Last 2 lines of code should work, but I experience a CATIA crash on my machine when I uncomment them. If you find a way how to get such a list, please let me know :).

One Response to “SelectionSets object in CATIA VBA”

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>