Selection by pickObject - Reference object to be set an instance of an object

Selection by pickObject - Reference object to be set an instance of an object

yekose
Participant Participant
4,316 Views
6 Replies
Message 1 of 7

Selection by pickObject - Reference object to be set an instance of an object

yekose
Participant
Participant

Hi,

 

The below code, i have found that example like everywhere but whenever i try to execute that example i get

 

"Object reference not set to an instance of an object."

 

exception in the line of get_parameter? 

 

Task dialog line works so the selection is successful.  

 

If you had any solution or anybody had this problem before i would love to hear a solution.

 

Regards 

 

 

Selection sel = uidoc.Selection;

 Reference hasPickOne = sel.PickObject(ObjectType.Element);

 

if (hasPickOne != null && hasPickOne.ElementId != ElementId.InvalidElementId)

TaskDialog.Show("Element", "Element selected");

 

Element selectedElement = doc.GetElement(hasPickOne);

 

double x = selectedElement.get_Parameter(BuiltInParameter.BASEPOINT_EASTWEST_PARAM).AsDouble();
double y = selectedElement.get_Parameter(BuiltInParameter.BASEPOINT_NORTHSOUTH_PARAM).AsDouble();

 

 

0 Likes
4,317 Views
6 Replies
Replies (6)
Message 2 of 7

matthew_taylor
Advisor
Advisor

Hi @yekose,

I think you'll find that the element you're selecting doesn't have parameters that match the builtinparameters you're specifying.

You should test that the parameter exists before calling .AsDouble().

 

If you haven't installed RevitLookup yet, do so now. It is an indispensable tool that will help you reach an understanding of how the Revit object model is put together. You can select an element, then investigate its properties/parameters etc.

 

I believe you're trying to get the coordinates of the selected element. To do this, you should use the .Location property. This property can then be cast as a LocationPoint or a LocationCurve. There will be many examples of this in the Software Development Kit (SDK).

 


Cheers,

-Matt
_______________________________________________________________________________
Marking a post as a 'solution' helps the community. Giving a post 'Kudos' is as good as saying thanks. Why not do both?
0 Likes
Message 3 of 7

yekose
Participant
Participant

Hi @matthew_taylor

 

I really appreciate with your answer and i ll definitely check for this RevitLookup but beside that i can use that parameters with the below code example,

 

ElementCategoryFilter siteCategoryfilter = new ElementCategoryFilter(BuiltInCategory.OST_ProjectBasePoint);

FilteredElementCollector collector = new FilteredElementCollector(doc);
IList<Element> siteElements = collector.WherePasses(siteCategoryfilter).ToElements();

foreach (Element ele in siteElements)
{
Parameter paramX = ele.ParametersMap.get_Item("E/W");
double x1 = ele.get_Parameter(BuiltInParameter.BASEPOINT_EASTWEST_PARAM).AsDouble();
double X = paramX.AsDouble();

Parameter paramY = ele.ParametersMap.get_Item("N/S");
double y1 = ele.get_Parameter(BuiltInParameter.BASEPOINT_NORTHSOUTH_PARAM).AsDouble();
double Y = paramY.AsDouble();

Parameter paramElev = ele.ParametersMap.get_Item("Elev");
double Elev = paramElev.AsDouble();

XYZ projectBasePoint = new XYZ(X, Y, Elev);
}

 

and it works totally fine, but the problem is i am using here the filter and i want to use that pickobject method which should be the same result but it is not happening? I really stuck in that.

 

Regards

0 Likes
Message 4 of 7

matthew_taylor
Advisor
Advisor

Hi @yekose,

I don't see the point, to be honest. There's only one project basepoint...

 

If you do want to do it:

If it's not working, it's likely that you've not got the same element that the filter got.

Try putting in a categoryFilter. (I've used a VB.NET->C# code converter - should be fine.)

 

The filter:

/// <summary>
/// A Filter for a single category
/// </summary>
public class MyCategoryFilter : UI.Selection.ISelectionFilter
{
	DB.BuiltInCategory m_bic;
	public MyCategoryFilter(DB.BuiltInCategory bic)
	{
		m_bic = bic;
	}
	/// <summary>
	/// Allow only category to be selected
	/// </summary>
	/// <param name="elem">A candidate element in selection operation.</param>
	/// <returns>Return true for wall. Return false for non wall element.</returns>
	public bool AllowElement(DB.Element elem)
	{
		DB.Category cat = elem.Category;
		if (cat != null && !(cat.Id == DB.ElementId.InvalidElementId) && cat.Id.IntegerValue == m_bic) {
			return true;
		} else {
			return false;
		}

	}

	/// <summary>
	/// Allow  reference to be selected
	/// </summary>
	/// <param name="refer">A candidate reference in selection operation.</param>
	/// <param name="point">The 3D position of the mouse on the candidate reference.</param>
	/// <returns>Return true for face reference. Return false for non face reference.</returns>
	public bool AllowReference(DB.Reference refer, DB.XYZ point)
	{
		return true;
	}
}

Example implementation:

Reference hasPickOne = docUi.Selection.PickObject(UI.Selection.ObjectType.Element, _
New MyCategoryFilter(BuiltInCategory.OST_ProjectBasePoint));

 


Cheers,

-Matt
_______________________________________________________________________________
Marking a post as a 'solution' helps the community. Giving a post 'Kudos' is as good as saying thanks. Why not do both?
0 Likes
Message 5 of 7

FAIR59
Advisor
Advisor

the call to get the element is the culprit

 

use: Element selectedElement = doc.GetElement(hasPickOne.ElementId);

Message 6 of 7

yekose
Participant
Participant

Hi @matthew_taylor,

 

Unfortunately it is giving the same error. It is all about the instance of an object because the exception is about that. There is only one element in the model so i can not pick any other element. It is really weird and i dont think either it is about the basepoint because as you said there is only only one project basepoint.

 

 

0 Likes
Message 7 of 7

yekose
Participant
Participant

Hi @FAIR59

 

Thank you for your advice i will keep that in my mind but this does not change the result for my case.

0 Likes