Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Define Circle Edges for Points in CATIA VBA?

wealnut

Automotive
Oct 26, 2024
1
I am working with VBA in CATIA and attempting to add points at the centers of the circle edges of a selected cylindrical face after assigning an axis line to that face. However, I'm having trouble defining the "StartEdge" and "EndEdge" references correctly. The code currently adds the points to the selected face instead of positioning them at the centers of the circle edges.

' Create a reference for the start edge Set referenceStart = part.CreateReferenceFromBRepName(GetBrep(cylindricalFace.Name & "StartEdge"), cylindricalFace.Parent)

' Function to construct a BRep name for a given geometric element
Public Function GetBrep(MyBRepName As String) As String
' Remove "Selection_" prefix from the name
MyBRepName = Replace(MyBRepName, "Selection_", "")
' Get the part of the name before the closing parentheses
MyBRepName = Left(MyBRepName, InStrRev(MyBRepName, "));"))
' Append necessary parameters for the BRep reference
MyBRepName = MyBRepName & ");WithPermanentBody;WithoutBuildError;WithSelectingFeatureSupport;MFBRepVersion_CXR15)"
' Return the constructed BRep name
GetBrep = MyBRepName
End Function

' Main subroutine to select a cylindrical face and create an axis line
Sub SelectCylindricalFaceAndCreateAxis()
' Declare necessary variables
Dim objSel As Selection
Dim objSelLB As Object
Dim strReturn As String
Dim varFilter(0) As Variant
Dim cylindricalFace As Face
Dim partDocument As PartDocument
Dim part As Part
Dim hybridBody1 As HybridBody
Dim hybridShapeFactory1 As HybridShapeFactory
Dim hybridShapeAxisLine1 As HybridShapeAxisLine
Dim reference1 As Reference
Dim strBrep As String

' Get the active document selection
Set objSel = CATIA.ActiveDocument.Selection
Set objSelLB = objSel
Set partDocument = CATIA.ActiveDocument
Set part = partDocument.Part

' Display a message box to instruct the user
MsgBox "Select the first cylindrical face of the tube. Then press the finish button in the tools palette toolbar."

' Set the selection filter to only allow face selections
varFilter(0) = "Face"
objSel.Clear

' Prompt the user to select a cylindrical face
strReturn = objSelLB.SelectElement3(varFilter, "Select a cylindrical face...", False, CATMultiSelTriggWhenUserValidatesSelection, False)

' Check if the user canceled the selection
If strReturn <> "Cancel" Then
' Store the selected cylindrical face
Set cylindricalFace = objSel.Item(1).Value

' Create a new hybrid body for the axis line
Set hybridBody1 = part.HybridBodies.Add()
hybridBody1.Name = "CenterLine"

' Create a reference from the selected cylindrical face
strBrep = GetBrep(cylindricalFace.Name)
Set reference1 = part.CreateReferenceFromBRepName(strBrep, cylindricalFace.Parent)

' Check if the reference was created successfully
If reference1 Is Nothing Then
MsgBox "Reference could not be created. Please check the cylindrical face."
Exit Sub
End If

' Create an axis line based on the cylindrical face reference
Set hybridShapeFactory1 = part.HybridShapeFactory
Set hybridShapeAxisLine1 = hybridShapeFactory1.AddNewAxisLine(reference1)

' Set the axis line type (1 for default axis line)
hybridShapeAxisLine1.AxisLineType = 1

' Append the axis line to the hybrid body
hybridBody1.AppendHybridShape hybridShapeAxisLine1

' Declare references for start and end points
Dim referenceStart As Reference
Dim referenceEnd As Reference
Dim hybridShapePointCenterStart As HybridShapePointCenter
Dim hybridShapePointCenterEnd As HybridShapePointCenter

' Create a reference for the start edge
Set referenceStart = part.CreateReferenceFromBRepName(GetBrep(cylindricalFace.Name & "StartEdge"), cylindricalFace.Parent)

' If the start reference is valid, create a center point
If Not referenceStart Is Nothing Then
Set hybridShapePointCenterStart = hybridShapeFactory1.AddNewPointCenter(referenceStart)
hybridBody1.AppendHybridShape hybridShapePointCenterStart
Else
MsgBox "Start reference could not be created."
Exit Sub
End If

' Create a reference for the end edge
Set referenceEnd = part.CreateReferenceFromBRepName(GetBrep(cylindricalFace.Name & "EndEdge"), cylindricalFace.Parent)

' If the end reference is valid, create a center point
If Not referenceEnd Is Nothing Then
Set hybridShapePointCenterEnd = hybridShapeFactory1.AddNewPointCenter(referenceEnd)
hybridBody1.AppendHybridShape hybridShapePointCenterEnd
Else
MsgBox "End reference could not be created."
Exit Sub
End If

