Revit Addin 在窗体关闭时运行
Revit Addin Runs When Form is Closed
我在 Revit 中的程序有两个列表框,根据选择的内容,它会对图元进行更改。但是,我发现如果用户选择了一些选项然后关闭了表单,它仍然会 运行 选择选项。我的代码如下。我尝试根据我在网上找到的内容添加一个 Form_Closing 方法,但我的执行方法 运行 一直到 none-the-less。
我在执行方法中有一个 if 语句应该 return 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
Execute
方法将在 GUI 线程之外的另一个线程上调用 - while
循环将看不到更新...您需要想办法两个线程互相发信号。
甚至更好:设置一个 ExternalEvent 处理程序而不是 while 循环,并将 while 循环的主体用作事件处理程序的主体。在 Execute
方法中启动处理程序,并在 window get 关闭时停止它。
我在 Revit 中的程序有两个列表框,根据选择的内容,它会对图元进行更改。但是,我发现如果用户选择了一些选项然后关闭了表单,它仍然会 运行 选择选项。我的代码如下。我尝试根据我在网上找到的内容添加一个 Form_Closing 方法,但我的执行方法 运行 一直到 none-the-less。
我在执行方法中有一个 if 语句应该 return 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
Execute
方法将在 GUI 线程之外的另一个线程上调用 - while
循环将看不到更新...您需要想办法两个线程互相发信号。
甚至更好:设置一个 ExternalEvent 处理程序而不是 while 循环,并将 while 循环的主体用作事件处理程序的主体。在 Execute
方法中启动处理程序,并在 window get 关闭时停止它。