Rhino Compute web application export to .sat/.dwg

Hi,
I am just getting satrted with Rhino Compute. I am developing a test MVC web application. After some geometry calculation (where compute is used) or simply after reading Rhino file(using Rhino3dmIO) I wish to export geometry as “.sat” file. How can I possibly do that?
see attached image.

Any help in this reagrd would be great.
Thanks,
OBH

You could create a plugin that you install to your Rhino.Compute server which allows you to export to those file formats. Here is a sample that does something similar: https://github.com/mcneel/rhino-developer-samples/tree/7/compute/cs/ComputePlugIn

1 Like

I am trying to implement the ComputePlugin example in C#. I don’t think my endpoints are set up properly. Can anybody offer some step-by-step advice? I’m also pasting the C# code I’m using to try to convert the .3dm:

using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

class Program
{
    private static readonly string computeUrl = "http://localhost:5000/";
    private static readonly string file3dmPath = "./files/fromCompute.3dm";
    private static readonly HttpClient client = new HttpClient();

    static async Task Main(string[] args)
    {
        // Example 1 - Convert a 3dm file to pdf
        try
        {
            string b64ba = Convert.ToBase64String(File.ReadAllBytes(file3dmPath));
            string endpoint = $"{computeUrl}sample/convert3dmtopdf-string";
            string response = await ComputeFetch(endpoint, new object[] { b64ba });

            // Save the response to a PDF file
            byte[] pdfBytes = Convert.FromBase64String(response);
            File.WriteAllBytes("out.pdf", pdfBytes);
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine(ex);
        }

        // Example 2 - Convert a single layer (by id) of a 3d file to dwg
        try
        {
            string b64ba = Convert.ToBase64String(File.ReadAllBytes(file3dmPath));
            string layerId = "2726aa36-732e-488e-9cf4-0b7cf82e65ce";
            string endpoint = $"{computeUrl}sample/convert3dmtodwg-string_guid";
            string response = await ComputeFetch(endpoint, new object[] { b64ba, layerId });

            // Save the response to a DWG file
            byte[] dwgBytes = Convert.FromBase64String(response);
            File.WriteAllBytes("out.dwg", dwgBytes);
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine(ex);
        }
    }

    private static async Task<string> ComputeFetch(string url, object[] args)
    {
        var content = new StringContent(JsonSerializer.Serialize(args), Encoding.UTF8, "application/json");
        HttpResponseMessage response = await client.PostAsync(url, content);
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }
}

I have gotten this to work with C# code based on the original js example in the repository; however, text objects are not showing up in the output .dwg. Any ideas why this might be happening? Does it have to do with the code or the conversion method itself? Any help would be appreciated!

using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

class Program
{
    private static readonly string computeUrl = "http://localhost:5000/";
    private static readonly string file3dmPath = "./files/fromCompute.3dm";
    private static readonly HttpClient client = new HttpClient();

    static async Task Main(string[] args)
    {
        try
        {
            // Read the Rhino 3DM file as bytes and encode to Base64
            byte[] fileBytes = File.ReadAllBytes(file3dmPath);
            string b64ba = Convert.ToBase64String(fileBytes);
            Console.WriteLine($"Base64 string before encoding: {b64ba}");

            // Example 2 - Convert a single layer (by id) of a 3d file to dwg
            string layerId = "4c8b2b44-4607-4e90-806d-ca429f07504d";
            string endpoint = $"{computeUrl}sample/convert3dmtodwg-string_guid";
            string response = await ComputeFetch(endpoint, new object[] { b64ba, layerId });
            Console.WriteLine($"Response from compute service: {response}");

            // Clean the response from non-Base64 characters (excluding whitespace)
            response = CleanNonBase64Characters(response);

            // Save the cleaned response to a DWG file
            byte[] dwgBytes = Convert.FromBase64String(response);
            File.WriteAllBytes("out.dwg", dwgBytes);
            Console.WriteLine("DWG file saved successfully.");
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine($"Error: {ex.Message}");
            Console.Error.WriteLine(ex.StackTrace);
        }
    }

    private static async Task<string> ComputeFetch(string url, object[] args)
    {
        try
        {
            // Serialize arguments to JSON
            var content = new StringContent(JsonSerializer.Serialize(args), Encoding.UTF8, "application/json");
            Console.WriteLine($"Sending request to: {url}");
            Console.WriteLine($"Request content: {await content.ReadAsStringAsync()}");

            // Send POST request to compute service
            HttpResponseMessage response = await client.PostAsync(url, content);

            // Check if response is successful
            if (!response.IsSuccessStatusCode)
            {
                string errorContent = await response.Content.ReadAsStringAsync();
                Console.Error.WriteLine($"Error response: {response.StatusCode} - {errorContent}");
                throw new HttpRequestException($"Server returned error: {response.StatusCode}");
            }

            // Read and return response content
            string responseContent = await response.Content.ReadAsStringAsync();
            Console.WriteLine($"Received response: {responseContent}");
            return responseContent;
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine($"Exception in ComputeFetch: {ex.Message}");
            Console.Error.WriteLine(ex.StackTrace);
            throw;
        }
    }

    private static string CleanNonBase64Characters(string input)
    {
        // Define characters allowed in Base64 (A-Z, a-z, 0-9, +, /, =)
        string allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

        // Filter out non-Base64 characters (excluding whitespace)
        return new string(input.Where(c => allowedChars.Contains(c) || char.IsWhiteSpace(c)).ToArray());
    }
}

I have been running the js example as well and noticed it also does not output text objects to the output .dwg