RhinoDoc from .3dm

How can I create a RhinoDoc object from a .3dm file? I am asking this in the context of unit testing.

The only approach I see is to

  1. Create a new RhinoDoc
  2. Create a new File3DM by reading the .3dm
  3. Serially add object to the RhinoDoc from File3DM

Is there any other way that I am missing?

You can open a 3dm file headless, that is, without the Rhino UI loaded

https://developer.rhino3d.com/api/rhinocommon/rhino.rhinodoc/openheadless

Also, check the Rhino.Testing nuget package, it is rather new though.

1 Like

Thanks @menno, I am in fact using that library. I tried the OpenHeadless method and it gives me null. I am making sure the path is correct.

I have the simplest test

using Rhino;
using NUnit.Framework;
using SpaceRhino.Commands;
using Rhino.Testing.Fixtures;

namespace SpaceRhino.UnitTests
{
    [TestFixture]
    public class Tests : RhinoTestFixture
    {
        
        [Test]
        public void PurgeMaterials_WhenCalledOnASpecificFile_ReturnsSpecificString()
        {
            // Arrange
            string projectDirectory = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName;
            string filePath = Path.Combine(projectDirectory, "Files", "purge_materials.3dm");
            RhinoDoc doc = RhinoDoc.OpenHeadless(filePath);

            // Act
            string result = SpacePurgeMaterials.PurgeMaterials(doc);

            // Assert
            Assert.AreEqual("Purged 36 materials from the file.", result);
        }


    }
}

csProj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net48</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
	  <langversion>10.0</langversion>
	  <RhinoSystemDirectory>C:\Program Files\Rhino 7\System</RhinoSystemDirectory>
  </PropertyGroup>

	<ItemGroup>
		<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.*" />
		<PackageReference Include="NUnit" Version="3.14" />
		<PackageReference Include="NUnit3TestAdapter" Version="4.5" />
		<PackageReference Include="Rhino.Testing" Version="8.0.5-beta" />
		<PackageReference Include="RhinoCommon" Version="7.30.23163.13001" />
	</ItemGroup>

	<ItemGroup>
	  <ProjectReference Include="..\SpaceRhino\SpaceRhino.csproj" />
	</ItemGroup>

	<ItemGroup>
		<None Update="Rhino.Testing.Configs.xml" CopyToOutputDirectory="always" />
	</ItemGroup>

</Project>

I have System.AccessViolationException : Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Where I am creating RhinoDoc.

I have made sure the file has full access control

@eirannejad,

Can you please create a sample test that uses RhinoDoc.OpenHeadless in this repo?

@Devang_Chauhan I can replicate this issue in Rhino 8 and net48 framework. Created a ticket for it to get fixed:

RH-80735 Rhino inside crashes with AccessViolationException when closing RhinoDoc instance

1 Like

@eirannejad is it possible to test commands with Rhino.Testing? Take this example:

[Test]
public void TestCommand()
{
    string output = Path.GetDirectoryName(GetType().Assembly.Location);
    string modelPath = Path.Combine(output, @"Files\circle.3dm");

    var doc = RhinoDoc.Open(modelPath, out bool _);
    var result = RhinoApp.RunScript("_-Line 0,0,0 10,10,10", false);
}

I expect this to add a line object to doc. However, it does not, and result == false.

Do you have an example for how to run commands with this library?