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.

I need a little help

hansjuerg_jenzerhansjuerg_jenzer Member Posts: 16

I have one last error in my script that I can't get rid of.
Thank you for your support
Hansjürg


CODE:
FeatureScript 2559;
import(path : "onshape/std/common.fs", version : "2559.0");

// Annotation für den Feature-Typ
annotation { "Feature Type Name" : "Grid Generator" }

// Definition der Feature-Funktion
//export const GridGenerator = defineFeature; function(context, id, definition) {
export const GridGenerator = defineFeature;function(context is Context, id is Id, definition is map) {

// Precondition für Parameterdefinitionen
precondition {
    annotation { "Name" : "Grid Spacing" }
    definition.spacing is LengthParameter({
        "default" : 10 * millimeter,
        "min" : 0.1 * millimeter,
        "max" : 100 * millimeter
    });

    annotation { "Name" : "Rows" }
    definition.rows is IntegerParameter({
        "default" : 10,
        "min" : 1,
        "max" : 100
    });

    annotation { "Name" : "Columns" }
    definition.columns is IntegerParameter({
        "default" : 10,
        "min" : 1,
        "max" : 100
    });

    annotation { "Name" : "Origin" }
    definition.origin is VectorParameter({
        "default" : vector(0, 0, 0)
    });

}

// Erstellung des Skizzenobjekts auf der definierten Ebene
const sketch = newSketchOnPlane(context, id + "sketch", {
    "plane" : plane(vector(0, 0, 0), vector(0, 0, 1))
});

// Vertikale Linien zeichnen
for (var i = 0; i <= definition.columns; i += 1) {
    skLineSegment(sketch, "vertical" ~ i, {
        "start" : definition.origin + vector(i * definition.spacing, 0, 0),
        "end" : definition.origin + vector(i * definition.spacing, definition.rows * definition.spacing, 0)
    });
}

// Horizontale Linien zeichnen
for (var j = 0; j <= definition.rows; j += 1) {
    skLineSegment(sketch, "horizontal" ~ j, {
        "start" : definition.origin + vector(0, j * definition.spacing, 0),
        "end" : definition.origin + vector(definition.columns * definition.spacing, j * definition.spacing, 0)
    });
}

// Die Skizze lösen
skSolve(sketch);

};
Fault:

extraneous input 'precondition' expecting {'annotation', '[', '+', '-', 'return', 'if', 'for', 'while', 'new', '!', '++', '--', '::', 'function', 'operator', 'undefined', 'break', 'continue', 'var', 'const', 'try', 'in', 'switch', 'throw', SEMI, '{', '(', '}', BOOLEAN, NUMBER, ID, BUILTIN, STRING}

12:5   

Feature Studio 1

Best Answer

  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,845
    edited March 27 Answer ✓

    Compare and contrast - this is why you should not use ChatGPT, it's not even close…

    FeatureScript 2615;
    import(path : "onshape/std/common.fs", version : "2615.0");
    
    annotation { "Feature Type Name" : "Grid generator", "Feature Type Description" : "" }
    export const gridGenerator = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "Grid spacing" }
            isLength(definition.spacing, { (millimeter) : [0.1, 100, 100] } as LengthBoundSpec);
    
            annotation { "Name" : "Rows" }
            isInteger(definition.rows, { (unitless) : [1, 10, 100] } as IntegerBoundSpec);
    
            annotation { "Name" : "Columns" }
            isInteger(definition.columns, { (unitless) : [1, 10, 100] } as IntegerBoundSpec);
        }
    
        {
            const sketch = newSketchOnPlane(context, id + "sketch", {
                        "sketchPlane" : plane(vector(0, 0, 0) * millimeter, vector(0, 0, 1))
                    });
    
            for (var i = 0; i < definition.columns; i += 1)
            {
                skLineSegment(sketch, "vertical" ~ i, {
                            "start" : vector(i * definition.spacing, 0 * millimeter),
                            "end" : vector(i * definition.spacing, definition.rows * definition.spacing)
                        });
            }
    
            for (var j = 0; j < definition.rows; j += 1)
            {
                skLineSegment(sketch, "horizontal" ~ j, {
                            "start" : vector(0 * millimeter, j * definition.spacing),
                            "end" : vector(definition.columns * definition.spacing, j * definition.spacing)
                        });
            }
    
            skSolve(sketch);
        });
    
    
    Senior Director, Technical Services, EMEA

