How to create a simple wall using C# command?

How to create a simple wall using C# command?

Anonymous
Not applicable
4,658 Views
2 Replies
Message 1 of 3

How to create a simple wall using C# command?

Anonymous
Not applicable

Hello guys, I'm newbie for Revit API.

I was trying to learn create a simple wall for few hours and frustrated... 😛

The problem is my `symbolID` always -1 and created null reference symbol.

Please 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);
	var wallTypeId = doc.GetDefaultElementTypeId(ElementTypeGroup.WallType);
	// create line
	XYZ point_a = new XYZ(-10, 0, 0);
	XYZ point_b = new XYZ(10, 10, 10);
	Line line = Line.CreateUnbound(point_a, point_b);

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

		Wall wall = Wall.Create(doc, arc, 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
4,659 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable

Hi je,

 

Try working through this code: I have added comments at several locations that explains the process. If you still face any doubt please ask again.

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;

namespace ClassLibrary1
{
    [Transaction(TransactionMode.Manual)]
    public class Class1 : IExternalCommand
    {
        public Autodesk.Revit.UI.Result Execute(Autodesk.Revit.UI.ExternalCommandData command_data, ref string message, Autodesk.Revit.DB.ElementSet elements)
        {
            Document document = command_data.Application.ActiveUIDocument.Document;// replace var with Document (statically-typed)

            ElementId level_id = new ElementId(1526); // replace var with ElementId, I assume "1526" is the correct level id in your local project
            ElementId wallTypeId = document.GetDefaultElementTypeId(ElementTypeGroup.WallType);// replace var with Element Id (statically-typed)
            //
            // create line on which you will construct a new wall
            //
            XYZ point_a = new XYZ(-100, 0, 0);
            XYZ point_b = new XYZ(100, 0, 0); // for start try making a wall in one plane
            Curve line = Line.CreateBound(point_a, point_b) as Curve; // Create a bound curve for function to work, a wall cannot be created with unbound line

            Transaction transaction = new Transaction(document);// no need to to use "Using", Autodesk disposes of all the methods and data in Execute method.
            {
                transaction.Start("create walls");

                // this command below creates a wall, with default wall type
                Wall wall = Wall.Create(document, line, level_id, false);

                // no need of this code, I have commented out this code ( you may remove it). Also the newfamilyinstance method is not used to create a wall.
/*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; } } }

If you find that this post solves your query, please mark this as answer.

Thank you

 

0 Likes
Message 3 of 3

Avaris.
Advisor
Advisor
0 Likes