admin管理员组

文章数量:1356595

I have developed a macro that can create thickness operation on a pasted solid from another Catpart .it's working , but the problem that I am facing is the faces ID's that I have in this code sometimes not aligned with what I have in the operation . is there a way to avoid faces ID's and catch all faces automatically from the pasted Solid and make the thickness operation. in the code below , you can see many faces Id , sometimes it works , sometime Catia ask about missing faces . many thanks

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

I have developed a macro that can create thickness operation on a pasted solid from another Catpart .it's working , but the problem that I am facing is the faces ID's that I have in this code sometimes not aligned with what I have in the operation . is there a way to avoid faces ID's and catch all faces automatically from the pasted Solid and make the thickness operation. in the code below , you can see many faces Id , sometimes it works , sometime Catia ask about missing faces . many thanks

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
Share Improve this question edited Mar 28 at 10:51 New User With you asked Mar 28 at 10:00 New User With youNew User With you 195 bronze badges 4
  • Which faces of the body do you need? (maybe show a picture) If you need all faces, maybe a selection.search (like on your other question) may work. – Shrotter Commented Mar 28 at 10:35
  • sorry, I fot about the picture. please check it now. thank you – New User With you Commented Mar 28 at 10:51
  • If you need only cylindrical faces: search for all faces (Topology.CGMFace,sel) and check there type for CylindiricalFace – Shrotter Commented Mar 28 at 11:11
  • thank you for your feedback, but the code that I provide in this question is not for EdgeFillet (that was for another topic) . because I find '"Topology.CGMFace,sel"`` 'only in that code . my which is to edit the code upper where it can automatically define all cylindrical faces and makes thickness . – New User With you Commented Mar 28 at 11:12
Add a comment  | 

1 Answer 1

Reset to default 0

You can get the cylindrical faces of a body by selection the body and search for the faces. By looping over the faces and checking the type you can pick the right faces.

An example code:

Sub CATMain()

Dim oPartDoc As Document
Dim oSel As Selection
Dim oPart as Part
Dim oBody as Body
Dim oShapeFactory As Factory
Dim oThickness As Thickness
Dim i as Long
Dim bodyName as String

'start
bodyName = "FOAM_HOLES"

Set oPartDoc = CATIA.ActiveDocument
Set oSel = oPartDoc.Selection
Set oPart = oPartDoc.Part
Set oShapeFactory = oPart.ShapeFactory

'get body
Set oBody = oPart.Bodies.Item(bodyName)

'select body and search for faces
oSel.Clear
oSel.Add oBody
oSel.Search "Topology.CGMFace,sel"

'create thickness and add cylindrical faces
If oSel.Count2 <> 0 Then
    oPart.InWorkObject = oBody
    Set oThickness = oShapeFactory.AddNewThickness(nothing, 5.000000)

    For i = 1 to oSel.Count2
        if oSel.Item2(i).Type = "CylindricalFace" Then
            oThickness.AddFaceToThicken oSel.Item2(i).Value  'or .Reference
        end if
    Next
    
    oPart.UpdateObject oBody

End If

End Sub

本文标签: vbaCATIAMACRO Thickness OperationStack Overflow