【问题标题】:Pull the part number from an assembly drawing depending on the part in that assembly drawing the user selected (CATIA VBA)根据用户选择的装配图中的零件,从装配图中提取零件编号 (CATIA VBA)
【发布时间】:2021-09-18 08:41:45
【问题描述】:

我正在尝试为 CATIA 编写一个 VBA 程序,该程序会生成一个引线,其中包含用户单击(选择)的图形中元素的零件号。

宏用于包含大量零件的装配图。用户应该能够单击图纸中的零件,并且该特定零件的零件编号应显示在领导者的文本中。

有两个问题。

  1. 我必须提供用户可以选择的参数。
    我不认为它可以是“DrawingView”,因为用户需要能够在装配视图中选择各个零件。

  2. 从该选择中提取零件编号。
    现在我的代码提取了生成视图的文件的名称。在这种情况下,这也是零件编号,但宏的主要用途是包含一堆零件的装配图。

我尝试选择“AnyObject”作为选择,但即使我单击视图内的不同部分,VBA 也只会选择视图。我花了很多时间查看https://catiadesign.org/_doc/V5Automation/generated/interfaces/_index/CAAHomeIdx.htm 上的不同对象、属性和方法,但我找不到任何可以根据选择的视图部分来操纵信息的东西。

我认为这是可能的,因为如果您将尺寸工具悬停在图纸上的零件上,CATIA 会在装配视图中给出不同零件的零件编号。所以 CATIA 可以通过某种方式获取这些信息。

Sub CatMain()
    'Sets drawing doc as active doc and makes sure a drawing is open
    Dim draw_doc As DrawingDocument
    On Error Resume Next
    Set draw_doc = CATIA.ActiveDocument
    If Err.Number <> 0 Then
        MsgBox "A drawing must be open to run this macro"
        End
    End If
    On Error GoTo 0
    
    Dim draw_sheets As DrawingSheets            'Create drawing sheets collection
    Set draw_sheets = draw_doc.Sheets           'Set the drawing sheets collection to be the collection for the drawing document
    Dim draw_sheet As DrawingSheet              'Create drawing sheet object
    Set draw_sheet = draw_sheets.ActiveSheet    'Makes that drawing sheet object the active sheet
    Dim draw_view As DrawingView                'Creates drawing view objec
    Dim draw_leaders As DrawingLeaders          'Creates drawing leaders collection
    Dim draw_leader As DrawingLeader            'Makes drawing leader object
    
    Dim selection_array(0)                      'Create array that stores the the types of things CATIA can select
    selection_array(0) = "DrawingView"          'Make drawing views be the only thing that can be selected
    Set selection_1 = draw_doc.Selection        'Set the selection object to select things in this drawing document
    'Enable CATIA to go into selection mode and let the user click on something to select it
    status = selection_1.SelectElement2(selection_array, "Select the View(s) to Re-link. DON'T FORGET TO CLICK 'FINISH' ON TOOLS PALETTE.", False)
    'If the user presses ctrl+z or cancels then we stop the program
    If status = "Undo" Or status = "Cancel" Then
        MsgBox "You have chosen to terminate this macro."
        End
    End If
    Set draw_view = selection_1.Item(1).Value   'The drawing view is set to be the value of the view that was selected
    
    Dim leader_pos_x, leader_pox_y As Double    '==\
    leader_pos_x = 20                           '===> Dimension and set leader position
    leader_pos_y = 20                           '==/
    'The name/part number of can be taken from the drawing view with the .GenerativeBehavior.Document.Name properties
    Dim part_number As String
    part_number = draw_view.GenerativeBehavior.Document.Name 'gets the name of the document that generated the drawing view
    part_number = Replace(part_number, "_", " ")
    Dim draw_texts As DrawingTexts              'Create drawing texts collection
    Set draw_texts = draw_sheet.Views.ActiveView 'Set the drawing texts to the avtive view
    Dim draw_text As DrawingText                'Make drawing text object
    'Set the drawing text and position we're goint to use for the leader
    Set draw_text = draw_view.Texts.Add(part_number, 30, 50)
    'Create the leader with x and y position relative to the drawing view
    Set draw_leader = draw_text.Leaders.Add(leader_pos_x, leader_pos_y)
    
    'MsgBox "Done"
End Sub

【问题讨论】:

    标签: vba catia


    【解决方案1】:

    您好,如果您不介意使用气球,不妨试试。他们可以为您提供 PartNumbers、InstanceNames,当然还有编号。您必须更改绘制气球的选项。这是一个屏幕

    然后你创建带有零件号的气球

    知道部件号的 Catia 并不意味着它在免费 api 中可用。恐怕你不能再深入了 - draw_view.GenerativeBehavior.Document

    只需尝试在宏中记录您的操作或插入对象分辨率,看看您能得到什么。

    我曾经做过一个类似的程序,它可以自动绘图。我阅读了装配中的零件获取它们的坐标,并根据装配零件坐标将零件编号放置在图纸中。如果您没有注意到,绘图和装配共享相同的轴系统(蓝色箭头是 3D 装配的起源)。但这只是特定装配的特例,一般不能用于随机装配。

    【讨论】:

      【解决方案2】:

      如果您不想实际使用 DJakub 建议的气球,这里有一个小解决方法。

      脚本通过CATIA.StartCommand 调用Catia 的气球命令并等待用户点击某处。特别是,它会一直等到添加了新的 DrawingText,然后获取其内容并将其删除。我想不出一个好的错误处理方法,但是 10 秒后脚本也会退出循环。

      Set oView = CATIA.ActiveDocument.DrawingRoot.ActiveSheet.Views.ActiveView
      numtexts = oView.Texts.Count
      
      'Change ToolsOptions so that balloons will be created with PartNumbers
      '(Hint from DJakub)
      Set settingRepository1 = CATIA.SettingControllers.Item("DraftingOptions")
      settingValueBeforeChange = settingRepository1.GetAttr("DrwBalloonAssocMod")
      settingRepository1.PutAttr "DrwBalloonAssocMod", 2 'Balloon creation with PartNumber
      
      'Start Catia's Balloon command
      CATIA.StartCommand "Balloon"
      
      'Wait until user clicks somewhere
      '(DrawingText with PartNumber will be added from Balloon command)
      tic = Timer
      Do
          DoEvents
          If oView.Texts.Count > numtexts Then
              'Get text and remove balloon
              Set oText = oView.Texts.Item(oView.Texts.Count)
              strPartNumber = oText.Text
              oView.Texts.Remove oView.Texts.Count
              
              Exit Do
          End If
          
          toc = Timer
      Loop Until toc - tic > 10 'Exit loop after 10 seconds
      
      'Exit Ballon command
      SendKeys "{ESC}", True
      
      'Reset setting to standard
      settingRepository1.PutAttr "DrwBalloonAssocMod", settingValueBeforeChange
      

      有些情况您可能需要处理

      • 如果用户手动退出气球命令怎么办? 10 秒内新的 DrawingText 将被删除
      • 如果用户没有点击 GeneratedItem 怎么办?
      • 此脚本仅获取 PartNumber。你可能知道如何做剩下的事情。我没有检查,但我打赌你也可以从临时气球的领导者那里得到点击点。

      【讨论】:

      • 请记住,如果您真的希望 PartNumber 是 DrawingText 并且您不喜欢气球的外观(没有圆圈),这只是一种解决方法
      【解决方案3】:

      感谢 Vyndell 和 DJakub 的帮助!我将您的答案实施到我的程序中,并且效果很好!下面是使用 Vyndell 在他的回答中显示的方法创建领导者的代码。

      显着变化:

      使用CATIA.StartCommand“选择”,因此用户在单击生成的项目(绘图中的部分)后不必处理气球创建框。

      如果用户单击绘图中的随机点,则使用检查。如果未选择零件,则零件序号默认为创建编号的零件序号。所以我让代码将零件号转换为整数。如果没有错误编号,则用户很可能单击了随机点并且程序结束。如果出现错误,程序将继续。

      程序代码如下。

      Sub CATMain()
      
      
      
      'Sets drawing doc as active doc and makes sure a drawing is open
      Dim draw_doc As DrawingDocument
      On Error Resume Next
      Set draw_doc = CATIA.ActiveDocument
      If Err.Number <> 0 Then
          MsgBox "A drawing must be open to run this macro"
          End
      End If
      On Error GoTo 0
      
      
      
      'Brings up macro instructions. The false sets the modal to false aka code runs with out messing with the form
      Leader_Gen_With_Pt_Num_Inst.Show (False)
      
      
      
      'Sets the view to be the current active view in CATIA
      Set oView = CATIA.ActiveDocument.DrawingRoot.ActiveSheet.Views.ActiveView
      'Stores the number of text associated with a view
      numtexts = oView.Texts.Count
      
      
      
      'Change Tools-->Options-->Drafting-->Annotation and Dress-up-->Balloon Creation so
      'that balloons will be created with PartNumbers. Thanks DJakub!
      Set settingRepository1 = CATIA.SettingControllers.Item("DraftingOptions")       'Set the options selection to drafting options
      settingValueBeforeChange = settingRepository1.GetAttr("DrwBalloonAssocMod")     'The original user settings
      settingRepository1.PutAttr "DrwBalloonAssocMod", 2                              'Balloon creation with PartNumber
      
      
      
      'Start Catia's Balloon command
      CATIA.StartCommand "Balloon"
      
      
      
      'Dimension positioning variables
      Dim i As Long
      Dim x As Double
      Dim y As Double
      'Create drawing leader object
      Dim draw_leader As DrawingLeader
      
      
      
      'Sets the starting time for the timer for the variable tic
      tic = Timer
      
      Do
      
          DoEvents
          'Goes into the if statment when a text object has been added
          If oView.Texts.Count > numtexts Then
          
              'Get most recently create text in a text object
              Set oText = oView.Texts.Item(oView.Texts.Count)
              'Set a leader object to be the leader created from the balloon
              Set draw_leader = oText.Leaders.Item(oText.Leaders.Count)
              'Store the position of the leader in the doubles x and y
              draw_leader.GetPoint i, x, y
              'Store the part number in the string strpartnumber
              strpartnumber = oText.Text
              'Deletes the most recent text. Also deletes the leader since the text is its parent
              oView.Texts.Remove oView.Texts.Count
              Exit Do
          End If
          
          'Sets the end time for the timer to the variable toc
          toc = Timer
          
          'Since toc is the start and tic is the end, then toc minus tic means the timer will go to 8 seconds
          If toc - tic = 8 Then
              MsgBox "Ending macro since eight seconds have passed"
              'Ends program
              End
          End If
          
      Loop Until toc - tic > 8 'Exit loop after 8 seconds
      
      'Exit Ballon command
      SendKeys "{ESC}", True
      
      
      
      'Setting the CATIA command to select makes it so the balloon creation box doesn't appear when the user clicks away
      CATIA.StartCommand "Select"
      
      
      
      'Returns the ballon settings to what the user originally had
      settingRepository1.PutAttr "DrwBalloonAssocMod", settingValueBeforeChange 'Will want to put this at the end of the main program
      
      
      
      Dim draw_sheets As DrawingSheets            'Create drawing sheets collection
      Set draw_sheets = draw_doc.Sheets           'Set the drawing sheets collection to be the collection for the drawing document
      Dim draw_sheet As DrawingSheet              'Create drawing sheet object
      Set draw_sheet = draw_sheets.ActiveSheet    'Makes that drawing sheet object the active sheet
      'Dim draw_view As DrawingView                'Creates drawing view objec
      Dim draw_leaders As DrawingLeaders          'Creates drawing leaders collection
      
      
      
      'When something that is not a part in a drawing is clicked on, a numbered balloon is created
      'So we cast the part number as an integer. When an error happens we know the user did not click on a random spot so we create the leader
      'When an error is not thrown the user didn't click on the part, so we end the program and tell them what went wrong.
      Dim test_int As Integer
      On Error Resume Next
      test_int = CInt(strpartnumber)
      If Err.Number <> 0 Then
      
          On Error GoTo 0
          
          Dim part_number As String
          'part_number stores the part number value of strpartnumber that we got from the balloon
          part_number = strpartnumber
          'Repalce underscores with spaces
          part_number = Replace(part_number, "_", " ")
          Dim draw_texts As DrawingTexts              'Create drawing texts collection
          Set draw_texts = draw_sheet.Views.ActiveView 'Set the drawing texts to the avtive view
          Dim draw_text As DrawingText                'Make drawing text object
          'Set the drawing text and position we're goint to use for the leader
          'The text is set 40 mm over and 40 mm up from the leader arrow
          Set draw_text = oView.Texts.Add(part_number, x + 40, y + 40)
          'Create the leader with x and y position relative to the drawing view
          Set draw_leader = draw_text.Leaders.Add(x, y)
      
      Else
          On Error GoTo 0
          'The message box actually doesn't run, but this code keeps the leader from being created if the user clicked a random spot
          MsgBox "You may have selected an item that is not an assembly part, so the program did not pull the part number"
          'Hides opened form
          Leader_Gen_With_Pt_Num_Inst.Hide
          End
      End If
      
      
      
      'Hides form once code has ran
      Leader_Gen_With_Pt_Num_Inst.Hide
      
      
      
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-10-15
        • 1970-01-01
        • 1970-01-01
        • 2022-11-10
        • 2020-10-27
        • 2011-03-05
        • 1970-01-01
        • 2021-09-14
        相关资源
        最近更新 更多