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.

Select assembly configuration in API?

eric_pestyeric_pesty Member Posts: 1,395 PRO
Started playing around a bit with the API.
I'm looking at what it would take to generate some images of different configurations of an assembly to use on our website.

Looking through the "Gif generator" example, I'm seeing a lot of the pieces I would need to make that happen, but one thing I'm not clear on is how to switch configurations. I tried pasting a link that includes a specific configuration but I couldn't seem to get it to propagate through... 

Ideally I'd be able to retrieve the lists of configuration and iterate through them with a for loop to generate a bunch of pictures automatically...

Is there an example on how to do this somewhere?

Answers

  • caden_armstrongcaden_armstrong Member, User Group Leader Posts: 127 ✭✭✭
    The configuration is under the elements endpoint
    https://cad.onshape.com/glassworks/explorer/#/Element/getConfiguration
    This endpoint will give you the list of all configuration parameters and possible values.

    The configuration is generally an encoded string of key value pairs, and most endpoints will take in a configuration as either a body parameter or a URL parameter.

    If you want an example of how the configuration looks, you can create an extension with the configuration included in the action URL, or another easy way is to just add a bunch of different instances to a test assembly in different configurations, use an endpoint like /assemblies/.../bom, and check the configuration parameter for each instance. You'll get a value like:
    "Width=Long;Height=Tall" etc, (or less human readable names likely)
  • eric_pestyeric_pesty Member Posts: 1,395 PRO
    edited January 2023
    Thanks @caden_armstrong
    That looks promising!
    I understand the document/workspace/element parts of the URL but is the wvmid only applicable to microversions (and in which case does it imply the API explorer requires a microversion to be used rather than a workspace?

    There are only 3 parts to the URL in the example colab notebook so that threw me off a bit!

    Also, when I put in the 4 parts of a microversion of my document in the explorer I get a 404... There's probably something obvious I'm missing (and that might where my problem is coming from...)!


    Sounds like a need to do some more background reading!

    For anyone reading this... the 4th part I was confused by is just asking for either "w", "v" or "m", to indicate a workspace version or microversion. Some sections in the API explorer offer a dropdown instead which makes things a lot clearer...
  • eric_pestyeric_pesty Member Posts: 1,395 PRO
    edited January 2023
    I've had a chance to work on this a bit and made some decent progress (thanks again to @caden_armstrongfor the pointers!) ...

    I did get something working to iterate through every combination of configuration with up to two "lists" configuration inputs but the way I "solved" this is really not scalable to more inputs so I'm thinking there's got to be a fundamentally better way...

    For context here is what I came up with:

    As a first step I build a table based on the "/configuration" response using the following code:
    rows=len(elementConfiguration['configurationParameters'])<div><div>if&nbsp;rows>2&nbsp;:&nbsp;print("2&nbsp;config&nbsp;inputs&nbsp;max!")</div><div>#print(rows)</div><div>configstrs=[]</div><div>i=0</div><div>for&nbsp;x&nbsp;in&nbsp;elementConfiguration['configurationParameters']:</div><div>&nbsp;</div><div>&nbsp;&nbsp;ParameterId&nbsp;=&nbsp;x['message']['parameterId']</div><div>&nbsp;&nbsp;print("Parameter&nbsp;name&nbsp;:&nbsp;"+x['message']['parameterName'])</div><div>&nbsp;&nbsp;#print(ParameterId)</div><div>&nbsp;&nbsp;j=0</div><div>&nbsp;&nbsp;temp=[]</div><div>&nbsp;&nbsp;for&nbsp;y&nbsp;in&nbsp;x['message']['options']:</div><div>&nbsp;&nbsp;&nbsp;&nbsp;#configstrs.append([i,j])</div><div>&nbsp;&nbsp;&nbsp;&nbsp;Option&nbsp;=&nbsp;y['message']['option']</div><div>&nbsp;&nbsp;&nbsp;&nbsp;print(Option)</div><div>&nbsp;&nbsp;&nbsp;&nbsp;configstring&nbsp;=&nbsp;ParameterId+"%3D"+Option</div><div>&nbsp;&nbsp;&nbsp;&nbsp;#print(configstring)</div><div>&nbsp;&nbsp;&nbsp;&nbsp;temp.append(configstring)</div><div>&nbsp;&nbsp;&nbsp;&nbsp;#configstrs[i][j]=configstring</div><div>&nbsp;&nbsp;&nbsp;&nbsp;#print(temp)</div><div>&nbsp;&nbsp;&nbsp;&nbsp;j=j+1</div><div>&nbsp;&nbsp;configstrs.append(temp)</div><div>&nbsp;&nbsp;#print(configstrs)</div><div>&nbsp;&nbsp;temp=[]</div><div>&nbsp;&nbsp;i=i+1</div><br><div>print(configstrs)</div></div>
    

    I then go through my "configstrs" array to generate every possible permutation like this:

    
    <div><div>i=0</div><div>for&nbsp;r&nbsp;in&nbsp;configstrs[0]:</div><div>&nbsp;&nbsp;j=0</div><div>&nbsp;&nbsp;config1=configstrs[0][i]</div><div>&nbsp;&nbsp;if&nbsp;rows==2:</div><div>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;c&nbsp;in&nbsp;configstrs[1]:</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config2=configstrs[1][j]</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config=config1+"%3B"+config2</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(config)</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;showImages&nbsp;==&nbsp;True:</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shadedimage&nbsp;=&nbsp;assembliesShadedView(url,config,viewMatrix,pixelSize,resH,resV)</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;im&nbsp;=&nbsp;Image(shadedimage)</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;display(im)</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j+=1</div><div>&nbsp;&nbsp;else:</div><div>&nbsp;&nbsp;&nbsp;&nbsp;config=config1</div><div>&nbsp;&nbsp;&nbsp;&nbsp;print(config)</div><div>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;showImages&nbsp;==&nbsp;True:</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shadedimage&nbsp;=&nbsp;assembliesShadedView(url,config,viewMatrix,pixelSize,resH,resV)</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;im&nbsp;=&nbsp;Image(shadedimage)</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;display(im)</div><br><div>&nbsp;&nbsp;i+=1</div></div>
    

    At first I had it working with exactly two input lists but then I went back and added some "if" so make it work with just one as well... The "assembliesShadedView" function also saves the image to my google drive in the process and that's working fine.

    This covers my basic needs at this point but it might come in handy down the road to make it a bit more "flexible" by allowing more inputs. I might also need to collect some more info to generate some mor useful filenames down the road (right now I include the actual "config" string in the filename and put in a folder named after the assembly description)...

    I'm guessing there's an opportunity to use recursion somewhere in there but in case you were wondering how far above my head most of this is, my current level requires googling "for loop in Python"...

    Thanks for reading this far!

    EDIT: what the heck is wrong with the "code" blocks now? I created the blocks before pasting and they look fine in the preview but are displaying in one line when posting!
Sign In or Register to comment.