Revit 2017 Add-In Executing a Custom External Command
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi
Having some external command issues, seems like my command executes just fine from the Revit UI, but when executed from my add-in there seems to be no exceptions - but the desired result is not achieved.
Thanks to Jeremy Tammik's post Programmatic Custom Add-In External Command Launch It seems like I am able to execute a custom external command using the UIApplication,.PostCommand by looking up the custom command's revit id using its description (manifest id)
Assemblies
RevitAPI RevitAPIUI
Namespaces
using Autodesk.Revit.DB; using Autodesk.Revit.UI;
Custom Addin for External Command...
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.ReadOnly)]
public class DocumentSwop : IExternalCommand
{
public static string filePlaceholderPath = @"\\someserver\C$\somedirectory\PlaceHolder.rvt";
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
Autodesk.Revit.UI.UIApplication uiapp = null;
Autodesk.Revit.DB.Document doc = null;
Autodesk.Revit.UI.UIDocument docPlaceHolder = null;
string filePlaceholder = null;
string fileRevitActive = null;
int fileCompare = 0;
message = "Velocity.Revit.AddIns.DocumentSwop...";
try
{
// Assign UIApplication
uiapp = commandData.Application;
message += Environment.NewLine + " UIApplication object assigned";
if (uiapp != null)
{
message += Environment.NewLine + " UIApplication not null";
// Assign curren tactive document
doc = uiapp.ActiveUIDocument.Document;
message += Environment.NewLine + " Current active document object assigned";
if (doc != null)
{
message += Environment.NewLine + " Document object not null";
// Continue processing if placeholder file exists
if (File.Exists(filePlaceholderPath))
{
message += Environment.NewLine + " Placeholder file found in file system for " + filePlaceholderPath;
// Assign filename of placeholder file
filePlaceholder = System.IO.Path.GetFileName(filePlaceholderPath);
message += Environment.NewLine + " Placeholder File = " + filePlaceholder;
// Assign filename of current active revit document
fileRevitActive = doc.Title;
message += Environment.NewLine + " Revit Active Document = " + fileRevitActive;
// Compare files
fileCompare = String.Compare(filePlaceholder, fileRevitActive);
message += Environment.NewLine + " File Compare = " + fileCompare.ToString();
// Perform document swop out if files dont match
message += Environment.NewLine + " Swop Active Document...";
if (fileCompare != 0)
{
docPlaceHolder = uiapp.OpenAndActivateDocument(filePlaceholderPath);
doc.Close(true);
}
message += Environment.NewLine + " Swop Active Document completed";
}
message += Environment.NewLine + "Velocity.Revit.AddIns.DocumentSwop completed";
return Autodesk.Revit.UI.Result.Succeeded;
}
else
{
message += Environment.NewLine + " Document object could not be set";
message += Environment.NewLine + "Velocity.Revit.AddIns.DocumentSwop completed";
return Autodesk.Revit.UI.Result.Failed;
}
}
else
{
message += Environment.NewLine + " UIApplication object could not be set";
message += Environment.NewLine + "Velocity.Revit.AddIns.DocumentSwop completed";
return Autodesk.Revit.UI.Result.Failed;
}
}
catch (Exception ex)
{
message += Environment.NewLine + " Velocity.Revit.AddIns.DocumentSwop exception: " + ex.Message;
message += Environment.NewLine + "Velocity.Revit.AddIns.DocumentSwop completed";
return Autodesk.Revit.UI.Result.Failed;
}
finally
{
filePlaceholder = null;
fileRevitActive = null;
fileCompare = 0;
uiapp = null;
doc = null;
docPlaceHolder = null;
}
}
}
Custom Addin for External Command Manifest...
<?xml version="1.0" encoding="utf-8"?>
<RevitAddIns>
<AddIn Type="Command">
<Name>Document Swop</Name>
<Description>Swop the active document</Description>
<Text>Document Swop</Text>
<Assembly>C:\My Addins\DocumentSwop.dll</Assembly>
<AddInId>21A8C920-ED49-434A-AACD-176784316B93</AddInId>
<FullClassName>AddIns.DocumentSwop</FullClassName>
<VendorId>Vendor Name</VendorId>
<VendorDescription>Vendor Desc</VendorDescription>
</AddIn>
</RevitAddIns>
Custom Addin calling External Command...
private static string comIdCommandDocumentSwop = "21A8C920-ED49-434A-AACD-176784316B93";
Autodesk.Revit.UI.RevitCommandId addinId = null;
try
{
addinId = RevitCommandId.LookupCommandId(comIdCommandDocumentSwop);
}
catch (Exception ex)
{
Misc.WriteLog(ex.Message);
}
if (addinId != null)
{
try
{
UIApplication.PostCommand(addinId);
}
catch (Exception ex)
{
Misc.WriteLog(ex.Message);
}
}
Seems like the custom external command is executed, but the desired result is not obtained - thus the document swop does not occur.
Anyone have some ideas?
Regards,
Zach