Hello everyone
Im very new to the CATIA software and i was asked to make a VBA macro that when the user runs this macro it will automatically insert the biggest possible blue sphere on the model of the part that was selected by the user
This is the code i got from the internet but it causes errors and i haven't been able to fix it
If anyone could help out in anyway i would appreciate it alot,thanks for reading!
Sub InsertBiggestSphere()<br /> Dim partDocument As partDocument<br /> Set partDocument = CATIA.ActiveDocument<br /><br /> Dim part As part<br /> Set part = partDocument.part<br /><br /> ' Call function to find the biggest possible sphere<br /> Dim center As Point, radius As Double<br /> FindBiggestSphere part, center, radius<br /><br /> ' Create the sphere<br /> CreateSphere part, center, radius<br />End Sub<br /><br />Sub FindBiggestSphere(part As part, ByRef center As Point, ByRef radius As Double)<br /> ' Implement your algorithm to find the biggest possible sphere<br /> ' This may involve traversing the part's geometry and analyzing the dimensions<br /> <br /> ' For example, you can loop through all faces in the part to find the maximum<br /> ' distance between any two points. The center of the sphere will be the midpoint<br /> ' of the line connecting these two points, and the radius will be half of the distance.<br /> <br /> Dim bodies As bodies<br /> Set bodies = part.bodies<br /> Dim hybridBody As Body<br /> Set hybridBody = bodies.Item(1) ' Assuming the first body is the one containing your faces<br /> <br /> Dim hybridShapes As hybridShapes<br /> Set hybridShapes = hybridBody.hybridShapes<br /> <br /> Dim maxDistance As Double<br /> Dim face1 As HybridShapeFaceExplicit<br /> Dim face2 As HybridShapeFaceExplicit<br /> <br /> For Each face1 In hybridShapes<br /> If TypeOf face1 Is HybridShapeFaceExplicit Then ' Check if it's a face<br /> For Each face2 In hybridShapes<br /> If TypeOf face2 Is HybridShapeFaceExplicit Then ' Check if it's a face<br /> If face1 <> face2 Then<br /> Distance = CalculateDistance(face1.StartPoint, face2.EndPoint)<br /> If Distance > maxDistance Then<br /> maxDistance = Distance<br /> center = CalculateMidPoint(face1.StartPoint, face2.EndPoint)<br /> radius = Distance / 2<br /> End If<br /> End If<br /> End If<br /> Next face2<br /> End If<br /> Next face1<br />End Sub<br /><br />Sub CreateSphere(part As part, center As Point, ByVal radius As Double)<br /> Dim hybridShapeFactory As hybridShapeFactory<br /> Set hybridShapeFactory = part.hybridShapeFactory<br /><br /> ' Create the sphere<br /> Dim sphere As HybridShapeSphere<br /> Set sphere = hybridShapeFactory.AddNewSphere(center, radius)<br /><br /> ' Update the part<br /> part.update<br />End Sub<br /><br />Function CalculateDistance(p1 As Point, p2 As Point) As Double<br /> Dim deltaX As Double<br /> Dim deltaY As Double<br /> Dim deltaZ As Double<br /> <br /> deltaX = p1.X - p2.X<br /> deltaY = p1.Y - p2.Y<br /> deltaZ = p1.Z - p2.Z<br /> <br /> CalculateDistance = Sqr(deltaX ^ 2 + deltaY ^ 2 + deltaZ ^ 2)<br />End Function<br /><br />Function CalculateMidPoint(p1 As Point, p2 As Point) As Point<br /> Dim midPoint As Point<br /> midPoint.X = (p1.X + p2.X) / 2<br /> midPoint.Y = (p1.Y + p2.Y) / 2<br /> midPoint.Z = (p1.Z + p2.Z) / 2<br /> <br /> Set CalculateMidPoint = midPoint<br />End Function<br />
