Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to make a selction inside a macro or vba? 3

Status
Not open for further replies.

jowr

Automotive
Mar 5, 2014
9
Hi,

The following simple code below generates a messagebox with the text of the feature that´s been (and needs to be) selected before running the macro:
But what if I would like to use the opposite order, first run the macro and then select feature and get the messagebox? How do I do that??

Sub CATMain()
Dim USel As Selection
Set USel = CATIA.ActiveDocument.Selection
If USel.Count > 0 Then
For I = 1 to USel.Count
MsgBox(USel.Item(I).Value.Name), ,"VBScript"
Next
End If
End Sub

Best regards
JO
 
Replies continue below

Recommended for you

Hi

Code:
Sub CATMain()

Dim USel As Selection
Dim InputObject(0)
Dim oStatus

InputObject(0) = "AnyObject"
Set USel = CATIA.ActiveDocument.Selection
Msgbox "This macro will give you the name of a selected element, you have to hit ESCAPE key when you want to finish" & vbCrLf & vbCrLf & "Maximum number of loops is 100. " & "Names will depend if you select in Specification Tree or Graphic Area"
    For i = 1 To 100

        oStatus = USel.SelectElement2(InputObject, "Select something in Specification Tree", True)

        If (oStatus = "Cancel") Then
            Exit Sub
            Else
        End If
        MsgBox(USel.Item(1).Value.Name), ,"VBScript"
        USel.Clear

    Next
End Sub

Regards
Fernando

 
WOW Ferdo!

I have seen your nice work here at the forum and read many posts that you have solved and now you solved my problem! :) I´m really greatful!

Sorry for another newbe question, but if I would like to use this code inside a catvba? Is it possible? I get "compile error" on USel.SelectElement2...

Thanx once again!

best regards from jowr
 
Does it say "Compile error: function marked as restricted… "?
 
Hi Lardman363,

Yes, that´s it. It says:

"Compile Error:
Function or interface marked as restricted, or the function uses an Automation typ not supported in Visual Basic."

I understand what it says but I suppose there must be a workaround for this? Or is it not possible to make a feature selection inside a userform?
 
Yes, if you are using CATScript or catvbs, they are late bound languages...meaning the compiler determines the variable type as the code runs...not ahead of time when the variable is declared. So there are two things you can try:
1. COMMENT OUT THE VARIABLE TYPE
Dim Usel 'As Selection
2. ADD A SECOND SELECTION VARIABLE THAT IS LATE BOUND and use the new variable when making the selection
Sub CATMain()

Dim USel As Selection
Dim USelLB
Dim InputObject(0)
Dim oStatus

InputObject(0) = "AnyObject"
Set USel = CATIA.ActiveDocument.Selection
Set USelLB = USel
Msgbox "This macro will give you the name of a selected element, you have to hit ESCAPE key when you want to finish" & vbCrLf & vbCrLf & "Maximum number of loops is 100. " & "Names will depend if you select in Specification Tree or Graphic Area"
For i = 1 To 100

oStatus = USelLB.SelectElement2(InputObject, "Select something in Specification Tree", True)

If (oStatus = "Cancel") Then
Exit Sub
Else
End If
MsgBox(USel.Item(1).Value.Name), ,"VBScript"
USel.Clear

Next
End Sub
 
GREAT lardman! It worked!

This totally made my day, Thank you very much! [bow]

With best regards
JOWR
 
Oh, if i dare to ask another question...? :)

If I would like to populate a listbox with the selections?
wouldn´t the code below together with your code (comment out the msgboxes) do the trick?

text = USel.Item(1).Value.Name
myListBox.AddItem (text)

I tried it but it would only populate one feature at a time.

best regards
JOWR



 
SelectElement2 only allows you to select 1 object at a time, but you are kind of tricking yourself because the selection is made in a loop. So, you would need to either store all your data in an array or use SelectElement3, which allows you to multi-select. Note: After you multi-select, a "tools palette" will open and you have to select the green ball icon called "finish" to complete the selection. Sometimes it is quirky and you have to click it twice or just hold the click for a second to get it to work consistently.

Sub CATMain()

Dim USel As Selection
Dim USelLB
Dim InputObject(0)
Dim oStatus

InputObject(0) = "AnyObject"'selection filter forces user to select specific objects, AnyObject allows selection of any object

Set USel = CATIA.ActiveDocument.Selection
Set USelLB = USel

Msgbox "This macro will give you the name of a selected element, you have to select the FINISH button on the Tools Palette when you want to finish selecting" & vbCrLf & "Names will depend if you select in Specification Tree or Graphic Area"

USel.Clear'You should clear the selection before making a selection

oStatus = USelLB.SelectElement3(InputObject, "Select objects to list names", True,CATMultiSelTriggWhenUserValidatesSelection, False)

If (oStatus = "Cancel") Then'User hit esc on keyboard
MsgBox "Macro canceled by user"
Exit Sub
Else'Loop through selected objects and list names
For i = 1 to USel.Count
sName = USel.Item(I).Value.Name
oListBox.AddItem (sName)
Next
'Insert the code you are using to display the ListBox and please share with us!
End If

USel.Clear

Next
End Sub
 
Or another solution could be after each selection (if you don't want to use multiple selection, maybe you want to see something in 3D or whatever) write the names in a temporary text file and open it when cancel selection.

On the other hand, is good when you are developing something, to think from the very beginning what you want to do.

Regards
Fernando

 
Hello Lardman and Ferdo, thank you very much for your help!
It worked perfectly!

With best regards
JOWR

Private Sub btn_start_Click()

btn_start.Enabled = False 'Disable button while running script

Dim USel 'As Selection
Dim USelLB
Dim InputObject(0)
Dim oStatus

InputObject(0) = "AnyObject"
Set USel = CATIA.ActiveDocument.Selection

If USel.Count > 0 Then 'Added to clear current selection to not be added from start
USel.Clear
End If

Set USelLB = USel

Label1.Caption = "Selection started ESC to stop." 'Added to prompt to user how to stop selection.

For i = 1 To 100

oStatus = USelLB.SelectElement2(InputObject, "Select something in Specification Tree", True)

If (oStatus = "Cancel") Then
Exit For 'Original row "Exit Sub"
End If

text = USel.Item(1).Value.Name
myListBox.AddItem (text)

USel.Clear

Next

Label1.Caption = "" 'Added to clear label
btn_start.Enabled = True 'Enabel button again
End Sub

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor