Hi,
I am currently trying to open a .3dm file and convert its geometry to meshes for processing (.NET 4.8 project). Following is what I have for now:
Program.cs:
using System;
using Rhino;
using Rhino.Runtime.InProcess;
namespace TestRhinoInside
{
class Program
{
static Rhino.Geometry.Mesh GetMesh(Rhino.RhinoDoc doc)
{
var mesh = new Rhino.Geometry.Mesh();
var count = 0;
foreach (var obj in doc.Objects)
{
if (!obj.IsNormal)
continue;
Console.WriteLine("[C#] {0} - {1} : {2}", ++count, obj.Id, obj.ObjectType.ToString());
if (obj.ObjectType == ObjectType.Brep)
{
var brep = Rhino.Geometry.Brep.TryConvertBrep(obj.Geometry);
if (brep != null)
{
var aggregate = new Rhino.Geometry.Mesh();
aggregate.Append(Rhino.Geometry.Mesh.CreateFromBrep(brep, Rhino.Geometry.MeshingParameters.Default));
mesh.Append(aggregate);
brep.Dispose();
}
}
}
Console.WriteLine("[C#] found {0} objects, generated mesh", doc.Objects.Count);
return mesh;
}
static string OpenRhinoFile(string path)
{
try
{
using (new RhinoCore(new string[] { "/NOSPLASH" }, WindowStyle.NoWindow))
{
Console.WriteLine("[C#] opening file at " + path);
var doc = RhinoDoc.Open(path, out bool wasAlreadyOpen);
if (doc == null)
{
Console.WriteLine("[C#] error, failed to load file");
return "'failed to load file'";
}
else
{
Console.WriteLine("[C#] file loaded successfully");
var mesh = GetMesh(doc);
doc.Dispose();
return "done...";
}
}
}
catch (Exception ex)
{
return "failed";
}
}
static void Main(string[] args)
{
var result = OpenRhinoFile("...PATH...TO...FILE...");
Console.WriteLine(result);
}
}
}
packages.config:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Grasshopper" version="7.16.22067.13001" targetFramework="net48" />
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net48" />
<package id="Rhino.Inside" version="7.0.0" targetFramework="net48" />
<package id="RhinoCommon" version="7.16.22067.13001" targetFramework="net48" />
<package id="RhinoWindows" version="7.16.22067.13001" targetFramework="net48" />
</packages>
I am getting various exceptions at runtime (following traces). A CryptographicException at the creation of RhinoCore, and an ExternalException (no idea where it comes from).
System.Security.Cryptography.CryptographicException
HResult=0x80131430
Message=The input data is not a complete block.
Source=System.Core
StackTrace:
at System.Security.Cryptography.CapiSymmetricAlgorithm.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
This exception was originally thrown at this call stack:
System.Security.Cryptography.CapiSymmetricAlgorithm.TransformFinalBlock(byte[], int, int)
System.Runtime.InteropServices.ExternalException
HResult=0x80004005
Message=A generic error occurred in GDI+.
Source=System.Drawing
StackTrace:
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
But even more problematic than this: depending of the file I am trying to open, the program either works successfully or exits with a 0xc0000409 code (no exception thrown). This happens during my traversal of the objects in the scene.
What am I doing incorrectly ?
Thanks for your help.