Opening a form through a Ribbon button

Opening a form through a Ribbon button

Anonymous
Not applicable
795 Views
4 Replies
Message 1 of 5

Opening a form through a Ribbon button

Anonymous
Not applicable

Hi!

 

I'm trying to open a form created in VisualStudio2019 (Form1.cs) through a button in the ribbon to import user data for a transaction (the form will import dimensions for placing families), I've manage to create the Ribbon and the button, and the button works to import the families and place them in pre-selected locations, but I'm not sure how to open the form and reference the data from it, I've tryed to look for tutorials and in the forum but not really sure how to proceed.

Do I have to copy the text of the Form1.cs into the Class.cs?

 

I would appreciate any reference to tutorial or advise, thanks!

0 Likes
796 Views
4 Replies
Replies (4)
Message 2 of 5

jeremytammik
Autodesk
Autodesk

Keep your form separate from your Revit API code.

 

Here is a nice old example:

 

https://thebuildingcoder.typepad.com/blog/2012/05/the-schedule-api-and-access-to-schedule-data.html

 

Here is an ongoing discussion on a similar topic:

 

https://forums.autodesk.com/t5/revit-api-forum/c-addin-form-reset-the-form-when-show/m-p/9447060

 

 

 



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

0 Likes
Message 3 of 5

corinne.astori
Advocate
Advocate

@Anonymous  I would take a good look at the SDK sample AllViews. There are also other SDK samples that implement this foncionnality. If you are new to the Revit API and programming in general you might need some time to fully understand the concept, but it is worth it.

0 Likes
Message 4 of 5

Anonymous
Not applicable
Dear Jeremy
 
Thanks so much for your fast reply, much appreciated.
 
I've gone through it for a couple of hours but not quite sure where I'm making the mistake.
 
Solution name: ImportDatafromForm
Form name: FormIndependent
Class name that calls the form:RunForm
 
When I press the button in the ribbon I get the error message:  Failed to initialize the add-in"Open form" because the class "ImportDatafromForm.RunForm" cannot be found in the add-inn assembly.
The FullClassName provides the entry point for Revit to call the add-in application.For Revit to run the add-in, you must ensure this class implements the "Autodesk.Revit.UI.IExternalCommand" Interface.
 
However in the adding I've included the class
 

 

  <AddIn Type="Command">
    <Name>ImportDatafromForm</Name>
    <Assembly>ImportDatafromForm.dll</Assembly>
    <AddInId>5493CF3C-C25D-41EB-B933-F0876E0D3819</AddInId>
    <FullClassName>ImportDatafromForm.RunForm</FullClassName>
    <Text>runs the form</Text>
    <Description>Calls the form to run it</Description>
    <VendorId>Test</VendorId>
    <VendorDescription>Test</VendorDescription>
    <VisibilityMode>NotVisibleWhenNoActiveDocument</VisibilityMode>
  </AddIn>

 

 
And the Form
 
 

 

<AddIn Type="Command">
    <Name>ImportDatafromForm</Name>
    <Assembly>ImportDatafromForm.dll</Assembly>
    <AddInId>A210D299-D826-4CB2-8E48-D42F200AE43C</AddInId>
    <FullClassName>ImportDatafromForm.FormIndependent</FullClassName>
    <Text>Form</Text>
    <Description>Collects data in a form</Description>
    <VendorId>Test</VendorId>
    <VendorDescription>Test</VendorDescription>
    <VisibilityMode>NotVisibleWhenNoActiveDocument</VisibilityMode>
  </AddIn>

 

 
I've follow the post https://forums.autodesk.com/t5/revit-api-forum/c-addin-form-reset-the-form-when-show/m-p/9447060 and I understand that the button in the ribbon need to call a class who will open the form, so in the ExternalApplication.cs I got:
 
 PushButtonData button6 = new PushButtonData("Button6", "calls the form", path, "ImportDatafromForm.RunForm");
 
Who should call the form RunForm:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System.IO;
using Autodesk.Revit.DB.Events;

namespace ImportDatafromForm.Commands
{
    [Transaction(TransactionMode.Manual)]

    [Regeneration(RegenerationOption.Manual)]
    public class RunForm : IExternalCommand
    {
        public static Document document;
        public static FormIndependent FormData = new FormIndependent();
        public static UIApplication application;
        public static UIDocument uiDocument;
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;

            Document doc = uiapp.ActiveUIDocument.Document;

            application = commandData.Application;
            document = application.ActiveUIDocument.Document;
            uiDocument = application.ActiveUIDocument;

            FormData.ShowDialog();

            return Result.Succeeded;
        }

    }

}

 

 
That should open the form FormIndependent:
 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace ImportDatafromForm.Commands
{

    public partial class FormIndependent : Form
    {
        public FormIndependent()
        {
            InitializeComponent();
           
        }
        
    }
}

 

0 Likes
Message 5 of 5

mhannonQ65N2
Collaborator
Collaborator

The FullName of a type consists of its namespace and its name. Your RunForm class is in the ImportDatafromForm.Commands namespace, so its FullName is ImportDatafromForm.Commands.RunForm.

 

Also, your FormIndependent class is not a command, so it should not be listed in the addin file.

0 Likes