help family instance error

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
i get the following error when trying to run following code
Object reference not set to an instance of an object. |
please help
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
[TransactionAttribute(TransactionMode.Manual)]
[RegenerationAttribute(RegenerationOption.Manual)]
public class LightingDistribution : IExternalCommand
{
public List<Face> m_faceList = new List<Face>();
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIApplication uiApp = commandData.Application;
Document doc = uiApp.ActiveUIDocument.Document;
try
{
// Reference pickedRef = null;
Reference pickedLit = null;
Selection sel = uiApp.ActiveUIDocument.Selection;
// Pick a light fixture
LightPickFilter LselFilter = new LightPickFilter();
pickedLit = sel.PickObject(ObjectType.Element, LselFilter,
"Please select Lighting Fixture");
Element Lelem = pickedLit.Element;
// Ask the user to pick target rooms
RoomPickFilter roomPickFilter = new RoomPickFilter();
IList<Reference> rooms =
sel.PickObjects(
ObjectType.Element,
roomPickFilter,
"Select target rooms for duplicate Lights");
FamilyInstance inst = Lelem as FamilyInstance;
// Get light symbol name
FamilySymbol Lt = GetInfo_FamilyInstance_Symbol(inst);
pickedLit = sel.PickObject(ObjectType.Element,
"Please select ceilingssssssssssss");
Element Lelem1 = pickedLit.Element;
object obj = Lelem1 as object;
Solid soli = obj as Solid;
FaceArray faces = soli.Faces;
int ii = 0;
foreach (Face tempFace in faces)
{
m_faceList.Add(tempFace);
ii++;
}
// Place light in each of the rooms transaction
Transaction trans = new Transaction(doc);
trans.Start("Lab");
PlaceLightInRooms(doc, rooms, m_faceList, Lt);
trans.Commit();
}
// If user right-clicks or presess Esc button,
// handle the exception
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
{
return Result.Cancelled;
}
// Catch other errors
catch (Exception ex)
{
message = ex.Message;
return Result.Failed;
}
return Result.Succeeded;
}
/// <summary>
/// Return the center of an element based on its BoundingBox
/// </summary>
public XYZ GetElementCenter(Element elem) //required for get room center
{
BoundingBoxXYZ bounding = elem.get_BoundingBox(null);
XYZ center = (bounding.Max + bounding.Min) * 0.5;
return center;
}
public FamilySymbol Getsymbol(Element LTelem)
{
FamilySymbol lit = LTelem as FamilySymbol;
return lit;
}
public FamilySymbol GetInfo_FamilyInstance_Symbol(FamilyInstance familyInstance)
{
FamilySymbol lit = familyInstance.Symbol;
return lit as FamilySymbol;
}
/// Return a room's center point coordinates.
public XYZ GetRoomCenter(Room room)
{
// Get the room center point.
XYZ boundCenter = GetElementCenter(room);
return boundCenter;
}
//Method
public void PlaceLightInRooms(
Document doc,
IList<Reference> rooms, List<Face> m_faceLis, FamilySymbol LT)
{
foreach (Reference r in rooms)
{
Room roomTarget = doc.get_Element(r.Element.Id) as Room;
if (roomTarget != null)
{
XYZ dir = new Autodesk.Revit.DB.XYZ(1.0f, 0f, 0f);
Face face = m_faceLis[1];
XYZ roomCenter = GetRoomCenter(roomTarget);
FamilyInstance newlit = doc.Create.NewFamilyInstance(face, roomCenter, dir, LT);
}
}
}
}
/// <summary>
/// Filter to constrain picking to Light fixtures
/// </summary>
public class LightPickFilter : ISelectionFilter
{
public bool AllowElement(Element e)
{
return (e.Category.Id.IntegerValue.Equals(
(int)BuiltInCategory.OST_LightingFixtures));
}
public bool AllowReference(Reference r, XYZ p)
{
return false;
}
}
/// <summary>
/// Filter to constrain picking to rooms
/// </summary>
public class RoomPickFilter : ISelectionFilter
{
public bool AllowElement(Element e)
{
return (e.Category.Id.IntegerValue.Equals(
(int)BuiltInCategory.OST_Rooms));
}
public bool AllowReference(Reference r, XYZ p)
{
return false;
}
}