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 partQuery in FeatureScript to retrieve parts

morteza_karimimorteza_karimi Member Posts: 13 PRO
Hi,

I noticed the "get parts in PartStudio" returns a partQuery that looks like below:

"partQuery": "query=qCompressed(1.0,\"&1c6$eJx1UMFygjAU/Jn06ggFhCOQYFMrwSQzVi9MgFSj1iDEtv59g8z00vH43u6+3X1PiQ9WV9ndlj5zXNCoXnxWancVRukzFEbEztL9j/BbK5nzDAjFc5zHbyVEBcohytMNi4Du1E6dxamPvXRqDwsgz0aZ26BKBEB/A/MAgnPEKrBXvdHduAxBSlHMMcktoFvZ3S1xk7gAN7HDHGcSgOw1CCOOk9ihcGHacvqtOkla634Z6twPNYAtEE9fSpRzzDdMgv4oTb0fA+CG1RMfHAoS37irzXLNbe69GSJTj/o0oDMaUkErWlPLBeF6So6rBakyHT0kcRjk8HBsI0RXD0mzD/2eXerMbH9O1EsD+4eEwM3IS6uxYwQyBbe6/WpI4Rf0YjvqFv2Y7tpIWtmnsjUqeDkIfwEBEZNK\",id);"

I was wondering if this query can be used in a FeatureScript to find a specific part. I'm looking for a way to link the metadata I'm getting through the API back to parts inside a FeatureScript and from what I understand PartIDs can't be used because they are assigned after featureScripts is done regenerating. But can this partQuery or any other property be used to achieve this?

Thanks,

