Grasshopper

algorithmic modeling for Rhino

Hi All,

I'm a newbie and still learning to use C# language. For the initial study I'd like to convert my python script into c#, and for this case I'd like to create script to export my list of points into csv file.

Here I attach my python script. I tried to look and search from google and it seems more complicated compare to python .

import rhinoscriptsyntax as rs
import Rhino as rc

if Run == True:
    fileName = open(filePath, 'w')
    for pt in pts:
        fileName.write(`pt.X` + ',' + `pt.Y` + ',' + `pt.Z` + '\n')
    fileName.close()


Please advice, or give me any hint to write the C# script.

Thank you in advance.

Tan

Views: 1659

Attachments:

Replies to This Discussion

I would recommend not using files as streams at all if you can help it. Compose the file contents as a string in memory and write it to a file in one go.

[Edit: code changed to fix a bug with string concatenation mentioned below)

string content = string.Empty;

foreach (Point3d pt in pts)

  content += string.Format("{0},{1},{2}\n", pt.X, pt.Y, pt.Z);

System.IO.File.WriteAllText(filePath, content);

If you want to improve upon this, you can use StringBuilder instead of string, and you should probably use the overload of WriteAllText which takes an Encoding.

Also, \n or even \r\n isn't particularly platform safe. Furthermore I think it better if the last line didn't end in a line break. I think I'd prefer this approach (assuming that pts is a collection of known size):

string[] lines = new string[pts.Length or pts.Count];

for (int i = 0; i < lines.Length; i++)

  lines[i] = string.Format("{0},{1},{2}", pt.X, pt.Y, pt.Z);

System.IO.File.WriteAllText(path,

  string.Join(Environment.NewLine, lines),

  System.Text.Encoding.UTF8);

Hi David,

Thank you for your answer, I learn a lot from your explanation. I really aprreciate it.
However, I still face problems to finish the script.

In your first solution I can't use '+=' operation, but It works well with '=' operation (unfortunately that's not what I need as well).

For your second answer, it also gives an error in 'Format (String,Object)' section. I can't get the x,y,z properties from my points.

I wonder, how to solve it? can you tell me where's my mistake?

Thank you!

Hi,

you should put the line

"System.IO.File.WriteAllText(path, string.Join(Environment.NewLine, lines),System.Text.Encoding.UTF8);"

out of your for loop.

Cheers,

Raul

Hi Raul & Tom,

Awesome, thanks for your suggestion!
Now it works.

And I realize the second solution much faster than the first one. It takes only 15-25ms to proceed while the first option takes around 2.0 s
Anyone know why this happen?



As David said, it is better to use StringBuilder

if(run)

{

var csv = new StringBuilder();

var newLine = $"{"X"};{"Y"};{"Z"}";
csv.AppendLine(newLine);

foreach(var pt in pts)
{
newLine = $"{pt.X};{pt.Y};{pt.XZ}";
csv.AppendLine(newLine);
}

File.WriteAllText(filename, csv.ToString());

}

ups, sorry for that... i used the example from a native C# app

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service