微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有谁知道如何根据用户选择的装配图中的零件从装配图中提取零件号? CATIA VBA

如何解决有谁知道如何根据用户选择的装配图中的零件从装配图中提取零件号? CATIA VBA

我正在尝试为 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"

结束子

解决方法

嗨,如果您不介意使用气球,请尝试使用它们。他们可以为您提供零件编号、实例名称,当然还有编号。您必须更改绘制气球的选项。这是一个屏幕

enter image description here

然后你用像这样的零件号创建气球enter image description here

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

试着用宏记录你的动作或插入对象分辨率,看看你能得到什么。

enter image description here

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

,

如果您不想像 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 会怎样?
  • 此脚本仅获取零件编号。你可能知道如何做剩下的事情。我没有检查,但我打赌你也可以从临时气球的领导者那里得到点击点。
,

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

显着变化:

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

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

程序代码如下。

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。