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!

Activate SelectElement2 immediately after userform

Status
Not open for further replies.

AlexMorasan

Automotive
Aug 17, 2020
13
Hello everyonge

I am creating a macro that creates a Plane (Angle/Normal to Plane) and a Line (Angle/Normal to Curve) after the user selects a point and a curve. To inform the user about what he/she needs to select my code shows a MsgBox with the proper instructions.

Examplo, the user runs the macro, the following Msgbox appears requesting the user to select a curve.

image_smvyij.png


After the user selects the curve then a new Msgbox appears a request the selection of a point, then the macro creates the geometry.

image_irbm8z.png


I am trying to improve my macro by adding a userform, like the following picture. However, I am not sure how to activate the SelectElement2 function right after the userform appears.

image_agayhy.png


For example when you add a plane in CATIA you press the button , the userform appears and you can immediately select your references to create the plane. I want my macro to function in a similar way, any ideas about how I can do it ? If you need to see my code I ll provide it ASAP.
So far I only tried o put this code , but it did not work

Private Sub UserForm_Initialize()
Set USel = CATIA.ActiveDocument.Selection
Set USelLB = USel
MsgBox "Select in the Specification tree the part where the tangency will be created. Press ESC to cancel"
oStatus = USelLB.SelectElement2(PartObject, "Select something in Specification Tree", False)
If (oStatus = "Cancel") Then
Exit Sub
Else
End If
Selected_Part = USel.Item(1).Value.Name
USel.Clear

End Sub

Thanks in advance for your help.
 
Replies continue below

Recommended for you

Code:

Private Sub UserForm_Initialize()
Set USel = CATIA.ActiveDocument.Selection
USel.Clear

Dim PartObject(0)
PartObject(0) = "AnyObject"

Dim ostatus
MsgBox "Select in the Specification tree the part where the tangency will be created. Press ESC to cancel"
ostatus = USel.SelectElement2(PartObject, "Select something in Specification Tree", False)

If (ostatus = "Cancel") Then
MsgBox "Macro aborted by user"
End

Else
End If

Selected_Item = USel.Item(1).Value.Name
USel.Clear

End Sub
 
First of all remove any interactive code from form's constructor.

If you want to mimic native commands then selection routine should occur every time a user clicks on the control displaying currently selected object.

To do so add Focus or Activate event handlers to each control (curve and point) and do start SelectElement2 there.

And I'm sure you've read documentation well enough and aware that you can't call SelectElement2 while previous call is still running?
Easiest way to achieve this is to move it to a separate function and put a guard variable in the very beginning.

Below is pseudocode:

Code:
Private mCurve, mPoint, mSelecting

Private Sub UserForm_Initialize()
  mSelecting = False
End Sub

Private Sub CurveControl_Focus()
  If TrySelectElement("Curve", mCurve) then
    CurveControl.Text = mCurve.Name
  End if
End Sub

Private Sub PointControl_Focus()
  If TrySelectElement("Point", mPoint) then
    PointControl.Text = mPoint.Name
  End if
End Sub

Private Function TrySelectElement (filter, selected)
  TrySelectElement = false
  If mSelecting then
    Exit function
  End if
  mSelecting = true

  Dim sel: set sel = CATIA.ActiveDocument.Selection
  If "Normal" <> sel.SelectElement2(Array(filter), "Select " + filter, false) then
    Exit function
  End if
  Set selected = sel.Item(1).Value
  TrySelectElement = false
End Function
 
Thanks both of you for your help

In the end I use the Activate event to do my macro. However after my test phase the users vote to change interface.

Little Cthulhu, thank you so much for all your feedback and the pseudo code. Being honest I am still trying to figure out how to properly use it, but I am sure I only need to keep studying to do it.

 
Glad I was of assistance.

What's your programming background and engineering area of expertise by the way?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor