Created
February 21, 2018 05:37
-
-
Save imAliAsad/2724fbc855b5aa4c64cc05a59fe4d34c to your computer and use it in GitHub Desktop.
Create a button in a custom ribbon in Revit and add the functionality to enable/disable the revit addin button
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class App : IExternalApplication | |
{ | |
public static PushButton Pushbutton { get; set; } | |
public Result OnStartup(UIControlledApplication application) | |
{ | |
OnButtonCreate(application); | |
return Result.Succeeded; | |
} | |
public Result OnShutdown(UIControlledApplication a) | |
{ | |
return Result.Succeeded; | |
} | |
/// <summary> | |
/// Create a custom button on the ribbon | |
/// </summary> | |
/// <param name="application"></param> | |
private void OnButtonCreate(UIControlledApplication application) | |
{ | |
// Create a custom ribbon tab | |
String tabName = "NewTab"; | |
//Create Ribbon panel if it is not created already | |
try { application.CreateRibbonTab(tabName); } catch { } | |
string executableLocation = Path.GetDirectoryName( | |
Assembly.GetExecutingAssembly().Location); | |
string dllLocation = Path.Combine(executableLocation, "ProjectName.dll"); | |
PushButtonData buttondata = new PushButtonData("ProjectNameBtn", "Project \nName", | |
dllLocation, "ProjectName.Command"); | |
buttondata.ToolTip = "This addin will do something"; | |
BitmapImage pb1Image = new BitmapImage(new Uri("pack://application:,,,/ProjectName;component/Resources/Project.ico")); | |
buttondata.LargeImage = pb1Image; | |
var ribbonPanel = application.CreateRibbonPanel(tabName, "Productivity"); | |
// Store the PushButton reference. Later we can use | |
// Pushbutton.Enable = false/true | |
Pushbutton = ribbonPanel.AddItem(buttondata) as PushButton; | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EventControllerWPF : IExternalApplication | |
{ | |
// class instance | |
public static EventControllerWPF Instance; | |
// ModelessForm instance | |
private UserControl defaultControl; | |
public System.Windows.Window Window = new System.Windows.Window(); | |
public Result OnStartup(UIControlledApplication application) | |
{ | |
defaultControl = null; | |
return Result.Succeeded; | |
} | |
public Result OnShutdown(UIControlledApplication application) | |
{ | |
if (Window != null) | |
{ | |
Window.Close(); | |
} | |
return Result.Succeeded; | |
} | |
public void ShowForm(UIApplication uiapp) | |
{ | |
if (defaultControl == null) | |
{ | |
if(Instance == null) | |
{ | |
Instance = this; | |
} | |
// A new handler to handle request posting by the dialog | |
ExternalRevitFileSelecter handler = new ExternalRevitFileSelecter(); | |
// External Event for the dialog to use (to post requests) | |
ExternalEvent exEvent = ExternalEvent.Create(handler); | |
// We give the objects to the new dialog; | |
// The dialog becomes the owner responsible for disposing them, eventually. | |
defaultControl = new DefaultControl(exEvent, handler); | |
this.Window.Content = defaultControl; | |
BitmapImage pb1Image = new BitmapImage(new Uri("pack://application:,,,/ProjectName;component/Resources/ProjectName.ico")); | |
this.Window.Icon = pb1Image; | |
this.Window.Title = "Project Name"; | |
this.Window.Show(); | |
// When application runs, make push button disable | |
App.Pushbutton.Enabled = false; | |
// Add an event to to trigger a method | |
// when user close the application | |
// so that we could enable the pushbutton again | |
this.Window.Closed += OnProjectNameClosed; | |
} | |
} | |
// When application closes, enable the Pushbutton again | |
private void OnProjectNameClosed(object sender, EventArgs e) | |
{ | |
App.Pushbutton.Enabled = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment