public Autodesk.Revit.UI.Result Execute(Autodesk.Revit.UI.ExternalCommandData revit, ref string message, ElementSet elements) { try { //create a log file string tempPath = System.IO.Path.GetTempPath(); string logPath = Path.Combine(tempPath, "dynamoLog.txt"); if (File.Exists(logPath)) { File.Delete(logPath); } tw = new StreamWriter(logPath); tw.WriteLine("Dynamo log started " + System.DateTime.Now.ToString()); m_revit = revit.Application; m_doc = m_revit.ActiveUIDocument; trans = new Transaction(m_doc.Document, "Dynamo"); trans.Start(); FailureHandlingOptions failOpt = trans.GetFailureHandlingOptions(); failOpt.SetFailuresPreprocessor(new DynamoWarningSwallower()); trans.SetFailureHandlingOptions(failOpt); m_revit.Idling += new EventHandler <IdlingEventArgs>(OnIdling); #region default level Level defaultLevel = null; FilteredElementCollector fecLevel = new FilteredElementCollector(m_doc.Document); fecLevel.OfClass(typeof(Level)); for (int i = 0; i < fecLevel.ToElements().Count; i++) { defaultLevel = fecLevel.ToElements()[i] as Level; break; } #endregion DynamoWarningSwallower swallow = new DynamoWarningSwallower(); dynElementSettings.SharedInstance.Revit = m_revit; dynElementSettings.SharedInstance.Doc = m_doc; dynElementSettings.SharedInstance.DefaultLevel = defaultLevel; dynElementSettings.SharedInstance.WarningSwallower = swallow; dynElementSettings.SharedInstance.MainTransaction = trans; dynElementSettings.SharedInstance.Writer = tw; //dynElementSettings settings = new dynElementSettings(m_revit, m_doc, //defaultLevel, swallow, trans); //show the log dynamoForm = new dynBench(); //get the window handle Process process = Process.GetCurrentProcess(); new System.Windows.Interop.WindowInteropHelper(dynamoForm).Owner = process.MainWindowHandle; dynamoForm.Show(); if (dynamoForm.DialogResult.HasValue && dynamoForm.DialogResult.Value == false) //the WPF false is "cancel" { tw.WriteLine("Dynamo log ended " + System.DateTime.Now.ToString()); tw.Close(); return(Autodesk.Revit.UI.Result.Cancelled); } } catch (Exception e) { trans.Dispose(); Debug.WriteLine(e.Message + ":" + e.StackTrace); Debug.WriteLine(e.InnerException); message = e.Message + " : " + e.StackTrace; if (tw != null) { tw.WriteLine(e.Message); tw.WriteLine(e.StackTrace); tw.Close(); } return(Autodesk.Revit.UI.Result.Failed); } trans.Commit(); return(Autodesk.Revit.UI.Result.Succeeded); }
public Autodesk.Revit.UI.Result Execute(Autodesk.Revit.UI.ExternalCommandData revit, ref string message, ElementSet elements) { try { //create a log file string tempPath = System.IO.Path.GetTempPath(); string logPath = Path.Combine(tempPath, "dynamoLog.txt"); if (File.Exists(logPath)) File.Delete(logPath); tw = new StreamWriter(logPath); tw.WriteLine("Dynamo log started " + System.DateTime.Now.ToString()); m_revit = revit.Application; m_doc = m_revit.ActiveUIDocument; trans = new Transaction(m_doc.Document, "Dynamo"); trans.Start(); FailureHandlingOptions failOpt = trans.GetFailureHandlingOptions(); failOpt.SetFailuresPreprocessor(new DynamoWarningSwallower()); trans.SetFailureHandlingOptions(failOpt); m_revit.Idling += new EventHandler<IdlingEventArgs>(OnIdling); #region default level Level defaultLevel = null; FilteredElementCollector fecLevel = new FilteredElementCollector(m_doc.Document); fecLevel.OfClass(typeof(Level)); for (int i = 0; i < fecLevel.ToElements().Count; i++) { defaultLevel = fecLevel.ToElements()[i] as Level; break; } #endregion DynamoWarningSwallower swallow = new DynamoWarningSwallower(); dynElementSettings.SharedInstance.Revit = m_revit; dynElementSettings.SharedInstance.Doc = m_doc; dynElementSettings.SharedInstance.DefaultLevel = defaultLevel; dynElementSettings.SharedInstance.WarningSwallower = swallow; dynElementSettings.SharedInstance.MainTransaction = trans; dynElementSettings.SharedInstance.Writer = tw; //dynElementSettings settings = new dynElementSettings(m_revit, m_doc, //defaultLevel, swallow, trans); //show the log dynamoForm = new dynBench(); //get the window handle Process process = Process.GetCurrentProcess(); new System.Windows.Interop.WindowInteropHelper(dynamoForm).Owner = process.MainWindowHandle; dynamoForm.Show(); if (dynamoForm.DialogResult.HasValue && dynamoForm.DialogResult.Value == false) //the WPF false is "cancel" { tw.WriteLine("Dynamo log ended " + System.DateTime.Now.ToString()); tw.Close(); return Autodesk.Revit.UI.Result.Cancelled; } } catch (Exception e) { trans.Dispose(); Debug.WriteLine(e.Message + ":" + e.StackTrace); Debug.WriteLine(e.InnerException); message = e.Message + " : " + e.StackTrace; if (tw != null) { tw.WriteLine(e.Message); tw.WriteLine(e.StackTrace); tw.Close(); } return Autodesk.Revit.UI.Result.Failed; } trans.Commit(); return Autodesk.Revit.UI.Result.Succeeded; }