Reading a text file..

Z

zaldy

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..
 
J

Jon Skeet [C# MVP]

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
 
Z

zaldy

Hello Jon.

Tekla structure is an application itself. It is an engineering detailing
software.

Here is an example of what the text file contains,
ContourPlate CP10 = new ContourPlate();
ContourPoint point101 = new ContourPoint(new
TSG.Point(0.0000,100.0000,150.0000), null);
ContourPoint point102 = new ContourPoint(new
TSG.Point(100.0000,100.0000,150.0000), null);
ContourPoint point103 = new ContourPoint(new
TSG.Point(100.0000,0.0000,150.0000), null);
ContourPoint point104 = new ContourPoint(new
TSG.Point(0.0000,0.0000,150.0000), null);
CP10.AddContourPoint(point101);
CP10.AddContourPoint(point102);
CP10.AddContourPoint(point103);
CP10.AddContourPoint(point104);
CP10.Name = "PEDESTAL";
CP10.Profile.ProfileString = "PL125";
CP10.Material.MaterialString = "SS400";
CP10.Class = "1";
CP10.Position.Depth = Position.DepthEnum.BEHIND;
bool Result10 = false;
Result10 = CP10.Insert();
CP10.Insert();
Model1.CommitChanges();

This will make a rectangle with thickness of 125mm with a name of pedestal,
and so forth..
What im trying to do is to compile an exe with the use of csharp. When I run
this exe file, it will read the text file and it will create a rectangle with
thickness of 125mm with a name of pedestal, and so forth, just like the
example above, in Tekla Structures. Think of that exe file as a macro inside
Tekla Structures.
I cannot compile my program since I still have to copy the contents of the
text file and paste it in the editor before it could be executed.

Hope you get my point.
Thank you very much

zaldy
 
J

Jon Skeet [C# MVP]

Tekla structure is an application itself. It is an engineering detailing
software.

So are you going to launch this as a separate process? Or is it code
you can use as a class library?
Here is an example of what the text file contains,

<snip>

Is that all valid C#? Does it need any other context (variables from
your code, etc)?
What im trying to do is to compile an exe with the use of csharp. When I run
this exe file, it will read the text file and it will create a rectangle with
thickness of 125mm with a name of pedestal, and so forth, just like the
example above, in Tekla Structures. Think of that exe file as a macro inside
Tekla Structures.

Inside Tekla Structures or inside your application? Or is your
application part of Tekla Structures?
I cannot compile my program since I still have to copy the contents of the
text file and paste it in the editor before it could be executed.

Hope you get my point.

I'm still not entirely sure I do, but I strongly suspect the
CSharpCodeProvider class will help - that will allow you to read in
your text file, add some things like using directives, and a class and
method declaration, and then compile the code dynamically into an
assembly. You can then call that code from your code, which sounds
like what you want to do.

Jon
 
Q

quasar

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.Structures.Model", "using Tekla.Structures.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.Insert and Model.CommitChanges).

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 CSharpCodeProvider 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.0000,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...
 
J

Jon Skeet [C# MVP]

On Apr 13, 11:04 pm, (e-mail address removed) wrote:

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 CSharpCodeProvider 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
 
Q

quasar

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.
 
Z

zaldy

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
 
Q

quasar

It will be the same as the sample text Ive shown you. To tell the truth, Im
tired of modellingstructuresjust 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 differentstructures, 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 inTeklaStructures.
Ah, that changes things. Since the code is being written by an
external application, CSharpCodeProvider seems the way to go.

http://msdn2.microsoft.com/en-us/library/microsoft.csharp.csharpcodeprovider.aspx

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
CSharpCodeProvider, create and modify a set of Compiler Parameters to
feed into your CSharpCodeProvider, then make a Compiler Results object
and call "CompileAssemblyFromSource".

string str = "MessageBox.Show(\"You should import your
code into this string.\")";
CSharpCodeProvider provider = new CSharpCodeProvider();
System.CodeDom.Compiler.CompilerParameters cp = new
System.CodeDom.Compiler.CompilerParameters();
cp.OutputAssembly = "Assem1";
CompilerResults cr =
provider.CompileAssemblyFromSource(cp, str);

Remember to add System.CodeDom.Compiler; and Microsoft.CSharp; 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top