Tekla Open API: Grids

This article explains how you can create/modify/delete grids in the model using the Tekla Open API in VB.Net.


Contents

Step 1:

Add reference to the Tekla.Structures.Model library located in the path “C:Program FilesTekla Structures20.0ntbinplugins” and manually open the tekla model. This has been covered in the article below:


Delete Existing Grids:

The following code will delete the existing grid from the model:

Imports TSM = Tekla.Structures.Model

    ''' <summary>
    ''' Deletes existing grids from the model
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub DeleteExistingGrids()
        Dim appModel As TSM.Model
        Dim objModel As TSM.ModelObject
        Dim objGrid As TSM.Grid

        appModel = New TSM.Model
        For Each objModel In appModel.GetModelObjectSelector.GetAllObjects
            objGrid = TryCast(objModel, Tekla.Structures.Model.Grid)
            If IsNothing(objGrid) = False Then
                objGrid.Delete()

            End If
        Next
        appModel.CommitChanges()

        appModel = Nothing
        objModel = Nothing
        objGrid = Nothing
    End Sub

The code basically loops through all the objects in the model. If the object is of the grid type the object will be deleted.


Add New Grid:

The following code will add a new grid to the model:

Imports TSM = Tekla.Structures.Model

    Private Sub Main()
        DeleteExistingGrids()
        SetGrid()
    End Sub

    ''' <summary>
    ''' Creates the new grid
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetGrid()
        Dim objGrid As TSM.Grid
        Dim appTekla As TSM.Model

        Dim strXCoor As String
        Dim strYCoor As String
        Dim strZCoor As String

        Dim strXLabel As String
        Dim strYLabel As String
        Dim strZLabel As String

        'grid spacing
        strXCoor = "0 1000 1000 1000"
        strYCoor = "0 2000 2000 2000 2000"
        strZCoor = "0 3000 3000"

        'grid labels
        strXLabel = "A B C D "
        strYLabel = "1 2 3 4 5"
        strZLabel = "E1 E2 E3"

        appTekla = New TSM.Model
        objGrid = New TSM.Grid

        objGrid.CoordinateX = strXCoor
        objGrid.CoordinateY = strYCoor
        objGrid.CoordinateZ = strZCoor
        objGrid.LabelX = strXLabel
        objGrid.LabelY = strYLabel
        objGrid.LabelZ = strZLabel
        objGrid.Insert()
        appTekla.CommitChanges()

        appTekla = Nothing
        objGrid = Nothing
    End Sub

    ''' <summary>
    ''' Deletes existing grids from the model
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub DeleteExistingGrids()
        Dim appModel As TSM.Model
        Dim objModel As TSM.ModelObject
        Dim objGrid As TSM.Grid

        appModel = New TSM.Model
        For Each objModel In appModel.GetModelObjectSelector.GetAllObjects
            objGrid = TryCast(objModel, Tekla.Structures.Model.Grid)
            If IsNothing(objGrid) = False Then
                objGrid.Delete()

            End If
        Next
        appModel.CommitChanges()

        appModel = Nothing
        objModel = Nothing
        objGrid = Nothing
    End Sub
End Class

Line 5 calls the DeleteExistingGrids function. The new grid will not be inserted when a grid already exists. Lines 26~28 define the grid spacing. It is important that the correct format is used or the grid will not be created.

Result:
blog_Tekla_TeklaAPI_Grid_1


Modify Existing Grid:

Modifying an existing grid can be done using the code below:

Imports TSM = Tekla.Structures.Model

  Private Sub Main2()
        Dim objGrid As TSM.Grid
        objGrid = GetExistingGrids()
        ModifyGrid(objGrid)
    End Sub

    ''' <summary>
    ''' Creates the new grid
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ModifyGrid(ByRef objGrid As TSM.Grid)

        Dim appTekla As TSM.Model

        Dim strXCoor As String
        Dim strYCoor As String
        Dim strZCoor As String

        Dim strXLabel As String
        Dim strYLabel As String
        Dim strZLabel As String

        'grid spacing
        strXCoor = "0 10000 10000 10000"
        strYCoor = "0 2000 2000 2000 2000"
        strZCoor = "0 3000 3000"

        'grid labels
        strXLabel = "A B C D "
        strYLabel = "1 2 3 4 5"
        strZLabel = "E1 E2 E3"

        appTekla = New TSM.Model
        objGrid = GetExistingGrids()

        objGrid.CoordinateX = strXCoor
        objGrid.CoordinateY = strYCoor
        objGrid.CoordinateZ = strZCoor
        objGrid.LabelX = strXLabel
        objGrid.LabelY = strYLabel
        objGrid.LabelZ = strZLabel
        objGrid.Modify()
        appTekla.CommitChanges()

        appTekla = Nothing
        objGrid = Nothing
    End Sub

    ''' <summary>
    ''' Gets the existing grids from the model
    ''' </summary>
    ''' <remarks></remarks>
    Private Function GetExistingGrids() As TSM.Grid
        Dim appModel As TSM.Model
        Dim objModel As TSM.ModelObject
        Dim objGrid As TSM.Grid

        appModel = New TSM.Model
        For Each objModel In appModel.GetModelObjectSelector.GetAllObjects
            objGrid = TryCast(objModel, Tekla.Structures.Model.Grid)
            If IsNothing(objGrid) = False Then
                appModel = Nothing
                objModel = Nothing
                Return objGrid

            End If
        Next

        Return Nothing
    End Function

The function GetExistingGrids gets reference to the existing grid object in the model.

You can download the sample file and code used in this article from the link below:

If you need assistance with your Tekla model, or you are looking for a Tekla Open API programmer to hire feel free to contact me. Also please visit my home page  www.software-solutions-online.com

Leave a Reply

Your email address will not be published. Required fields are marked *