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.

Custom Feature: Non-uniform patterns by csv input

Adam_GolderAdam_Golder Member Posts: 6 PRO

I'd like to be able to input a csv with four columns: X, Y, Z, and Theta, and pattern out mate connectors to those positions and clock the mate connector by theta about the Z axis. The express intent is to then be able to distribute solid bodies to those mate connectors. I see custom features available which can do this and generate a point cloud in X Y and Z, but this does not pattern out mate connectors nor does it include the option to specify theta clocking:

cad.onshape.com/documents/b961f94c2da22e7ea6118a12/v/95b214269a46b74b86bce3fd/e/a295f547458d7f1547546337

I've also tried to generate a feature script (using ChatGPT) that can create this functionality, but I'm unable to figure out the syntax error that is thrown:

FeatureScript 2656;
import(path : "onshape/std/common.fs", version : "2656.0");

/* MC-from-CSV ─ one Mate Connector per X,Y,Z,θ row */
annotation { "Feature Type Name" : "MC-from-CSV" }
export const mcfromcSV =
{defineFeature:(function (context is Context,
id is Id,
definition is map)

precondition
{
    annotation { "Name"  : "Paste X,Y,Z,θ list",
                 "UIHint": UIHint.Multiline }
    definition.table is string;
}
{
    const upDir = vector(0, 0, 1);
    const rows  = split(definition.table, "\n");

    for (var idx = 0; idx < size(rows); idx += 1)
    {
        // strip whitespace
        const line = trim(rows[idx]);

        // ignore blank lines and lines starting with #
        if (line == "" || startsWith(line, "#"))
            continue;

        const parts = split(line, ",");

        if (size(parts) != 4)
        {
            // tell the user exactly which row is wrong
            regenError("Need 4 comma-separated numbers per row (X,Y,Z,θ)",
                       { "row" : idx + 1 });
            continue;
        }

        // convert each CSV token to a number
        var nums = [];
        for (var p in parts)
            nums = append(nums, stringToNumber(trim(p)));  // string → number :contentReference[oaicite:0]{index=0}

        // build geometry
        const pos   = vector(nums[0], nums[1], nums[2]) * millimeter;
        const theta = nums[3] * degree;
        const xDir  = vector(cos(theta), sin(theta), 0);

        // unique ID for each connector: id + ("_row" ~ idx)  :contentReference[oaicite:1]{index=1}
        opMateConnector(context,
                        id + ("_row" ~ idx),
                        { "plane" : plane(pos, upDir, xDir) });
    }
}

}

Tagged:

Comments

  • Caden_ArmstrongCaden_Armstrong Member Posts: 279 PRO

    Theres more than just a single syntax error wrong with this script. The longer I look, the more problems I see.
    Chat GPT just cannot make featurescripts. You're better off spending the time learning featurescript rather than wrestling with the hallucination machine.


    I suggest taking the learning center course:
    https://learn.onshape.com/courses/featurescript-fundamentals
    Or maybe just start with the tutorial:
    https://cad.onshape.com/FsDoc/tutorials/create-a-slot-feature.html

    And read the documentation:
    https://cad.onshape.com/FsDoc/library.html#opMateConnector-Context-Id-map

    Use those other features that you've seen as reference. Creating a mate connector vs a point isn't a big difference in process.

    What you are trying to achieve isn't too difficult for a beginner.

    www.smartbenchsoftware.com --- fs.place --- Renaissance
    Custom FeatureScript and Onshape Integrated Applications
  • EvanReeseEvanReese Member, Mentor Posts: 2,399 ✭✭✭✭✭

    GPT and others will hallucinate a bit with FeatureScript. For example there is no "UIHint.Multiline". It made that up. The Onshape AI advisor is useful for asking specific how-to questions about FS even if it can't write the whole thing for you.

    As for your intent of creating mate connectors to pattern to, you could just have the feature do the patterning itself and skip the mate connectors altogether. Here's the general logical approach that comes to mind for me.

    1. Import CSV with a reference parameter in the precondition (UI area). It will look like this annotation { "Name" : "CSV file" }
      definition.myCsvFile is TableData;
    2. process the table in a for loop. For each row do the following:
      1. figure out the vector representing X rotated by theta. your code above does this already with xDir.
      2. create a coordinate system using coordSystem(). at the xyz coordinates, and use the vector from above as the xAxis.
      3. optionally if you want mate connectors at these locations, use opMateConnector with the coordinate system as input. or go ahead and use some combination of fromWorld() and toWorld() to create a transform from an input coordinate system to the new ones.
    3. if patterning the parts in this feature, use opPattern().
    Evan Reese
    The Onsherpa | Reach peak Onshape productivity
    www.theonsherpa.com
  • _anton_anton Member, Onshape Employees Posts: 456
    edited May 29

    LLMs are (so far) useless for producing working FS code. You'll probably lose time by using them.

    I think your best bet is to modify the 3D Point feature with opMateConnector instead of opPoint.

  • Adam_GolderAdam_Golder Member Posts: 6 PRO

    Short of taking the time to use Feature Scripts, does anyone have any other ideas of how I might implement this? I'm very much a beginner programmer

  • EvanReeseEvanReese Member, Mentor Posts: 2,399 ✭✭✭✭✭

    Here's a bare bones draft to get you going! https://cad.onshape.com/documents/5e76707630d6a64b888e564c/w/20d7e510ac219d2f35743419/e/17589401124085e6cee56921

    FeatureScript 2656;
    import(path : "onshape/std/common.fs", version : "2656.0");
    annotation { "Feature Type Name" : "CSV to mate connectors", "Feature Type Description" : "" }
    export const csvToMateConnectors = defineFeature(function(context is Context, id is Id, definition is map)
    precondition
    {
    annotation { "Name" : "CSV file" }
    definition.csv is TableData;
    }
    {
    const lengthUnits = inch;
    const angleUnits = degree;
    const tableData = definition.csv.csvData -> removeElementAt(0); // removeElementAt(0) to get rid of columns header row
    const coordinateSystems = mapArray(tableData, function(row) {
    const xAxis  = vector(cos(row[3] * angleUnits), sin(row[3] * angleUnits), 0);
    // map CSV columns 0, 1, and 2 to a vector of X, Y, Z
    const origin = vector(row[0], row[1], row[2]) * lengthUnits;
    return coordSystem(origin, xAxis, Z_DIRECTION);
    });
    
        for (var i, coordSystem in coordinateSystems)
        {
            opMateConnector(context, id + "mateConnector" + i, {"coordSystem" : coordSystem,"owner" : qOrigin(EntityType.BODY)});
        }
    });
    
    Evan Reese
    The Onsherpa | Reach peak Onshape productivity
    www.theonsherpa.com
  • Adam_GolderAdam_Golder Member Posts: 6 PRO

    @EvanReese I cannot thank you enough!

  • MichaelPascoeMichaelPascoe Member Posts: 2,333 PRO

    .

    @Adam_Golder Looks like your all set now. If you do continue to get into FS development, here are two valuable recourses that I recommend:

    .


    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   Learn How to FeatureScript Here 🔴
Sign In or Register to comment.