Hello everyone
I am in need of assistance in making a macro for CATIA V5
i do not know almost anything about this program but i was asked to make a macro in VBA that when the user runs it will insert the biggest possible sphere inside of the model that was opened,regardless of what model it was opened
Since i dont know anything about this program i tried asking chatgpt and i got this in return
	
	
	
		
But this code keeps on giving me errors and i really dont know what to do,plus the fact that there is almost no information regarding macros for CATIA on the internet does not really help me in the slightest
If anyone knows anything about this i would appreciate a little guidance
Thank you for reading
 I am in need of assistance in making a macro for CATIA V5
i do not know almost anything about this program but i was asked to make a macro in VBA that when the user runs it will insert the biggest possible sphere inside of the model that was opened,regardless of what model it was opened
Since i dont know anything about this program i tried asking chatgpt and i got this in return
		Code:
	
	
	Sub InsertBiggestSphere()
    Dim partDocument As PartDocument
    Set partDocument = CATIA.ActiveDocument
    Dim part As Part
    Set part = partDocument.Part
    ' Call function to find the biggest possible sphere
    Dim center As Point, radius As Double
    FindBiggestSphere part, center, radius
    ' Create the sphere
    CreateSphere part, center, radius
End Sub
Sub FindBiggestSphere(part As Part, ByRef center As Point, ByRef radius As Double)
    ' Implement your algorithm to find the biggest possible sphere
    ' This may involve traversing the part's geometry and analyzing the dimensions
    
    ' For example, you can loop through all faces in the part to find the maximum
    ' distance between any two points. The center of the sphere will be the midpoint
    ' of the line connecting these two points, and the radius will be half of the distance.
    
    Dim bodies As Bodies
    Set bodies = part.Bodies
    Dim hybridBody As Body
    Set hybridBody = bodies.Item(1)  ' Assuming the first body is the one containing your faces
    
    Dim hybridShapes As HybridShapes
    Set hybridShapes = hybridBody.HybridShapes
    
    Dim maxDistance As Double
    Dim face1 As HybridShapeFaceExplicit
    Dim face2 As HybridShapeFaceExplicit
    
    For Each face1 In hybridShapes
        If TypeOf face1 Is HybridShapeFaceExplicit Then ' Check if it's a face
            For Each face2 In hybridShapes
                If TypeOf face2 Is HybridShapeFaceExplicit Then ' Check if it's a face
                    If face1 <> face2 Then
                        distance = CalculateDistance(face1.StartPoint, face2.EndPoint)
                        If distance > maxDistance Then
                            maxDistance = distance
                            center = CalculateMidPoint(face1.StartPoint, face2.EndPoint)
                            radius = distance / 2
                        End If
                    End If
                End If
            Next face2
        End If
    Next face1
End Sub
Sub CreateSphere(part As Part, center As Point, ByVal radius As Double)
    Dim hybridShapeFactory As HybridShapeFactory
    Set hybridShapeFactory = part.HybridShapeFactory
    ' Create the sphere
    Dim sphere As HybridShapeSphere
    Set sphere = hybridShapeFactory.AddNewSphere(center, radius)
    ' Update the part
    part.Update
End Sub
Function CalculateDistance(p1 As Point, p2 As Point) As Double
    Dim deltaX As Double
    Dim deltaY As Double
    Dim deltaZ As Double
    
    deltaX = p1.X - p2.X
    deltaY = p1.Y - p2.Y
    deltaZ = p1.Z - p2.Z
    
    CalculateDistance = Sqr(deltaX ^ 2 + deltaY ^ 2 + deltaZ ^ 2)
End Function
Function CalculateMidPoint(p1 As Point, p2 As Point) As Point
    Dim midPoint As Point
    midPoint.X = (p1.X + p2.X) / 2
    midPoint.Y = (p1.Y + p2.Y) / 2
    midPoint.Z = (p1.Z + p2.Z) / 2
    
    Set CalculateMidPoint = midPoint
End Function
	But this code keeps on giving me errors and i really dont know what to do,plus the fact that there is almost no information regarding macros for CATIA on the internet does not really help me in the slightest
If anyone knows anything about this i would appreciate a little guidance
Thank you for reading