CATIA Macro that inserts the biggest possible sphere inside of a model (1 Viewer)

goncalo

Member
Local time
Today, 15:02
Joined
May 23, 2023
Messages
51
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
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
 

CJ_London

Super Moderator
Staff member
Local time
Today, 15:02
Joined
Feb 19, 2013
Messages
16,614
But this code keeps on giving me errors
I doubt anyone here is familiar with CATIA but some might be able to suggest something if you provided the error messages and the line it occurs on.

I presume you have compiled the code and resolved those errors before trying to use it

Also recommend you find a CATIA forum - perhaps this one

or this one



 

Gasman

Enthusiastic Amateur
Local time
Today, 15:02
Joined
Sep 21, 2011
Messages
14,306
And you could not possibly say what those errors are? :(
 

goncalo

Member
Local time
Today, 15:02
Joined
May 23, 2023
Messages
51
the error i get when running the code occurs on this line
Code:
    Dim face1 As HybridShapeFaceExplicit

and the error is a "user-defined type not defined" error.

Ill give those forums a look and see if anything can help me out
 

Gasman

Enthusiastic Amateur
Local time
Today, 15:02
Joined
Sep 21, 2011
Messages
14,306
the error i get when running the code occurs on this line
Code:
    Dim face1 As HybridShapeFaceExplicit

and the error is a "user-defined type not defined" error.

Ill give those forums a look and see if anything can help me out
That infers you have a missing reference, so check those.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 15:02
Joined
Feb 19, 2013
Messages
16,614
Or HybridShapeFaceExplicit is spelt wrong or needs something else such as (shape is my word)

HybridShapeFaceExplicit.shape

or

shape.HybridShapeFaceExplicit
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 09:02
Joined
Feb 28, 2001
Messages
27,187
We also have to take into account that ChatGPT is noted as having created "air code" that would never run because it "makes up" functions and makes up other things that essentially do not exist, OR they exist only in some obscure product that it doesn't bother to tell you about.
 

goncalo

Member
Local time
Today, 15:02
Joined
May 23, 2023
Messages
51
We also have to take into account that ChatGPT is noted as having created "air code" that would never run because it "makes up" functions and makes up other things that essentially do not exist, OR they exist only in some obscure product that it doesn't bother to tell you about.
Yep,and that does not help me at all
If anything it makes me even more confused cuz like i said i have no idea how to use CATIA or how CATIA arguments are supposed to be made in VBA soo imaginary code doesn't really help me here XD
Ill keep on trying to search for information about this topic though
 

Gasman

Enthusiastic Amateur
Local time
Today, 15:02
Joined
Sep 21, 2011
Messages
14,306
Perhaps look in the Object Browser for a similar name?

Edit: TBH, I am unsure why you are asking all this in an Access site? I would have thought a site dedicated to that software would be more logical?
Like https://www.3dcadforums.com/forums/catia-forum.10/
 
Last edited:

goncalo

Member
Local time
Today, 15:02
Joined
May 23, 2023
Messages
51
Perhaps look in the Object Browser for a similar name?

Edit: TBH, I am unsure why you are asking all this in an Access site? I would have thought a site dedicated to that software would be more logical?
Like https://www.3dcadforums.com/forums/catia-forum.10/
well i asked it here because its been almost impossible to find any info on any other site,and since people here have been able to help me out alot with my problems thought i should try here :)

I came across that site but i dunno why it doesn't let me create an account,always says "Server error"
 

Users who are viewing this thread

Top Bottom