Comments

  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 940
    I'm not quite following -- Ids should work for everything: since you're using "get parts in PartStudio", I assume you're running code outside Onshape.  For referencing the part in "evaluate FeatureScript" ids will work.  For adding features with query parameters (our features or custom features) ids can specify what's in those query parameters.  The only use for the query I can think of is if you want to create a feature studio through the API and put it in there.  But I think a better solution is to create that feature studio with a query parameter and then pass it in from the part studio.
    Ilya Baran \ Director, Architecture and FeatureScript \ Onshape Inc
  • morteza_karimimorteza_karimi Member Posts: 13 PRO
    edited March 13
    Hi @ilya_baran , Thanks for your prompt reply.

    So you're saying I can query for a specific partID inside FeatureStudio? can you help me understand what the query would look like?
    This query will be a parameter inside a custom feature that I'd like to add to a document through the API, passing in the partID as a parameter.
    I'm thinking the body of my request would look something like below, but I can't figure out how to query for the partID I'm passing in on the FeatureStudio side.

    "feature":{
        "type": 134,
        "typeName": "BTMFeature",
        "message": {
            "featureType": "add_custom_attributes",
            "name": "aca-1",
            "namespace": "e5f4833f4feab2a1258969790::me4f227b60dcb6ada238eb126",
            "parameters": [
    "partId": "JUD",
    "customAttr": {OBJECT}
            ]
        }
    }

  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 940
    You can't query by part id from a feature studio, but you can pass the part id as a parameter to your feature (Onshape will convert it to a query internally).  Add a query parameter (with an EntityType.BODY filter) to your custom feature, manually add the feature to a part studio, select a couple of parts as an input and do a "Get Feature List" to see the form of the part studio then.  It's a bit big, but you'll see the part ids (as geometryIds in the JSON).
    Ilya Baran \ Director, Architecture and FeatureScript \ Onshape Inc
  • morteza_karimimorteza_karimi Member Posts: 13 PRO
    edited March 13
    Thanks @ilya_baran, But I'm afraid this isn't what I'm trying to do. I think I wasn't very clear about the function of this custom feature.
    Let's call the custom feature "Part_Iterator". What Part_Iterator does is, it goes through all existing parts in a document and adds some custom attributes to each part. I have a JSON file with all the attributes for each part, categorized by "partId"s.
    As you can see, Part_iterator needs to know what the partId is for each part in order to retrieve the right attribute from the JSON file. Or the other way around, it needs to be able to query for a specific partId.

    Thanks


  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 940
    Ok, I see a little more of the picture now.  So you're doing some calculations outside of Onshape, producing some data for each part, and then you want to attach the results of those calculations as attributes on those parts?

    Are these part studios constructed interactively in Onshape?  Is someone going to want to modify those part studios after the attributes are attached?  Because if so, you definitely don't want to refer to parts by part ids, as those will change if parts are edited.

    One possibility is creating a custom feature with an array parameter, where each array item consists of a query for a part and an isAnything parameter for the attributes.

    Another possibility (though less nice) is using the compressed query string in the JSON file, as I think you suggested -- you'd iterate over the data in the JSON file and attach the attributes to the query results.

    Does this help?
    Ilya Baran \ Director, Architecture and FeatureScript \ Onshape Inc
  • morteza_karimimorteza_karimi Member Posts: 13 PRO
    edited March 14
    @ilya_baran That's exactly what we want to do. Some of the parts are constructed using featurescript and some manually. This custom feature would only be added at the last step right before exporting to a STEP file and deleted after export is completed. So to answer your question, no, the parts don't need to be modified after attributes are attached.
    Both of your solutions sound promising, can you please elaborate a bit more? I'm still very new to featurescript.
    -When you say "each array item consists of a query for a part", what does that query look like?
    -I did try using the qCompressed, but was getting a CANNOT_RESOLVE error. I'm probably not using it the right way.

    setProperty(context, {                "entities":qCompressed(1.0,'&253$eJx1kU1zgjAQhv9Meu00KChHSCLGKmJC68eFCSRirALyMYq/vqgzvXQ47u6z7767++aaYNWosl2YHBpA6kqcY502otZ5hkUtHLgw/lfCtlAcDsCSUY/6zjzCJCA+Jj7achvkpU51Jk6VM0YfnbAAKqt13T66XAHIX8CHgGCP8BgcdFXn5Ss5BogRJ6RLvyvkhSqfI6l0DUClAzmE7xaYzDiaatNmi3MzCL4jeNWlWhbd9MtjnaeQBPyThGgaET+k4ZYrUP2oOjm8DFDJk04mRXLQYm3kT69syExmsREbM8FilrAHdGxtZBvGyFrVbS8EVXbFe7LbMe/eCzXZUIenleT4cuqFCpkmATe/jtuZ1QsZm+DHtTNvgxPZC20ml/Mih/QkbvNeaByuJm3geOm94myIrO4p7hJvXxyKXwe3wYQc9ciY0TnM95/rLIJ5QW512UjF4u7DfE2CMHo0/gKtw73l',id),
                        "propertyType" : PropertyType.APPEARANCE,
                        "value" : color(definition.R / 255., definition.G / 255., definition.B / 255., 1)
                    });

    Thanks,
  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 940
    Unfortunately, one of the not-nice things with the compressed query solution is that debugging it is very hard.  If I had to guess, it's that you should replace the parameter id with newId() (because inside a feature the id includes the feature id).

    I recommend the first approch.  Here's a public document that shows how the feature can work: https://cad.onshape.com/documents/0f85058bb5f40b8535a4f8de/w/7daa1dc09bec441651d39f8d/e/0637ea3ac59e4d5f24be41b1
    To answer your question, on the FS side, the query is just a query.  For work with the API, Onshape exposes the query as a partId.
    Ilya Baran \ Director, Architecture and FeatureScript \ Onshape Inc
  • morteza_karimimorteza_karimi Member Posts: 13 PRO
    edited March 14
    That's great, thank you so much @ilya_baran . the qCompressed worked like a charm by replacing id with newId().
    I will look into the other approach as well, I just need to figure out how to structure the body of my request for AddFeature endpoint. Working with simple quantity parameters I had learned I could just take the response from "Get Feature Studio Specs" and start stripping out unnecessary key,values. But for an Array parameter, specs are nested and more confusing. Is there a better way to do this?
    here are the specs for the feature you sent;
    {
      "featureSpecs" : [ {
        "type" : 129,
        "typeName" : "BTFeatureSpec",
        "message" : {
          "featureType" : "partColors",
          "featureTypeName" : "Part Colors",
          "manipulatorChangeFunction" : "",
          "parameters" : [ {
            "type" : 2600,
            "typeName" : "BTParameterSpecArray",
            "message" : {
              "parameters" : [ {
                "type" : 174,
                "typeName" : "BTParameterSpecQuery",
                "message" : {
                  "filter" : {
                    "type" : 124,
                    "typeName" : "BTEntityTypeFilter",
                    "message" : {
                      "entityType" : "BODY"
                    }
                  },
                  "maxNumberOfPicks" : -1,
                  "additionalBoxSelectFilter" : {
                    "type" : 0
                  },
                  "parameterId" : "query",
                  "parameterName" : "Part Query",
                  "uiHint" : "",
                  "defaultValue" : {
                    "type" : 148,
                    "typeName" : "BTMParameterQueryList",
                    "message" : {
                      "queries" : [ ],
                      "parameterId" : "query",
                      "hasUserCode" : false,
                      "nodeId" : "MnrEFMPwxpoD12Abk"
                    }
                  },
                  "visibilityCondition" : {
                    "type" : 0
                  },
                  "uiHints" : [ ],
                  "columnName" : ""
                }
              }, {
                "type" : 173,
                "typeName" : "BTParameterSpecQuantity",
                "message" : {
                  "quantityType" : "ANYTHING",
                  "ranges" : [ ],
                  "parameterId" : "data",
                  "parameterName" : "Data, e.g. RGB",
                  "uiHint" : "",
                  "defaultValue" : {
                    "type" : 147,
                    "typeName" : "BTMParameterQuantity",
                    "message" : {
                      "units" : "",
                      "value" : 0.0,
                      "expression" : "0",
                      "isInteger" : false,
                      "parameterId" : "data",
                      "hasUserCode" : false,
                      "nodeId" : "MgoD4pCV/gl/wvy16"
                    }
                  },
                  "visibilityCondition" : {
                    "type" : 0
                  },
                  "uiHints" : [ ],
                  "columnName" : ""
                }
              } ],
              "itemName" : "Part",
              "itemLabelTemplate" : "",
              "drivenQuery" : "query",
              "parameterId" : "parts",
              "parameterName" : "Parts",
              "uiHint" : "",
              "defaultValue" : {
                "type" : 2025,
                "typeName" : "BTMParameterArray",
                "message" : {
                  "items" : [ ],
                  "parameterId" : "parts",
                  "hasUserCode" : false,
                  "nodeId" : "MEuldRVLx+pMrZSCO"
                }
              },
              "visibilityCondition" : {
                "type" : 0
              },
              "uiHints" : [ ],
              "columnName" : ""
            }
          } ],
          "uiHints" : [ ],
          "namespace" : "e32c93d3896b65163a3ca1fbc::mc1c4abff1a359f9996f90365",
          "filterSelectors" : [ ],
          "featureNameTemplate" : "",
          "editingLogic" : {
            "type" : 0
          },
          "languageVersion" : 1024,
          "sourceMicroversionId" : "c1c4abff1a359f9996f90365",
          "sourceLocation" : {
            "type" : 226,
            "typeName" : "BTLocationInfo",
            "message" : {
              "document" : "32c93d3896b65163a3ca1fbc",
              "version" : "c1c4abff1a359f9996f90365",
              "topLevel" : "",
              "parseNodeId" : "PI1qCJfM+Bhe8Acdy",
              "character" : 160,
              "line" : 5,
              "column" : 27,
              "endCharacter" : 966,
              "endLine" : 28,
              "endColumn" : 7,
              "languageVersion" : 1024,
              "moduleIds" : {
                "type" : 1897,
                "typeName" : "BTDocumentVersionElementIds",
                "message" : {
                  "documentId" : "",
                  "versionId" : "",
                  "elementId" : "32c93d3896b65163a3ca1fbc"
                }
              },
              "elementMicroversion" : "c1c4abff1a359f9996f90365",
              "nodeId" : "DTiQTQHJeo1K+HZ6"
            }
          },
          "linkedLocationName" : ""
        }
      } ],
      "serializationVersion" : "1.1.16",
      "sourceMicroversion" : "89a192cfd82bb8cafcfb693e",
      "rejectMicroversionSkew" : false,
      "microversionSkew" : false,
      "libraryVersion" : 0
    }


  • ilya_baranilya_baran Onshape Employees, Developers, HDM Posts: 940
    Yeah, the messages are a bit verbose -- would be nice to fix.  I would recommend doing a  "Get Feature List" from the part studio in the document I shared -- and then modify the results (i.e., figure out which portion repeats per-part, change the partId, and change the data on the rgb parameter).
    Ilya Baran \ Director, Architecture and FeatureScript \ Onshape Inc
Sign In or Register to comment.