Monday, March 26, 2012

Vasari SDK Sample 7 - ExportElementXYZtoTextFile


Sample #7 of the new Vasari SDK samples is ExportElementXYZtoTextFile. The SDK sample contains code in  both VB.NET and C#.

This is a simple sample that can export XYZ coordinates for a selected category. Another sweet feature within this sample is the ability to also filter by Design Option. Filtering by Design Option can be tricky of you've never had to do it before. This sample can be run on any ol file you want.


The codes used to filter b category and design option is quite simple. An option in the API exists for you to filter by ContainedInDesignOption as shown below in a short snippet.


        ' Filter by the Category Selected - by Category Name
        Dim m_col As New FilteredElementCollector(_doc)
        m_col.OfCategory(_doc.Settings.Categories.Item(Me.ComboBoxCategory.SelectedItem.ToString).Id.IntegerValue)
        m_col.WhereElementIsNotElementType()

        ' Get the selected item as an object
        Dim m_desOpt As clsDesignOption = Me.ComboBoxDesignOption.SelectedItem

        ' Do we have a Design Option Filter?
        If Not m_desOpt.DesignOption Is Nothing Then

            ' Apply the Option Set ID to the Filter
            m_col.ContainedInDesignOption(m_desOpt.DesignOption.Id)

        End If

The element processing to get the element XYZ is fairly simple as well. This is a very basic implementation that doesn't take into account any corrections for offsets or hosting.


        ' Process the Elements
        For Each x As Element In m_col.ToElements

            Try

                ' Get the XYZ Location
                Dim m_location As Location = x.Location

                ' Continue if we have a location
                If Not m_location Is Nothing Then

                    ' Get the XYZ object
                    Dim m_xyz As XYZ = DirectCast(m_location, LocationPoint).Point

                    ' Design Option Data
                    Dim m_dopt As String = ""
                    Dim m_set As String = ""
                    If Not x.DesignOption Is Nothing Then
                        m_dopt = x.DesignOption.Name
                        ' Cast the Object to a Design Option Object
                        Dim mm_DesignOption = TryCast(x.DesignOption, DesignOption)
                        ' Get the Set Name
                        m_set = _doc.Element(New ElementId(mm_DesignOption.Parameter(BuiltInParameter.OPTION_SET_ID).AsElementId.IntegerValue)).Name
                    End If

                    ' Write out the Element Data
                    Dim m_line As New List(Of String)

                    With m_line
                        .Add(x.Id.ToString)
                        .Add(x.Name)
                        .Add(x.Category.Name)
                        .Add(m_set)
                        .Add(m_dopt)
                        .Add(m_xyz.X.ToString)
                        .Add(m_xyz.Y.ToString)
                        .Add(m_xyz.Z.ToString)
                    End With

                    m_fs.WriteLine(m_line)

                End If

            Catch ex As Exception

            End Try

        Next