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?
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.
Has anyone else try ChatGPT to help with scripting? Was this too easy or am I missing something?
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