Revit crashes when I bind a list of sheets to a form

Revit crashes when I bind a list of sheets to a form

Anonymous
Not applicable
731 Views
7 Replies
Message 1 of 8

Revit crashes when I bind a list of sheets to a form

Anonymous
Not applicable

I'm trying to create a fairly simple batch printing tool and keep running into a really frustrating problem.

The tool displays a form with the list of sheets in the active model, the user selects the sheets they want to print and then click a button to run the batch process. What normally happens is the sheets are printed OK but then at the end of the process Revit will crash with a "An unrecoverable error has occurred..." message box.

If I don't show the form and simply just print all the sheets in the model non-interactively then it works fine.

Initially I tried a WPF form with a list of ViewSheet objects bound to a ListView, then I tried an ObservableCollection instead of a list and even tried populating the ListView without any binding. The result was the same.

I also tried a WinForms form as well and the crash still happens.

 

0 Likes
732 Views
7 Replies
Replies (7)
Message 2 of 8

jeremytammik
Autodesk
Autodesk

Dear Sean,

 

It sounds to me as if you are doing something wrong.

 

Sorry for this rather useless answer.

 

Unfortunately, I cannot say much more (without adding 99% guesswork on my side) based on the input you provide.

 

I hope this helps.

 

Maybe somebody can say more if you clarify exactly what you are doing to cause the problem.

 

By the way, are you absolutely certain that all of your code that interacts with Revit in any way whatsoever is running within a valid Revit API context?

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 3 of 8

Anonymous
Not applicable

Hi Jeremy,

Sorry about the vagueness of my question, i'll try and elaborate a bit.

I'm using a MVVM pattern where I have a model object with a List<ViewSheet> property, a view model that 'wraps' the model's List<ViewSheet> with an ObservableCollection<ViewSheet> property and an xaml window with DataContext set to the view model object and a 

ListView bound to the view model's ObservableCollection<ViewSheet> property.

 

My external command looks like this:

 

 

[Transaction(TransactionMode.Manual)]
public class CmdIssue : IExternalCommand
{
	public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet)
	{
		Model model = new Model(cmdData.Application.ActiveUIDocument);
		ViewModel viewModel = new ViewModel(model);
		ViewWindow window = new ViewWindow();
		window.DataContext = viewModel;
		window.ShowDialog();
		return Result.Succeeded;
	}	
}

 

The model's sheet List:

 

 

internal List<ViewSheet> Sheets {
	get {
		if (_sheets == null) {
			var collector = new FilteredElementCollector(doc).OfClass(typeof(ViewSheet));
			_sheets = new List<ViewSheet>();
			foreach (var sheet in collector) {
				_sheets.Add(sheet as ViewSheet);
			}
		}
		return _sheets;
	}
}

 

The view model's sheet ObservableCollection:

 

public IEnumerable<ViewSheet> Sheets {
	get {
		if(_sheets == null) {
			_sheets = new ObservableCollection<ViewSheet>(model.Sheets);
		}
		return _sheets;
	}
}

 

 

And in the xaml window (ViewWindow):

 

...
<StackPanel> <Button Command="{Binding ProcessSheetsCommand}">Create</Button> <ListView ItemsSource="{Binding Sheets}"/> </StackPanel>
...

I'm not starting any threads and i'm calling the form's ShowDialog method so I assume everything is within the Revit API thread.

0 Likes
Message 4 of 8

Mustafa.Salaheldin
Collaborator
Collaborator

Dear Sean/Jeremy

 

I faced the same problem a few weeks ago. The problem depends on the Printer Drivers, if you tried to print to PDF you may not face the same problem but with physical printers Revit will crash.

 

I think Revit also has to handle some callback function from the print method to complete the printing process but I didn't dig any deeper.

 

My printer is Aficio MP C4000/C5000 Please if you know the name of your printer mention it.

Also it may be due to the speed of sending the print commands to the printer. If you looping through the sheets to print and inside the loop you are sending the print command, this will be too fast for the printer to catch all the sheets printing command. I think also the print tasking schedule should be handled by Revit .


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes
Message 5 of 8

Anonymous
Not applicable

Hi Mustafa,

I don't think its related to the print driver because it actually crashes the same way if I replace my pdf export function with a dwg export function. And if I don't show the form at all and just call model.ProcessSheets() it works fine.

0 Likes
Message 6 of 8

jeremytammik
Autodesk
Autodesk

Dear Sean,

 

I have no idea what might be going wrong.

 

However, i do have a suggestion to make, the same one as always:

 

 

A result of that is a sub-suggestion:

 

  • Decouple

 

In this case, it appears to me that you are feeding native (well, semi-native .NET) Revit objects into some convoluted display system (your form displayed to the user).

 

How about this:

 

  • Grab the pure data that you wish to display, i.e., strings and numbers and stuff
  • Let go of the Revit objects, don't touch them anymore
  • Pass in the pure simple data to your display system

 

If no interaction is required, the result should be identical.

 

I hope this helps.

 

Did I misunderstand?

 

Cheers,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 7 of 8

Anonymous
Not applicable

Hi Jeremy,

Yes that's good advice, I will try using something lighter than the list of ViewSheet objects.

Meanwhile one change that seems to have made a difference is to close the WPF window before calling the ProcessSheets function. When I do that I no longer get the crash.

0 Likes
Message 8 of 8

jeremytammik
Autodesk
Autodesk

It could indeed be a combination of having those ViewSheet objects inside that thing and it doing something horrible to them when it is destroyed, or not.



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes