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.

AI Text-To-CAD using Onshape!

Chris_TiltonChris_Tilton Member Posts: 10
Hey All!

My name is Chris, I'm an Onshape Intern and an undergrad Mechanical Engineering student at Tufts. I'm currently doing an Undergrad Research Project surrounding using OpenAI and the Onshape API to create CAD models via text input. So far, my model is trained using some of the examples from the Feature List API and is able to make spheres and cubes of different sizes, or any number of the combination of the two, using the JSON payloads from the examples that call custom features and some python code that I wrote and trained it on. This is a great step, but before presenting this at the end of the semester I would love to take some huge steps forward and turn this into something more useful. So, as a novice programmer and API noob, I turn to the wonderful Onshape community for some guidance. If you feel you have the knowledge to answer one or more of the following questions, please do, the more help I can get, the more impressive of a tool this can be.

Here's a quick demo of the tool in action, with the very limited capabilities at the moment.

1. Can anyone explain in better detail how to figure out how to call any given custom feature with the API? For example, one of the main features I would like to call is the Spur Gear Custom feature, however I am unsure how to find the right values for the "featureType" and "parameters" values to include in the payload. Would this be found in the original definition of the custom feature? Or in the FeatureScript generated by calling this custom feature. Here is the Payload I am using for creating a sphere feature with radius 3 inches:
<div><div>#Payloard for a sphere with radius 3 inches
{
</div><div>&nbsp; &nbsp; &nbsp; &nbsp; "feature": {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "type": 134,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "typeName": "BTMFeature",</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "message": {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "featureType": "sphere",</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "Sphere1",</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "parameters": [</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "type": 147,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "typeName": "BTMParameterQuantity",</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "message": {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "expression": "3*in",</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "parameterId": "radius"</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; }</div></div>
2. Is there any sort of publicly available robust documentation on the features that can be created by the Feature List API and their necessary parameters? It might be helpful to be able to train my bot on this kind of document.

3. Any general code advice? Like I said I am new to this, but really hungry to learn and take this tool to the next level. Here's the link to the (quite messy) GitHub Repo.

If you have any questions, ideas, or feedback please let me know in the replies, your help is more than appreciated!

Comments

  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,068 PRO
    That sounds like a great project.

    The api is totally detached from featurescript. 

    Start here for the api:
    https://cad.onshape.com/glassworks/explorer/

    Remember that it'll authorize when you log into onshape. 

    On a server, you have to authorize each post with the a server in the header. There are plenty of examples of this. I think github & onshape.

    There are a lot of featurescript calls in the api and I haven't played with them yet. Post some cool stuff when you discover something calling featurescript from the api.

    Last thing, you can post your api thingy in the app store after registering it. The app store allows you to access your data, or, access other people's data if the user allows it. This last option does require more scrutiny from OS since you can see other people's data. 

    If you create the next AI app that'll create a production worthing CAD model by prompting "make me a pixel 100" you can probably sell it for $5 buck.

    Good luck and have fun,




  • tabetha_bulnestabetha_bulnes Member, Developers, HON-TS Posts: 25 PRO
    edited February 29
    Hi Chris,

    I am mostly a FS developer at my company but I work with a team that use API calls so I am aware of some of these questions and can help where I can but can also provide some contacts for additional help. Our company is interested in doing some machine learning as well so would be interested in you project and working with you for some back forth discussions. 

    For the first point you will find what you need in the FS code or in a "Show Code" of the part studio. The featureType is the FeatureScript function name. In the FS you will find this in the main FeatureScript function as the export const featureType = defineF...... where in the case of the Spur Gear the featureType is SpurGear. The parameters will be defined in the precondition as the definition map. Below is a snip of the Spur Gear FS to show where these are found
    annotation { "Feature Type Name" : "Spur Gear", "Feature Name Template" : "Spur Gear (#teeth teeth)", "Editing Logic Function" : "editGearLogic" }
    export const SpurGear = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "Number of teeth" }
            isInteger(definition.numTeeth, TEETH_BOUNDS);
    
            annotation { "Name" : "Input type" }
            definition.GearInputType is GearInputType;
    <br>

     In the "Show Code", when you select in the tree the FS it will direct you to where the code is and you see something similar to the below. Here you will again the featureType just before the (context, id...... ) as well as the map for the parameters. 

    features.Fdy9RsN4NaT0rs3_1 = function(id)
                {
                    annotation { 'unused' : true }
                    var features = features;
                    if (true)
                    {
                        {
                        }
                        annotation { "Feature Name" : "Spur Gear (#teeth teeth)" }
                        e01a666571e625f8b819fd75b::m376a1982a13b44699a90596a::SpurGear(context, id + "Fdy9RsN4NaT0rs3_1", { "numTeeth" : { 'value' : try(25.0), 'expression' : "25.0" }.value, "GearInputType" : e01a666571e625f8b819fd75b::m376a1982a13b44699a90596a::GearInputType.module, "module" : { 'value' : try(1.5 * millimeter), 'expression' : "1.5*millimeter" }.value, "diametralPitch" : { 'value' : try(16.933333333333334), 'expression' : "16.933333333333334" }.value, "circularPitch" : { 'value' : try(4.71238898038469 * millimeter), 'expression' : "4.71238898038469*millimeter" }.value, "pitchCircleDiameter" : { 'value' : try(37.5 * millimeter), 'expression' : "37.5*millimeter" }.value, "pressureAngle" : { 'value' : try(28 * degree), 'expression' : "28*degree" }.value, "centerHole" : false, "centerHoleDia" : { 'value' : try(10.0 * millimeter), 'expression' : "10.0*millimeter" }.value, "key" : false, "keyWidth" : { 'value' : try(3.0 * millimeter), 'expression' : "3.0*millimeter" }.value, "keyHeight" : { 'value' : try(3.0 * millimeter), 'expression' : "3.0*millimeter" }.value, "centerPoint" : false, "center" : qUnion([]), "gearDepth" : { 'value' : try(3 * millimeter), 'expression' : "3*millimeter" }.value, "flipGear" : true, "offset" : false, "offsetClearance" : { 'value' : try(0.0 * millimeter), 'expression' : "0.0*millimeter" }.value, "offsetDiameter" : { 'value' : try(0.0 * millimeter), 'expression' : "0.0*millimeter" }.value, "offsetAngle" : { 'value' : try(0.0 * degree), 'expression' : "0.0*degree" }.value, "rootFillet" : e01a666571e625f8b819fd75b::m376a1982a13b44699a90596a::RootFilletType.third, "dedendumFactor" : e01a666571e625f8b819fd75b::m376a1982a13b44699a90596a::DedendumFactor.d250 });
                    }
                };
            try(features.Fdy9RsN4NaT0rs3_1(id));
    For point 2, we use the Glassworks API explorer to support our API investigations. https://cad.onshape.com/glassworks/explorer

    For point 3, please feel free to reach out to myself and if I can answer any questions I would be happy to help and if not we can work with one of API developer to work through your questions.
  • Chris_TiltonChris_Tilton Member Posts: 10
    Hmm. I tried running my code with "SpurGear" as the featureType and it's throwing this response.
    {
      "message" : "Feature has invalid type",    
      "moreInfoUrl" : "",
      "status" : 400,
      "code" : 9999
    }

    Assuming this means that the feature needs to either be defined somewhere for me to call it using the API, or I need to use a different featureType. Any thoughts?

  • Tom_FoslerTom_Fosler Member Posts: 2 PRO
    Hi Chris, looking at your github for the spur gear, it says your featureType is "Spur gear", and it should be "SpurGear". My recommendation is that you first create the part physically in Onshape and then do a getFeatures API request to see the return value. Then you can match the part you create with what Onshape is returning. I'd also change the create_cube variable to something different like create_spurgear to avoid any confusion.
  • Tom_FoslerTom_Fosler Member Posts: 2 PRO
    edited March 4
     
Sign In or Register to comment.