Answers

  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,845

    Looks like ChatGPT to me - don't trust it.

    Should be:

    export const GridGenerator = defineFeature(function(context is Context, id is Id, definition is map) {

    Senior Director, Technical Services, EMEA
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,845

    Also, no such keywords as LengthParameter, IntegerParameter, VectorParameter

    Use the code snippets in the menu at the top of the Feature Studio for the correct syntax,

    Senior Director, Technical Services, EMEA
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,845

    Why rejected answer?

    Senior Director, Technical Services, EMEA
  • MichaelPascoeMichaelPascoe Member Posts: 2,256 PRO
    edited March 27

    The best place to learn FeatureScript is here. It isn't to hard and doesn't take too long either, you just have to watch the lessons. 😎

    Onshape's FeatureScript Fundamentals Course

    CADSharp's FeatureScript Course

    .


    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   Learn How to FeatureScript Here 🔴
  • hansjuerg_jenzerhansjuerg_jenzer Member Posts: 16

    Thank you for trying to help me.
    I am an absolute greenhorn at feature scripting and have spent hours working with Perplexity and CahtGPT to come up with the above code.
    Only the last error I can't get rid of.
    It doesn't help much if you point out the lessons I need to learn. Which ones in this specific case?
    I'm quite willing to learn something, but general hints won't get me anywhere:

    What do I want to do anyway?

    It is not possible to show a screen grid in Onshape. I would like this to give me a rough overview of the dimensions and the relationships.
    This code should show a sketch with a line grid in it. If I can also set the spacing, that would be great.

    xport const GridGenerator = defineFeature(function(context is Context, id is Id, definition is map) {

    is also wrong

  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,845

    It's not wrong, you need to close the parenthesis at the end of the code too.

    Senior Director, Technical Services, EMEA
  • MichaelPascoeMichaelPascoe Member Posts: 2,256 PRO

    Well Neil already answered the question, I was just elaborating on how to not have this issue in the future:

    Which lesson? All of them =)

    The lessons are incremental, so each one builds on the previous lesson. This means you need to start at lesson 1. They teach you the foundation of FeatureScript so that you will understand how to make all kinds of features.


    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   Learn How to FeatureScript Here 🔴
  • hansjuerg_jenzerhansjuerg_jenzer Member Posts: 16

    Hi Neil
    That doesn't really help.
    Here is the new code and the error messages
    Here s the code:
    FeatureScript 2559;
    import(path : "onshape/std/common.fs", version : "2559.0");

    // Annotation für den Feature-Typ
    annotation { "Feature Type Name" : "Grid Generator" }

    // Definition der Feature-Funktion
    //export const GridGenerator = defineFeature; function(context, id, definition) {
    export const GridGenerator = defineFeature ( function(context is Context, id is Id, definition is map) {
    // Precondition für Parameterdefinitionen
    precondition {
    annotation { "Name" : "Grid Spacing" }
    definition.spacing is LengthParameter({
    "default" : 10 * millimeter,
    "min" : 0.1 * millimeter,
    "max" : 100 * millimeter
    });

        annotation { "Name" : "Rows" }
        definition.rows is IntegerParameter({
            "default" : 10,
            "min" : 1,
            "max" : 100
        });
    
        annotation { "Name" : "Columns" }
        definition.columns is IntegerParameter({
            "default" : 10,
            "min" : 1,
            "max" : 100
        });
    
        annotation { "Name" : "Origin" }
        definition.origin is VectorParameter({
            "default" : vector(0, 0, 0)
        });
    

    }

    // Erstellung des Skizzenobjekts auf der definierten Ebene
    const sketch = newSketchOnPlane(context, id + "sketch", {
        "plane" : plane(vector(0, 0, 0), vector(0, 0, 1))
    });
    
    // Vertikale Linien zeichnen
    for (var i = 0; i <= definition.columns; i += 1) {
        skLineSegment(sketch, "vertical" ~ i, {
            "start" : definition.origin + vector(i * definition.spacing, 0, 0),
            "end" : definition.origin + vector(i * definition.spacing, definition.rows * definition.spacing, 0)
        });
    }
    
    // Horizontale Linien zeichnen
    for (var j = 0; j <= definition.rows; j += 1) {
        skLineSegment(sketch, "horizontal" ~ j, {
            "start" : definition.origin + vector(0, j * definition.spacing, 0),
            "end" : definition.origin + vector(definition.columns * definition.spacing, j * definition.spacing, 0)
        });
    }
    
    // Die Skizze lösen
    skSolve(sketch);
    

    ;}

    Here are the faults:
    > 9:44 Error in inializer function arguments
    > 9:46 missing TOP_SEMI at 'function'
    > 11:5 extraneous input 'precondition' expecting {'annotation', '[', '+', '-', 'return', 'if', 'for', 'while', 'new', '!', '++', '--', '::', 'function', 'operator', 'undefined', 'break', 'continue', 'var', 'const', 'try', 'in', 'switch', 'throw', SEMI, '{', '(', '}', BOOLEAN, NUMBER, ID, BUILTIN, STRING}

    Hansjürg

  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,845
    edited March 27 Answer ✓

    Compare and contrast - this is why you should not use ChatGPT, it's not even close…

    FeatureScript 2615;
    import(path : "onshape/std/common.fs", version : "2615.0");
    
    annotation { "Feature Type Name" : "Grid generator", "Feature Type Description" : "" }
    export const gridGenerator = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "Grid spacing" }
            isLength(definition.spacing, { (millimeter) : [0.1, 100, 100] } as LengthBoundSpec);
    
            annotation { "Name" : "Rows" }
            isInteger(definition.rows, { (unitless) : [1, 10, 100] } as IntegerBoundSpec);
    
            annotation { "Name" : "Columns" }
            isInteger(definition.columns, { (unitless) : [1, 10, 100] } as IntegerBoundSpec);
        }
    
        {
            const sketch = newSketchOnPlane(context, id + "sketch", {
                        "sketchPlane" : plane(vector(0, 0, 0) * millimeter, vector(0, 0, 1))
                    });
    
            for (var i = 0; i < definition.columns; i += 1)
            {
                skLineSegment(sketch, "vertical" ~ i, {
                            "start" : vector(i * definition.spacing, 0 * millimeter),
                            "end" : vector(i * definition.spacing, definition.rows * definition.spacing)
                        });
            }
    
            for (var j = 0; j < definition.rows; j += 1)
            {
                skLineSegment(sketch, "horizontal" ~ j, {
                            "start" : vector(0 * millimeter, j * definition.spacing),
                            "end" : vector(definition.columns * definition.spacing, j * definition.spacing)
                        });
            }
    
            skSolve(sketch);
        });
    
    
    Senior Director, Technical Services, EMEA
  • _anton_anton Member, Onshape Employees Posts: 436

    Yeah, you shouldn't use LLMs for this. They don't replace understanding, especially for a specialized language like FS. You'll get a nice-looking snippet quickly and then spend a hundred times as long finding the issues.

    Michael's suggestion is really the answer here. Failing that, search the forum for custom features that already do this, or find similar custom features and understand their code.

  • hansjuerg_jenzerhansjuerg_jenzer Member Posts: 16

    Hello Neil
    Many thanks for that.
    It looks much clearer and more logical, even for a layman.

Sign In or Register to comment.