What's new

Using ChatGPT to help with scripting - second moments of inertia

oilman

Member
I've been messing around with ChatGPT-4 quite a bit and for giggles I tried plugging in a script for calculating second moments of area from Stefan (idslk) to add functionality to calculate the moment of inertia about the y-axis (iyy) and the product moment of inertia (ixy). It spit out the code below.

Has anyone else try ChatGPT to help with scripting? Was this too easy or am I missing something? :p

To test this script, simply replace the path for the AlibreX.dll with the path of the file where it's located on your PC.

Code:
# Area moment of inertia calculation 
import clr
clr.AddReferenceToFileAndPath(r'C:\Program Files\Alibre Design 26.0.0.26040\Program\AlibreX.dll')
from AlibreX import ADGeometryType
from System import Enum

figuretype = []

# Get a figuretype list
for i in System.Enum.GetValues(ADGeometryType):
    figuretype.append(i)

stringlist = []
enviroment = CurrentPart()
env_obj = enviroment._Part

# Get a list of used sketches
for i in range(env_obj.Sketches.Count):
    stringlist.append(env_obj.Sketches.Item(i).Name)

# Create a selection window for the sketches
win = Windows()
values = []
options = []
options.append(['Choose a Sketch', WindowsInputTypes.StringList, stringlist])
values = win.OptionsDialog('Only a Test. Use at your own risk!!', options, 200)

if values == None:
    print 'Canceled... Exiting.'
    sys.exit()

sketchnr = values[0]
sketchname = env_obj.Sketches.Item(sketchnr).Name
skizze = enviroment.GetSketch(sketchname)

# Coordinates/Points
xc = []
yc = []

for x in range(env_obj.Sketches.Item(sketchnr).Figures.Count):
    if env_obj.Sketches.Item(sketchnr).Figures.Item(x).IsReference == 0:
        if env_obj.Sketches.Item(sketchnr).Figures.Item(x).FigureType == figuretype[0]:
            sp = skizze.Figures[x].StartPoint
            xc.append(sp[0])
            yc.append(sp[1])

xc.append(xc[0])
yc.append(yc[0])

# Variable for area moment of inertia
Ixx = 0
Iyy = 0
Ixy = 0

# Number of points
i = len(xc)

# Calculation
for loop in range(i - 1):
    a = float(xc[loop] * yc[loop + 1] - xc[loop + 1] * yc[loop])
    
    Ixxa = (yc[loop] ** 2 + yc[loop] * yc[loop + 1] + yc[loop + 1] ** 2) * a
    Ixx += Ixxa
    
    Iyya = (xc[loop] ** 2 + xc[loop] * xc[loop + 1] + xc[loop + 1] ** 2) * a
    Iyy += Iyya
    
    Ixya = (xc[loop] * yc[loop] + xc[loop] * yc[loop + 1] + xc[loop + 1] * yc[loop] + xc[loop + 1] * yc[loop + 1]) * a
    Ixy += Ixya

win.InfoDialog('Sample script – please verify results\n' +
               'The Ixx for ' + sketchname + ' : ' + str((1.0 / 12.0) * Ixx) + 'mm^4\n' +
               'The Iyy for ' + sketchname + ' : ' + str((1.0 / 12.0) * Iyy) + 'mm^4\n' +
               'The Ixy for ' + sketchname + ' : ' + str((1.0 / 24.0) * Ixy) + 'mm^4',
               'Sample script for area moment of inertia ')

print 'Ixx:', (1.0 / 12.0) * Ixx
print 'Iyy:', (1.0 / 12.0) * Iyy
print 'Ixy:', (1.0 / 24.0) * Ixy
 

NateLiquidGravity

Alibre Super User
I've played around with it a bit. I've found it's code helpful but also often inaccurate. It could give me code that looked like it could be right or was a high quality pseudocode. If I saw something wrong I could tell it and it would offer up something else. The problem though is if you don't know what a result should be then you can't tell if it has something wrong.
 

bolsover

Senior Member
I have asked ChatGPT to generate code (C#) for involute gear geometries. The results are interesting and occasionally useful. However the devil is in the detail and to date, non of the ChatGPT code has actually made the cut. Like @NateLiqGrav suggests, if you don't know what the result should be...

David
 

oilman

Member
are you guys using GPT-3 or GPT-4?

i found GPT-3 to be underwhelming. it didn't really get interesting until i started plugging stuff into GPT-4 :cool:
 
Top