hexagon logo

Need a little help with basic

I'm new to basic and VB so i'm just trying stuff to see how it works. Well i don't quite understand everything. Here's the code from Pc-Dmis (v4.3):
V1         =FILE/EXISTS,FILENAME
            IF/V1<>0
FPTR       =FILE/OPEN,FILENAME,READ
V2         =FILE/READLINE,FPTR,{STR_DRWSTAT}
V3         =FILE/READLINE,FPTR,{STR_RPT}
            FILE/CLOSE,FPTR,KEEP
            END_IF/
            ASSIGN/STR_DRWSTAT=STR_DRWSTAT
            ASSIGN/STR_RPT=STR_RPT
            ASSIGN/STR_OPER=STR_OPER
CS1        =FORM/FILENAME= D:\MERITVE\PROGRAMI_4.3\CADONLY_LANDSCAPE.FORM, AUTOPRINT=NO
            PARAM/TXTDRWSTAT.TEXT=STR_DRWSTAT
            PARAM/TXTRPT.TEXT=STR_RPT
            PARAM/TXTOPER.TEXT=STR_OPER
            PARAM/=
            ENDFORM/
FPTR       =FILE/OPEN,FILENAME,WRITE
            FILE/WRITELINE,FPTR,STR_DRWSTAT
            FILE/WRITELINE,FPTR,STR_RPT
            FILE/WRITELINE,FPTR,STR_OPER
            FILE/CLOSE,FPTR,KEEP

As you can see i want to check if the file exists and then read some stuff from it. In case it doesn't exist i just let the program go forward. This works fine if the upper code in inserted manualy in Pc-Dmis. Then i saved this as BASIC script. Now when I recall it in another program usig Insert-Basic script... the IF block doesn't work since it returns an error when running the program and it wan'ts to read lines though the file doesn't exist. The code for IF as BASIC looks like this:
  Set DmisCommand = DmisCommands.Add(FILE_IO_OBJECT, TRUE)
    DmisCommand.Marked = TRUE
  ' Set Destination Expression  = V1
    retval = DmisCommand.PutText ("V1", DEST_EXPR, 0)
  ' Set Filename Item 1 = H1000011127AB - HALTER PORSCHE 9X1 (1000011127).TXT
    retval = DmisCommand.PutText ("H1000011127AB - HALTER PORSCHE 9X1 (1000011127).TXT", FILE_NAME, 1)
  ' Set File Command  = EXISTS
    retval = DmisCommand.SetToggleString (15, FILE_COMMAND_TYPE, 0)
  Result = DmisCommand.SetExpression("FILENAME", FILE_NAME, 1)
  
  Set DmisCommand = DmisCommands.Add(IF_BLOCK_COMMAND, TRUE)
    DmisCommand.Marked = TRUE
  ' Set Source Expression  = 1
    retval = DmisCommand.PutText ("1", SRC_EXPR, 0)
  Result = DmisCommand.SetExpression("V1<>0", SRC_EXPR, 0)

Now what and how would i need to change the BASIC code for it to work properly?? Please help.
  • Below is an export from your code:

    Dim DmisApp As Object
    Dim DmisPart As Object
    Dim DmisCommands As Object
    Dim DmisCommand As Object
    
    Sub Main
      Set DmisApp = CreateObject("PCDLRN.Application")
      Set DmisPart = DmisApp.ActivePartProgram
      Set DmisCommands = DmisPart.Commands
      CommandCount = DmisCommands.Count
      Set DmisCommand = DmisCommands.Item(CommandCount)
      DmisCommands.InsertionPointAfter DmisCommand
      
      Set DmisCommand = DmisCommands.Add(FILE_IO_OBJECT, TRUE)
        DmisCommand.Marked = TRUE
        retval = DmisCommand.PutText ("V1", DEST_EXPR, 0)
        retval = DmisCommand.SetToggleString (15, FILE_COMMAND_TYPE, 0)
      
      Set DmisCommand = DmisCommands.Add(IF_BLOCK_COMMAND, TRUE)
        DmisCommand.Marked = TRUE
        retval = DmisCommand.PutText ("3", MODE_TYPE, 0)
        retval = DmisCommand.PutText ("0", SRC_EXPR, 0)
      Result = DmisCommand.SetExpression("V1<>0", SRC_EXPR, 0)
      
      Set DmisCommand = DmisCommands.Add(FILE_IO_OBJECT, TRUE)
        DmisCommand.Marked = TRUE
        retval = DmisCommand.PutText ("FPTR", FILE_POINTER, 0)
        retval = DmisCommand.PutText ("H1000011127AB - HALTER PORSCHE 9X1 (1000011127).TXT", FILE_NAME, 1)
        retval = DmisCommand.SetToggleString (1, READ_WRITE, 0)
        retval = DmisCommand.SetToggleString (1, FILE_COMMAND_TYPE, 0)
      Result = DmisCommand.SetExpression("FILENAME", FILE_NAME, 1)
      
      Set DmisCommand = DmisCommands.Add(FILE_IO_OBJECT, TRUE)
        DmisCommand.Marked = TRUE
        retval = DmisCommand.PutText ("V2", DEST_EXPR, 0)
        retval = DmisCommand.PutText ("{STR_DRAWSTAT}", SRC_EXPR, 0)
        retval = DmisCommand.PutText ("FPTR", FILE_POINTER, 0)
        retval = DmisCommand.SetToggleString (4, FILE_COMMAND_TYPE, 0)
      
      Set DmisCommand = DmisCommands.Add(FILE_IO_OBJECT, TRUE)
        DmisCommand.Marked = TRUE
        retval = DmisCommand.PutText ("V3", DEST_EXPR, 0)
        retval = DmisCommand.PutText ("{STR_RPT}", SRC_EXPR, 0)
        retval = DmisCommand.PutText ("FPTR", FILE_POINTER, 0)
        retval = DmisCommand.SetToggleString (4, FILE_COMMAND_TYPE, 0)
      
      Set DmisCommand = DmisCommands.Add(FILE_IO_OBJECT, TRUE)
        DmisCommand.Marked = TRUE
        retval = DmisCommand.PutText ("FPTR", FILE_POINTER, 0)
        retval = DmisCommand.SetToggleString (2, FILE_COMMAND_TYPE, 0)
        retval = DmisCommand.SetToggleString (1, DELETE_TYPE, 0)
      
      Set DmisCommand = DmisCommands.Add(END_IF_COMMAND, TRUE)
        DmisCommand.Marked = TRUE
        retval = DmisCommand.PutText ("3", MODE_TYPE, 0)
    
      DmisPart.RefreshPart
      
    End Sub


    Tried that?
  • Thanks vpt.se but as far as i can tell i get the same result. If txt file allready exists then everything works fine. Hovewer if the txt file isn't there yet i get an error about not being able to open file.... and unable to read.... and so on. The program still runs though. It's just those annoying messages for the first run. I was hoping to get rid of them. Any other suggestions?
  • Create the .txt file first?

    Opening as Append and immediately closing the file will create the file if it doesn't exist.
  • So the problem is not the code part - as that seem to work - but the if/then statements and what to do if the file doesn't exist?
  • Something like that. The thing is that the if/then statements work fine if i put them into pcdmis edit window manualy. But i wan't to use this in most of my programs so i exported it to basic so i can recall/insert the script whenever i need it. However when it gets inserted via
    CS1        =SCRIPT/FILENAME= D:\MERITVE\PROGRAMI_4.3\VW_CADONLY_DEFAULT.BAS
                FUNCTION/Main,SHOW=YES,,
                STARTSCRIPT/
    

    ...the if/then statements don't work anymore. Any suggestions how to solve this?
  • Create the .txt file first?

    Opening as Append and immediately closing the file will create the file if it doesn't exist.


    OK. I could give it a try. But what happens if the file allready exists? Will it get overwritten or not?
  • If you choose to open the file as APPEND, the file will not be overwritten. If you choose to write to it while in APPEND mode, stuff will be added to the end of the file.
  • Many thanks guys. Tried it with APPEND and it works perfectly. Just what i wanted.