Modifying Grid Lines in Excel

Gridlines are useful to show the shape of hull forms. They can be added, deleted and moved from inside of Maxsurf through the Grid Space Dialog (Main Menu | Data | Grid Spacing . . ). Grid lines can also be moved in through the Automation interface. This example shows how to replicate the Grid Space Dialog in Excel so that the Grid can be edited from Excel. The Excel file detailed in this example can be found in C:\Program Files\Maxsurf\Automation Samples\Maxsurf\Editing Gridlines.xls, or in the Maxsurf install directory.

·   Open the Excel spreadsheet

 

Figure 12 Layout For the Editing grid lines Example, Command Buttons will import, export and clear the grid lines.

There are four procedures in this Spreadsheet; linked to the four Command buttons. The buttons will:

·   Load a Design into Maxsurf

·   Import the current grid lines from Maxsurf

·   Clear all the grid lines in Maxsurf

·   Export the grid lines in Excel, back into Maxsurf

 

The first procedure has been used already in this manual to open files. The procedure is described on page 36 and won’t be described again now.

 

·   Open a sample design, using the button on the Excel spreadsheet

 

If the Maxsurf Application does not start, or the design does not load, refer to Initial Settings on page 16.

Get Grid Lines from Maxsurf

This procedure uses a number of loops (one for each set of grid lines) to determine the label, position and angle (appropriate only for diagonals) of each grid line.

The GetGridLine method reads in the grid line type and the index, it returns the label, position and angle (where appropriate):

        msApp.Design.Grids.GetGridLine msGTSections, s, Sect_Label, sVal, sAngle

 

The Label and position values can then be written into Excel using the Range or Cells properties:

        Range("A" & s + 10) = Sect_Label

        Range("B" & s + 10) = sVal

 

When placed in a For Next statement with s values from 1 to the number of sections (using LineCount property), this will get all the Grid lines in the section direction.

 

We also need to determine the split section line, this is the point where section lines aft of this one, will show on the portside of the model, forward will show on starboard in body plan view. It is found in two lines of code:

    i = msApp.Design.Grids.SectionSplit

    Range("C" & i + 10) = "Split"

 

This could be further shortened to:

    Range("C" & msApp.Design.Grids.SectionSplit + 10) = "Split"

 

Combining four loops to find the grid line locations and the code to find the split line gives this completed code to get the grid lines.

Private Sub GetGridLines_Click()

'Get the locations of the grid lines from Maxsurf into Excel

 

 

    Dim Sect_Label As String

    Dim sVal As Double

    Dim sAngle As Double

    Dim Buttock_Label As String

    Dim bVal As Double

    Dim bAngle As Double

    Dim Waterline_Label As String

    Dim wVal As Double

    Dim wAngle As Double

    Dim Diagonal_Label As String

    Dim dVal As Double

    Dim dAngle As Double

 

    'This loop gets the section grid line locations

    For s = 1 To msApp.Design.Grids.LineCount(msGTSections)

   

        'The following line gets the section line data.

        'The input information is msGTSections (get sections) and s (the index value)

        'The method returns Sect_Label (the section Label), longitudinal position and the angle

        msApp.Design.Grids.GetGridLine msGTSections, s, Sect_Label, sVal, sAngle

        Range("A" & s + 10) = Sect_Label

        Range("B" & s + 10) = sVal

    Next s

   

    'Write Split in the Split Section Line

    i = msApp.Design.Grids.SectionSplit

    Range("C" & i + 10) = "Split"

 

 

   

    'This loop gets the buttock line locations

    For B = 1 To msApp.Design.Grids.LineCount(msGTButtocklines)

        msApp.Design.Grids.GetGridLine msGTButtocklines, B, Buttock_Label, bVal, bAngle

        Range("E" & B + 10) = Buttock_Label

        Range("F" & B + 10) = bVal

    Next B

   

    'This loop gets the Waterline locations

    For w = 1 To msApp.Design.Grids.LineCount(msGTWaterlines)

        msApp.Design.Grids.GetGridLine msGTWaterlines, w, Waterline_Label, wVal, wAngle

        Range("H" & w + 10) = Waterline_Label

        Range("I" & w + 10) = wVal

    Next w

   

    'This loop gets the diagonal line locations

    For D = 1 To msApp.Design.Grids.LineCount(msGTDiagonals)

        msApp.Design.Grids.GetGridLine msGTDiagonals, D, Diagonal_Label, dVal, dAngle

        Range("K" & D + 10) = Diagonal_Label

        Range("L" & D + 10) = dVal

        Range("M" & D + 10) = dAngle

    Next D

   

    msApp.Refresh

   

End Sub

Set the Grid Lines in Maxsurf

The concept of exporting grid lines into Maxsurf is the same as importing them. However, the method you have to use is reasonably different. We no longer have a definitive number of entries, so we cannot do a For Next statement through all of the data. Instead we need to use a Do While Loop, testing each time whether the target cell is empty or not.

 

There are two methods for defining grid line locations, SetGridLines and AddGridLines. Adding grid lines will create new grid lines, where setting grid lines will update a current grid line to new data.

 

