How to export TXT

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
lordsansui
Posts: 45
Joined: Thu Sep 15, 2022 8:54 pm

How to export TXT

Post by lordsansui »

Hello,

I'm starting as beginner regarding macro on Freecad.

Is it possible to export sketch or spreadsheet data as txt with specific format?

For instance consider the data below

Sketch.Constraints.Width (this variable has the value = 1500)
Spreadsheet.A15 (this variable has the value = 750)
Pad003.Lenght (this variable has the value = 500)

Can I export the above data in the below format?

S1 = 1500.00
S2 = 750.00
S3 = 500.00

If it's possible, could you share the syntax to do it and I will learn from there.
jfc4120
Posts: 448
Joined: Sat Jul 02, 2022 11:16 pm

Re: How to export TXT

Post by jfc4120 »

Here is an example of exporting to csv.

Code: Select all

    import csv

    with open('c:\\mydocs\\tube.csv', 'a') as f:
        writer = csv.writer(f)
        #writer.writerow([])

        if TOTALNUM == 1:
            writer.writerow(['1', L12, A123, 0, 0, SETBK1, LGBD1])
            writer.writerow(['2', L23, A234, THETA, TOTAL, SETBK2, LGBD2])
            writer.writerow(['3', L34, 0, 0, 0, 0, 0])

        if TOTALNUM > 1:
            if PDT == 1:
                writer.writerow(['1', L12, A123, 0, 0, SETBK1, LGBD1])
                writer.writerow(['2', L23, A234, THETA, TOTAL, SETBK2, LGBD2])
            if PDT > 1 and PDT < TOTALNUM:
                writer.writerow([PDT + 1, L23, A234, THETA, TOTAL, SETBK2, LGBD2])
            if PDT == TOTALNUM:
                writer.writerow([PDT + 1, L23, A234, THETA, TOTAL, SETBK2, LGBD2])
                writer.writerow([PDT + 2, L34, 0, 0, 0, 0, 0])
Maybe adapt to your data.
lordsansui
Posts: 45
Joined: Thu Sep 15, 2022 8:54 pm

Re: How to export TXT

Post by lordsansui »

Hello,

Wasn't able to catch how do you got a internal variable and export it.

From you code looks like you were creating a ROW.

How to access and existing data?
jfc4120
Posts: 448
Joined: Sat Jul 02, 2022 11:16 pm

Re: How to export TXT

Post by jfc4120 »

Normally you export results:

Code: Select all

1500.00
750.00
500.00
But you would have a header row also.

Are you wanting the s = also? I suggest looking up exporting to text file in python, and maybe you will find a closer example to fit what you are trying to do.
lordsansui
Posts: 45
Joined: Thu Sep 15, 2022 8:54 pm

Re: How to export TXT

Post by lordsansui »

Once I mention syntax, I think you answer in the last post isn't the good way to go, but thanks for trying to help.

To be able to save a TXT I found an official documentation, see below:

https://wiki.freecadweb.org/Dialog_crea ... ting_files

To access internal drawing variable in order to export, I was able to reach to the syntax below with a lot of trial and error.

Code: Select all

App.ActiveDocument.Spreadsheet.A11
The above syntax will access the value of the cell A11 in the spreadsheet, and so I can save it, see below as I implemented:

Code: Select all

file.write("S1 = "+str("{:.2f}".format(float(App.ActiveDocument.Spreadsheet.A11)))+"\n")   
Spreadsheet.A11 = 550.0 mm
float() = 550.0 (this will remove the unit)
{:.2f}".format = 550.00 *format to 2 decimals
str() = convert to string

so in the exported txt file I will find:

S1 = 550.00
Post Reply