Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

Accessing SelectionSets using Catia VBA

Status
Not open for further replies.

hdwdlnd

Aerospace
Sep 1, 2009
16
0
0
US
I would like to resurrect an old thread related to selection sets and accessing them through VBA. Like the other poster I see the commands in the scripting documentation but I am unaware how to actually use them. Anyone out there care to share? Thanks.
 
Replies continue below

Recommended for you

Hi,
working with selection sets is in fact quite easy once you know how to get SelectionSets object. This is what I find completely unintuitive, however, you can find some hints on google.

How it works
[ul]
[li]Open product[/li]
[li]Select some parts or assemblies and launch macro[/li]
[li]New selection set named "Example" is created & highlighted and it contains selection content[/li]
[/ul]

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

    sel.Clear
    
    selSets.PutSelectionSetIntoCSO setName      ' activate the content of selection set
    
'    Dim a()
'    selSets.GetListOfSelectionSet a
End Sub


However, I have a problem to get list of selection sets. Last 2 lines of code should work, but I experience CATIA crash on my machine when I uncomment them. But maybe it is only problem with my CATIA installation.


Tesak
- Text along a curve for Catia V5
 
Thanks for the help. The "CATIAVBSelectionSetsImpl" is what I was needing. I revised your code so that what items are selected in the tree get their own selection sets or the option to drop them in a named set which is why I was needing the script. Thanks again.

Code:
Sub CATMain()

Dim SelCheck As Integer

SelCheck = CATIA.ActiveDocument.Selection.Count2

Dim HCheck As String

If SelCheck = 0 Then

    MsgBox ("Items must be selected first")
    Exit Sub
    
End If

If SelCheck <= 1 Then

    HCheck = MsgBox("Are child components highlighted?", vbYesNo)
    
    If HCheck = vbNo Then
    
        MsgBox ("Hightlight items to be added and try again")
     
        Exit Sub
     
    End If
    
End If

Dim doc, sel, setName

Set doc = CATIA.ActiveDocument
Set sel = doc.Selection

Dim product1 As Product
Set product1 = doc.Product
  
Dim i As Integer

i = 1

Dim n As Integer

n = CATIA.ActiveDocument.Selection.Count2

Dim selSets

Dim NameCheck As String

Dim iSel As Selection

ReDim SelComp(n)

Dim Test As String

If CATIA.ActiveDocument.Selection.Count2 = 1 Then

    setName = sel.Item(1).Value.PartNumber
     
    Set selSets = doc.Product.GetItem("CATIAVBSelectionSetsImpl")   ' get SelectionSets object (a tricky part)
    
    selSets.CreateSelectionSet setName          ' create new selection set
    selSets.AddCSOIntoSelectionSet setName      ' add selected elements to selection set
    
Else

    NameCheck = MsgBox("Do you want each item to have a selection set?", vbYesNo)
    
    If NameCheck = vbYes Then
            
        For i = 1 To n
        
            Set SelComp(i) = sel.Item(i).Value
        
        Next
        
        For i = 1 To n
        
            Set iSel = CATIA.ActiveDocument.Selection
            
            iSel.Clear
            
            iSel.Add SelComp(i)
                          
            setName = iSel.Item(1).Value.PartNumber
         
            Set selSets = doc.Product.GetItem("CATIAVBSelectionSetsImpl")   ' get SelectionSets object (a tricky part)
         
            selSets.CreateSelectionSet setName          ' create new selection set
            selSets.AddCSOIntoSelectionSet setName      ' add selected elements to selection set
     
        Next
     
    Else
     
        setName = InputBox("Selection Set Name", "Input Selection Set Name", sel.Item(1).Value.PartNumber)
     
        Set selSets = doc.Product.GetItem("CATIAVBSelectionSetsImpl")   ' get SelectionSets object (a tricky part)
     
        selSets.CreateSelectionSet setName          ' create new selection set
        selSets.AddCSOIntoSelectionSet setName      ' add selected elements to selection set
        
    End If
     
End If
    
End Sub
 
You do not need to call these two lines as in my example:
Code:
sel.Clear        
selSets.PutSelectionSetIntoCSO setName      ' activate the content

It is just slowing your script down (especially if you call it in a loop). For creation and adding content to selection set only following lines are required:
Code:
selSets.CreateSelectionSet setName          ' create new selection set  
selSets.AddCSOIntoSelectionSet setName      ' add selected elements to selection set

So you can save some resources and make your script shorter :).

Tesak
- Text along a curve for Catia V5
 
I see what you mean. I updated my code above so that it actually works now...I hope. I had botched a copy and paste or something last time. Where did you come up with "CATIAVBSelectionSetsImpl" if you don't mind me asking?
 
Status
Not open for further replies.
Back
Top