What's new

AlibreScript Code Assistant - Custom GPT

stepalibre

Senior Member
First script generated by ChatGPT that worked without needing cleanup:
1700448966976.png
Python:
from AlibreScript import *

def create_base_plate_with_holes():
    # Initialize a part session
    part = CurrentPart()

    # Define dimensions for the base plate and holes
    plate_length = 100.0  # Length of the base plate
    plate_width = 50.0    # Width of the base plate
    plate_thickness = 5.0 # Thickness of the base plate
    hole_diameter = 5.0   # Diameter of the holes
    hole_offset = 10.0    # Offset of the holes from the edges

    # Create a base plate
    base_sketch = part.AddSketch('BasePlateSketch', part.XYPlane)
    base_sketch.AddRectangle(-plate_width / 2, -plate_length / 2, plate_width / 2, plate_length / 2, False)
    part.AddExtrudeBoss('BasePlate', base_sketch, plate_thickness, False, part.EndCondition.ToDepth, None, 0, part.DirectionType.Normal, None, 0, False)

    # Create holes
    for x in [-plate_width / 2 + hole_offset, plate_width / 2 - hole_offset]:
        for y in [-plate_length / 2 + hole_offset, plate_length / 2 - hole_offset]:
            hole_sketch = part.AddSketch('HoleSketch', part.XYPlane)
            hole_sketch.AddCircle(x, y, hole_diameter / 2, False)
            part.AddExtrudeCut('ThroughHole', hole_sketch, 0, False, part.EndCondition.ThroughAll, None, 0, part.DirectionType.Normal, None, 0, False)

    # Save the part
    #part.Save()

# Run the function to create a simple base plate with four holes
create_base_plate_with_holes()
The prompt/input was [ make a base plate with 4 holes ].
Follow this work here for example scripts and other resources:
 
Last edited:

stepalibre

Senior Member
I did many tests, and the beta is buggy. I think this is the actual chat. I manually deleted the formatted strings and commented out part.Save()

1700531422132.png

I added input/prompt instructions in the GPT for formatted strings and related things to exclude. You can also tell ChatGPT to remove them during a conversation.
 
Last edited:

stepalibre

Senior Member
[ EDIT ]
Here are some examples testing the GPT's ability to generate Alibre Script API code only, without verifying 3D model output or code correctness.

how to add a chamfer to an edge?
Python:
import math
from AlibreScript import *

# Initialize the part session
part = CurrentPart()

# Assuming you have a part with an edge to chamfer.
# The edge can be obtained in various ways, depending on the context.
# Here's a generic way to get an edge, replace this with your specific method.
edges = part.GetEdges()
edge_to_chamfer = edges[0]  # Assuming you want to chamfer the first edge in the list

# Define chamfer parameters
distance1 = 1  # The first chamfer distance
distance2 = 1  # The second chamfer distance (for unequal chamfer, otherwise set same as distance1)

# Add chamfer to the selected edge
part.AddChamfer("ChamferName", edge_to_chamfer, distance1, distance2, False)

# Regenerate the part to update the view
part.Regenerate()
basic hex head bolt code
Python:
import math
from AlibreScript import *

# Initialize the part session
part = CurrentPart()

# Define the dimensions of the bolt
hex_head_diameter = 10  # Diameter of the hex head
hex_head_height = 5     # Height of the hex head
shaft_diameter = 5      # Diameter of the shaft
shaft_length = 20       # Length of the shaft

# Create a hexagon sketch for the bolt head
hex_sketch = part.AddSketch("HexHeadSketch", part.XYPlane)
for i in range(6):
    angle1 = math.radians(60 * i)
    angle2 = math.radians(60 * (i + 1))
    x1 = hex_head_diameter / 2 * math.cos(angle1)
    y1 = hex_head_diameter / 2 * math.sin(angle1)
    x2 = hex_head_diameter / 2 * math.cos(angle2)
    y2 = hex_head_diameter / 2 * math.sin(angle2)
    hex_sketch.AddLine([x1, y1], [x2, y2], False)