' Set the active object to the start center point
part.InWorkObject = hybridShapePointCenterStart

' Update the part document to reflect changes
partDocument.Part.Update
Else
MsgBox "Selection was canceled."
End If
End Sub
 
Replies continue below

Recommended for you

From the selected face you could create a solid with "Close Surface" and then do an intersect between that and your Axis
 
Code:
' Function to construct a BRep name for a given geometric element
Public Function GetBrep(MyBRepName As String) As String
' Remove "Selection_" prefix from the name
MyBRepName = Replace(MyBRepName, "Selection_", "")
' Get the part of the name before the closing parentheses
MyBRepName = Left(MyBRepName, InStrRev(MyBRepName, "));"))
' Append necessary parameters for the BRep reference
MyBRepName = MyBRepName & ");WithPermanentBody;WithoutBuildError;WithSelectingFeatureSupport;MFBRepVersion_CXR15)"
' Return the constructed BRep name
GetBrep = MyBRepName
End Function

' Main subroutine to select a cylindrical face and create an axis line
Sub CATMain() 'SelectCylindricalFaceAndCreateAxis()
' Declare necessary variables
Dim objSel
Dim strReturn As String
Dim varFilter(0) As Variant
Dim cylindricalFace As Face
Dim partDocument As partDocument
Set partDocument = CATIA.ActiveDocument
Dim part1 As part
Set part1 = partDocument.part
Dim hybridBody1 As HybridBody
Dim hybridShapeFactory1 As HybridShapeFactory
Set hybridShapeFactory1 = part1.HybridShapeFactory
Dim shapeFactory1 As ShapeFactory
Set shapeFactory1 = part1.ShapeFactory
Dim hybridShapeAxisLine1 As HybridShapeAxisLine
Dim reference1 As Reference
Dim strBrep As String

    ' Get the active document selection
    Set objSel = CATIA.ActiveDocument.Selection
    objSel.Clear
    ' Display a message box to instruct the user
    MsgBox "Select the first cylindrical face of the tube."
    
    ' Set the selection filter to only allow face selections
    varFilter(0) = "Face"
    ' Prompt the user to select a cylindrical face
    strReturn = objSel.SelectElement2(varFilter, "Select a cylindrical face...", False)
            
    ' Check if the user canceled the selection
    If strReturn <> "Cancel" Then
    ' Store the selected cylindrical face
    Set cylindricalFace = objSel.item(1).Value
    
    ' Create a new hybrid body for the axis line
    Set hybridBody1 = part1.HybridBodies.Add()
    hybridBody1.name = "CenterLine"
    
    ' Create a reference from the selected cylindrical face
    strBrep = GetBrep(cylindricalFace.name)
    Set reference1 = part1.CreateReferenceFromBRepName(strBrep, cylindricalFace.Parent)
    
    ' Check if the reference was created successfully
    If reference1 Is Nothing Then
    MsgBox "Reference could not be created. Please check the cylindrical face."
    Exit Sub
    End If
    
    ' Create an axis line based on the cylindrical face reference
    Set hybridShapeAxisLine1 = hybridShapeFactory1.AddNewAxisLine(reference1)
    ' Set the axis line type (1 for default axis line)
    hybridShapeAxisLine1.AxisLineType = 1
    ' Append the axis line to the hybrid body
    hybridBody1.AppendHybridShape hybridShapeAxisLine1
    part1.UpdateObject hybridShapeAxisLine1
    
    Dim hybridShapeExtract1 As HybridShapeExtract
    Set hybridShapeExtract1 = hybridShapeFactory1.AddNewExtract(reference1)
    
    hybridShapeExtract1.PropagationType = 3
    hybridShapeExtract1.ComplementaryExtract = False
    hybridShapeExtract1.IsFederated = False
    hybridBody1.AppendHybridShape hybridShapeExtract1
    part1.UpdateObject hybridShapeExtract1
    
    Dim reference2 As Reference
    Set reference2 = part1.CreateReferenceFromName("")
    
    part1.InWorkObject = part1.MainBody
    Dim closeSurface1 As CloseSurface
    Set closeSurface1 = shapeFactory1.AddNewCloseSurface(reference2)
    
    Dim reference3 As Reference
    Set reference3 = part1.CreateReferenceFromObject(hybridShapeExtract1)
    closeSurface1.Surface = reference3
    
    Dim hybridShapeIntersection1 As HybridShapeIntersection
    Set hybridShapeIntersection1 = hybridShapeFactory1.AddNewIntersection(closeSurface1, hybridShapeAxisLine1)
    hybridShapeIntersection1.PointType = 0
    hybridBody1.AppendHybridShape hybridShapeIntersection1
    part1.UpdateObject hybridShapeIntersection1
Else
    MsgBox "Selection was canceled."
End If
End Sub
 

Part and Inventory Search

Sponsor