how to put outlet in the wall?

ferreirasou_gabriell
Explorer
Explorer

how to put outlet in the wall?

ferreirasou_gabriell
Explorer
Explorer

I'm trying to create code to add an outlet to the wall. (Same as done in Revit's Systems tab)

when I run the code I get the following error


ferreirasou__0-1673037413318.png

 

 

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

namespace IExternalCommand1
{
[Transaction(TransactionMode.Manual)]
public class Class1 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uIDocument = commandData.Application.ActiveUIDocument;
Document document = uIDocument.Document;

#region

FilteredElementCollector outlet = new FilteredElementCollector(document);
outlet.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_ElectricalFixtures);
FamilySymbol familySymbol = outlet.FirstElement() as FamilySymbol;
ElementId elementId = new ElementId(308696);
Wall wall = document.GetElement(elementId) as Wall;
var reference = HostObjectUtils.GetSideFaces(wall, ShellLayerType.Interior).First();


using (Transaction transaction = new Transaction(document))
{
transaction.Start("add");

if (wall != null)
{
var instance = document.Create.NewFamilyInstance(reference,
new XYZ(13.3997604933275, -1.35161192601243, 9.0571329707487),
new XYZ(0, 0, 0),
familySymbol);
if (instance == null)
TaskDialog.Show("ERROR", "Instance is null!");
}

transaction.Commit();

}

#endregion

return Result.Succeeded;
}
}
}

 

0 Likes
Reply
194 Views
2 Replies
Replies (2)

jeremy_tammik
Autodesk
Autodesk

Hmm. I would suggest you browse this forum a bit and peruse the existing discussions on related topics. I searched a bit for you myself using the search keywords 'outlet', 'electrical' and 'place fixture'. I found a number of interesting closely related threads, but nothing that exactly fits yet. Most of the discussions are just one tiny step further advanced.

  

Next, your could try searching a bit further afield, e.g., using the entire Internet:

  

  

That turns up these useful-looking q&a's on StackOverflow:

  

 

Moral? Search, search, search. Mostly more efficient than asking. Many experienced programmers still search a lot.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes

mhannonQ65N2
Advocate
Advocate

It looks like element 308696 is not a wall (or does not exist) in your model.

Try changing your line

Wall wall = document.GetElement(elementId) as Wall;

to a direct cast, so it will throw an invalid cast exception if the element is not a wall:

Wall wall = (Wall)document.GetElement(elementId);