Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Import CSV Points Add-in

17 REPLIES 17
Reply
Message 1 of 18
hanskellner
3429 Views, 17 Replies

Import CSV Points Add-in

I created a simple addin that allows importing points from a CSV file and creating either sketch points, lines, or splines:

 

https://github.com/hanskellner/Fusion360ImportCSVPoints

 

 Import CSV Points SettingsDialog.png

 

It's a variation of the CSV import code that's been posted on the forum.  I needed something that allowed creating of either of the three types. And it also supports creating multiple lines/splines.  Multiple lines/splines may be defined by inserting a blank line between groups of points within the CSV file.  Each group of points will be used to create a line or spline.

 

 

I'm posting it in case others might find it helpful.

 

Cheers.



Hans Kellner
Senior Manager, Principal Engineer
17 REPLIES 17
Message 2 of 18
Anonymous
in reply to: hanskellner

I really like this script but I may have encountered a bug. It seems like when there are multiple lines to be drawn from a CSV file, the second point in any given line gets skipped. I'm having a hard time tracking down why, but I imagine the fix would be fairly simple.

Message 3 of 18
Anonymous
in reply to: Anonymous

I think I've tracked down the issue and have made some changes to the script that seem to work, which I've attached. In a nutshell, I replaced this section:

 

if (line === "") {
    // A blank line indicates a break in the point sequence and to start
    // a new set of points.  For example, for creating multiple lines.
    // If we have any lines then bump index to start a new line.
    if (lines3d.length > 0) {
        lines3dindex = lines3dindex + 1;    // Next line
    }

    // Skip over multiple blank lines (treat as one) 
    for (++i ; line === "" && i < linesCSVCount; ++i) {
        line = linesCSV[i].trim();
    }

 

With this:

  

if (line === "" || line === ",," || line === ",") {
    // A blank line indicates a break in the point sequence and to start
    // a new set of points.  For example, for creating multiple lines.
    // If we have any lines then bump index to start a new line.
    if (lines3d.length > 0) {
        lines3dindex = lines3dindex + 1;    // Next line
    }

    // Skip over multiple blank lines (treat as one)
    while ((line === "" || line === ",," || line === ",") && i < linesCSVCount) {
        ++i;
        if (i == linesCSVCount) {
            break;  // No more lines
        }
        line = linesCSV[i].trim();
    }

 

As an additional benefit, it better handles CSV files created by Excel. Excel insists on placing commas in otherwise blank rows of CSV files, which I was having to manually remove until I made the above edits to the script.

Message 4 of 18
hanskellner
in reply to: Anonymous

Nice find there and thanks for the fix!  I'll also update the GIT repo with the change.

 

Hans



Hans Kellner
Senior Manager, Principal Engineer
Message 5 of 18
Anonymous
in reply to: Anonymous

Hello gregorytill

 

I am having errors occur when I save files using Excel (the exact same ones that are included as samples). I have also searched the new Excel CSV copies for commas (extra or otherwise), and I have found none. I have placed the script dated August 28, 2016, 11:10 PM in the proper script folder within Fusion 360.

 

Basically the unedited sample files work correctly, and when those unchanged sample files are saved using Excel, an error is introduced. The message reads: No 2d or 3d point at line 0: - CSV file." and that is followed by the file path listing.

 

When you wrote: "As an additional benefit, it better handles CSV files created by Excel. Excel insists on placing commas in otherwise blank rows of CSV files, which I was having to manually remove until I made the above edits to the script.", I was hoping you could elaborate on this issue. I hope I am missing some small detail. 

 

Thank you in advance!

 

Message 6 of 18
Anonymous
in reply to: Anonymous

I took a look and haven't been able to replicate. I re-saved one of the sample files in Excel and ran it in Fusion without issue. The original sample runs under my changes to the script as well. Does your problem crop up if you use the original script, as opposed to my revised one? (The original is still available at the GIT repo link in the top post.)

 

I've been using Excel to create data points for a sketch to import. It consists of multiple splines, which requires each spline to be separated by a blank row in the Excel worksheet. However, when I save the Excel worksheet as a CSV, the blank row in the worksheet results in a row consisting of two commas in the CSV file. My version of Excel "sees" three columns of empty cells for that worksheet row (X, Y, Z), and adds commas into the CSV file even though there's nothing to separate. When I opened the sample CSV in Excel and re-saved it, the commas showed up there as well when I viewed the contents of the CSV file. I'm using Excel 2016 on Windows 10. It looks like you're running Mac OS, and Excel may work differently in that environment.

 

The script reads in data one row (called a "line" in the script) at a time before parsing the results. The original already had the ability to skip blank lines in CSV files. In the snippet above, that's the portion that acts whenever: 

 

line===""

 

My row-skipping changes add the ability to skip rows if the row consists exclusively a single comma (if the file consists only of X & Y data) or two commas, so the script skips a row if either the row is blank, consists of nothing but a comma, or consists of nothing but two commas.

Message 7 of 18
hanskellner
in reply to: Anonymous

Updated the GIT repo with your changes.  Thanks for the update.



Hans Kellner
Senior Manager, Principal Engineer
Message 8 of 18
hanskellner
in reply to: Anonymous

If you open and look at the CSV file that has the error, does the first line have the column names?  On save as CSV one option is to save with column names as the first line.  That might be what's causing the error.  There should be an option in Excel to not write out this header line.



Hans Kellner
Senior Manager, Principal Engineer
Message 9 of 18
hanskellner
in reply to: hanskellner

I have posted a new version of Import CVS Points on Github.  Please download and install this version.  Also, be sure to read the README for details of this version and the updates.

 

Thanks

 

https://github.com/hanskellner/Fusion360ImportCSVPoints



Hans Kellner
Senior Manager, Principal Engineer
Message 10 of 18
Anonymous
in reply to: hanskellner

Hi @hanskellner,

 

thank you for sharing this Add-In, it is exactly what I needed. I have a follow-up question regarding it since I want to expand the code and automate some upstream operations. Using your Add-In, I was able to import the following splines, as shown on the image below (left). I would now like to revolve each closed section as a profile around the X-axis. However, Fusion does not recognize the closed sketches as profiles and therefore I cannot automate the revolving operation. Instead, I need to copy these sketches to another new sketch and then the profiles are appearing closed (as shown on the right of the same image), allowing me to rotate them. 

panajotovic_0-1628510230473.png

Do you maybe have an idea why the sketches are not creating closed profiles? How could I solve this? Thanks in advance!

Best regards

Stefan 

Message 11 of 18
hanskellner
in reply to: Anonymous

Hi@Anonymous -

 

Would it be possible for you to share the document with the sketch?  The one shown on the left.

 

Thanks,

 

Hans



Hans Kellner
Senior Manager, Principal Engineer
Message 12 of 18
Anonymous
in reply to: hanskellner

Hi @hanskellner,

 

the document is a little sensitive, and the community forum does not allow me to upload .csv, .txt or .xlsx files, so here is a simplified version copied directly from the .csv file:

 

46.02	20	0
46.02	8.5	0
		
162.07	20	0
162.07	8.5	0
		
46.02	8.5	0
162.07	8.5	0
		
46.02	20	0
162.07	20	0

 

In any case, even with this simple a data set, the imported sketch is still not recognized as a closed profile:

 

panajotovic_0-1628578638088.png

 

Hopefully, there is a fix for this. Thanks in advance!

 

Best regards

 

Stefan 

Message 13 of 18
hanskellner
in reply to: Anonymous

Hi @Anonymous -

 

I'm exploring how to solve this issue.  The un-closed profiles are even visible in the sample file "simple2D.csv" that's included with the add-in.

 

Sorry I don't yet have a solution.

 

Hans



Hans Kellner
Senior Manager, Principal Engineer
Message 14 of 18
kandennti
in reply to: Anonymous

Hi @Anonymous.

 

I have tested with the attached file.

If you go into Edit Sketch after execution and check "Show Profile", will it not be recognized as a profile?

1.png

The add-in is running with this unchecked to speed up the process.

 

If you are allowed to modify it, you can add the following line to end up with it checked.

・・・
class MyCommandExecuteHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        eventArgs = adsk.core.CommandEventArgs.cast(args)
・・・
                # Done creating sketch entities
                theSketch.isComputeDeferred = False
                theSketch.areProfilesShown = True # <-Here
 
                # Request to create pipes and were any skecth lines added?
                if cmdCreatePipes and len(new_sketch_lines) > 0:
・・・
Message 15 of 18
hanskellner
in reply to: hanskellner

Hi@kandennti -

 

Yes!  That was the issue.  With that disabled, the app wasn't creating the closed profiles.  Thanks for pointing that out.

 

I also found an issue with closing and connecting the start point and last point of a set of lines.

 

The fixes are now posted in the GitHub repo:

 

https://github.com/hanskellner/Fusion360ImportCSVPoints

 

Either grab the archive and copy over your existing files.  Or grab the Python file and replace the one in your installation folder:

 

https://github.com/hanskellner/Fusion360ImportCSVPoints/blob/master/ImportCSVPoints.py

 

Note, be sure to view and download the "raw" version of the Python file.

 

Thanks,

 

Hans



Hans Kellner
Senior Manager, Principal Engineer
Message 16 of 18
kandennti
in reply to: hanskellner

Message 17 of 18
josephp8314
in reply to: hanskellner

Would it be possible to reverse this and be able to export xyz coordinates of point to a csv file?

Message 18 of 18
kandennti
in reply to: josephp8314

Hi @josephp8314 .

 

I created this script a long time ago, but it is available here to export the coordinate values of the displayed sketch points as a CSV file.

https://github.com/kantoku-code/Fusion360-ExportSketchPointsCoordinate 

Since it is a script, it is not easy to use, so if you need it, change it to an add-in.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report