Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An option to enable a headless ActiveDoc #703

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/compute.geometry/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ static class Config
/// </summary>
public static int LogRetainDays { get; private set; }

/// <summary>
/// RHINO_COMPUTE_CREATE_HEADLESS_DOC: create a headless Rhino document for each request.
/// </summary>
public static bool CreateHeadlessDoc { get; private set; }

public static string[] GetDeprecationWarnings() => _warnings.ToArray();

/// <summary>
Expand All @@ -50,6 +55,7 @@ public static void Load()
ApiKey = GetEnvironmentVariable<string>(RHINO_COMPUTE_KEY, null);
LogPath = GetEnvironmentVariable(RHINO_COMPUTE_LOG_PATH, Path.Combine(Path.GetTempPath(), "Compute", "Logs"), COMPUTE_LOG_PATH);
LogRetainDays = GetEnvironmentVariable(RHINO_COMPUTE_LOG_RETAIN_DAYS, 10, COMPUTE_LOG_RETAIN_DAYS);
CreateHeadlessDoc = GetEnvironmentVariable<bool>(RHINO_COMPUTE_CREATE_HEADLESS_DOC, false);

#if DEBUG
Debug = true;
Expand All @@ -73,6 +79,7 @@ public static void Load()
const string RHINO_COMPUTE_LOG_PATH = "RHINO_COMPUTE_LOG_PATH";
const string RHINO_COMPUTE_LOG_RETAIN_DAYS = "RHINO_COMPUTE_LOG_RETAIN_DAYS";
const string RHINO_COMPUTE_DEBUG = "RHINO_COMPUTE_DEBUG";
const string RHINO_COMPUTE_CREATE_HEADLESS_DOC = "RHINO_COMPUTE_CREATE_HEADLESS_DOC";

// deprecated
const string COMPUTE_BIND_URLS = "COMPUTE_BIND_URLS";
Expand Down
4 changes: 4 additions & 0 deletions src/compute.geometry/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ static void Main(string[] args)

RhinoInside.Resolver.LoadRhino();
LogVersions();

if (Config.CreateHeadlessDoc)
Log.Information("Compute to use headless Rhino documents");

var host = Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
Expand Down
19 changes: 19 additions & 0 deletions src/compute.geometry/ResthopperEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Rhino.Geometry;
using Rhino;

namespace compute.geometry
{
Expand Down Expand Up @@ -75,6 +76,19 @@ static string GrasshopperSolveHelper(Schema input, string body, System.Diagnosti
SetDefaultTolerances(input.AbsoluteTolerance, input.AngleTolerance);
SetDefaultUnits(input.ModelUnits);

// Instantiate headless doc
if (Config.CreateHeadlessDoc)
{
RhinoDoc.ActiveDoc = RhinoDoc.CreateHeadless(null);
RhinoDoc.ActiveDoc.ModelAbsoluteTolerance = input.AbsoluteTolerance;
RhinoDoc.ActiveDoc.ModelAngleToleranceDegrees = input.AngleTolerance;

if (Enum.TryParse(input.ModelUnits, out UnitSystem units))
{
RhinoDoc.ActiveDoc.ModelUnitSystem = units;
}
}

int recursionLevel = input.RecursionLevel + 1;
definition.Definition.DefineConstant("ComputeRecursionLevel", new Grasshopper.Kernel.Expressions.GH_Variant(recursionLevel));

Expand All @@ -98,6 +112,11 @@ static string GrasshopperSolveHelper(Schema input, string body, System.Diagnosti
DataCache.SetCachedSolveResults(body, returnJson, definition);
}
}

// Dispose headless doc
if (RhinoDoc.ActiveDoc is object)
RhinoDoc.ActiveDoc.Dispose();

return returnJson;
}

Expand Down