0

My program in Revit has two list boxes and depending on what is selected, it will make changes to elements. However, I have found that if a user selects some options but then closes the form, it will still run with the selected options. My code is below. I tried to add a Form_Closing method based on what I have found online but my Execute method runs all the way through none-the-less.

I have an if statement in the execute method that should return Result.Cancelled and should stop the program. I feel that it has something to do with the event of my Form_Closing method. Thank you for the help.

    namespace CircuitCheck
{
    [Transaction(TransactionMode.Manual)]
    [RegenerationAttribute(RegenerationOption.Manual)]
    public class Command : IExternalCommand
    {
        private static Autodesk.Revit.DB.Color color;
        private static OverrideGraphicSettings ogs;
        private static OverrideGraphicSettings ogsOriginal;
        private static Boolean cancelled;


        public Command()
        {
            color = new Autodesk.Revit.DB.Color(138, 43, 226); // RGB
            ogs = new OverrideGraphicSettings();
            ogsOriginal = new OverrideGraphicSettings();
            ogs.SetProjectionLineColor(color);
            cancelled = false;
        }


        public partial class CircuitCheckerForm : System.Windows.Forms.Form
        {
            private Boolean CheckClicked;
            public CircuitCheckerForm(IWin32Window owner)
            {
                InitializeComponent();
                this.ShowDialog(owner);
            }

            public String[] getSelectionElementsLB()
            {
                String[] sel = new String[7];
                int count = 0;
                foreach ( object li in ElementsLB.SelectedItems )
                {
                    String text = li.ToString();
                    sel[count] = text;
                    count++;
                }
                return sel;
            }
            public String getSelectionPlaceLB()
            {
                return PaceLB.SelectedItem.ToString();
            }

            private void Check_Click(object sender, EventArgs e)
            {
                this.Hide();
                CheckClicked = true;
            }

            private System.ComponentModel.IContainer components = null;

            private void Form_Closing(FormClosedEventArgs e)
            {
                if (!CheckClicked)
                {
                    cancelled = true;
                    this.Close();
                    System.Windows.Forms.Application.Exit();
                }
            }

            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
            private void InitializeComponent()
            {
                CheckClicked = false;

               //setup for form not shown

            }
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.ListBox PaceLB;
            private System.Windows.Forms.ListBox ElementsLB;
            private System.Windows.Forms.Label label2;
            private System.Windows.Forms.Label label3;
            private System.Windows.Forms.Button Check;
        }
        class RevisionData
        {
            //public int Sequence { get; set; }
            //public RevisionNumberType Numbering { get; set; }
            //public string Date { get; set; }
            //public string Description { get; set; }
            //public bool Issued { get; set; }
            //public string IssuedTo { get; set; }
            //public string IssuedBy { get; set; }
            //public RevisionVisibility Show { get; set; }
        }




        public Result Execute(
      ExternalCommandData commandData,
      ref string message,
      ElementSet elements)
        {
            Document document = commandData.Application.ActiveUIDocument.Document;
            using (Transaction trans = new Transaction(document))
            {
                IWin32Window revit_window
              = new JtWindowHandle(
                ComponentManager.ApplicationWindow);

                UIApplication uiapp = commandData.Application;
                UIDocument uidoc = uiapp.ActiveUIDocument;
                Document doc = uidoc.Document;
                trans.Start("Check");
                if (doc.IsFamilyDocument)
                {
                    TaskDialog.Show("Not a Revit RVT Project",
                      "This command requires an active Revit RVT file.");

                    return Result.Failed;
                }





                Boolean messedUp = true;
                Boolean All = false, lightF = false, recep = false, elecEquip = false, equipCon = false, justView = true;

                while (messedUp)
                {
                    CircuitCheckerForm form = new CircuitCheckerForm(revit_window);
                    if(cancelled) //**************************************
                    {
                        trans.Dispose();
                        return Result.Cancelled;
                    }             //**************************************
                    String[] item = form.getSelectionElementsLB();
                    int numSel = 0;
                    for (int x = 0; x < item.Length; x++)
                    {
                        if (item[x] != null)
                        {
                            if (item[x].Equals("All"))
                            {
                                All = true;
                                messedUp = false;
                                numSel++;
                                break;
                            }
                            else if (item[x].Equals("Ligthing Fixtures"))
                            {
                                lightF = true;
                                messedUp = false;
                                numSel++;
                            }
                            else if (item[x].Equals("Recepticales"))
                            {
                                recep = true;
                                messedUp = false;
                                numSel++;
                            }
                            else if (item[x].Equals("Electrical Equipment (including Panels)"))
                            {
                                elecEquip = true;
                                messedUp = false;
                                numSel++;
                            }
                            else if (item[x].Equals("Equipment Connection"))
                            {
                                equipCon = true;
                                messedUp = false;
                                numSel++;
                            }
                        }
                    }
                    if(numSel == 0)
                    {
                        TaskDialog.Show("Error", "No elements were selected for checking. Please relaunch the program to try again.");
                        trans.Dispose();
                        return Result.Failed;
                    }

                    if (form.getSelectionPlaceLB().Equals("Entire Project"))
                    {
                        justView = false;
                    }
                    else if (form.getSelectionPlaceLB().Equals("Elements in Current View"))
                    {
                        justView = true;
                    }
                    else
                    {
                        messedUp = true;
                        TaskDialog.Show("Error", "A place must be selected.");
                    }


                    int notCircuited = 0;


                    Autodesk.Revit.DB.View view = doc.ActiveView;




                    if (All)
                    {
                        if (justView)
                        {
                            notCircuited += CheckLightF(doc, doc.ActiveView);
                            notCircuited += CheckRecep(doc, doc.ActiveView);
                            notCircuited += CheckElecEquip(doc, doc.ActiveView);
                            notCircuited += CheckEquipCon(doc, doc.ActiveView);
                        }
                        else
                        {
                            FilteredElementCollector viewCollector = new FilteredElementCollector(document);
                            viewCollector.OfClass(typeof(Autodesk.Revit.DB.ViewPlan));

                            foreach (Element viewElement in viewCollector)
                            {
                                Autodesk.Revit.DB.View view2 = (Autodesk.Revit.DB.View)viewElement;
                                notCircuited += CheckLightF(doc, view2);
                                notCircuited += CheckRecep(doc, view2);
                                notCircuited += CheckElecEquip(doc, view2);
                                notCircuited += CheckEquipCon(doc, view2);
                            }

                        }
                        if (notCircuited == 0)
                        {
                            TaskDialog.Show("Circuit Checker", notCircuited + " elements are not circuited in this view.\nYou did good, mate.");
                            trans.Commit();
                        }
                        else
                        {
                            TaskDialog.Show("Circuit Checker", notCircuited + " elements are not circuited in this view.\nGet your shit together...");
                            trans.Commit();
                        }
                    }

                    if (!trans.HasEnded())
                    {
                        if (lightF)
                        {
                            if (justView)
                            {
                                notCircuited += CheckLightF(doc, doc.ActiveView);
                            }
                            else
                            {
                                FilteredElementCollector viewCollector = new FilteredElementCollector(document);
                                viewCollector.OfClass(typeof(Autodesk.Revit.DB.ViewPlan));

                                foreach (Element viewElement in viewCollector)
                                {
                                    Autodesk.Revit.DB.View view2 = (Autodesk.Revit.DB.View)viewElement;
                                    notCircuited += CheckLightF(doc, view2);
                                }
                            }
                        }

                        if (recep)
                        {
                            if (justView)
                            {
                                notCircuited += CheckRecep(doc, doc.ActiveView);
                            }
                            else
                            {
                                FilteredElementCollector viewCollector = new FilteredElementCollector(document);
                                viewCollector.OfClass(typeof(Autodesk.Revit.DB.ViewPlan));

                                foreach (Element viewElement in viewCollector)
                                {
                                    Autodesk.Revit.DB.View view2 = (Autodesk.Revit.DB.ViewPlan)viewElement;
                                    notCircuited += CheckRecep(doc, view2);
                                }
                            }
                        }
                        if (elecEquip)
                        {
                            if (justView)
                            {
                                notCircuited += CheckElecEquip(doc, doc.ActiveView);
                            }
                            else
                            {
                                FilteredElementCollector viewCollector = new FilteredElementCollector(document);
                                viewCollector.OfClass(typeof(Autodesk.Revit.DB.ViewPlan));

                                foreach (Element viewElement in viewCollector)
                                {
                                    Autodesk.Revit.DB.View view2 = (Autodesk.Revit.DB.ViewPlan)viewElement;
                                    notCircuited += CheckElecEquip(doc, view2);
                                }
                            }
                        }
                        if (equipCon)
                        {
                            if (justView)
                            {
                                notCircuited += CheckEquipCon(doc, doc.ActiveView);
                            }
                            else
                            {


                                FilteredElementCollector viewCollector = new FilteredElementCollector(document);
                                viewCollector.OfClass(typeof(Autodesk.Revit.DB.ViewPlan));

                                foreach (Element viewElement in viewCollector)
                                {
                                    Autodesk.Revit.DB.View view2 = (Autodesk.Revit.DB.ViewPlan)viewElement;
                                    notCircuited += CheckEquipCon(doc, view2);
                                }
                            }
                        }

                        if (notCircuited == 0)
                        {
                            TaskDialog.Show("Circuit Checker", notCircuited + " elements are not circuited in this view.\nYou did good, mate.");
                            trans.Commit();
                        }
                        else
                        {
                            TaskDialog.Show("Circuit Checker", notCircuited + " elements are not circuited in this view.\nGet your shit together...");
                            trans.Commit();
                        }
                    }

                }


                return Result.Succeeded;
            }
        }

        //some methods used are not shown as they are only used in the execute method
Jacob Bunzel
  • 165
  • 1
  • 4
  • 15

1 Answers1

0

The Execute method will be called on another thread than the GUI thread - the while loop won't see the update... you'll need to figure out a way for the two threads to signal each other.

Or even better: Set up an ExternalEvent handler instead of the while loop and use the body of the while loop as the body of the event handler. Start the handler in the Execute method and stop it when the window get's closed.

Daren Thomas
  • 67,947
  • 40
  • 154
  • 200