Laurens Ruben
Laurens Ruben

Reputation: 109

CATIA VBA Macro for inserting axis lines for slotted holes (drafting)

I decided to make a macro because my colleagues and I are sick of having to click the mouse many times to create the desired axis lines for every slotted hole on a drawing.
Here is the desired result:
https://i.imgur.com/ENErDRX.png

The context is a generated drawing view in the drafting environment.
Currently we accomplish this by:

This is quite a lot of clicks.

(Yes I realize there is a thing called "Center Line" and also "Axis and Center Line" but we prefer not to use these for slotted holes)

For now I'm starting simple and I'd like to make a macro that will add the first 2 axis lines (as in the sequence above). The user will have to select the 2 circles and 2 lines of the slotted hole by dragging a box over them with the mouse before running the macro.

So far this is what I've gotten:

Sub CATMain()

Dim drawingDocument1 As DrawingDocument
Set drawingDocument1 = CATIA.ActiveDocument

Dim SelectionsAll As selection
Set SelectionsAll = drawingDocument1.selection

Dim Selection1
Set Selection1 = SelectionsAll.Item(1)

Dim Selection2
Set Selection2 = SelectionsAll.Item(2)

Dim Selection3
Set Selection3 = SelectionsAll.Item(3)

Dim Selection4
Set Selection4 = SelectionsAll.Item(4)

Dim circle1 As selection
Dim circle2 As selection
Dim line As selection

If Selection1 = "circle" Then
    circle1.Add.SelectionsAll.Item (1)
Else
    line.Add.SelectionsAll.Item (1)
End If

If Selection2 = "circle" Then
    circle2.Add.SelectionsAll.Item (2)
Else
    line.Add.SelectionsAll.Item (2)
End If

If Selection3 = "circle" Then
    If circle1 Is Nothing Then
        circle1.Add.SelectionsAll.Item (3)
    Else
        circle2.Add.SelectionsAll.Item (3)
    End If
Else
    line.Add.SelectionsAll.Item (3)
End If

If circle2 Is Nothing Then circle2.Add.SelectionsAll.Item (4)

SelectionsAll.Clear

CATIA.StartCommand "Axis Line"

SelectionsAll.Add circle1
SelectionsAll.Add line

CATIA.StartCommand "Axis Line"

SelectionsAll.Add circle2
SelectionsAll.Add line

End Sub

As you can see my first problem arises on this line:
("circle" is of course filler nonsense)

If Selection1 = "circle" Then

I can't find a way to distinguish between a selected line or a selected circle.
I've used the watch tool on Selection1, Selection2, Selection3 and Selection4 to try and find differences between a generated circle and a generated line but so far it's proven unsuccessful.

If anyone knows how I could distinguish between a circle and a line then I'd be very grateful for the answer indeed.

Upvotes: 1

Views: 2380

Answers (1)

Quima
Quima

Reputation: 914

The kind of Line and Circle you are trying to get are probably Line2D and Circle2D.

The code bellow will retrieve from selection on arrays Circles and Lines two of each of those kinds of elements on indexes 1 and 2, if more or less than 2 of each were selected the code will be interrupted.

Sub CreateAxes()

    Dim Document As DrawingDocument
    Set Document = CATIA.ActiveDocument

    Dim Selection As Selection
    Set Selection = Document.Selection

    Dim Circles(2) As Circle2D
    Dim Lines(2) As Line2D

    Dim i As Integer

    Dim CircleCount As Integer
    Dim LineCount As Integer

    CircleCount = 0
    LineCount = 0

    For i = 1 To Selection.Count2
        Select Case TypeName(Selection.Item2(i).Value)
            Case "Circle2D"
                CircleCount = CircleCount + 1
                If CircleCount > 2 Then
                    MsgBox "More than 2 Circles were selected!", vbCritical
                    Exit Sub
                End If
                Set Circles(CircleCount) = Selection.Item2(i).Value

            Case "Line2D"
                LineCount = LineCount + 1
                If LineCount > 2 Then
                    MsgBox "More than 2 Lines were selected!", vbCritical
                    Exit Sub
                End If
                Set Lines(LineCount) = Selection.Item2(i).Value

        End Select
    Next

    If LineCount < 2 Or CircleCount < 2 Then
        MsgBox "This operation requires 2 circles and 2 lines to be selected", vbCritical
        Exit Sub
    End If


    'Lines(1) = First Line
    'Lines(2) = Second Line
    'Circles(1) = First Circle
    'Circles(2) = Second Circle

End Sub

Unfortunatly Catia.StartCommand does not work well for commands that needs 2 selections, and I could not make Catia.StartCommand("Axis Line") work.

There may be some other way to create an Axis Line using automation, but I couldn't find it myself.

Upvotes: 1

Related Questions