# Extrude the hex head
hex_head = part.AddExtrudeBoss("HexHead", hex_sketch, hex_head_height, False)

# Create a circle sketch for the shaft
shaft_sketch = part.AddSketch("ShaftSketch", part.XYPlane)
shaft_sketch.AddCircle(0, 0, shaft_diameter / 2, False)

# Extrude the shaft
shaft = part.AddExtrudeBoss("Shaft", shaft_sketch, shaft_length, False)

# Regenerate the part to update the view
part.Regenerate()
give me an example using this method with dummy data:
Method->AddSweepBoss(String Name, Sketch ProfileSketch, ISweepPath PathSketch, Boolean IsRigid, EndCondition EndCondition, ISketchSurface EndPlane, Double EndOffset, Double DraftAngle, Boolean OutwardDraft)

Python:
import math
from AlibreScript import *

# Initialize the part session
part = CurrentPart()

# Fetch the standard planes
xy_plane = part.GetPlane("XY-Plane")
zx_plane = part.GetPlane("ZX-Plane")

# Create a profile sketch on the XY plane
profile_sketch = part.AddSketch("ProfileSketch", xy_plane)

# Add a circle to the profile sketch as the sweep profile
profile_sketch.AddCircle(0, 0, 2, False)  # Center at (0,0), Radius = 2

# Create a path sketch on the ZX plane
path_sketch = part.AddSketch("PathSketch", zx_plane)

# Add a line to the path sketch as the sweep path
path_sketch.AddLine([0, 0], [10, 10], False)  # Line from (0,0) to (10,10)

# Define parameters for the sweep boss
is_rigid = False
end_condition = part.EndCondition.EntirePath  # Using EntirePath as the end condition
end_plane = None
end_offset = 0
draft_angle = 0  # No draft angle
outward_draft = False  # No outward draft

# Add the sweep boss feature
part.AddSweepBoss("SweepBossFeature", profile_sketch, path_sketch, is_rigid, end_condition, end_plane, end_offset, draft_angle, outward_draft)

# Regenerate the part to update the view
part.Regenerate()
10 pipe flange model
Python:
import math
from AlibreScript import *
# Initialize the part session
part = CurrentPart()
# Flange dimensions (in mm)
flangeDiameter = 565  # Flange outer diameter
flangeThickness = 30   # Flange thickness
pipeDiameter = 254     # 10 inches converted to mm for pipe
boltHoleDiameter = 40  # Bolt hole diameter
boltCircleDiameter = 175  # Bolt circle diameter
numberOfBoltHoles = 12
# Create main flange disc
flangeSketch = part.AddSketch('FlangeBase', part.XYPlane)
flangeSketch.StartEditing()
flangeSketch.AddCircle(0, 0, flangeDiameter / 2, False)  # Center at (0,0)
flangeSketch.StopEditing()
flange = part.AddExtrudeBoss('FlangeDisc', flangeSketch, flangeThickness, False)
# Create bolt holes
for i in range(numberOfBoltHoles):
    angle = 2 * math.pi * i / numberOfBoltHoles
    x = boltCircleDiameter / 2 * math.cos(angle)
    y = boltCircleDiameter / 2 * math.sin(angle)
    boltHoleSketch = part.AddSketch('BoltHole', part.XYPlane)
    boltHoleSketch.StartEditing()
    boltHoleSketch.AddCircle(x, y, boltHoleDiameter / 2, False)
    boltHoleSketch.StopEditing()
    part.AddExtrudeCut('BoltHoleCut', boltHoleSketch, flangeThickness, False)
