-1

我想编写一个宏来选择:

  • 所有名为“name_1”的实体并将它们添加到名为“new group 1”的新实体中
  • 所有名为“name_2”的实体并将它们添加到名为“new group 2”的新实体中
  • 所有名为“name_3”的实体并将它们添加到名为“new group 3”的新实体中

我在我的 CATIA R20 上成功编写了这段代码,并且工作正常。它将所有实体添加到新实体中。但后来我尝试在 CATIA R19 和 R24 上运行宏,但它不起作用!在 R19/R24 中,它不会添加所有名为“name_1”的实体,但它只会在新实体中添加一个实体!

Sub CATMain()
    Dim partDocument1 As PartDocument    
    Set partDocument1 = CATIA.ActiveDocument    
    Set objSel = partDocument1.Selection

    Dim part1 As Part    
    Set part1 = partDocument1.Part
    Dim bodies1 As Bodies    
    Set bodies1 = part1.Bodies

    Set shapeFactory1 = part1.ShapeFactory    
    Set bodies1 = part1.Bodies    
    Dim body1 As Body    
    Set body1 = bodies1.Item("PartBody")    
    Dim shapes1 As Shapes    
    Set shapes1 = body1.Shapes    
    objSel.Clear

    '******************name_1*********************    
    objSel.Search ("Name=name_1,all")    
    objcount = objSel.Count    

    Set body1 = bodies1.Add()    
    body1.Name = "new_name_1"

    Set body1 = bodies1.Item("new_name_1")
    part1.InWorkObject = body1

    For i = 1 To objcount
    Set body11 = bodies1.Item("name_1")
    Set add1 = shapeFactory1.AddNewAdd(body11)
    Next
    objSel.Clear

End Sub
4

1 回答 1

0

在 Catia 中,如果您有多个同名特征,Catia 只识别第一个特征。所以当你:

Set body11 = bodies1.Item("name_1")

您之前搜索了所有名为“name_1”的实体,如果有多个,它将​​只搜索第一个。

所以我会做类似的事情:

Sub CATMain()
Dim partDocument1 As PartDocument    
Set partDocument1 = CATIA.ActiveDocument    
Set objSel = partDocument1.Selection

Dim part1 As Part    
Set part1 = partDocument1.Part
Dim bodies1 As Bodies    
Set bodies1 = part1.Bodies

Dim cBodies as new collection

Set shapeFactory1 = part1.ShapeFactory    
Set bodies1 = part1.Bodies    
Dim body1 As Body    
Set body1 = bodies1.Item("PartBody")    
Dim shapes1 As Shapes    
Set shapes1 = body1.Shapes    
objSel.Clear

'******************name_1*********************    
objSel.Search ("Name=name_1,all")    
objcount = objSel.Count    

'Check if search found something
If objcount > 0 then
    'add selected objects to a collection
     For i = 1 To objcount
        cBodies.Add objSel.Item(i).value
     next
     objSel.clear

     Set body1 = bodies1.Add()    
     body1.Name = "new_name_1"

    'Set body1 = bodies1.Item("new_name_1") 'You dont need to do this because you set it 3 lines up
    'part1.InWorkObject = body1 'Not needed, Any new feature will automatically be the inworkobject

For i = 1 To objcount
    Set add1 = shapeFactory1.AddNewAdd(cBodies1.Item(i))
Next

part1.UpdateObject body1 'Update the body

End if

End Sub
于 2015-09-20T12:30:45.720 回答