Place Lights on Selected 3D points

Place Lights on Selected 3D points

Anonymous
Not applicable
560 Views
3 Replies
Message 1 of 4

Place Lights on Selected 3D points

Anonymous
Not applicable

Hi,

    I want to place Lights on selected 3D points.I selected 3D points from active view and place Lights on that points,But its give me run time errror.

 

This is the code:-

 

using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.DB.Structure;

namespace FamilyPlacing
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class Class1 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Application app = uiapp.Application;
Document doc = uidoc.Document;

XYZ point_in_3d;

if (SetWorkPlaneAndPickObject(uidoc, out point_in_3d))
{
TaskDialog.Show("3D Point Selected",
"3D point picked on the plane"
+ " defined by the selected face: "
+ "X: " + point_in_3d.X.ToString()
+ ", Y: " + point_in_3d.Y.ToString()
+ ", Z: " + point_in_3d.Z.ToString());

double x, y, z;
x = point_in_3d.X;
y = point_in_3d.Y;
z = point_in_3d.Z;


StructuralType light = StructuralType.NonStructural;
FilteredElementCollector collector1 = new FilteredElementCollector(doc);
collector1.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_LightingFixtures);
FamilySymbol symbol1 = collector1.FirstElement() as FamilySymbol;

 

//Here I want to place Lights
Level level = null;
if (level.Name.Equals("Level 1"))
{
XYZ startPoint1 = new XYZ(x, y, z);
FamilyInstance fi1 = doc.Create.NewFamilyInstance(startPoint1, symbol1, level, light);

}

return Result.Succeeded;

}
else
{
message = "3D point selection failed";
return Result.Failed;
}

}

private bool SetWorkPlaneAndPickObject(UIDocument uidoc, out XYZ point_in_3d)
{

point_in_3d = null;
Document doc = uidoc.Document;

Reference r = uidoc.Selection.PickObject(
Autodesk.Revit.UI.Selection.ObjectType.Face,
"Please select a planar face to define work plane");
Element e = doc.GetElement(r.ElementId);
if (null != e)
{
PlanarFace face = e.GetGeometryObjectFromReference(r)
as PlanarFace;

GeometryElement geoEle = e.get_Geometry(new Options());
Transform transform = null;

foreach (GeometryObject gObj in geoEle)
{
GeometryInstance gInst = gObj as GeometryInstance;
if (null != gInst)
transform = gInst.Transform;
}

if (face != null)
{
// face.Normal face.FaceNormal
Plane plane = null;

if (null != transform)
{//uiDoc.ActiveView.ViewDirection, uiDoc.ActiveView.Origin
//transform.OfVector(face.FaceNormal),
//transform.OfPoint(face.Origin)
plane = new Plane(uidoc.ActiveView.ViewDirection, uidoc.ActiveView.Origin);
}
else
{
plane = new Plane(face.FaceNormal, face.Origin);
}

Transaction t = new Transaction(doc);

t.Start("Temporarily set work plane"
+ " to pick point in 3D");

SketchPlane sp = SketchPlane.Create(doc,plane);

uidoc.ActiveView.SketchPlane = sp;
uidoc.ActiveView.ShowActiveWorkPlane();

try
{
point_in_3d = uidoc.Selection.PickPoint(
"Please pick a point on the plane"
+ " defined by the selected face");
}
catch (OperationCanceledException)
{
}

t.RollBack();
}
}
return null != point_in_3d;
}
}
}

0 Likes
561 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable

Its a bit hard to look at your code its not formatted...

but according to the erros you have a unexpected unhandled null value on line 46.. in debug it would be the first place where to look.

0 Likes
Message 3 of 4

Anonymous
Not applicable

Instead of placing lights I place Structural Column and it run successfull. And next thing I want to do that I want to select multiple points at a time and place the column. How can I do this?

0 Likes
Message 4 of 4

Anonymous
Not applicable

What? So you want to place  the lights on top of the columns?

 

Also In your code:

FamilyInstance fi1 = doc.Create.NewFamilyInstance(startPoint1, symbol1, level, light); 

is not included in a transaction, 

&

level is always null;

 

This could cause the problem?

 

0 Likes