/// <summary> /// Get a property value from a member /// </summary> /// <param name="args">The incoming arguments</param> /// <param name="api_base_type">The base type</param> /// <param name="pi">The property info object for which you will return the value.</param> /// <param name="return_type">The expected return type.</param> /// <returns></returns> public static Value GetAPIPropertyValue(FSharpList <Value> args, Type api_base_type, PropertyInfo pi, Type return_type) { //var arg0 = (Autodesk.Revit.DB.HermiteFace)DynamoTypeConverter.ConvertInput(args[0], typeof(Autodesk.Revit.DB.HermiteFace)); //var result = arg0.Points; var results = new List <object>(); //there should only be one argument foreach (var arg in args) { if (arg.IsList) { FSharpList <Value> lst = FSharpList <Value> .Empty; //the values here are the items whose properties //you want to query. nothing fancy, just get the //property for each of the items. results.AddRange(((Value.List)arg).Item. Select(v => DynamoTypeConverter.ConvertInput(v, api_base_type)). Select(invocationTarget => pi.GetValue(invocationTarget, null))); } else { var invocationTarget = DynamoTypeConverter.ConvertInput(args[0], api_base_type); results.Add(pi.GetValue(invocationTarget, null)); } } return(ConvertAllResults(results)); }
/// <summary> /// Builds a parameter list given a list of arguments and a list of parameter info /// </summary> /// <param name="args">A list of Values which will be distributed to the output lists</param> /// <param name="pi">An array of parameter info for the method</param> /// <param name="end">The end count</param> /// <param name="parameters">A parameters List to add to.</param> private static void BuildParameterList(FSharpList <Value> args, ParameterInfo[] pi, int end, List <List <object> > parameters) { //for a static method, the number of parameters //will equal the number of arguments on the node if (args.Count() == pi.Count()) { //ARGUMENT LOOP for (int j = 0; j < end; j++) { //create a list to hold each set of arguments var currParams = new List <object>(); //PARAMETER LOOP for (int i = 0; i < pi.Count(); i++) { var arg = args[i]; //if the value is a list, add the jth item converted //or the last item if i exceeds the count of the list if (arg.IsList) { var lst = (Value.List)arg; var argItem = (j < lst.Item.Count() ? lst.Item[j] : lst.Item.Last()); currParams.Add(DynamoTypeConverter.ConvertInput(argItem, pi[i].ParameterType)); } else { //if the value is not a list, //just add the value currParams.Add(DynamoTypeConverter.ConvertInput(arg, pi[i].ParameterType)); } } parameters.Add(currParams); } } //for instance methods, the first argument will be the //item or list of items which will be the target of invocation //in this case, skip parsing the first argument else { //ARGUMENT LOOP for (int j = 0; j < end; j++) { //create a list to hold each set of arguments var currParams = new List <object>(); //PARAMETER LOOP for (int i = 0; i < pi.Count(); i++) { var arg = args[i + 1]; //if the value is a list, add the jth item converted //or the last item if i exceeds the count of the list if (arg.IsList) { //var argItem = ((Value.List)arg).Item.Count() < end ? args.Last() : args[j]; var lst = (Value.List)arg; var argItem = (j < lst.Item.Count() ? lst.Item[j] : lst.Item.Last()); currParams.Add(DynamoTypeConverter.ConvertInput(argItem, pi[i].ParameterType)); } else { //if the value is not a list, //just add the value currParams.Add(DynamoTypeConverter.ConvertInput(arg, pi[i].ParameterType)); } } parameters.Add(currParams); } } }
/// <summary> /// Invoke an API method, using the node's lacing strategy to build lists of arguments /// </summary> /// <param name="node">The node.</param> /// <param name="args">The incoming Values on the node.</param> /// <param name="api_base_type">The API's base type whose method we will invoke.</param> /// <param name="pi">An array of parameter info for the method.</param> /// <param name="mi">The method info for the method.</param> /// <param name="return_type">The expected return type from the method.</param> /// <returns></returns> public static Value InvokeAPIMethod(RevitTransactionNode node, FSharpList <Value> args, Type api_base_type, ParameterInfo[] pi, MethodBase mi, Type return_type) { //if any argument are a list, honor the lacing strategy //compile a list of parameter lists to be used in our method invocation List <List <object> > parameters = null; switch (node.ArgumentLacing) { case LacingStrategy.First: parameters = GetSingleArguments(args, pi); break; case LacingStrategy.Shortest: parameters = GetShortestArguments(args, pi); break; case LacingStrategy.Longest: parameters = GetLongestArguments(args, pi); break; default: parameters = GetSingleArguments(args, pi); break; } var invocationTargetList = new List <object>(); if (api_base_type == typeof(Document) || api_base_type == typeof(FamilyItemFactory) || api_base_type == typeof(ItemFactoryBase)) { if (dynRevitSettings.Doc.Document.IsFamilyDocument) { invocationTargetList.Add(dynRevitSettings.Doc.Document.FamilyCreate); } else { invocationTargetList.Add(dynRevitSettings.Doc.Document.Create); } } else if (api_base_type == typeof(Application)) { invocationTargetList.Add(dynRevitSettings.Revit.Application.Create); } else { if (!mi.IsStatic && !mi.IsConstructor) { if (args[0].IsList) { invocationTargetList.AddRange(((Value.List)args[0]).Item.Select(x => DynamoTypeConverter.ConvertInput(x, api_base_type))); } else { //the first input will always hold the instance //whose methods you want to invoke invocationTargetList.Add(DynamoTypeConverter.ConvertInput(args[0], api_base_type)); } } } //object result = null; List <object> results = null; //if the method info is for a constructor, then //call the constructor for each set of parameters //if it's an instance method, then invoke the method for //each instance passed in. results = mi.IsConstructor ? parameters.Select(x => ((ConstructorInfo)mi).Invoke(x.ToArray())).ToList() : invocationTargetList.SelectMany(x => parameters.Select(y => mi.Invoke(x, y.ToArray())).ToList()).ToList(); StoreElements(node, results); return(ConvertAllResults(results)); }