我已经开发了一个宏,它可以在从另一个 Catpart 粘贴的实体上创建厚度操作。它可以工作,但我面临的问题是,此代码中的面 ID 有时与操作中的面 ID 不一致。有没有办法避免面 ID 并自动从粘贴的实体中捕获所有面并进行厚度操作。在下面的代码中,您可以看到许多面 ID,有时它可以工作,有时 Catia 会询问缺失的面。非常感谢
Sub ThicknessOP()
Dim partDocument1 As Object
Set partDocument1 = CATIA.activeDocument
Dim part1 As Object
Set part1 = partDocument1.part
Dim bodies1 As Object
Set bodies1 = part1.bodies
' Modify input body name and thickness name here
Dim bodyName As String
Dim thicknessName As String
bodyName = "FOAM_HOLES" ' Change this to the desired body name
thicknessName = "ThicknessOperation" ' Change this to the desired thickness name
Dim body1 As Object
Set body1 = bodies1.Item(bodyName)
Dim shapes1 As Object
Set shapes1 = body1.Shapes
' Try to retrieve the thickness shape
Dim thickness1 As Object
On Error Resume Next ' Enable error handling
Set thickness1 = shapes1.Item(thicknessName)
On Error GoTo 0 ' Disable error handling
' Check if the thickness has been retrieved
If thickness1 Is Nothing Then
MsgBox "Error: " & thicknessName & " not found in " & bodyName & ". Please verify the name.", vbExclamation
Exit Sub
End If
Dim solid1 As Object
Dim solidFound As Boolean
solidFound = False
' Loop through all solids in the body to find the desired solid
Dim i As Integer
For i = 1 To shapes1.Count
Set solid1 = shapes1.Item(i)
If solid1.Name Like "Solid.*" Then
solidFound = True
Exit For
End If
Next i
If Not solidFound Then
MsgBox "Error: Solid not found in " & bodyName & ". Please verify the name.", vbExclamation
Exit Sub
End If
Dim faceIds As Variant
faceIds = Array(4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38) ' Face IDs from 4 to 38
Dim successfullyAdded As Integer
Dim failedIds As String
successfullyAdded = 0
failedIds = ""
' Try to add each face to the thickness operation
For i = LBound(faceIds) To UBound(faceIds)
Dim reference As Object
On Error Resume Next ' Enable error handling
Set reference = part1.CreateReferenceFromBRepName("RSur:(Face:(Brp:(" & solid1.Name & ";%" & faceIds(i) & ");None:();Cf11:());WithTemporaryBody;WithoutBuildError;WithSelectingFeatureSupport;MFBRepVersion_CXR15)", solid1)
If Err.Number = 0 Then
thickness1.AddFaceToThicken reference
successfullyAdded = successfullyAdded + 1
Else
failedIds = failedIds & faceIds(i) & ", "
End If
On Error GoTo 0 ' Disable error handling
Next i
' Update the thickening object
part1.UpdateObject thickness1
part1.Update
' Provide feedback on the operation
If successfullyAdded > 0 Then
MsgBox successfullyAdded & " face(s) added to the thickness operation." & vbCrLf & _
"Failed face IDs: " & IIf(failedIds = "", "None", Left(failedIds, Len(failedIds) - 2)), vbInformation
Else
MsgBox "No faces were successfully added to the thickness operation." & vbCrLf & _
"Failed face IDs: " & IIf(failedIds = "", "None", Left(failedIds, Len(failedIds) - 2)), vbExclamation
End If
End Sub
您可以通过选择物体并搜索面来获取物体的圆柱面。通过循环遍历面并检查类型,您可以选择正确的面。
示例代码: