2
\$\begingroup\$

What is the best approach for creating textfiles from/with a template ? Currently I just read the template from a textfile and Replace all values needed, but I don't think that this is the most solid approach since I already have problems with text encodings etc.
I would be grateful for any ideas.

CODE:

Sub GenerateProgram(item As Integer, revision As Integer)

    Dim para As New MeasureParameter

    Try
        Dim template As String = System.IO.File.ReadAllText(My.Application.Info.DirectoryPath & "\data\zeiss\inspection_temp.para", System.Text.Encoding.UTF8)
        Dim rawfile As String = System.IO.File.ReadAllText(My.Settings.g250path & "\cav_" & item & "_" & revision & ".spf")
        Dim inspectiondata As String = System.IO.File.ReadAllText(My.Settings.inspectionpath & "\" & item & "-" & revision & "_inspection.htm")

        Dim sb As New System.Text.StringBuilder(template)

        'replace placeholders with parameters
        With sb
            .Replace("$PARA1$", GetValue(rawfile, "I_R875")) 'datum B

            If GetValue(rawfile, "I_R825") <> 14.5 Then
                .Replace("$PARA2$", 0)                          'outside nr
                .Replace("$PARA3$", 0)
            Else
                .Replace("$PARA2$", GetValue(rawfile, "I_R877")) 'outside nr
                .Replace("$PARA3$", 24.5)
            End If

            .Replace("$PARA4$", GetValue(rawfile, "I_R872")) 'length
            .Replace("$PARA5$", GetValue(rawfile, "I_R877")) 'outside gi
            .Replace("$PARA6$", GetValue(rawfile, "I_R838")) 'heigth outside gi

            If GetValue(rawfile, "I_R838") = 10 Then
                .Replace("$PARA7$", 355)                      'start angle outside gi
            Else
                .Replace("$PARA7$", 0)                        'start angle outside gi
            End If

            .Replace("$PARA8$", 1)                              'gi style
            .Replace("$PARA9$", GetValue(rawfile, "I_R830"))    'molding gi
            .Replace("$PARA10$", GetValue(rawfile, "I_R829"))    'molding nr
            .Replace("$PARA11$", GetAngleNrSide(inspectiondata))  'molding angle

           [...]

        End With

        WriteAllFiles(sb.ToString) 'write files

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub

TEMPLATE EXAMPLE:

    //***************************** AUSSEN  *******************************
Ø_DATUM_B        = $PARA1$
HEIGHT_Ø_DATUM_B = 1.6                      // Höhe Datum_B

Ø_OUTSIDE_NR_SIDE             = $PARA2$     // FALLS VORHANDEN - WENN NICHT MUSS 0 EINGEGEBEN WERDEN!
HEIGHT_Ø_OUTSIDE_NR_SIDE      = $PARA3$         // FALLS VORHANDEN - WENN NICHT MUSS 0 EINGEGEBEN WERDEN!
START_ANGLE_Ø_OUTSIDE_NR_SIDE = 0           // UM DIE ANTAST PUNKTE ZU VERSCHIEBEN.  - WENN NICHT MUSS 0 EINGEGEBEN                                                                               WERDEN!
Ø_OUTSIDE_NR_SIDE_STYLUS_2_4  = 2           // nicht aendern

LENGHT                        = $PARA4$
Ø_OUTSIDE_GI_SIDE             = $PARA5$    // Wassermass
HEIGHT_Ø_OUTSIDE_GI_SIDE      = $PARA6$      // Höhe Wassermass
START_ANGLE_Ø_OUTSIDE_GI_SIDE = $PARA7$         // Start Winkel
Ø_OUTSIDE_GI_SIDE_STYLE       = $PARA8$         // Messstrategie 1=360Grad/2=2x120Grad
//***************************** MOLDING  *******************************

Ø_MOLDING_GI_SIDE       = $PARA9$           // kleiner Moldingdia
Ø_MOLDING_NR_SIDE       = $PARA10$          // grosser Moldingdia
ANGLE_Ø_MOLDING_NR_SIDE = $PARA11$          // Winkel aus Inspection Sheet

//**********************************************************************
OFFSET_FOR_Ø_MOLDING_NR_SIDE = $PARA12$         // Längenaufmass (siehe PO -> "stock length")

SPLITLINE_SL_3 = $PARA13$                               // FÜR 0.3ER KUGEL = 1 ANSONSTEN = 0 EINGEBEN!
SPLITLINE_SL_5 = 0                              // FÜR 0.5ER KUGEL = 1 ANSONSTEN = 0 EINGEBEN!

LIP_DEPTH = $PARA14$                                        // FALLS VORHANDEN - WENN NICHT MUSS 0 EINGEGEBEN WERDEN!
//************************* MOLDING KONTUR  *****************************
Ø_MOLDING_NR_SIDE_CYLINDER_CHECK        = $PARA15$      
\$\endgroup\$
  • \$\begingroup\$ It seems like you didn't post the whole method. Please add the remaining code otherwise we can't review your code completely. \$\endgroup\$ – Heslacher Dec 6 '18 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.