Revit crash when call Export() function after call C# SaveFileDialog

Revit crash when call Export() function after call C# SaveFileDialog

winderbless
Explorer Explorer
965 Views
5 Replies
Message 1 of 6

Revit crash when call Export() function after call C# SaveFileDialog

winderbless
Explorer
Explorer

hi everyone,

i meet a strange problem, thus

First, i call Document.Export() function export a view to dwg;

Secong, i call C# Class  SaveFileDialog want to select a path,

but when  SaveFileDialog.ShowDialog() called, Revit will Crash.

a necessary condition to show this problem is when command begin runing, change window focus to another window, like Windows Exploer.

 

my code is like this:

public class ExportCmd : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            Document doc = commandData.Application.ActiveUIDocument.Document;            
 
            ICollection<Element> views = new FilteredElementCollector(doc).OfClass(typeof(ViewPlan)).ToElements();
 
            List<ElementId> lstPlanViews = new List<ElementId>();           
            foreach (Element v in views)
            {
                ViewPlan tv = (ViewPlan)v;
                if (tv.CanBePrinted)
                {
                    lstPlanViews.Add(tv.Id);
                    break;
                }
            }
 
            DWGExportOptions m_dwgOptions = new DWGExportOptions
            {
                FileVersion = ACADVersion.R2013,
            };
            doc.Export(@"d:/aaa", "Teeee", lstPlanViews, m_dwgOptions);
 
            Microsoft.Win32.SaveFileDialog saveDlg = new Microsoft.Win32.SaveFileDialog();            
            if (saveDlg.ShowDialog() == true)
            {
                ;
            }
 
            return Result.Succeeded;
        }
    }

This problem is shown in Revit 2015 to Revit 2018.

 

Did any one meeted this problem?

thx.

0 Likes
966 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable

Check using 'try.....catch()' in your code.

You will get better idea about crash, when debugger goes into 'catch' section.

try
{
         //Execute function code
         //your code

}
catch(Exception ex)
{
        System.Windows.Forms.MessageBox.Show(ex.Message);
}
0 Likes
Message 3 of 6

BIM.Frankliang
Collaborator
Collaborator

Dear Friend,

 

         First, the output folder must exist (API required)

         Second, I used to System.Windows.Forms.SaveFileDialog, you could try

         Wish it is helpful for you 🙂

 

Best Regards

Frank Liang

TJAD BIM Software Dev Lead

0 Likes
Message 4 of 6

winderbless
Explorer
Explorer

Here is some more detail about this problem:

1. In Revit 2015 - Revit 2018, when this problem shown, Revit will crash immediately, and show this dialog, when click the button(the text in button means OK), revit will close and show Autodesk Error Report Dialog. 

2016Crash.png

2. After I installed Revit 2018.0.3 Security Fix Updata, when this problem shown, Revit 2018 show more information:

2018Deatil.png

after click the button OK, Revit work well, but when i close Revit will get this message:

(the chinese means "meet uncomfortab parameter", and the button means close.)

2018unexpected.png

 

3. if I use Autodesk.Revit.UI.FileSaveDialog replace WinForm`s SaveFileDialog,  it worked well.

 

A necessary condition to show this problem is when command begin runing, change window focus to another window, like Windows Explorer. if did`t change window focus, this problem whould`t shown.

and i write a function to active Revit`s Window manual before i show FileSaveDialog. it worked in most times.

[System.Runtime.InteropServices.DllImport("user32.dll")]
[return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)] private static extern bool SetForegroundWindow(IntPtr hWnd);       private static bool ActivateWindow() {     System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessesByName("Revit").FirstOrDefault();     IntPtr ptr = p.MainWindowHandle;     if (ptr != IntPtr.Zero)     {         SetForegroundWindow(ptr);         return true;     }     return false; }

 

so, i think this problem is interrelated Window Focus Change, maybe is a bug in Revit API Document.Export() function.

0 Likes
Message 5 of 6

winderbless
Explorer
Explorer

thank your reply.

i tryed this, but it dit`t work,.

i think is because Revit has crash, so the program in catch did`t have chance run.

0 Likes
Message 6 of 6

winderbless
Explorer
Explorer

thank your reply and 

1. i created the output folder manual just for test.

2. at begining i truly used System.Windows.Forms.SaveFileDialog, i think problem in this dialog, so i change to Win32 dialog.

0 Likes