# Create center pipe hole
centerHoleSketch = part.AddSketch('CenterPipeHole', part.XYPlane)
centerHoleSketch.StartEditing()
centerHoleSketch.AddCircle(0, 0, pipeDiameter / 2, False)
centerHoleSketch.StopEditing()
part.AddExtrudeCut('CenterHoleCut', centerHoleSketch, flangeThickness, False)
print("10-inch pipe flange model created.")
complete hex head nut code
Python:
import math
from AlibreScript import *
# Initialize the part session
part = CurrentPart()
# Nut dimensions (in mm)
threadDiameter = 10  # M10 thread
pitch = 1.5          # Typical pitch for M10
acrossFlats = 17     # Width across flats for M10
thickness = 8        # Nut thickness for M10
cornerRadius = 1     # Corner radius
# Number of sides for the hexagon
numSides = 6
# Create the base hexagonal profile
hexSketch = part.AddSketch('HexBase', part.XYPlane)
hexSketch.StartEditing()
# Calculate vertices for the hexagon
vertices = []
for i in range(numSides + 1):  # +1 to close the hexagon
    angle = 2 * math.pi * i / numSides
    x = acrossFlats / (2 * math.cos(math.pi / numSides)) * math.cos(angle)
    y = acrossFlats / (2 * math.cos(math.pi / numSides)) * math.sin(angle)
    vertices.append([x, y])
# Add lines to create the hexagon
for i in range(numSides):
    startX, startY = vertices[i]
    endX, endY = vertices[i + 1]
    hexSketch.AddLine(startX, startY, endX, endY, False)  # Adding line using coordinates
hexSketch.StopEditing()
# Extrude the hexagon to form the nut body
nutBody = part.AddExtrudeBoss('NutBody', hexSketch, thickness, False)
# Create internal thread hole
threadHoleSketch = part.AddSketch('ThreadHole', part.XYPlane)
threadHoleSketch.StartEditing()
threadHoleSketch.AddCircle(0, 0, threadDiameter / 2, False)
threadHoleSketch.StopEditing()
part.AddExtrudeCut('ThreadHoleCut', threadHoleSketch, thickness, False)

print("M10 hex nut model created.")
 
Last edited:

NateLiquidGravity

Alibre Super User
Might want to check those again. The results are not all matching the input numbers.

Most importantly the top of each script with any math it should include from __future__ import division to fix integer division in ironpython 2.7 or for example 5/2 will equal 2 instead of 2.5

In the first one hole_sketch.AddCircle(x, y, hole_diameter / 2, False) should be hole_sketch.AddCircle(x, y, hole_diameter, False) as the AddCircle expects a diameter not a radius.

Basic hex head bolt code has the same AddCircle and division problem and also since both extrusions are going the same direction from the same face the bolt shaft ends up being too short.

AddSweepBoss has same AddCircle issues. I think I found a bug in AlibreScript when testing a draft angle - it converts it to radians when it should not (or maybe twice?).

10 pipe flange model has the same AddCircle and division problems and bogus flange data - boltCircleDiameter can not be smaller than pipeDiameter or the bolt holes all get cut off.
I'd suggest adding all holes to one sketch and doing a single extrude cut to keep the feature history shorter. Hopefully Alibre will add the ability to add patterns using API / AlibreScript soon.

The M10 hex nut has the same AddCircle and division problems. The pitch and cornerRadius variables are not used.

I'd also suggest also including Units.Current = UnitTypes.Millimeters
or Units.Current = UnitTypes.Centimeters
or Units.Current = UnitTypes.Inches
so users can easily see a default and where to change it.
 

stepalibre

Senior Member
The goal right now is to get it to output the correct API, not design and logic correctness. All I'm focusing on right now is making sure It generates the right code from Alibre Script API. In other words I want the code to run, not checking for anything else. This is step 1. I have no rules regrading coding style, best practices, program logic or any details.

I give it an input/prompt and it is now able to generate real Alibre Script API code. The next step would be structure and rules to control the order of the code. I only gave the input you see there. I didn't give any other instructions I prompted it with "complete hex head nut code". And it generated that code, more information could be given in the conversation or in the GPT. I give it a wide range of input/prompt lengths, more or less descriptive and specific with details to test whether it would still provide good Alibre Script API code. In general GPTs only need a single sentence or a few words to understand what you're asking.

Your comments can be added as rules. If you or anyone see more in the other examples let me know. Make a list of rules for Alibre Script coding if you have time it would help.

I've only spent a few days on this and the beta is painfully slow. I hope it'll get faster soon. I don't know how long the beta will last.
 
Last edited:
Top