Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Selecting a room

6 REPLIES 6
Reply
Message 1 of 7
drewj
440 Views, 6 Replies

Selecting a room

I have a room ID stored as an integer in a datagrid and I am trying to modify the room using this as a way of finding the room object.

 

I get the element with the following:

 

Dim RoomID As ElementId = TryCast(objRow.Item("Element_Id"), ElementId)

 

then try to use

 

Dim objRoom As Room = doc.Element(RoomID)

 

to get the object, but it gives me an error:

 

System.NullReferenceException: Object reference not set to an instance of an object.

 

I know the elementID is the correct number, I am thinking it is the wrong type? or maybe its the Element() that i am doing wrong?!?

 

Any ideas or examples of something like this that work? c# or vb is fine, thanks!

6 REPLIES 6
Message 2 of 7
adam.nagy
in reply to: drewj

Hi there,

 

Instead of trying to cast a numeric value to ElementId, you should create a new instance of ElementId and pass in the value in its constructor:

 

Something like:

Dim RoomID As New ElementId(objRow.Item("Element_Id"))

 

I hope this helps.

 

______________________________________________________________

If my post answers your question, please click the "Accept as Solution"

button. This helps everyone find answers more quickly!



Adam Nagy
Autodesk Platform Services
Message 3 of 7
peterjegan
in reply to: drewj

I don't use VB, so it might be different, but for C# the syntax is:

 

doc.get_Element(RoomID) (in 2012)

 

or

 

doc.GetElement(RoomID) (in 2013)

Message 4 of 7
drewj
in reply to: adam.nagy

@adam.nagy wrote:

Hi there,

 

Instead of trying to cast a numeric value to ElementId, you should create a new instance of ElementId and pass in the value in its constructor:

 

Something like:

Dim RoomID As New ElementId(objRow.Item("Element_Id"))

 

I hope this helps.

 

Thanks Adam, I tried that but I get the following message:

 

Overload resolution failed because no accessible 'New' can be called without a narrowing conversion:
    'Public Sub New(id As Integer)': Argument matching parameter 'id' narrows from 'Object' to 'Integer'.
    'Public Sub New(categoryId As Autodesk.Revit.DB.BuiltInCategory)': Argument matching parameter 'categoryId' narrows from 'Object' to 'Autodesk.Revit.DB.BuiltInCategory'.
    'Public Sub New(parameterId As Autodesk.Revit.DB.BuiltInParameter)': Argument matching parameter 'parameterId' narrows from 'Object' to 'Autodesk.Revit.DB.BuiltInParameter'.

Message 5 of 7
drewj
in reply to: peterjegan


@peterjegan wrote:

I don't use VB, so it might be different, but for C# the syntax is:

 

doc.get_Element(RoomID) (in 2012)

 

or

 

doc.GetElement(RoomID) (in 2013)


Thanks Peter, What would I need to import to use get_element? I get an error that get_element is not a member of 'Autodesk.Revit.DB.Document'.

 

I really appreciate your time in helping me learn this!

Message 6 of 7
peterjegan
in reply to: drewj

Sorry, it looks like doc.Element(ID) is the correct syntax in VB.

 

That leads me to think that you are not getting the correct ElementId.

 

1. How sure are you that you actually have a usable integer at objRow.Item when you retrieve the RoomID? Can you debug or MsgBox the RoomID at that point?

 

2. I am not familiar with DataGrid, but is there a reason you are storing the ElementId as an integer in the first place? In C#, Arrays, Lists and Dictionaries allow you to specify an ElementId as a storage type.

Message 7 of 7
Joe.Ye
in reply to: drewj

 

Hi there,

 

The error reason for the error message is that you use the inappropriate objec type for this statement

New ElementId();

 

The constructor of ElementId only accepts the integer. Howeve the value you get directly from DataGrid cell is Object type. You need to  convert it to Interger first. Then passed in to the New ElementId().

 

After construct the right ElementId instance for room, you can use it to retrieve the room by peterjegan suggested.

 

doc.get_Element(RoomID) (in 2012)

 

or

 

doc.GetElement(RoomID) (in 2013)



Joe Ye
Contractor
Developer Technical Services
Autodesk Developer Network

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community