Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

Using AI for FS generation.. and failing

fnxffnxf Member, User Group Leader Posts: 115 PRO
edited February 24 in FeatureScript
So yes, I'm no coder, but have been prompting ChatGPT to see if it can write me some code. I find the conversational style very engaging.
Here's my current non-working result:

https://cad.onshape.com/documents/ae1931dd79abf7662aea410a/w/7528de4b57e62d87e25e4f0c/e/aa2e0934fc6a93830a0bf384
FeatureScript 1963;
import(path : "onshape/std/geometry.fs", version : "1963.0");
import(path : "onshape/std/common.fs", version : "1963.0");

annotation { "Feature Type Name" : "Zugdreieck" }
export const myFeature = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {
        annotation { "Name" : "Base Curve", "Filter" : EntityType.EDGE_OR_SKETCH_CURVE }
        definition.baseCurve is Query;

        annotation { "Name" : "Extrude Height" }
        definition.extrudeHeight is value with unitType(distance);
    }
    {
        // Get the curve from the query
        const curve = definition.baseCurve;
        var curveGeometry;
        if (isEdge(curve))
        {
            curveGeometry = evEdgeGeometry(context, {"edge" : curve});
        }
        else if (isSketchCurve(curve))
        {
            curveGeometry = evSketchCurveGeometry(context, {"sketchCurve" : curve});
        }
        else
        {
            throw regenError("The selected entity is not a valid curve");
        }

        // Get the length of the curve
        const b = length(curveGeometry);

        // Define the variables needed for the formula
        const h = definition.extrudeHeight.getValue() / 2.0;
        const l = sqrt(b * b / 2.0);

        // Create a sketch on the plane of the curve
        const sketch1 = newSketchOnPlane(context, id + "sketch1", {
            "sketchPlane" : evPlane(context, {"curve" : curve})
        });

        // Draw the three lines using the formula
        const line1 = sketch1.lineTo(point(b, 0));
        const line2 = sketch1.lineTo(point(b / 2.0, l));
        const line3 = sketch1.lineTo(point(0, 0));

        // Close the sketch
        sketch1.closePath();

        // Extrude the sketch
        const extrude1 = newLinearExtrude(context, id + "extrude1", {
            "entities" : sketch1.id,
            "direction" : vector(0, 0, 1),
            "distance" : definition.extrudeHeight
        });

        // Return the extruded solid
        return extrude1;
    });
Apparently there's an issue on line 13.. And I got the AI stumped as well ;-) because it keeps presenting "solutions" which give more errors ..
This was fun, and I wanted to share that :)

Comments

  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 4,796
    It's not bad, but it's making up functions as it goes along...
    Director, Technical Services, EMEA 
  • Urs_Egger_OptrelUrs_Egger_Optrel Member Posts: 7 PRO
    That's a great idea. Seems like I should dive into this AI coding stuff
  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 1,944 PRO
    edited February 24
    Could you ask it to finish your project while out eating lunch?  B)
  • S1monS1mon Member Posts: 1,710 PRO
    Yeah, I tried this earlier, but found that ChatGPT doesn't seem to know much about Featurescript. I would love to have some help from a tool which ingested all the public Featurescripts and documentation. I'm sure it would make some weird mistakes and blatantly make stuff up, but it continues to be amazing how it can be successful in some contexts.
  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 1,944 PRO
    edited February 26


    There's a node module for talking to chatgpt. It wouldn't be hard to stuff it full of feature scripts. 

    I guess someone should ask OS 1st & then how would you do it?

    Is it "hey you this is a feature script" and then list the script in the dialog?


  • fnxffnxf Member, User Group Leader Posts: 115 PRO
    billy2 said:
    Could you ask it to finish your project while out eating lunch?  B)
    I will try that next time! But see what kind of prompts I was using, and now imagine that instead of just making things up, as @NeilCooke noticed, it would actually know FS?







  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 1,944 PRO
    Ok, the api is now available. 




    chatgpt api

    Are there any other Node guys out there?


Sign In or Register to comment.