Execute external command via ribbon button

Execute external command via ribbon button

mileta_pejovic
Enthusiast Enthusiast
2,677 Views
3 Replies
Message 1 of 4

Execute external command via ribbon button

mileta_pejovic
Enthusiast
Enthusiast

Hi to all, steeping into Revit API for the first time and  I'm having issues with the first attempt to create addin. Keep in mind that I do not have any previous experience with programming and API-s, so i would appreciate if someone could walk me through the process. 

 

I created ExternalCommand for making sheets and it works from AddIns tab > External Commands.
I'm trying to make it executable trough its Button on the ribbon tab, but I'm facing the problem.

 

This is a screenshot of notification window appeared when I try to execute the command by clicking Ribbon panel button:

error.jpg

 

I'm guessing that the problem is somewhere in my addin manifest file or somewhere with the lines for External command executing, but I can't solve it myself... unfortunately...  Any help?

This is my code: 

namespace SheetAddin
{
    class App : IExternalApplication
    {
        public Result OnStartup(UIControlledApplication a)
        {
            
            a.CreateRibbonTab("Toolbar");
           
            RibbonPanel P01 = a.CreateRibbonPanel("Toolbar", "Toolbar");
            string path = Assembly.GetExecutingAssembly().Location;
       
            PushButtonData buttonShow = new PushButtonData("CreateSheets", "CreateSheets", path, "SheetAddin.Toolbars");
            buttonShow.LargeImage = GetImage(Resources.SheetIcon.GetHbitmap());

            //Add the buttons to the panel
            RibbonItem ri1 = P01.AddItem(buttonShow);
         
            return Result.Succeeded;
        }

        //Buttons image size loading on startup
        private System.Windows.Media.Imaging.BitmapSource GetImage(IntPtr bm)
        {
            System.Windows.Media.Imaging.BitmapSource bmSource
              = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                bm,
                IntPtr.Zero,
                System.Windows.Int32Rect.Empty,
                System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

            return bmSource;
        }
        public Result OnShutdown(UIControlledApplication a)
        {
            return Result.Succeeded;
        }

   public class Toolbars : IExternalCommand
        {
            public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
            {
                #region CREATE SHEETS

                UIApplication uiApp = commandData.Application;
                Document doc = uiApp.ActiveUIDocument.Document;
                UIDocument uidoc = uiApp.ActiveUIDocument;

                //define starting sheet number
                int sheetNum = 101;

                  FilteredElementCollector viewColl = new FilteredElementCollector(doc);
                viewColl.OfClass(typeof(ViewPlan));
 
                FilteredElementCollector tblockColl = new FilteredElementCollector(doc);
                tblockColl.OfCategory(BuiltInCategory.OST_TitleBlocks);
                           
                Transaction curTrans = new Transaction(doc, "Create Sheets");
                curTrans.Start();
         
                foreach (ViewPlan curView in viewColl.OrderBy(zz => zz.Name))
                {
                    if (curView.ViewType == ViewType.FloorPlan)
                    {
                        if (curView.IsTemplate == false)
                        {
                            ViewSheet newSheet;
                            newSheet = ViewSheet.Create(doc, tblockColl.FirstElementId());

                            newSheet.Name = curView.Name;
                            newSheet.SheetNumber = "A" + sheetNum.ToString();
                            sheetNum++;

                        }
                    }
                }
                //comit changes and close transaction
                curTrans.Commit();
                curTrans.Dispose();
                #endregion

                return Result.Succeeded;
            }

        }
    }
}

and this is the addin manifest:

<?xml version="1.0" encoding="utf-8"?>
<RevitAddIns>
  </AddIn>-->
  <AddIn Type="Application">
    <Name>Application SheetAddin</Name>
    <Assembly>SheetAddin.dll</Assembly>
    <FullClassName>SheetAddin.App</FullClassName>
    <ClientId>9fb2e77f-2f3f-41ef-9e56-77c7debafc19</ClientId>
    <VendorId>testingrevitapi</VendorId>
    <VendorDescription>MiletaP</VendorDescription>
  </AddIn>
</RevitAddIns>

 

0 Likes
Accepted solutions (1)
2,678 Views
3 Replies
Replies (3)
Message 2 of 4

jeremytammik
Autodesk
Autodesk
Accepted solution

Dear Mileta,

 

Thank you for your query.

 

Welcome to Revit API programming!

I suggest that you first of all take a look at the getting started material and work through the step-by-step instructions provided by the DevTV and My First Revit Plugin video tutorials:

http://thebuildingcoder.typepad.com/blog/about-the-author.html#2

That will show you what other important material is available that you MUST be aware of, answer this question of yours, and many, many more besides.

 

You might also want to check out the walkthroughs in the developer guide:

 

http://help.autodesk.com/view/RVT/2018/ENU/?guid=GUID-93BC4416-FA94-44B3-AA66-931839DA44B4

 

I hope this helps.

Good luck and have fun!

 

Best regards,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 3 of 4

mileta_pejovic
Enthusiast
Enthusiast

Thank You, Jeremy,

didn't really expect someone as Yourself to answer my beginner's question.

I'm total newbie in API and programming world, learning very basics of C# and revit API.

I already found thebuildingcoder site and started to read topics. Have to say it is extremely informative, extremely useful and extremely comprehensive and really appreciate the time and effort you made making it.

Hopefully, I'll have more time in the future, to dedicate to learning revit API and reading more topics from the site.

Anyway, I managed to resolve the problem with that basic HelloWorld integration walktrough guide.

 

Solution: I didn't really separate external application and external command properly and also I forgot to include the command in my manifest file.

Now, the query is resolved and my addin is working via ribbon button.

 

Just to clarify, this may be a dumb question - Does any additional external command in the code have to be separately specified in the addin manifest?

Each external command must have a respective text in the addin manifest (or separate .addin file) in order to be executed? Assuming yes, just to be sure 🙂

0 Likes
Message 4 of 4

jeremytammik
Autodesk
Autodesk

Dear Mileta,

Thank you for your appreciation.

 

The answer is no.

 

In general, in a professional but simple add-in, you will have one single entry for the external application.

 

The external application will define its own ribbon panel and buttons and associate those with its own external commands.

 

You have to register the external commands in the add-in manifest only if you want them to appear in the default built-in Revit External Tools menu instead of defining your own buttons for them.

 

Cheers,

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder