首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据所选择的装配图中的部件从装配图中提取零件编号(CATIA VBA)

根据所选择的装配图中的部件从装配图中提取零件编号(CATIA VBA)
EN

Stack Overflow用户
提问于 2021-07-07 19:42:57
回答 3查看 745关注 0票数 0

我正在为CATIA编写一个VBA程序,该程序生成一个领导者,其中包含用户单击(选择)的绘图中元素的部分编号。

宏用于具有大量零件的装配图。用户应该能够点击绘图中的部件,并且该特定部件的部件号应该由领导在文本中显示。

有两个问题。

  1. 我必须为用户可以选择的内容提供一个参数。

我不认为它可以是"DrawingView“,因为用户需要能够在程序集视图中选择各个部件。

  1. 从该选择中提取零件编号。

现在,我的代码提取生成视图的文件名。在本例中,这也是零件编号,但是宏的主要用途是一组包含大量部件的装配图。

我尝试了"AnyObject“作为选择,但是VBA只是选择视图,即使我单击视图中的不同部分。我在https://catiadesign.org/_doc/V5Automation/generated/interfaces/_index/CAAHomeIdx.htm上花了大量的时间研究不同的对象、属性和方法,但是我找不到任何可以根据视图中选择的部分来操作信息的东西。

我认为这可能是可能的,因为CATIA在装配视图中给出了不同零件的数量,如果您将尺寸工具悬停在绘图上的零件上。这样CATIA就能以某种方式得到这些信息。

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2021-07-10 22:44:12

如果你不想像DJakub提议的那样使用气球,这里有一个小的解决办法。

该脚本通过CATIA.StartCommand调用Catia的气球命令,并等待用户单击某个位置。特别是,它等待一个新的DrawingText被添加,获取它的内容并删除它。我无法给出一个很好的错误处理,但是10秒后脚本也会退出循环。

代码语言:javascript
复制
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

有些案子你可能需要处理

如果用户手动退出气球命令,

  • 怎么办?一个新的DrawingText在10秒内就会被删除,如果用户不点击DrawingText脚本只得到PartNumber,怎么办?你可能知道怎么做剩下的事。我没有检查,但我敢打赌你也可以从临时气球的领队处得到点击点。
票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68292176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档