2017-07-07 13 views
0

Revitのプログラムには2つのリストボックスがあり、選択内容に応じて要素が変更されます。しかし、ユーザーがいくつかのオプションを選択してフォームを閉じると、選択されたオプションで実行されることがわかりました。私のコードは以下の通りです。私は、私がオンラインで見つけたものに基づいてForm_Closingメソッドを追加しようとしましたが、私のExecuteメソッドはnone-the-lessを通してすべて実行されます。フォームが閉じているときRevit Addinが実行されます

私は、Result.Cancelledを返すべきexecuteメソッドのif文を持っており、プログラムを停止する必要があります。私はそれが私のForm_Closingメソッドのイベントと関係があると感じています。お手伝いありがとう。

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 

答えて

0

Execute方法は、GUIのスレッドよりも、別のスレッドで呼び出されます - whileループは、更新は表示されません...あなたは、2つのスレッドがお互いを通知するための方法を把握する必要があります。

またはそれ以上:whileループの代わりにExternalEventハンドラを設定し、whileループの本体をイベントハンドラの本体として使用します。 Executeメソッドでハンドラを起動し、ウィンドウが閉じられると停止します。

関連する問題