Reading a text file..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?emFsZHk=?=

    Reading a text file..

    Hi!

    I have a question..
    How can csharp read a text file and execute it in another application? What
    I mean is that, Im doing a drawing using Tekla Structures. But instead of
    doing the same thing over and over again, I want to create an exe file that
    allows it to read a text file and execute it in Tekla Structures. All I can
    do now is to copy the contents of that text file and paste it in csharp
    editor and run it, therefore I cannot compile my program.
    Can anybody help me?
    Thanks..
  • Jon Skeet [C# MVP]

    #2
    Re: Reading a text file..

    On Apr 10, 1:37 am, zaldy <za...@discussi ons.microsoft.c omwrote:
    I have a question..
    How can csharp read a text file and execute it in another application?
    It's not clear to me what you mean by "execute it". Text files are
    just data -
    you don't "execute" them.

    Is Tekla Structures an application in itself? What's its input? Could
    you give more details of an example?

    Jon

    Comment

    • quasar@beagle.com.au

      #3
      Re: Reading a text file..

      Hi Zaldy, Jon, I'm Qu. I'm using C# with Tekla Stuctures, so I thought
      I'd join the discussion.

      A quick explanation for Jon: Tekla is an external 3d modelling
      program. It provides several dll files which are accessed the normal
      way ("using Tekla.Structure s.Model", "using Tekla.Structure s.UI").
      These dll files contain classes like the contour points and contour
      plates above, as well as some commands to connect to the model. These
      commands are run when you compile your program, and connect to the
      first open Tekla document and act on it (examples are
      ContourPlate.In sert and Model.CommitCha nges).

      The code Zaldy pasted above would compile and run straight from the C#
      executable... you could paste it into Visual C# and it would work.
      What he wants to do access a .txt file with it, import it, then
      compile and run it. I gather you've already worked this out.

      I can't find the CSharpCodeProvi der class referenced in the local 2005
      MSDN... is it available only to non-express users?

      My approach to this problem would be more along the lines of: Create a
      dynamic list of Contour Points, and several other variables (Name,
      ProfileString, MaterialString, Class, etc) in a separate class, then
      make your text file as a delimited file, as seen below. Extract the
      variables using string functions (IndexOf, subString, and such), place
      them into the constructor of your special class. Finally, get Tekla to
      generate all the classes at run time.

      ~~~~~~~~~~~~~~~ ~~~
      - PEDESTAL
      - PL125
      - SS400
      - 1

      ~ 0.0000,100.0000 ,150.0000
      ~ 100.0000,100.00 00,150.0000
      ~ 100.0000,0.0000 ,150.0000
      ~ etc...
      ~~~~~~~~~~~~~~~ ~~~~
      Of course, you might want more flexability than this method allows, in
      which case Jon's idea is much better and heaps easier...

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Reading a text file..

        On Apr 13, 11:04 pm, qua...@beagle.c om.au wrote:

        <snip>
        The code Zaldy pasted above would compile and run straight from the C#
        executable... you could paste it into Visual C# and it would work.
        What he wants to do access a .txt file with it, import it, then
        compile and run it. I gather you've already worked this out.
        Yes.
        I can't find the CSharpCodeProvi der class referenced in the local 2005
        MSDN... is it available only to non-express users?
        No, it's available to everyone although it might not be in the cut-
        down MSDN installed with Express. Search for it on the full MSDN - you
        can still use it from Express.
        My approach to this problem would be more along the lines of: Create a
        dynamic list of Contour Points, and several other variables (Name,
        ProfileString, MaterialString, Class, etc) in a separate class, then
        make your text file as a delimited file, as seen below. Extract the
        variables using string functions (IndexOf, subString, and such), place
        them into the constructor of your special class. Finally, get Tekla to
        generate all the classes at run time.
        Yes - that does indeed sound like a better idea than having code
        snippets.
        Of course, you might want more flexability than this method allows, in
        which case Jon's idea is much better and heaps easier...
        Sort of - it really depends on who's going to be producing the text
        file. Writing code outside an IDE is quite error-prone, for example.

        Jon

        Comment

        • quasar@beagle.com.au

          #5
          Re: Reading a text file..

          Sort of - it really depends on who's going to be producing the text
          file. Writing code outside an IDE is quite error-prone, for example.
          Good point. Hey Zaldy, what are you going to place in the text files?
          A database of code snippets to create specific individual objects,
          variables to apply to a specific type of object, or something else?
          Also important is who is going to be making the code: are you going to
          code it in the IDE and copy-paste to the txt file, or is a non-
          programmer going to try to write it?

          Each of these would warrent a different method.

          Cheers,
          Qu.

          Comment

          • =?Utf-8?B?emFsZHk=?=

            #6
            Re: Reading a text file..



            "quasar@beagle. com.au" wrote:
            Sort of - it really depends on who's going to be producing the text
            file. Writing code outside an IDE is quite error-prone, for example.
            Good point. Hey Zaldy, what are you going to place in the text files?
            A database of code snippets to create specific individual objects,
            variables to apply to a specific type of object, or something else?
            It will be the same as the sample text Ive shown you. To tell the truth, Im
            tired of modelling structures just to import it in PDS (another application)
            and be used for interference checking...It will just be a code to create
            specific individual objects.

            Also important is who is going to be making the code: are you going to
            code it in the IDE and copy-paste to the txt file, or is a non-
            programmer going to try to write it?
            No one will write the text file. An external application will be the one
            doing that for me. Ive already tried it and its 0% error as of now. That
            external application will write the text file in drive c. For different
            structures, the contents of that text file will also be different. I want to
            to create an compiled exe file that could read that text file and create
            whats inside the text file in Tekla Structures.
            Each of these would warrent a different method.
            >
            Cheers,
            Qu.
            >
            Thanks.
            Zaldy

            Comment

            • quasar@beagle.com.au

              #7
              Re: Reading a text file..

              It will be the same as the sample text Ive shown you. To tell the truth, Im
              tired of modellingstruct uresjust to import it in PDS (another application)
              and be used for interference checking...It will just be a code to create
              specific individual objects.
              >
              No one will write the text file. An external application will be the one
              doing that for me. Ive already tried it and its 0% error as of now. That
              external application will write the text file in drive c. For differentstruct ures, the contents of that text file will also be different. I want to
              to create an compiled exe file that could read that text file and create
              whats inside the text file inTeklaStructur es.
              >
              Ah, that changes things. Since the code is being written by an
              external application, CSharpCodeProvi der seems the way to go.



              As far as I understand it (remembering that I've never had to use it
              before), you make a string containing your code, create a new
              CSharpCodeProvi der, create and modify a set of Compiler Parameters to
              feed into your CSharpCodeProvi der, then make a Compiler Results object
              and call "CompileAssembl yFromSource".

              string str = "MessageBox.Sho w(\"You should import your
              code into this string.\")";
              CSharpCodeProvi der provider = new CSharpCodeProvi der();
              System.CodeDom. Compiler.Compil erParameters cp = new
              System.CodeDom. Compiler.Compil erParameters();
              cp.OutputAssemb ly = "Assem1";
              CompilerResults cr =
              provider.Compil eAssemblyFromSo urce(cp, str);

              Remember to add System.CodeDom. Compiler; and Microsoft.CShar p; to your
              using statments

              I can't really help you any further at the moment, because I'm not
              sure how to call an assembly and don't have time to work it out. Good
              luck, though.

              Cheers,
              Qu.

              Comment

              Working...