Skip to content

Commit

Permalink
Merge pull request #703 from tt-acm/dev_headless-doc
Browse files Browse the repository at this point in the history
An option to enable a headless ActiveDoc
  • Loading branch information
andyopayne authored Jan 27, 2025
2 parents c1ecf6b + 03bb178 commit d187c57
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
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

0 comments on commit d187c57

Please sign in to comment.