Tuesday, April 17, 2012

Make spheres in CATIA with VBA

How to create spheres in CATIA with a VBA macro

One of the most asked questions of me is how do you make a sphere in CATIA V5? There are multiple methods, of course, but today I am going to share with you a method to create spheres with a VB CATScript. This will be a good tutorial for those who want to learn CATIA V5 macros. First, create a geometrical set containing nothing but a point out in space. This macro will make a new sphere at the point at a radius of a size you specify via an input box. Below is the CATScript for CATIA example. I've inserted a few screenshots along the way to help you understand script what does what.
(I use an underscore anytime a line of code gets too long and runs onto a second line but please note these need to be deleted and the code restored to one line in order for the macro to work).

Sub CATMain() 

Msgbox "Select the geometrical set containing the points to create spheres" 

Dim InputObjectType(0),PointIndex,PointReference,HybridShapeSymmetry

Set Document = CATIA.ActiveDocument : Set Part = Document.Part : Set Selection = _Document.Selection

Set HybridShapeFactory = Part.HybridShapeFactory
Status=Selection.SelectElement3(InputObjectType,"Select the geometrical set containing points",_

if (Status = "Cancel") then Exit Sub
set hybridbody1 = Selection.Item(1).Value

Dim Dia As String
Dim DiaInch As Integer
Dia = InputBox("What size radius? - Spheres will have radius in mm") 

DiaMm = Dia
'~ DiaInch = Dia * 25.4
Dim hybridShapes1 As HybridShapes
Set hybridShapes1 = hybridBody1.HybridShapes

For i=1 to hybridShapes1.Count
 Dim hybridShapePointCoord1 As HybridShape
 Set hybridShapePointCoord1 = hybridShapes1.Item(i)
 Dim reference1 As Reference
 Set reference1 = part.CreateReferenceFromObject(hybridShapePointCoord1)
 Dim hybridShapeFactory1 As Factory
 Set hybridShapeFactory1 = part.HybridShapeFactory
 Dim hybridShapeSphere1 As HybridShapeSphere
 Set hybridShapeSphere1 = hybridShapeFactory1.AddNewSphere(reference1, Nothing, DiaMm,_-45.000000, 45.000000, 0.000000, 180.000000) 
hybridShapeSphere1.Limitation = 1
hybridBody1.AppendHybridShape hybridShapeSphere1 

Next ‘i
End Sub

Notice in the above code where you can change the end angles if you don't want a complete sphere. After running the macro you will have a 3D surface in the shape of a sphere. Next, you can go into the Part Design workbench and do a “close surface” on the sphere geometry to create a solid part body. And now that we have our geometry we can learn how to shell the sphere if needed.

Learn how to write more CATIA catscript macros. 


  1. I tried the above code. it's not working

    1. Why not? Are you getting an error? Did you try as Catscript or ms vbscript?

  2. Thanks. I have learned a lot from your code.

  3. No problem. Glad to have helped.

  4. This helps me get an understanding of basic scripting, but unfortunately, it doesn't work for me. I tried copy/pasting the code verbatim to see if it was an error in my typing. The line Dim Dia As String is giving me a compilation error. Description: Expected end of statement. I'm writing in the Catscript window as opposed to the VBA extension. Any ideas what's wrong?

    1. Dim Dia As String is causing a problem? Hmm that doesn't make any sense. Try just Dim Dia and delete the As String.

    2. Thanks, that ended up fixing that problem. I had to delete all the "as 'x'" functions in order to get any further. It works great now! One minor issue though, there needs to be a space between DimDocument. This could always be a display issue on my laptop at work, but this was a whim attempt at a fix for me that happened to work. Thanks for the help!

  5. I have related question :
    if i now split this sphere can i add an "Element to keep" in the code ?

    here is the code i used
    sometimes it keeps the top / sometimes it keeps the bottom

    Dim reference12 As Reference
    Set reference12 = Part.CreateReferenceFromObject(closeSurface1)

    Dim reference13 As Reference
    Set reference13 = Part.CreateReferenceFromObject(hybridShapeTranslate1)

    Dim hybridShapeSplit1 As HybridShapeSplit
    Set hybridShapeSplit1 = hybridShapeFactory1.AddNewHybridSplit(reference12, reference13, 1)

    hybridShapeFactory1.GSMVisibility reference12, 0

    hybridShapeSplit1.VolumeResult = 1

    hybridBody1.AppendHybridShape hybridShapeSplit1

    Part.InWorkObject = hybridShapeSplit1

  6. This comment has been removed by the author.

  7. Simple is better :)

    Sub CATMain()

    Set Document1 = CATIA.ActiveDocument
    Set Part1 = Document1.Part

    Set HSF = Part1.HybridShapeFactory
    Set GSet = Part1.InWorkObject
    Set HShapes = GSet.HybridShapes

    For i = 1 to HShapes.Count

    Set NewSphere = HSF.AddNewSphere(HShapes.Item(i), Nothing, 10, -90, 90, 0, 360)

    GSet.AppendHybridShape NewSphere



    End Sub


I'd love to hear from you!