Revit Add-in Runs First Time Only

SillyLucky2_0
Explorer
Explorer

Revit Add-in Runs First Time Only

SillyLucky2_0
Explorer
Explorer

Hello,

 

I have created an add-in to create documents/drawings. The creation of the documents has been tested and works. I have a Form with a few checkboxes to allow the selection of what file types are needed. The issue I am having is the first time I open Revit and run the add-in, everything functions correctly. However, when I try to use the add-in a second time in the session, the form opens, but on submit_click, nothing happens. The dialog closes and nothing happens. Any insight as to why it works the first time and not after that would be appreciated. Class and Form scripts can be found below. Sorry for the length of the scripts, I tried to remove all unnecessary items. 

 

Class.cs

 

namespace ProjectPrintingOptions
{
    [Transaction(TransactionMode.Manual)]

    public class Command : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Autodesk.Revit.ApplicationServices.Application app = uiapp.Application;
            Document doc = uidoc.Document;

            ProjectPrintForm Form1 = new ProjectPrintForm(commandData);

            Form1.ShowDialog();

            //Create ViewSet of sheets for printing/exporting

            ViewSet mySheets = new ViewSet();

            using (Transaction trans = new Transaction(doc))
            {
                try
                {
                    trans.Start("Print PDFs");

                    //Code to set print settings and export settings

                    foreach (ViewSheet vv in mySheets)
                    {
  			//Code to print and export sheets as needed
                    }

                    trans.Commit();
                }
                catch (Exception e)
                {
                    Debug.Print(e.Message);
                    trans.RollBack();
                }
            }
            return Result.Succeeded;
        }
    }
}

 

 

Form.cs

 

namespace ProjectPrintingOptions
{
    public partial class ProjectPrintForm : System.Windows.Forms.Form
    {
        private UIApplication uiapp;
        private UIDocument uidoc;
        private Autodesk.Revit.ApplicationServices.Application app;
        private Document doc;

        public ProjectPrintForm (ExternalCommandData commandData)
        {
            InitializeComponent();
            uiapp = commandData.Application;
            uidoc = uiapp.ActiveUIDocument;
            app = uiapp.Application;
            doc = uidoc.Document;
        }

        private void Box1checkbox_CheckedChanged(object sender, EventArgs e)
        {
        }

        private void Box2checkbox_CheckedChanged(object sender, EventArgs e)
        {
        }

        private void Box3checkbox_CheckedChanged(object sender, EventArgs e)
        {
        }

        private void Box4checkbox_CheckedChanged(object sender, EventArgs e)
        {
        }

        public bool Box1Test
        { 
            get { return Box1checkbox.Checked; }
            set { Box1checkbox.Checked = value; }
        }

        public bool Box2Test
        {
            get { return Box2checkbox.Checked; }
            set { Box2checkbox.Checked = value; }
        }

        public bool Box3Test
        {
            get { return Box3checkbox.Checked; }
            set { Box3checkbox.Checked = value; }
        }

        public bool Box4Test
        {
            get { return Box4checkbox.Checked; }
            set { Box4checkbox.Checked = value; }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            cancelButton.DialogResult = DialogResult.Cancel;
        }

        private void submitButton_Click(object sender, EventArgs e)
        {
            submitButton.DialogResult = DialogResult.OK;
            Close();
            return;
        }
    }
}

 

 

0 Likes
Reply
Accepted solutions (1)
473 Views
6 Replies
Replies (6)

scgq425
Advocate
Advocate

Hi @SillyLucky2_0 : i uncomplete to saw the targer problem , but whih the description , this problem like the form-parameter not display , u can debug the code , can check some check statement or push more code to thia page.

LanHui Xu
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature

0 Likes

jeremy_tammik
Autodesk
Autodesk

Thanks to @scgq425  for the helpful suggestion. I agree; there are various options that you can set on the Windows form component that affect how the buttons behave, e.g., when closing the form; maybe some of those settings are involved in this behaviour.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes

nia98bim
Enthusiast
Enthusiast
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
    UIApplication uiapp = commandData.Application;
    UIDocument uidoc = uiapp.ActiveUIDocument;
    Autodesk.Revit.ApplicationServices.Application app = uiapp.Application;
    Document doc = uidoc.Document;

    try
    {
        ProjectPrintForm form = new ProjectPrintForm(commandData);
        form.ShowDialog();

        if (form.DialogResult == DialogResult.OK)
        {
            using (Transaction trans = new Transaction(doc, "Print PDFs"))
            {
                trans.Start();

                // Code to set print settings and export settings
                ViewSet mySheets = form.SelectedSheets; // Assuming ProjectPrintForm exposes SelectedSheets

                foreach (ViewSheet sheet in mySheets)
                {
                    // Code to print and export sheets as needed
                    // Ensure you have proper logic here to handle each selected sheet
                }

                trans.Commit();
            }
        }

        return Result.Succeeded;
    }
    catch (Exception e)
    {
        message = e.Message;
        return Result.Failed;
    }
}

GaryOrrMBI
Collaborator
Collaborator

@SillyLucky2_0 ,

Quick question: Is the failure on second run of the function something that you ran into while testing the function and you're trying to resolve it before putting it into the production environment?

 

If it is then, are you selecting the same views to print both times? Are you using the same output file names and/or location? If so, you are perhaps running into an error when trying to overwrite existing files. Your Try/Catch block may be causing the silent exit in Revit. If you're loading and running the command from within Revit as opposed to running it in debug mode you will not be seeing the Debug.Print(e.message) call from the failure of the Try/Catch block.

 

You can test this by either running it in debug mode from your authoring program or by changing (or adding a line to) the Catch to call up a Revit TaskDialog to show you the error within the Revit environment.

 

All of this assumes that you got carried away deleting stuff to create your post-able version of the command (since I don't see you checking for the returned value from the form).

 

Good luck

-G

Gary J. Orr
GaryOrrMBI (MBI Companies 2014-Current)
aka (past user names):
Gary_J_Orr (GOMO Stuff 2008-2014);
OrrG (Forum Studio 2005-2008);
Gary J. Orr (LHB Inc 2002-2005);
Orr, Gary J. (Gossen Livingston 1997-2002)
0 Likes

admaecc211151
Advocate
Advocate

the same opinion as nia98bim. It seems you not returning anything to main command in the form.

cancelButton.DialogResult = DialogResult.Cancel;
submitButton.DialogResult = DialogResult.OK;

change these to 

this.DialogResult = DialogResult.Cancel;
this.DialogResult = DialogResult.OK;

 so that your code in command area can read it.

And if the Boxcheckboxs will get or change something immediately. Check it would get or set correct value or not when the second time you open form.

0 Likes

SillyLucky2_0
Explorer
Explorer
Accepted solution

Thank you to everyone for the helpful suggestions. The windows form turned out to not be the issue and it was within my print settings. The issue was committing the transaction with all of the print settings. It was failing to save the print settings over the top of the print settings that were saved previously. I'm confused why it was working for me when I was adding the settings without the form. I have adjusted it to rollback the print settings and now it's working properly. Sorry for asking the wrong question, I appreciate everyone taking time to look into it.

0 Likes