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.

Can I import .dat files using a feature script?

darren_13darren_13 Member, Developers Posts: 117 PRO
so I have finished my NACA 4 digit aerofoil script and I now want to have the ability to use the vast amount of more comely aerofoils available for .dat download. Am I able to read a .dat file and store the co-ords in an array for aerofoil creation?

Kind regards,
Darren Lynch
Tagged:

Best Answers

Answers

  • darren_13darren_13 Member, Developers Posts: 117 PRO
    Ok thank you, Ill give this a go.
  • darren_13darren_13 Member, Developers Posts: 117 PRO
    ok, so now my matlab file passes .csv instead of .dat (the standard for aerofoil profiles) and I want to import as you describe in the link and I come to two difficulties, how do I know the path? and if the user types the profiles name i.e. Aerofoil5.csv how do I make OnShape recognise this as its path? in the future I wish to run this in a loop importing a different profile (all exported from matlab) onto a new plane at say 1 meter intervals. before I go any further is this possible? or will I have to run the script for each profile defining the name?

    This script is to be used in a parametric analysis of a wind turbine in SimScale.

    Kind regards,
    Darren Lynch
  • darren_13darren_13 Member, Developers Posts: 117 PRO
    @kevin_o_toole_1, I'm revisiting this. I am creating a script (python not FS) that pulls, converts and generates the code to past into feature script to have all 1600 aerofoils in a list. I'll be dumping the file with all the CSV but I will need to import them to my script, is there any way I can get the path and version:

    PROFILE_1::import(path : "d6c489e34a7389ebc8c3364a", version : "d89c86b0c14dfd85bee00095");
    without manually going through importing all 1600 files? Ideally, I would like to determine the path based upon its location within my document? 

    The reason I want to do this is because I believe people would rather copy the file, add their own profiles without getting me to update the script. This isn't a problem, however, others then don't benefit from the addition of those profiles. I have contemplated linking this to an app created using your API, however, wouldnt know where to start and for now, feature script is comfortable to use, although I feel I'm gonna have to bit the bullet at some point.

    Kind regards,
    Darren
  • kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 447
    The "Path" for intraworkspace imports (like the one you pasted) is just the unique id for that tab (AKA element). The preferred way to get these ids is through the API. However, if it's easier, when looking at that tab, you could also get it from end of the URL, which has the format cad.onshape.com/documents/<document id>/w/<workspace id>/e/<element id that you need>, however, can't think of a good automated way of getting these from every tab without the API. This also doesn't save you the step of uploading them all in the first place (which, without the API, would require many, many clicks).

    The version for intraworkspace imports will automatically be updated to the latest version in the workspace of whatever you're pointing to. So, for your purposes, you can leave this blank (i.e. import(path: "elementid123123123", version: "") ). When you paste the code and commit it, the latest version will automatically be populated as the version string.

    It likely won't surprise you to hear that we would one day like to have the ability for users to specify an new import right in the feature dialog (similar to what happens in derived part), instead of needing to modify the FeatureScript every time. In the meantime, it sounds like your python script could be a good solution.

  • darren_13darren_13 Member, Developers Posts: 117 PRO
    Hi guys thank you for your responses. This seems the way to go, compiling into a single file then uploading as one. Then calling the coordinates that I know corresponds to each profile. Once again thanks for your out of the box thinking :)

    Kind regards,
    Darren
  • darren_13darren_13 Member, Developers Posts: 117 PRO
    Hi @kevin_o_toole_1, if im doing this it is going to be just as easy for me to auto write the lookup table with coordinates included. Is this more efficient that trying to pull a set data set from one massive file? Also could I make the lookup table a function in another script and import it? or can I not use functions for prerequisits? 

    Thanks in advance,
    Darren
  • darren_13darren_13 Member, Developers Posts: 117 PRO
    Hi again, This appears to be working for me thanks for the help. If any one in the future faces this problem I am posting the code I used.

    import os

    allowed = "0123456789-+. ()"

    path = '/Users/darrenlynch/PycharmProjects/OnShape_Import/coord_seligFmt/'
    file_names = os.listdir(path)
    with open('lookUpTable.txt', 'w') as lookUpTable:
    print('export const aerofoilTable = {\n\t\t"name" : "DataBase",\n\t\t"displayName" : "Data Base",\n\t\t"default" : "UIUC",\n\t\t"entries" : {', file=lookUpTable)
    print('\t\t\t"UIUC": {', file=lookUpTable)
    print('\t\t\t\t"name" : "Aerofoil",\n\t\t\t\t"displayName" : "Aerofoil",\n\t\t\t\t"default" : "YS-930",\n\t\t\t\t"entries" : {', file=lookUpTable)


    for file_name in file_names:
    line = 0;
    old_lines = ""

    try:

    with open(path + str(file_name), 'r') as data1:
    for lines in data1:
    if line > 0:
    lines = ' '.join(lines.split())
    for char in lines:
    if char not in allowed:
    lines = ''
    if lines == '':
    break
    lines = lines.replace('.......', '0.00000')
    lines = lines.replace('......', '0.00000')
    lines = lines.replace('(', '')
    lines = lines.replace(')', '')
    lines = lines.replace(' ', ', ', 1)
    if lines == old_lines:
    continue
    old_lines = lines
    with open('lookUpTable.txt', 'a') as lookUpTable:
    print("\t\t\t\t\t\t\tvector(" + lines + "),", file=lookUpTable)
    else:
    lines = ' '.join(lines.split())
    with open('lookUpTable.txt', 'a') as lookUpTable:
    print('\t\t\t\t\t"' + lines + '": [', file=lookUpTable)
    line += 1
    except:
    with open('ErrorLog.txt', 'a') as errors:
    print("\n\nError: Unable to read file: {}\n\n".format(path + str(file_name)), file=errors)
    continue

    with open('lookUpTable.txt', 'a') as lookUpTable:
    print('\t\t\t\t\t\t],', file=lookUpTable)

    with open('lookUpTable.txt', 'a') as lookUpTable:
    print('\t\t\t}\n\t\t}\n\t}\n};', file=lookUpTable)

    Once again many thanks,
    Darren
Sign In or Register to comment.