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.

Options

how to get the coordinates from all parts? loop through all parts en print the coordinates

rooiejorisrooiejoris OS Professional Posts: 16 PRO
edited February 14 in Community Support
Hi,

I am not a programmer, but I think I want something fairly simple:
I want a list of coordinates of all parts in the part studio.

Basically loop through all parts and print the  coordinates in the monitor (or save to csv, but that is luxury... : )

https://cad.onshape.com/documents/bced954384551d9a45056666/w/1c4ec0f34b9d2439e54967bb/e/efbe92356f7e4fdb9a7ffea8?renderMode=0&uiState=65cc8eb5ce42ed723b91737c

I tried chatgpt for inspiration. (see the featurestudio starting with 'chatgpt' and 'google' There are probably some hints in there, but I didn't get it to work.

Can someone give me a better hint, or a working script... : )

cheers / joris


Best Answers

  • Options
    chadstoltzfuschadstoltzfus Member, Developers, csevp Posts: 131 PRO
    Answer ✓
    When you display the results of box3dCenter(), it will always be in meters since it is displaying the raw value stored by the variable coord in your example. If you display a ValueWithUnits in a custom table, it will go off of your workspace's unit settings. If you want to bypass that, you'll have to do some conversion math and display it as a string instead. 

    The box3dCenter function returns a Vector, which is an array of ValueWithUnits values (x, y, z). So to display an individual value of a vector, you can call it like so

    coord[0]
    To display the value in your table, you can add the key/value pair onto the map when appending a row. 
    rows = append(rows, tableRow({ "name" : partName, "coord" : coord, "coordx" : coord[0], "coordy": coord[1] }));


    Applications Developer at Premier Custom Built
    chadstoltzfus@premiercb.com
  • Options
    rooiejorisrooiejoris OS Professional Posts: 16 PRO
    Answer ✓
    Thanks...!
    Also improved my script in de mean time.
    Nice to know was to divide by millimeter to 'just get the value'.

    key was here:

                var xcoord = roundToPrecision(coord[0]/millimeter,3);
                var ycoord = roundToPrecision(coord[1]/millimeter,3);

    cheers / joris


Answers

  • Options
    Konst_ShKonst_Sh Member Posts: 42 PRO
    Looping through parts is possible for sure, but how you define the coordinate of a part? Do you have some mate connector associated with that part or just take a center of gravity or the center of bounding box?
  • Options
    rooiejorisrooiejoris OS Professional Posts: 16 PRO
    the onshape example works, I get a custom table
    now I cant get the units to mm instead of meters

    and is it easy to make a column for just X and Y? in other words how to extract the X and Y value from:

    var coord = box3dCenter(evBox3d(context, {
                        "topology" : part,
                        "tight" : true
                }));


    see the "onshapeexample_volumetable Copy 1" featurescript

  • Options
    chadstoltzfuschadstoltzfus Member, Developers, csevp Posts: 131 PRO
    Answer ✓
    When you display the results of box3dCenter(), it will always be in meters since it is displaying the raw value stored by the variable coord in your example. If you display a ValueWithUnits in a custom table, it will go off of your workspace's unit settings. If you want to bypass that, you'll have to do some conversion math and display it as a string instead. 

    The box3dCenter function returns a Vector, which is an array of ValueWithUnits values (x, y, z). So to display an individual value of a vector, you can call it like so

    coord[0]
    To display the value in your table, you can add the key/value pair onto the map when appending a row. 
    rows = append(rows, tableRow({ "name" : partName, "coord" : coord, "coordx" : coord[0], "coordy": coord[1] }));


    Applications Developer at Premier Custom Built
    chadstoltzfus@premiercb.com
  • Options
    rooiejorisrooiejoris OS Professional Posts: 16 PRO
    Answer ✓
    Thanks...!
    Also improved my script in de mean time.
    Nice to know was to divide by millimeter to 'just get the value'.

    key was here:

                var xcoord = roundToPrecision(coord[0]/millimeter,3);
                var ycoord = roundToPrecision(coord[1]/millimeter,3);

    cheers / joris


Sign In or Register to comment.