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 some testing. 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 hint how 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 a functionality of selection sets in VBA.
How things work
- Open product
- Select some parts or assemblies and launch 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 sel.Clear ' 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 :).