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.

future script help - beginner

rlbrkrlbrk Member Posts: 19
I asked Chat gpt to generate me a future script code for a gridfinity bin. 
I tried to import the code and I got errors. I watched some tutorials on how to use it but I didn't manage to make it work.
how should I paste it correctly?
here is the code: 

FeatureScript 1200;
import(path : "onshape/std/geometry.fs", version : "1200.0");

annotation { "Feature Type Name" : "Gridfinity Bin" }
export const gridfinityBin = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {
        annotation { "Name" : "Width", "Default" : 42 * millimeter }
        definition.width is Length;
        
        annotation { "Name" : "Depth", "Default" : 42 * millimeter }
        definition.depth is Length;
        
        annotation { "Name" : "Height", "Default" : 7 * millimeter }
        definition.height is Length;
        
        annotation { "Name" : "Wall Thickness", "Default" : 2 * millimeter }
        definition.wallThickness is Length;
    }
    {
        // Main body of the bin
        var binProfile = rectangleSketch(context, id + "binProfile", {
            "cornerA" : vector(0, 0) * millimeter,
            "cornerB" : vector(definition.width, definition.depth)
        });

        var binBody = extrude(context, id + "binBody", {
            "entities" : qSketchRegion(id + "binProfile"),
            "direction" : evOwnerSketchPlane(context, {
                "entity" : qSketchRegion(id + "binProfile")
            }).normal,
            "endBound" : BoundingType.BLIND,
            "endDepth" : definition.height
        });

        // Hollow out the bin
        var hollowProfile = rectangleSketch(context, id + "hollowProfile", {
            "cornerA" : vector(definition.wallThickness, definition.wallThickness),
            "cornerB" : vector(definition.width - definition.wallThickness, definition.depth - definition.wallThickness)
        });

        var hollowBody = extrude(context, id + "hollowBody", {
            "entities" : qSketchRegion(id + "hollowProfile"),
            "direction" : evOwnerSketchPlane(context, {
                "entity" : qSketchRegion(id + "hollowProfile")
            }).normal,
            "endBound" : BoundingType.BLIND,
            "endDepth" : definition.height - definition.wallThickness,
            "operationType" : NewBodyOperationType.REMOVE
        });

        // Create bottom cutouts
        var cutoutDiameter = 8 * millimeter;
        var cutoutRadius = cutoutDiameter / 2;
        var cutoutSpacing = 10 * millimeter;
        var cutoutPositions = [];
        for (var x = cutoutRadius; x < definition.width; x += cutoutSpacing) {
            for (var y = cutoutRadius; y < definition.depth; y += cutoutSpacing) {
                append(cutoutPositions, vector(x, y));
            }
        }

        for (var cutoutPosition in cutoutPositions) {
            var cutoutSketch = circleSketch(context, id + "cutoutSketch" + cutoutPosition, {
                "center" : cutoutPosition,
                "radius" : cutoutRadius
            });

            var cutoutExtrude = extrude(context, id + "cutoutExtrude" + cutoutPosition, {
                "entities" : qSketchRegion(id + "cutoutSketch" + cutoutPosition),
                "direction" : evOwnerSketchPlane(context, {
                    "entity" : qSketchRegion(id + "cutoutSketch" + cutoutPosition)
                }).normal,
                "endBound" : BoundingType.BLIND,
                "endDepth" : definition.wallThickness,
                "operationType" : NewBodyOperationType.REMOVE
            });
        }
    });

function rectangleSketch(context is Context, id is Id, corners is map)
{
    var sketch = newSketch(context, id, {
        "sketchPlane" : qCreatedBy(makeId("Top"), EntityType.FACE)
    });
    skRectangle(sketch, "rectangle", {
        "cornerA" : corners.cornerA,
        "cornerB" : corners.cornerB
    });
    skSolve(sketch);
    return sketch;
}

function circleSketch(context is Context, id is Id, parameters is map)
{
    var sketch = newSketch(context, id, {
        "sketchPlane" : qCreatedBy(makeId("Top"), EntityType.FACE)
    });
    skCircle(sketch, "circle", {
        "center" : parameters.center,
        "radius" : parameters.radius
    });
    skSolve(sketch);
    return sketch;
}


Comments

  • Caden_ArmstrongCaden_Armstrong Member Posts: 195 PRO
    ChatGPT is not capable of creating a working featurescript. It only produces garbage.
    You'll have to learn how to write FeatureScript yourself. But Onshape has some great learning material to get you started:
    https://learn.onshape.com/courses/featurescript-fundamentals
    www.smartbenchsoftware.com --- fs.place --- Renaissance
    Custom FeatureScript and Onshape Integrated Applications
  • Max_KlugeMax_Kluge Member Posts: 22

    As Caden said, ChatGPT is not capable of writing code properly and the OS Learning Center may help you out pretty good. As some kind of additional help: I made a Udemy course in JavaScript (you may also do TypeScript) to really learn the basics of codeing. Otherwise you will have a hard time understanding how FS really works.

  • MichaelPascoeMichaelPascoe Member Posts: 2,012 PRO

    Haven't tried it yet because it requires a subscription I believe, but C2I made a gpt app for FeatureScript. Not sure how well it works:
    https://forum.onshape.com/discussion/22785/introducing-gpt-c2i-featurescript-helper-for-chatgpt-testers-wanted..

    Definitely complete the course Caden mentioned: FeatureScript Fundamentals

    I've also been working on another FeatureScript course that has a different teaching style. It teaches on the fly so it may be easier to follow depending on your learning style: CADSharp FeatureScript Video Lessons


    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   Learn How to FeatureScript Here 🔴
  • joshtargojoshtargo Member Posts: 269 EDU
    edited October 10

    I've had fairly good luck with GPT to help me get something 90% of the way there, but of course the last 10% can be tricky. You can look at the standard library documentation https://cad.onshape.com/FsDoc/library.html but for non-programmers like me, it's very difficult to get a grasp on what it's telling you, and what it's leaving out.

    You can also use the many examples from the standard feature doc:

    https://cad.onshape.com/documents/12312312345abcabcabcdeff/w/a855e4161c814f2e9ab3698a/e/81869edac6224be29410b2a3Or use other people's featurescripts as a starting point and slowly modify them to do what you want.

    after that, I recommend re-writing the whole working script from scratch (using the existing script to learn from) so you can try to understand what each line is doing.

  • armandoRRarmandoRR Member Posts: 18 PRO

    I created a custom GPT through ChatGPT and fed in the entire standard features in text form and the API. It was a large improvement but still resulted in what everyone is saying, 90% of it is good, 10% isn't and you need 100% for the featurescript to run correctly.

    Philosophically, ChatGPT is not a replacement for learning a programming language.

  • joshtargojoshtargo Member Posts: 269 EDU

    not a replacement, but 90% of the time, it's the patient, instantaneous teacher that I need.

Sign In or Register to comment.