How to create a simple wall using ExternalCommand?

How to create a simple wall using ExternalCommand?

Anonymous
Not applicable
693 Views
2 Replies
Message 1 of 3

How to create a simple wall using ExternalCommand?

Anonymous
Not applicable
I just wanted to learn Revit API and create a simple wall using ExternalCommand.

I think my problem is here:

var symbolId = document.GetDefaultFamilyTypeId(new ElementId(BuiltInCategory.OST_Walls));

When I debug it symbolId always -1.

 

Can you help me what is wrong with this code snippet?

public Autodesk.Revit.UI.Result Execute(
	Autodesk.Revit.UI.ExternalCommandData command_data,
	ref string message,
	Autodesk.Revit.DB.ElementSet elements)
{
	var document = command_data.Application.ActiveUIDocument.Document;

	var level_id = new ElementId(1526);
	// create line
	XYZ point_a = new XYZ(-10, 0, 0);
	XYZ point_b = new XYZ(10, 10, 10);
	Line line = Line.CreateBound(point_a, point_b);

	using (var transaction = new Transaction(doc))
	{
		transaction.Start("create walls");

		Wall wall = Wall.Create(doc, line, level_id, false);
		var position = new XYZ(0, 0, 0);
		var symbolId = document.GetDefaultFamilyTypeId(new ElementId(BuiltInCategory.OST_Walls));
		if (symbolId == ElementId.InvalidElementId) {
			transaction.RollBack();
			return Result.Failed;
		}

		var symbol = document.GetElement(symbolId) as FamilySymbol;
		var level = (Level)document.GetElement(wall.LevelId);
		document.Create.NewFamilyInstance(position, symbol, wall, level, StructuralType.NonStructural);

transaction.Commit(); }

return Result.Succeeded; }
0 Likes
Accepted solutions (1)
694 Views
2 Replies
Replies (2)
Message 2 of 3

Moustafa_K
Advisor
Advisor

Hi,

Wall is a builtin Family and not the standard one.

so you need to call the walltype through this

document.GetDefaultElementTypeId(ElementTypeGroup.WallType)

and you can't use CreateFamilyInstance to create Builtin Wall Family.

Hope that helps

Moustafa Khalil
Cropped-Sharp-Bim-500x125-Autodesk-1
Message 3 of 3

so-chong
Advocate
Advocate
Accepted solution

Hi,

If you are new to the Revit API, then you should refer to the revit apidocs.

 

https://apidocs.co/apps/revit/2019/d4648875-d41a-783b-d5f4-638df39ee413.htm

 

You are using the Wall Create Method, the one which take 4 parameters.

 

https://apidocs.co/apps/revit/2019/4a42066c-bc44-0f99-566c-4e8327bc3bfa.htm

 

If you may have noticed the example code, there is no need to write code to get the default Walltype, the create method is already doing it for you.

 

My suggestion would be to write something like this (i have added some comments for explanation)

 

Hope this helps.

 

     // be sure your active view is a plan view when you run the external command
    ViewPlan view = doc.ActiveView as ViewPlan;               
    Level level = view.GenLevel;
    
    // leave 'Z' coordinate of point_b to '0', you are trying to create a none rectangular wall,
    // only rectangular wall creation is accepted
    XYZ point_a = new XYZ(-1000);
    XYZ point_b = new XYZ(10100);
    Line line = Line.CreateBound(point_a, point_b);
    
    using (var transaction = new Transaction(doc))
    {
        transaction.Start("create wall");
    
        Wall wall = Wall.Create(doc, line, level.Id, false);
    
        transaction.Commit();
    }