When exporting the grid lines to Maxsurf we need to use the SetGridLines method to move all existing gridlines to new locations, then if there are insufficient grid lines, we need to add the remaining grid lines using the AddGridLines method.

The loop for creating the section lines is:

s = 1

Do While Range("B" & s + 10) <> "" 'While the position column has an entry in it

   Sect_Label = Range("A" & s + 10) 'Set the values for the label, position and angle

   sVal = Range("B" & s + 10)

 

   'This If statement will overwrite existing sections, or add more if there aren't enough

   If s <= msApp.Design.Grids.LineCount(msGTSections) Then

     'All 5 variables are inputs here, they are the same definition as in GetGridLines

     msApp.Design.Grids.SetGridLine msGTSections, s, Sect_Label, sVal, 0

   Else

      msApp.Design.Grids.AddGridLine msGTSections, Sect_Label, sVal, 0

   End If

       

   If Range("C" & s + 10) = "Split" Then

      msApp.Design.Grids.SectionSplit = s

   End If

       

   s = s + 1

Loop

 

This shows the If Then Else statement testing if s (the section index number) is less than or equal to the number of section lines. If it is, it will set the section lines to a new value, if it isn’t, it will add new section lines.

 

The complete code for setting all the grid lines is as follows;

 

Private Sub SetGridLines_Click()

'Set the locations of the grid lines in Maxsurf from Excel

 

    Dim Sect_Label As String

    Dim sVal As Double

    Dim Buttock_Label As String

    Dim bVal As Double

    Dim Waterline_Label As String

    Dim wVal As Double

    Dim Diagonal_Label As String

    Dim dVal As Double

    Dim dAngle As Double

   

    'This Do While loop sets the section lines in Maxsurf

    s = 1

    Do While Range("B" & s + 10) <> "" 'While the position column has an entry in it

        Sect_Label = Range("A" & s + 10) 'Set the values for the label, position and angle

        sVal = Range("B" & s + 10)

 

        'This if loop will overwrite existing sections, or add more if there aren't enough

        If s <= msApp.Design.Grids.LineCount(msGTSections) Then

            'All 5 variables are inputs here, they are the same definition as in GetGridLines

            msApp.Design.Grids.SetGridLine msGTSections, s, Sect_Label, sVal, 0

        Else

            msApp.Design.Grids.AddGridLine msGTSections, Sect_Label, sVal, 0

        End If

       

        If Range("C" & s + 10) = "Split" Then

            msApp.Design.Grids.SectionSplit = s

        End If

       

        s = s + 1

    Loop

 

    'This loop gets the buttock lines

    B = 1

    Do While Range("F" & B + 10) <> ""

        Buttock_Label = Range("E" & B + 10)

        bVal = Range("F" & B + 10)

 

        If B <= msApp.Design.Grids.LineCount(msGTButtocklines) Then

            msApp.Design.Grids.SetGridLine msGTButtocklines, B, Buttock_Label, bVal, 0

        Else

            msApp.Design.Grids.AddGridLine msGTButtocklines, Buttock_Label, bVal, 0

        End If

        B = B + 1

    Loop

   

    'This loop gets the waterlines

    w = 1

    Do While Range("I" & w + 10) <> ""

        Waterline_Label = Range("H" & w + 10)

        wVal = Range("I" & w + 10)

        If w <= msApp.Design.Grids.LineCount(msGTWaterlines) Then

            msApp.Design.Grids.SetGridLine msGTWaterlines, w, Waterline_Label, wVal, 0

        Else

            msApp.Design.Grids.AddGridLine msGTWaterlines, Waterline_Label, wVal, 0

        End If

        w = w + 1

    Loop

   

    'This loop gets the Diagonal Lines

    D = 1

    Do While Range("L" & D + 10) <> ""

        Diagonal_Label = Range("K" & D + 10)

        dVal = Range("L" & D + 10)

        dAngle = Range("M" & D + 10)

        If D <= msApp.Design.Grids.LineCount(msGTDiagonals) Then

            msApp.Design.Grids.SetGridLine msGTDiagonals, D, Diagonal_Label, dVal, dAngle

        Else

            msApp.Design.Grids.AddGridLine msGTDiagonals, Diagonal_Label, dVal, dAngle

        End If

        D = D + 1

    Loop

   

    msApp.Refresh

End Sub

Clear all Grid Lines in Maxsurf

This procedure simply deletes all the grid lines in Maxsurf; it uses one method, applied to each type of grid line.

 

The code reads:

Private Sub ClearAllGridinMS_Click()

    'This deletes all existing grid lines in Maxsurf when the "Clear All Grid in Maxsurf" button is Clicked

    'Each Grid direction is deleted individually

    msApp.Design.Grids.DeleteAllLines (msGTButtocklines)

    msApp.Design.Grids.DeleteAllLines (msGTDiagonals)

    msApp.Design.Grids.DeleteAllLines (msGTSections)

    msApp.Design.Grids.DeleteAllLines (msGTWaterlines)

    'Refresh must be included to update the views in Maxsurf to show no grid lines

    msApp.Refresh

 

End Sub