【发布时间】:2021-09-18 08:41:45
【问题描述】:
我正在尝试为 CATIA 编写一个 VBA 程序,该程序会生成一个引线,其中包含用户单击(选择)的图形中元素的零件号。
宏用于包含大量零件的装配图。用户应该能够单击图纸中的零件,并且该特定零件的零件编号应显示在领导者的文本中。
有两个问题。
-
我必须提供用户可以选择的参数。
我不认为它可以是“DrawingView”,因为用户需要能够在装配视图中选择各个零件。 -
从该选择中提取零件编号。
现在我的代码提取了生成视图的文件的名称。在这种情况下,这也是零件编号,但宏的主要用途是包含一堆零件的装配图。
我尝试选择“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
【问题讨论】: