TXT issue - Inconsistant file format/data

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

TXT issue - Inconsistant file format/data

Post by lordsansui »

Hello,

Finally. I was able to complete my first macro (simple one) to create a TXT file with some data, currently it's not linked to the CAD, it just create a simple TXT file with specific data format so I can import from a different software (automation).

If i try to import the TXT file created from the macro to the other software it generates an error but if I just open the file in Kate and save it, the software is able to ready it perfectly. So it looks like the way Kate save the TXT data is different from the away Freecad save it trough the macro script.

Trying to figure out what was causing the issue I opened the file properties using Dolphin file manager and I was able to identify two main differences. See attachment.

File saved from Macro shows 2,5 KiB size vs File resaved from Kate shows 2,7 KiB size
File saved from Macro shows Line Count: 1 vs File resaved from Kate shows Line Count: 166

All the rest is the same, encode type, file type, data contents, etc. below the code so you can check and compare too.

Is there any suggestion to how to fix it? thanks in advanced.

Code: Select all

# -*- coding: utf-8 -*-
import PySide
from PySide import QtGui ,QtCore
from PySide.QtGui import *
from PySide.QtCore import *
path = FreeCAD.ConfigGet("UserAppData")

try:
    SaveName = QFileDialog.getSaveFileName(None,QString.fromLocal8Bit("Save a file txt"),path,             "*.txt") # PyQt4
#                                                                     "here the text displayed on windows" "here the filter (extension)"
except Exception:
    SaveName, Filter = PySide.QtGui.QFileDialog.getSaveFileName(None, "Save a file txt", path,             "*.txt") # PySide
#                                                                     "here the text displayed on windows" "here the filter (extension)"
if SaveName == "":                                                            # if the name file are not selected then Abord process
    App.Console.PrintMessage("Process aborted"+"\n")
else:                                                                         # if the name file are selected or created then
    App.Console.PrintMessage("Registration of "+SaveName+"\n")                # text displayed to Report view (Menu > View > Report view checked)
    try:                                                                      # detect error ...
        file = open(SaveName,'wt')                                            # open the file selected to write (w)
        try:                                                                  # if error detected to write ...
            print("Exporting")
            #file.write("S1 = "+str("{:.2f}".format(float(App.ActiveDocument.Spreadsheet.A11)))+"\n")                                           # write the number convert in text with (str())
            file.write("ID = 54.10"+"\n\r")
            file.write("Comment = Paraflex Type C"+"\n\r")
            file.write("|RADIATION, SOURCE AND MOUTH PARAMETER VALUES:"+"\n\r")
            file.write("Ang = 2.0 x Pi"+"\n")
            file.write("Eg = 2.83"+"\n")
            file.write("Rg = 0.00"+"\n")
            file.write("Fta = 0.00"+"\n\r")
            file.write("|HORN PARAMETER VALUES:"+"\n\r")
            file.write("S1 = 1500.00"+"\n")
            file.write("S2 = 1500.00"+"\n")
            file.write("Par = 20.00"+"\n")
            file.write("F12 = 0.00"+"\n")
            file.write("S2 = 1500.00"+"\n")
            file.write("S3 = 1500.00"+"\n")
            file.write("Par = 6.00"+"\n")
            file.write("F23 = 0.00"+"\n")
            file.write("S3 = 1500.00"+"\n")
            file.write("S4 = 1500.00"+"\n")
            file.write("Par = 6.00"+"\n")
            file.write("F34 = 0.00"+"\n")
            file.write("S4 = 1500.00"+"\n")
            file.write("S5 = 1500.00"+"\n")
            file.write("Par = 2.00"+"\n")
            file.write("F45 = 0.00"+"\n\r")
            file.write("|TRADITIONAL DRIVER PARAMETER VALUES:"+"\n\r")
            file.write("Sd = 880.00"+"\n")
            file.write("Bl = 23.08"+"\n")
            file.write("Cms = 1.37E-04"+"\n")
            file.write("Rms = 2.94"+"\n")
            file.write("Mmd = 94.99"+"\n")
            file.write("Le = 1.08"+"\n")
            file.write("Re = 4.70"+"\n")
            file.write("PH1 = 1"+"\n\r")
            file.write("|ADVANCED DRIVER PARAMETER VALUES FOR SEMI-INDUCTANCE MODEL:"+"\n\r")
            file.write("Re' = 0.00"+"\n")
            file.write("Leb = 0.00"+"\n")
            file.write("Le = 0.00"+"\n")
            file.write("Ke = 0.00"+"\n")
            file.write("Rss = 0.00"+"\n\r")
            file.write("|ADVANCED DRIVER PARAMETER VALUES FOR FREQUENCY-DEPENDENT DAMPING MODEL:"+"\n\r")
            file.write("Rms = 0.00"+"\n")
            file.write("Ams = 0.00"+"\n\r")
            file.write("|PASSIVE RADIATOR PARAMETER VALUE:"+"\n\r")
            file.write("Added Mass = 0.00"+"\n\r")
            file.write("|CHAMBER PARAMETER VALUES:"+"\n\r")
            file.write("S6 = 800.00"+"\n")
            file.write("Par = 14.00"+"\n")
            file.write("S8 = 1200.00"+"\n")
            file.write("Par = 180.00"+"\n")
            file.write("S9 = 1200.00"+"\n")
            file.write("Par = 1.50"+"\n\r")
            file.write("Acoustic Path Length = 0.0"+"\n\r")
            file.write("|MAXIMUM SPL PARAMETER VALUES:"+"\n\r")
            file.write("Pamp = 100"+"\n")
            file.write("Vamp = 25"+"\n")
            file.write("Iamp = 4"+"\n")
            file.write("Pmax = 800"+"\n")
            file.write("Xmax = 8.5"+"\n\r"+"\n")
            file.write("Maximum SPL Setting = 3"+"\n\r")
            file.write("|ABSORBENT FILLING MATERIAL PARAMETER VALUES:"+"\n\r")
            file.write("Fr1 = 0.00"+"\n")
            file.write("Fr2 = 0.00"+"\n")
            file.write("Fr3 = 0.00"+"\n")
            file.write("Fr4 = 0.00"+"\n\r")
            file.write("Tal1 = 100"+"\n")
            file.write("Tal2 = 100"+"\n")
            file.write("Tal3 = 100"+"\n")
            file.write("Tal4 = 100"+"\n\r")
            file.write("|ACTIVE BAND PASS FILTER PARAMETER VALUES:"+"\n\r")
            file.write("High Pass Frequency = 0"+"\n")
            file.write("High Pass Slope = 1"+"\n")
            file.write("Low Pass Frequency = 0"+"\n")
            file.write("Low Pass Slope = 1"+"\n\r")
            file.write("Butterworth High Pass Order = 1"+"\n")
            file.write("Butterworth Low Pass Order = 1"+"\n")
            file.write("Linkwitz-Riley High Pass Order = 2"+"\n")
            file.write("Linkwitz-Riley Low Pass Order = 2"+"\n")
            file.write("Bessel High Pass Order = 1"+"\n")
            file.write("Bessel Low Pass Order = 1"+"\n\r"+"\n")
            file.write("2nd Order High Pass Q = 0.5"+"\n")
            file.write("2nd Order Low Pass Q = 0.5"+"\n")
            file.write("4th Order High Pass Q = 0.5"+"\n")
            file.write("4th Order Low Pass Q = 0.5"+"\n\r")
            file.write("Active Filter Alignment = 1"+"\n")
            file.write("Active Filter On / Off Switch = 1"+"\n\r")
            file.write("|PASSIVE FILTER PARAMETER VALUES:"+"\n\r")
            file.write("Series / Parallel 1 = S"+"\n")
            file.write("Series / Parallel 2 = S"+"\n")
            file.write("Series / Parallel 3 = S"+"\n")
            file.write("Series / Parallel 4 = S"+"\n\r")
            file.write("|EQUALISER FILTER PARAMETER VALUES:"+"\n\r")
            file.write("Band 1 Frequency = 0"+"\n")
            file.write("Band 1 Q Factor = 0.01"+"\n")
            file.write("Band 1 Gain = 0.0"+"\n")
            file.write("Band 1 Type = -1"+"\n")
            file.write("Band 2 Frequency = 0"+"\n")
            file.write("Band 2 Q Factor = 0.01"+"\n")
            file.write("Band 2 Gain = 0.0"+"\n")
            file.write("Band 2 Type = -1"+"\n")
            file.write("Band 3 Frequency = 0"+"\n")
            file.write("Band 3 Q Factor = 0.01"+"\n")
            file.write("Band 3 Gain = 0.0"+"\n")
            file.write("Band 3 Type = -1"+"\n")
            file.write("Band 4 Frequency = 0"+"\n")
            file.write("Band 4 Q Factor = 0.01"+"\n")
            file.write("Band 4 Gain = 0.0"+"\n")
            file.write("Band 4 Type = -1"+"\n")
            file.write("Band 5 Frequency = 0"+"\n")
            file.write("Band 5 Q Factor = 0.01"+"\n")
            file.write("Band 5 Gain = 0.0"+"\n")
            file.write("Band 5 Type = -1"+"\n")
            file.write("Band 6 Frequency = 0"+"\n")
            file.write("Band 6 Q Factor = 0.01"+"\n")
            file.write("Band 6 Gain = 0.0"+"\n")
            file.write("Band 6 Type = -1"+"\n\r")
            file.write("|STATUS FLAGS:"+"\n\r")
            file.write("Auto Path Flag = 1"+"\n")
            file.write("Lossy Inductance Model Flag = 0"+"\n")
            file.write("Semi-Inductance Model Flag = 0"+"\n")
            file.write("Damping Model Flag = 0"+"\n")
            file.write("Closed Mouth Flag = 0"+"\n")
            file.write("Continuous Flag = 1"+"\n\r")
            file.write("|OTHER SETTINGS:"+"\n\r")
            file.write("Filter Type Index = 0"+"\n")
            file.write("Filter Input Index = 0"+"\n")
            file.write("Filter Output Index = 0"+"\n\r")
            file.write("Filter Type = 1"+"\n\r")
            file.write("MEH Configuration = 0"+"\n")
            file.write("ME Amplifer Polarity Value = 1"+"\n")
        except Exception:                                                     # if error detected to write
            App.Console.PrintError("Error write file "+"\n")                  # detect error ... display the text in red (PrintError)
        finally:                                                              # if error detected to write ... or not the file is closed
            file.close()                                                      # if error detected to write ... or not the file is closed
            print("File Saved")
    except Exception:
        App.Console.PrintError("Error Open file "+SaveName+"\n")      # detect error ... display the text in red (PrintError)
Attachments
teste-code.png
teste-code.png (125.12 KiB) Viewed 666 times
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: TXT issue - Inconsistant file format/data

Post by Roy_043 »

You are using a mix of EOL (end of line) characters. Use either only Windows EOL: "\r\n" (NOT: "\n\r") or Unix EOL: "\n".
lordsansui
Posts: 45
Joined: Thu Sep 15, 2022 8:54 pm

Re: TXT issue - Inconsistant file format/data

Post by lordsansui »

I did what you suggested using "\r\n"replacing "\n\r", it create an issue because it removes the empty the format needs at specific places.

I tried also "\n\n" and it creates the empty line that I need, the Line count was fixed but the file size didn't. Now even saving the file again trough Kate does't fix the issue anymore. so looks like this command is part of the issue. Calling "\n\n" or +"\n""+\n" make no difference.

Maybe this command is the issue or part of it.

I'm attaching both files as indicate below. The internal content is the same, but the software see one as good and the other as broken. Maybe you can check things that i'm not able to.

paraflex.txt - good one 2,7 KiB
teste.txt - broken one 2,5 KiB

Thanks for the help.
Attachments
teste.txt
(2.53 KiB) Downloaded 23 times
paraflex.txt
(2.69 KiB) Downloaded 20 times
lordsansui
Posts: 45
Joined: Thu Sep 15, 2022 8:54 pm

Re: TXT issue - Inconsistant file format/data

Post by lordsansui »

Roy_043 wrote: Wed Sep 28, 2022 8:05 am You are using a mix of EOL (end of line) characters. Use either only Windows EOL: "\r\n" (NOT: "\n\r") or Unix EOL: "\n".
Your suggestions gave me some tip thanks again for it, searching little more I found the site below with gave me additional tips

https://jdhao.github.io/2018/12/08/newl ... e_notepad/

checking pyton function open() it has an specific argument called newline as you can see below.

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True)

To fix the issue It was need to set up the function open() to use Windows end of line "\r\n" seting newline argument to "\r\n" so the file is saved slightly different changing the file size to correct value 2.7 KiB

Thank you again
Post Reply