C# assemblies in Grasshopper

Hi all,

I’m struggling with assemblies in C# scripting.

I need to use:

System.Net.Sockets;
System.Text;
System.IO;
System.Data.SqlClient;
System.Collections;
System.Threading.Tasks

Some are in there by default. Like System.IO. But apprently it does not contain .StreamWriter?

If I add assemblies that is already in there (System.Data), I cannot remove it again?

There is clearly something I need to read up on, but where do I find info on the procedure of adding and removing reference assemblies in grasshopper?


Hans

1 Like

The only thing I know is a right click on a c# component and manage assemblies (dll)

If you need a lot of custom functionality, you might be better off writing a Grasshopper component, rather than a script. https://marketplace.visualstudio.com/items?itemName=McNeel.GrasshopperAssemblyforv5

2 Likes

Yes, but there are advantages of using scripts and it does not answer the question. “Manage assemblies” seems…half-finished?

Have you tried using StreamWriter and the other system classes you are looking for? You might just need to use the full path System.IO.xxx… I tried this example in a c# component and it worked just fine:

// Create a string array with the lines of text
string[] lines = { "First line", "Second line", "Third line" };

// Set a variable to the My Documents path.
string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

// Write the string array to a new file named "WriteLines.txt".
using (StreamWriter outputFile = new StreamWriter(mydocpath + @"\WriteLines.txt")) {
    foreach (string line in lines)
        outputFile.WriteLine(line);
}

In the referenced assembly dialog, just delete the assembly you no longer want referenced.[quote=“hjandersen, post:1, topic:42317”]
There is clearly something I need to read up on, but where do I find info on the procedure of adding and removing reference assemblies in grasshopper?
[/quote]

I’m not sure if this reply from @DavidRutten is still current. Maybe @DavidRutten can chime in on that.

Yeah the interface is pretty clunky and unpleasant. It does however allow you to add and remove any assembly you want, so the base-minimum functionality is there.

Note that not all types that share a namespace in .NET are also defined in the same dll. Sometimes you need to load a different dll to get access to a type in a namespace you’ve already got.

But like Giulio said, at some point the added pain of managing assemblies just doesn’t make it worth it and switching to Visual Studio is the most sensible thing to do.

Yes, I have tried using the full path. That was when I got confused.

In Visual studio:

GrassHopper c# editor:

Hans

The c# editor is based on QWhale. It might not be doing autocomplete as you would expect. As I mentioned, the example above worked fine, but I just pasted it into the c# component without going through the hassle of typing. The QWhale editor might not autocomplete on that namespace for several reasons.

You are right. You code is working. I guess I got so annoyed that I began to trust the intellisense. As you all suggested I did write the code in vs instead. Working fine. As c# script I cannot get it to work (code belove is a reconstruct. The old code is long gone):

private void RunScript(ref object st)
{

System.Net.Sockets.TcpClient client = new TcpClient("10.0.0.3", 50000);
System.Net.Sockets.NetworkStream nwStream = client.GetStream();

System.Byte[] bytesToSend =  System.Text.ASCIIEncoding.ASCII.GetBytes("echo");
System.Byte[] bytesToRead = null;
int bytesRead = 0;

//---send the text---
nwStream.Write(bytesToSend, 0, bytesToSend.Length);

//---read received text---
bytesToRead = new System.Byte[client.ReceiveBufferSize];
bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);

client.Close();

st = System.Text.Encoding.ASCII.GetString(bytesToRead, 0, bytesRead);

}

Thank you for the help.