Sometimes it’s not you

Here’s a really simple bit of code to create a wall and change the wall type

public void crash()
{
	Document doc = this.ActiveUIDocument.Document;
	WallType wt = new FilteredElementCollector(doc)
		.OfClass(typeof(WallType))
		.Cast<WallType>()
		.FirstOrDefault(q => q.Kind == WallKind.Basic);
	
	using (Transaction t = new Transaction(doc, "test"))
	{
		t.Start();
		Wall wall = Wall.Create(doc, 
            Line.CreateBound(XYZ.Zero, XYZ.BasisX),
            new FilteredElementCollector(doc).OfClass(typeof(Level)).FirstOrDefault().Id,
            false);		
		wall.ChangeTypeId(wt.Id);
		t.Commit();
	}
}

The problem is that when you create a wall with the Revit API, Revit uses the wall type last used when a wall was created with the user interface. If the last wall type used was a basic wall, then everything is fine. But if the last wall type used in the interface was a Curtain Wall…

You can work around this by adding a Document.Regenerate() just before changing the wall type. But it is a good reminder to think about how Revit’s state can affect your add-in, be careful working in the API with newly created elements, and that sometimes by adding a “regenerate” or changing how you are using transactions you can find a solution.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s