Skip to content
Open
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
49 changes: 49 additions & 0 deletions SampleCommandSet/Commands/Access/GetParametersCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Autodesk.Revit.UI;
using Newtonsoft.Json.Linq;
using revit_mcp_sdk.API.Base;
using System;
using System.Collections.Generic;

namespace SampleCommandSet.Commands.Access
{
/// <summary>
/// Batch read parameters for elements.
/// JSON-RPC method: get_parameters
/// params: {
/// elementIds: string[], // required, ElementId as string
/// paramNames?: string[] // optional, return all parameters by name when omitted
/// }
/// returns: array of { elementId, parameters: [{ name, value, storageType, isReadOnly, hasValue }] }
/// </summary>
public class GetParametersCommand : ExternalEventCommandBase
{
private GetParametersEventHandler _handler => (GetParametersEventHandler)Handler;

public override string CommandName => "get_parameters";

public GetParametersCommand(UIApplication uiApp)
: base(new GetParametersEventHandler(), uiApp)
{
}

public override object Execute(JObject parameters, string requestId)
{
if (parameters == null)
throw new ArgumentException("params cannot be null");

var elementIds = parameters["elementIds"]?.ToObject<List<string>>();
if (elementIds == null || elementIds.Count == 0)
throw new ArgumentException("elementIds is required and cannot be empty");

var paramNames = parameters["paramNames"]?.ToObject<List<string>>();

_handler.ElementIds = elementIds;
_handler.ParamNames = paramNames;

if (!RaiseAndWaitForCompletion(20000))
throw new TimeoutException("get_parameters timeout");

return _handler.Result;
}
}
}
127 changes: 127 additions & 0 deletions SampleCommandSet/Commands/Access/GetParametersEventHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using revit_mcp_sdk.API.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace SampleCommandSet.Commands.Access
{
public class GetParametersEventHandler : IExternalEventHandler, IWaitableExternalEventHandler
{
public List<string> ElementIds { get; set; }
public List<string> ParamNames { get; set; }

public object Result { get; private set; }

private readonly ManualResetEvent _resetEvent = new ManualResetEvent(false);

public bool WaitForCompletion(int timeoutMilliseconds = 15000)
{
return _resetEvent.WaitOne(timeoutMilliseconds);
}

public void Execute(UIApplication app)
{
try
{
var doc = app.ActiveUIDocument.Document;
var results = new List<object>();

foreach (var idStr in ElementIds ?? Enumerable.Empty<string>())
{
if (!int.TryParse(idStr, out int idVal))
continue;

var element = doc.GetElement(new ElementId(idVal));
if (element == null)
continue;

var paramEntries = new List<object>();

if (ParamNames != null && ParamNames.Any())
{
foreach (var name in ParamNames)
{
var p = element.LookupParameter(name);
if (p != null)
paramEntries.Add(MakeParamEntry(p));
}
}
else
{
foreach (Parameter p in element.Parameters)
{
if (p?.Definition == null) continue;
paramEntries.Add(MakeParamEntry(p));
}
}

results.Add(new
{
elementId = idStr,
parameters = paramEntries
});
}

Result = results;
}
catch (Exception ex)
{
Result = new { error = ex.Message };
}
finally
{
_resetEvent.Set();
}
}

private object MakeParamEntry(Parameter p)
{
string name = p.Definition?.Name ?? "";
string storageType = p.StorageType.ToString();
bool isReadOnly = p.IsReadOnly;
bool hasValue = p.HasValue;

object value = null;
try
{
switch (p.StorageType)
{
case StorageType.String:
value = p.AsString();
break;
case StorageType.Double:
value = p.AsDouble();
break;
case StorageType.Integer:
value = p.AsInteger();
break;
case StorageType.ElementId:
value = p.AsElementId()?.IntegerValue;
break;
default:
value = null;
break;
}
}
catch { }

return new
{
name,
value,
storageType,
isReadOnly,
hasValue
};
}

public string GetName()
{
return "get parameters";
}
}
}

49 changes: 49 additions & 0 deletions SampleCommandSet/Commands/Modify/SetParametersCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Autodesk.Revit.UI;
using Newtonsoft.Json.Linq;
using revit_mcp_sdk.API.Base;
using System;
using System.Collections.Generic;

namespace SampleCommandSet.Commands.Modify
{
/// <summary>
/// Batch write element parameters.
/// JSON-RPC method: set_parameters
/// params: {
/// updates: [ { elementId: string, parameters: [ { name: string, value: any } ] } ],
/// dryRun?: bool
/// }
/// returns: summary and per-element results
/// </summary>
public class SetParametersCommand : ExternalEventCommandBase
{
private SetParametersEventHandler _handler => (SetParametersEventHandler)Handler;

public override string CommandName => "set_parameters";

public SetParametersCommand(UIApplication uiApp)
: base(new SetParametersEventHandler(), uiApp)
{
}

public override object Execute(JObject parameters, string requestId)
{
if (parameters == null)
throw new ArgumentException("params cannot be null");

var updates = parameters["updates"]?.ToObject<List<SetParametersEventHandler.ElementUpdate>>();
if (updates == null || updates.Count == 0)
throw new ArgumentException("updates is required and cannot be empty");

bool dryRun = parameters["dryRun"]?.Value<bool>() ?? false;

_handler.Updates = updates;
_handler.DryRun = dryRun;

if (!RaiseAndWaitForCompletion(30000))
throw new TimeoutException("set_parameters timeout");

return _handler.Result;
}
}
}
Loading