Skip to content

Commit

Permalink
rds : improve generation of scripts for static members
Browse files Browse the repository at this point in the history
  • Loading branch information
NeVeSpl committed May 15, 2024
1 parent 496fa08 commit 3de56bd
Show file tree
Hide file tree
Showing 19 changed files with 106 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@ public static IMemberOverride ByFunc<TReturnType>(Expression<Func<Document, TFor
{
var compiledGetter = getter.Compile();
var methodCallExpression = getter.Body as MethodCallExpression;
//var syntax = methodCallExpression.ToString();

string syntax = null;
if (methodCallExpression.Object is ParameterExpression)
{
var uniformMethodCallExpression = methodCallExpression.Update(Expression.Parameter(methodCallExpression.Object.Type, "item"), methodCallExpression.Arguments);
syntax = uniformMethodCallExpression.ToString();
}
if (methodCallExpression.Object == null)
{
syntax = $"{methodCallExpression.Method.DeclaringType.Name}." + methodCallExpression.ToString();
}
var uniqueId = getter.GetUniqueId();

return new MemberOverride<TForType>()
Expand All @@ -35,7 +45,7 @@ public static IMemberOverride ByFunc<TReturnType>(Expression<Func<Document, TFor
MemberAccessorFactory = () =>
{
var accessor = new MemberAccessorByFunc<TForType, TReturnType>(compiledGetter);
//accessor.DefaultInvocation.Syntax = syntax;
accessor.DefaultInvocation.Syntax = syntax;
return accessor;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,37 @@ public bool CanBeUsedWith(object @object)
}


public static ISnoopableMemberTemplate Create<TReturnType>(Expression<Func<Document, TForType, TReturnType>> getter, Func<TForType, bool> canBeUsed = null, MemberKind kind = MemberKind.StaticMethod)
public static ISnoopableMemberTemplate Create<TReturnType>(Expression<Func<Document, TForType, TReturnType>> getter,
Func<TForType, bool> canBeUsed = null,
MemberKind kind = MemberKind.StaticMethod)
{
var compiledGetter = getter.Compile();
var methodCallExpression = getter.Body as MethodCallExpression;
var memberAccessor = new MemberAccessorByFunc<TForType, TReturnType>(compiledGetter);

return Create(methodCallExpression.Method.DeclaringType, methodCallExpression.Method.Name, memberAccessor, canBeUsed, kind, () => RevitDocumentationReader.GetMethodComments(methodCallExpression.Method));
memberAccessor.UniqueId = $"{typeof(TForType).Name}_{getter.GetUniqueId()}";


return WithCustomAC(methodCallExpression.Method.DeclaringType, methodCallExpression.Method.Name, memberAccessor, canBeUsed, kind, () => RevitDocumentationReader.GetMethodComments(methodCallExpression.Method));
}
public static ISnoopableMemberTemplate Create(Type declaringType, string memberName, IAccessor memberAccessor, Func<TForType, bool> canBeUsed = null, MemberKind kind = MemberKind.StaticMethod, Func<DocXml> documentationFactoryMethod = null)


public static ISnoopableMemberTemplate WithCustomAC(Type declaringType,
string memberName,
IAccessor memberAccessor,
Func<TForType, bool> canBeUsed = null,
MemberKind kind = MemberKind.StaticMethod,
Func<DocXml> documentationFactoryMethod = null)
{
if (string.IsNullOrEmpty(memberAccessor.UniqueId))
{
memberAccessor.UniqueId= $"{typeof(TForType).Name}_{memberAccessor.GetType().Name}.{memberName}";
}
if (string.IsNullOrEmpty(memberAccessor.DefaultInvocation.Syntax))
{

}

return new MemberTemplate<TForType>()
{
Descriptor = new MemberDescriptor(typeof(TForType), kind, memberName, declaringType, memberAccessor, documentationFactoryMethod),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static IAccessor CreateMemberAccessor(MethodInfo getMethod, MethodInfo se
memberAccessor.UniqueId = getMethod.GetUniqueId();
if (string.IsNullOrEmpty(memberAccessor.DefaultInvocation.Syntax))
{
memberAccessor.DefaultInvocation.Syntax = getMethod.GenerateInvocation();
memberAccessor.DefaultInvocation.Syntax = "item." + getMethod.GenerateInvocation();
}

return memberAccessor;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.Generic;
using Autodesk.Revit.DB;
using RevitDBExplorer.Domain.DataModel.Members;
using RevitDBExplorer.Domain.DataModel.Members.Base;

// (c) Revit Database Explorer https://github.com/NeVeSpl/RevitDBExplorer/blob/main/license.md

namespace RevitDBExplorer.Domain.DataModel.MembersOverrides
{
internal class Document_Overrides : IHaveMembersOverrides
{
public IEnumerable<IMemberOverride> GetOverrides() =>
[
MemberOverride<Document>.ByFunc((doc, document) => Document.GetDocumentVersion(document)),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal class HostObject_FindInserts : MemberAccessorByFunc<HostObject, IList<E

public HostObject_FindInserts() : base((document, hostObject) => hostObject.FindInserts(true, true, true, true))
{
DefaultInvocation.Syntax = "FindInserts(true, true, true, true)";
DefaultInvocation.Syntax = "item.FindInserts(true, true, true, true)";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class Rebar_GetCenterlineCurves : MemberAccessorByType<Rebar>, ICanCrea

public Rebar_GetCenterlineCurves()
{
DefaultInvocation.Syntax = "GetCenterlineCurves(false, true, false, MultiplanarOption.IncludeOnlyPlanarCurves, 0)";
DefaultInvocation.Syntax = "item.GetCenterlineCurves(false, true, false, MultiplanarOption.IncludeOnlyPlanarCurves, 0)";
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class Rebar_GetTransformedCenterlineCurves : MemberAccessorByType<Rebar

public Rebar_GetTransformedCenterlineCurves()
{
DefaultInvocation.Syntax = "GetTransformedCenterlineCurves(false, true, false, MultiplanarOption.IncludeOnlyPlanarCurves, 0)";
DefaultInvocation.Syntax = "item.GetTransformedCenterlineCurves(false, true, false, MultiplanarOption.IncludeOnlyPlanarCurves, 0)";
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal class BoundingBox_Templates : IHaveMemberTemplates
{
public IEnumerable<ISnoopableMemberTemplate> GetTemplates() =>
[
MemberTemplate<BoundingBoxXYZ>.Create(typeof(BoundingBoxXYZ), "BoundingBoxIntersectsFilter", new BoundingBox_BoundingBoxIntersectsFilter(), kind: MemberKind.Extra),
MemberTemplate<BoundingBoxXYZ>.WithCustomAC(typeof(BoundingBoxXYZ), "BoundingBoxIntersectsFilter", new BoundingBox_BoundingBoxIntersectsFilter(), kind: MemberKind.Extra, documentationFactoryMethod: () => new DocXml() { Summary ="TEST" }),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ namespace RevitDBExplorer.Domain.DataModel.MembersTemplates
internal class Document_Templates : IHaveMemberTemplates
{
public IEnumerable<ISnoopableMemberTemplate> GetTemplates() =>
[
MemberTemplate<Document>.Create((doc, target) => Document.GetDocumentVersion(target), kind: MemberKind.StaticMethod),
[
#if R2023_MIN
MemberTemplate<Document>.Create((doc, target) => target.GetChangedElements(Guid.Empty), kind: MemberKind.Method),
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public IEnumerable<ISnoopableMemberTemplate> GetTemplates() =>

#if R2023_MIN
MemberTemplate<Element>.Create((doc, target) => AnalyticalNodeData.GetAnalyticalNodeData(target), kind: MemberKind.StaticMethod, canBeUsed: x => x is ReferencePoint),
MemberTemplate<Element>.Create(typeof(AnalyticalToPhysicalAssociationManager), nameof(AnalyticalToPhysicalAssociationManager.HasAssociation), new AnalyticalToPhysicalAssociationManager_HasAssociation(), kind: MemberKind.AsArgument),
MemberTemplate<Element>.Create(typeof(AnalyticalToPhysicalAssociationManager), nameof(AnalyticalToPhysicalAssociationManager.GetAssociatedElementId), new AnalyticalToPhysicalAssociationManager_GetAssociatedElementId(), kind: MemberKind.AsArgument),
MemberTemplate<Element>.WithCustomAC(typeof(AnalyticalToPhysicalAssociationManager), nameof(AnalyticalToPhysicalAssociationManager.HasAssociation), new AnalyticalToPhysicalAssociationManager_HasAssociation(), kind: MemberKind.AsArgument),
MemberTemplate<Element>.WithCustomAC(typeof(AnalyticalToPhysicalAssociationManager), nameof(AnalyticalToPhysicalAssociationManager.GetAssociatedElementId), new AnalyticalToPhysicalAssociationManager_GetAssociatedElementId(), kind: MemberKind.AsArgument),
#endif
#if R2024_MIN
MemberTemplate<Element>.Create((doc, target) => AnalyticalToPhysicalAssociationManager.IsAnalyticalElement(doc, target.Id), kind: MemberKind.StaticMethod),
MemberTemplate<Element>.Create((doc, target) => AnalyticalToPhysicalAssociationManager.IsPhysicalElement(doc, target.Id), kind: MemberKind.StaticMethod),
MemberTemplate<Element>.Create(typeof(AnalyticalToPhysicalAssociationManager), nameof(AnalyticalToPhysicalAssociationManager.GetAssociatedElementIds), new AnalyticalToPhysicalAssociationManager_GetAssociatedElementIds(), kind: MemberKind.AsArgument),
MemberTemplate<Element>.WithCustomAC(typeof(AnalyticalToPhysicalAssociationManager), nameof(AnalyticalToPhysicalAssociationManager.GetAssociatedElementIds), new AnalyticalToPhysicalAssociationManager_GetAssociatedElementIds(), kind: MemberKind.AsArgument),
#endif
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public IEnumerable<ISnoopableMemberTemplate> GetTemplates() =>
[
MemberTemplate<HostObject>.Create((doc, target) => HostObjectUtils.GetTopFaces(target), kind: MemberKind.StaticMethod),
MemberTemplate<HostObject>.Create((doc, target) => HostObjectUtils.GetBottomFaces(target), kind: MemberKind.StaticMethod),
MemberTemplate<HostObject>.Create(typeof(HostObjectUtils), "GetSideFaces", new HostObjectUtils_GetSideFaces(), kind: MemberKind.StaticMethod ),
MemberTemplate<HostObject>.WithCustomAC(typeof(HostObjectUtils), "GetSideFaces", new HostObjectUtils_GetSideFaces(), kind: MemberKind.StaticMethod ),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class IExternalApplication_Templates : IHaveMemberTemplates
{
public IEnumerable<ISnoopableMemberTemplate> GetTemplates() =>
[
MemberTemplate<IExternalApplication>.Create(typeof(AddInManifestUtility), "GetRevitAddInManifest", new MemberAccessorByFunc<IExternalApplication, RevitAddInManifest>((doc, target) => AddInManifestWizard.Get(target.GetType().Assembly.Location))),
MemberTemplate<IExternalApplication>.WithCustomAC(typeof(AddInManifestUtility), "GetRevitAddInManifest", new MemberAccessorByFunc<IExternalApplication, RevitAddInManifest>((doc, target) => AddInManifestWizard.Get(target.GetType().Assembly.Location))),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class JoinGeometryUtils_Templates : IHaveMemberTemplates
public IEnumerable<ISnoopableMemberTemplate> GetTemplates() =>
[
MemberTemplate<Element>.Create((doc, target) => JoinGeometryUtils.GetJoinedElements(doc, target)),
MemberTemplate<Element>.Create(typeof(JoinGeometryUtils), nameof(JoinGeometryUtils.IsCuttingElementInJoin), new JoinGeometryUtils_IsCuttingElementInJoin())
MemberTemplate<Element>.WithCustomAC(typeof(JoinGeometryUtils), nameof(JoinGeometryUtils.IsCuttingElementInJoin), new JoinGeometryUtils_IsCuttingElementInJoin())
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using RevitDBExplorer.Domain.DataModel.Members.Base;
using RevitDBExplorer.Domain.DataModel.Members;
using Autodesk.Revit.DB.Structure;

namespace RevitDBExplorer.Domain.DataModel.MembersTemplates
{
internal class RebarHostData_Templates : IHaveMemberTemplates
{

public IEnumerable<ISnoopableMemberTemplate> GetTemplates() =>
[
MemberTemplate<Element>.Create((doc, target) => RebarHostData.IsValidHost(target), kind: MemberKind.StaticMethod),
MemberTemplate<Element>.Create((doc, target) => RebarHostData.GetRebarHostData(target), canBeUsed: element => RebarHostData.IsValidHost(element) , kind: MemberKind.StaticMethod),
#if R2025_MIN
MemberTemplate<Element>.Create((doc, target) => RebarHostData.GetRebarHostDirectNeighbors(target), canBeUsed: element => RebarHostData.IsValidHost(element) , kind: MemberKind.StaticMethod),
#endif
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ internal class Schema_Templates : IHaveMemberTemplates
{
public IEnumerable<ISnoopableMemberTemplate> GetTemplates() =>
[
MemberTemplate<Schema>.Create(typeof(Schema), "Get all elements that have entity of this schema", new Schema_GetAllElements(), kind: MemberKind.Extra),
MemberTemplate<Schema>.Create(typeof(Schema), "Erase schema and all entities from the document", new Schema_EraseSchemaAndAllEntities(), kind: MemberKind.Extra),
MemberTemplate<Schema>.WithCustomAC(typeof(Schema), "Get all elements that have entity of this schema", new Schema_GetAllElements(), kind: MemberKind.Extra),
MemberTemplate<Schema>.WithCustomAC(typeof(Schema), "Erase schema and all entities from the document", new Schema_EraseSchemaAndAllEntities(), kind: MemberKind.Extra),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal class UpdaterInfo_Templates : IHaveMemberTemplates
{
public IEnumerable<ISnoopableMemberTemplate> GetTemplates() =>
[
MemberTemplate<UpdaterInfo>.Create(typeof(IUpdater), "GetUpdaterId", new MemberAccessorByFunc<UpdaterInfo, UpdaterId>((doc, target) => UpdaterInfoWizard.Get(target.ApplicationName, target.UpdaterName))),
MemberTemplate<UpdaterInfo>.WithCustomAC(typeof(IUpdater), "GetUpdaterId", new MemberAccessorByFunc<UpdaterInfo, UpdaterId>((doc, target) => UpdaterInfoWizard.Get(target.ApplicationName, target.UpdaterName))),
];
}
}
2 changes: 1 addition & 1 deletion sources/RevitDBExplorer/Domain/DataModel/SnoopableItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public string GenerateScript(TemplateInputsKind inputsKind)
{
return accessorWithCodeGeneration.GenerateInvocationForScript(inputsKind);
}
var invocation = accessor.DefaultInvocation.Syntax ?? Name;
var invocation = accessor.DefaultInvocation.Syntax ?? $"item.{Name}";

return new MemberInvocationTemplateSelector().Evaluate(parent.Object.GetType(), invocation, TemplateCmdKind.Select, inputsKind);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ internal class MemberInvocationTemplateSelector
{
public string Evaluate(MethodInfo getMethod, string invocation, TemplateInputsKind inputsKind)
{
invocation ??= getMethod.GenerateInvocation();
if (string.IsNullOrEmpty(invocation))
{
var prefix = getMethod.IsStatic == false ? "item." : $"{getMethod.DeclaringType.Name}.";
invocation = prefix + getMethod.GenerateInvocation();
}
var cmdkind = getMethod.ReturnType == typeof(void) ? TemplateCmdKind.Update : TemplateCmdKind.Select;

return Evaluate(getMethod.DeclaringType, invocation, cmdkind, inputsKind);
Expand All @@ -22,7 +26,7 @@ public string Evaluate(Type type, string invocation, TemplateCmdKind cmdkind, Te
{
var template = GetTemplateType(cmdkind, inputsKind);

return Evaluate(template, type, invocation);
return EvaluateInternal(template, type, invocation);
}
private Type GetTemplateType(TemplateCmdKind cmdKind, TemplateInputsKind inputsKind)
{
Expand All @@ -48,13 +52,13 @@ private Type GetTemplateType(TemplateCmdKind cmdKind, TemplateInputsKind inputsK



private string Evaluate(System.Type templateType, System.Type type, string invocation)
private string EvaluateInternal(System.Type templateType, System.Type type, string invocation)
{
var template = CodeToStringRepo.GetText(templateType.Name, true);
var result = template.ReplaceMany(new[]
{
("TypePlaceholder", type.GetCSharpName()),
("MethodPlaceholder()", invocation)
("item.MethodPlaceholder()", invocation)
});

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ internal class MemberInvocation_SelectSingle_Template
[CodeToString(nameof(MemberInvocation_SelectSingle_Template))]
object Select(Document document, IEnumerable<TypePlaceholder> inputs)
{
var input = inputs.FirstOrDefault();
return input.MethodPlaceholder();
var item = inputs.FirstOrDefault();
return item.MethodPlaceholder();
}
}

Expand All @@ -50,8 +50,8 @@ internal class MemberInvocation_UpdateSingle_Template
[CodeToString(nameof(MemberInvocation_UpdateSingle_Template))]
void Update(Document document, IEnumerable<TypePlaceholder> inputs)
{
var input = inputs.FirstOrDefault();
input.MethodPlaceholder();
var item = inputs.FirstOrDefault();
item.MethodPlaceholder();
}
}
}

0 comments on commit 3de56bd

Please sign in to comment.