0
Revit中的我的程序有兩個列表框,根據選擇的內容,它會更改元素。但是,我發現如果用戶選擇了一些選項但是關閉了表單,它仍然會以選定的選項運行。我的代碼如下。我嘗試添加一個基於我在網上找到的Form_Closing方法,但是我的Execute方法一直運行通過一切。Revit Addin在表單關閉時運行
我有一個if語句在執行方法,應該返回Result.Cancelled並應該停止程序。我覺得這與我的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