Tuesday 17 July 2012

Selecting an object and getting some data from it

As promised, here is my post regarding getting information from selected elements within Revit.
In order to get information from selected elements within the project, the 1st thing you need to do is select an object.
the following seems to work for that:


Imports Autodesk.Revit.UI.Selection

Dim uiapp As UIApplication = commandData.Application
Dim uidoc As UIDocument = uiapp.ActiveUIDocument
Dim sel As Selection = uidoc.Selection
Dim doc As Document = uidoc.Document

Dim elem As Element = doc.GetElement(sel.PickObject(ObjectType.Element))

You will be asked to select an element on the screen which will be saved as elem so that you can start to get information from it.

for example:

MsgBox("Category = " & elem.Category.Name.ToString)

will give you the category of the object you select in a message box (I am a big fan of message boxes as I don't really understand immediate windows, debugging and the like if I am honest).

I decided to try to make a command that would isolate a workset within a view based on the selection a user makes. Something like LayerIsolate in AutoCAD, so WorkSetIsolate it shall be.


So after selecting the element, next thing is to get the workset of the object, this involves accessing the WorkSetTable of the document that stores the names of the worksets.

Dim worksetTable As WorksetTable = doc.GetWorksetTable()
Dim worksetIdByElement As WorksetId = doc.GetWorksetId(elem.Id)
    If worksetIdByElement <> WorksetId.InvalidWorksetId Then
        Dim worksetByElement As Workset = worksetTable.GetWorkset(worksetIdByElement)
        Dim WorkSetName As String = worksetByElement.Name
    End If

I put a check in there to confirm that the worksetIdByElement was valid to hopefully avoid errors

So now we have the name of the workset of the selected element. Next thing is to use the visibility controls to turn off the visibility of all of the User-Created Worksets apart from the one we just found...

When we do this we will be editing the project file, therefore we need to start a transaction and then commit it at the end of our modifications in order that the changes happen.

I place:

Transaction.Start("Hide WorkSet")

just after my Dim's

In order to hide the selected workset the following does the work:

Dim visibility As WorksetVisibility = view.GetWorksetVisibility(worksetIdByElement)
    If visibility <> WorksetVisibility.Hidden Then
        view.SetWorksetVisibility(worksetIdByElement, WorksetVisibility.Hidden)
    End If

and then once out of the If I put in the commit to actually do the work defined in the transaction:

Transaction.Commit()

So at the end of it all I have this:

Dim uiapp As UIApplication = commandData.Application
Dim uidoc As UIDocument = uiapp.ActiveUIDocument
Dim sel As Selection = uidoc.Selection
Dim doc As Document = uidoc.Document
Dim view As View = uidoc.ActiveView
Dim elem As Element = doc.GetElement(sel.PickObject(ObjectType.Element))
Dim worksetTable As WorksetTable = doc.GetWorksetTable()
Dim worksetIdByElement As WorksetId = doc.GetWorksetId(elem.Id)
Dim Transaction As New Transaction(doc)
Transaction.Start("Hide WorkSet")
If worksetIdByElement <> WorksetId.InvalidWorksetId Then
    Dim worksetByElement As Workset = worksetTable.GetWorkset(worksetIdByElement)
    Dim WorkSetName As String = worksetByElement.Name
    Dim visibility As WorksetVisibility = view.GetWorksetVisibility(worksetIdByElement)
    If visibility <> WorksetVisibility.Hidden Then
view.SetWorksetVisibility(worksetIdByElement, WorksetVisibility.Hidden)
    End If
End If
Transaction.Commit()

This allows me to select an element on the screen and the workset of the selected element is hidden.

Next time I will look at a toggle to change a visibility setting and linking this to a keyboard shortcut.

1 comment:

  1. Thanks Mr. Jarvis. I am also a person with VBA background in excel and autocad. started going through your blog. Hopefully with this, I will learn vb.net macros for revit. Radhesh

    ReplyDelete