Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

How to Define Circle Edges for Points in CATIA VBA?

wealnut

Automotive
Oct 26, 2024
1
0
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
 
Back
Top