Quantcast
Channel: All Revit API Forum posts
Viewing all articles
Browse latest Browse all 67020

Re: Revit 2017 Add-In Executing a Custom External Command

$
0
0

Hi zach.bester,

 

there may be another approach for invoking commands, using Reflection.

 

using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Reflection;

namespace MyNamespace
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.ReadOnly)]
    [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
    public class Command_InvokeThirdPartyCommand : IExternalCommand
    {
        public Autodesk.Revit.UI.Result Execute(ExternalCommandData revit, ref String message, ElementSet elements)
        {
            try
            {
		// dll path, can be read from addin manifest file
                Assembly asm = Assembly.LoadFrom(@"D:\Tools\2016\RevitLookup.dll");
				
		// Namespace.ClassName, can be read from addin manifest file, too
                Type t = asm.GetType("RevitLookup.CmdSnoopModScope", true); 
				
		// creating an instance of the CmdSnoopModScope
                object obj = Activator.CreateInstance(t);

		// we just hand over the parameters of our own Execute method
                object[] args = new object[3] { revit, message, elements };

                // some modfications must be made because 'message' is a ref parameter
                // http://stackoverflow.com/questions/9881069/why-cant-i-retrieve-the-value-for-parameters-of-type-out-or-ref-using-type-invo
                ParameterModifier pMod = new ParameterModifier(3);
                pMod[1] = true;
                ParameterModifier[] mods = { pMod };
				
		// here we run the instance's Execute method and get the result...
                Result res = (Result)(t.InvokeMember("Execute", 
                    BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Public| BindingFlags.Instance | BindingFlags.Static, 
                    null, obj, args, mods, null, null));

		// ...for just returning it by our own Execute method
                return res;
            }
            catch (Exception ex)
            {
                // log Exception...
                return Autodesk.Revit.UI.Result.Failed;
            }
        }
    }
}

I tested the code with two readonly commands, RevitLookup's 'CmdSnoopModScope' and 'HelloWorld'.
Don't know if the TransactionMode needs to be changed to 'Manual' if the invoked command needs to modify the database.
I suppose that the target method's own TransactionMode attribute will be used.

 

 

Best regards, have a nice weekend,

 

Revitalizer

 

 

 


Viewing all articles
Browse latest Browse all 67020

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>