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

RHINO.INSIDE.REVIT RhinoCode Platform - DO NOT MERGE #1199

Draft
wants to merge 6 commits into
base: 1.x
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/RhinoInside.Revit.GH/Scripting/Refs/Rhino.Runtime.Code.dll filter=lfs diff=lfs merge=lfs -text
2 changes: 2 additions & 0 deletions src/RhinoInside.Revit.GH/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public override GH_LoadingInstruction PriorityLoad()
var types = Assembly.GetExecutingAssembly().GetTypes().Where(x => x.IsDefined(typeof(AssemblyPriorityAttribute), false));
foreach (var type in types)
System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(type.TypeHandle);

Rhino.Runtime.Code.RhinoCode.Platforms.TryRegister(Scripting.RhinoInsideRevitPlatform.Instance);
}
catch { return GH_LoadingInstruction.Abort; }

Expand Down
4 changes: 4 additions & 0 deletions src/RhinoInside.Revit.GH/RhinoInside.Revit.GH.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<ProjectReference Include="..\RhinoInside.Revit\RhinoInside.Revit.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="Scripting\Refs\Rhino.Runtime.Code.dll" Private="False" />
</ItemGroup>

<PropertyGroup>
<RiROutputPath>$(RevitAddinsPath)RhinoInside.Revit\R$(RhinoVersion)\</RiROutputPath>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;

using Rhino.Runtime.Code;
using Rhino.Runtime.Code.Execution;

namespace RhinoInside.Revit.GH.Scripting.Converters
{
public sealed class ElementIdConverter : ParamValueConverter
{
public static ParamConverterIdentity Identity { get; }
= new ParamConverterIdentity(new Guid("5716bd31-2735-4be5-88ac-bcc0304be776"), "ElementId", "mcneel.rhino3dinrevit.rhino");

public ElementIdConverter()
: base(Identity, new ParamType(typeof(Autodesk.Revit.DB.ElementId)))
{
Image = default;
Category = "Revit";
Description = "Converts DB.Element to DB.ElementId";
}

public override bool Cast(ConvertDirection direction, object data, out object target)
{
target = default;

if (ConvertDirection.Incoming == direction
&& data is Autodesk.Revit.DB.Element element)
{
target = element.Id;
return true;
}

return false;
}
}
}
Git LFS file not shown
161 changes: 161 additions & 0 deletions src/RhinoInside.Revit.GH/Scripting/RhinoInsideRevitPlatform.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#if RHINO_8
using System;
using System.Linq;
using System.Collections.Generic;

using Rhino;

using Rhino.Runtime.Code;
using Rhino.Runtime.Code.Editing;
using Rhino.Runtime.Code.Environments;
using Rhino.Runtime.Code.Execution;
using Rhino.Runtime.Code.Execution.Debugging;
using Rhino.Runtime.Code.Platform;
using Rhino.Runtime.Code.Storage;

namespace RhinoInside.Revit.GH.Scripting
{
public sealed class RhinoInsideRevitPlatform : Platform
{
public static IPlatform Instance { get; } = new RhinoInsideRevitPlatform();

#region Converters
public static IParamValueConverter ElementIdConverter { get; } = new Converters.ElementIdConverter();
#endregion

#region Platform
public override PlatformIdentity Id { get; } = new PlatformIdentity(
name: "Rhino.Inside.Revit",
shortName: "RIR",
description: "Rhino.Inside.Revit platform",
domain: "rhino3dinrevit",
taxonomy: "mcneel.rhino3dinrevit.rhino",
RhinoApp.Version
);

public override IPlatformDocument ActiveDocument { get; } = default;

public override IEnumerable<CompileReference> References
{
get
{
yield return CompileReference.FromAssembly(typeof(Autodesk.Revit.DB.IExternalDBApplication).Assembly);
yield return CompileReference.FromAssembly(typeof(Autodesk.Revit.UI.IExternalApplication).Assembly);
yield return CompileReference.FromAssembly(typeof(Autodesk.Windows.IRibbonPopup).Assembly);
}
}

public override IEnumerable<IStorageExtensionFilter> ReferenceFilters
{
get
{
// TODO:
// return extension filters for platfrom plugin file exts
yield break;
}
}

public override IEnumerable<EditorLibrary> EditorLibraries
{
get
{
var revitVersion = new Version(Revit.ActiveDBApplication.SubVersionNumber);
var nugetSpec = new PackageSpec($"Autodesk.Revit.SDK.refs.{revitVersion.Major}");
var nugetPackage = NuGetEnvirons.User.AddPackage(nugetSpec);
var refsDir = revitVersion.Major >= 2025 ? "ref/net8.0" : "ref/net48";
foreach (string revitSDK in nugetPackage.GetFiles(refsDir)
.Where(l => l.EndsWith("RevitAPI.dll") || l.EndsWith("RevitAPIUI.dll")))
{
AssemblyEditorLibrary revitSDKLib = default;
try
{
// TODO:
// implement url builder for https://www.revitapidocs.com/ maybe?
revitSDKLib = new AssemblyEditorLibrary(revitSDK);
}
catch (Exception ex)
{
RhinoCode.Logger.Warn($"Error generating docs for Autodesk.Revit {revitVersion.Major} libraries | {ex.Message}");
}

if (revitSDKLib is AssemblyEditorLibrary)
yield return revitSDKLib;
}
}
}

public override IEnumerable<IParamValueConverter> Converters
{
get
{
// TODO:
// add more converters
yield return ElementIdConverter;
}
}

public override IEnumerable<CompileGuard> GetCompileGuards(BuildKind buildKind)
{
const int FIRST_RHINOCODE_SUPPORTED_REVIT = 2018;
var revitVersion = new Version(Revit.ActiveDBApplication.SubVersionNumber);

// this creates 'flexible' guards for major versions starting from Revit FIRST_RHINOCODE_SUPPORTED_REVIT
// Example: REVIT_2018_OR_GREATER, REVIT_2019_OR_GREATER
foreach (int nextMajor in Enumerable.Range(FIRST_RHINOCODE_SUPPORTED_REVIT,
revitVersion.Major + 1 - FIRST_RHINOCODE_SUPPORTED_REVIT))
{
yield return new CompileGuard($"REVIT_{nextMajor}_OR_GREATER", true);
}

// this creates 'specific' guards that are only available on this Revit version
// Example: REVIT_2025_1, REVIT_2025
yield return new CompileGuard($"REVIT_{revitVersion.Major}_{revitVersion.Minor}", true);
yield return new CompileGuard($"REVIT_{revitVersion.Major}", true);
}

public override IDebugControls CreateDebugControls()
{
// This is not used for now.
// ScriptEditor uses debug controls provided by Rhino3d platform
throw new NotImplementedException();
}

public override void Pause(PauseContext context)
{
// TODO:
// implement a way to pause/disable Revit UI
// this is called when editor debugger is pausing on a breakpoint
// and wants to deactivate the platform functions to disallow
// changing document state
}

public override void Resume()
{
// TODO:
// unpause from paused/disabled state
}

public override bool TryGetAssemblyPath(string name, out string path)
{
// TODO:
// not necessary at this point

path = default;
return false;
}

public override void Write(string text)
{
// TODO:
// not necessary at this point
}

public override void WriteError(string text)
{
// TODO:
// not necessary at this point
}
#endregion
}
}
#endif
Loading