用于插入槽孔轴线的CATIA VBA宏(绘图)

时间:2018-01-24 09:51:06

标签: vba catia

我决定制作一个宏,因为我和我的同事不得不多次点击鼠标来为图纸上的每个槽孔创建所需的轴线。
这是期望的结果:
https://i.imgur.com/ENErDRX.png

上下文是绘图环境中生成的图纸视图 目前我们通过以下方式实现这一目标:

  • 双击轴线命令
  • 选择一个圈子&选择一行
  • 选择另一个圈子&选择一行
  • 选择刚刚创建的2轴线
  • 选择两个圈子

这是非常多的点击次数。

(是的,我知道有一个叫做“中心线”的东西,还有“轴线和中心线”,但我们不想将这些用于开槽孔)

现在我开始简单了,我想制作一个宏,它将添加前两条轴线(如上面的顺序)。在运行宏之前,用户必须通过用鼠标拖动一个方框来选择2个圆和2个开槽孔。

到目前为止,这是我得到的:

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

正如你所看到的,我的第一个问题出现在这一行上:
(“圆圈”当然是填充废话)

If Selection1 = "circle" Then

我无法找到区分所选行或所选圈子的方法 我在Selection1,Selection2,Selection3和Selection4上使用了监视工具来尝试找到生成的圆和生成的行之间的差异,但到目前为止它已被证明是不成功的。

如果有人知道如何区分圆圈和线条,那么我会非常感谢答案。

1 个答案:

答案 0 :(得分:1)

您尝试获得的线条和圆圈可能是Line2DCircle2D

下面的代码将从数组CirclesLines上的选择中检索索引12上每种元素中的两种元素,如果多于或少于每个选择2个,代码将被中断。

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

不幸的是Catia.StartCommand对于需要2个选项的命令效果不佳,我无法使Catia.StartCommand("Axis Line")工作。

可能还有其他方法可以使用自动化创建Axis Line,但我自己找不到。