Export Significant Digits

I made a python script that generates a series of points. At the end of the script, I would like to export said points to a .txt file. The export is executing smoothly; however, I would like to export 3 significant digits for each point coordinate. I tried modifying the input to “file.write(node_base.format(rs.PointCoordinates(point)))” using a round function but am having difficulty. I would appreciate any advice:

def exportTxt():
    rs.UnselectAllObjects()
    rs.Command('-_SelDup')
    rs.Command('-_SelCrv')
    rs.Command('-_Delete')
    rs.UnselectAllObjects()
    rs.Command('-_SelPt')
    points = rs.GetObjects('Select points to export to txt file',filter = 1,preselect=True)
    if not points: return
    folder = rs.BrowseForFolder()
    fName = rs.GetString("Input File Name")
    if not fName: return
    file_name = os.path.join(folder,fName+".txt")
    if not file_name: return
    
    file = open(file_name, 'w') #writing
    node_base = '{0.X} {0.Y} {0.Z}\n'
    
    for point in points:
        file.write(node_base.format(rs.PointCoordinates(point)))
    file.close()
    return


def exportCheck():
    gs = Rhino.Input.Custom.GetString()
    gs.SetCommandPrompt("Output data points to txt file?")
    boolOption = Rhino.Input.Custom.OptionToggle(True,"No","Yes")
    gs.AddOptionToggle("Boolean",boolOption)
    get_rc = gs.Get()
    print " Boolean =", boolOption.CurrentValue
    
    #export points if user chooses Yes
    if boolOption.CurrentValue == True:
        exportTxt()
    if boolOption.CurrentValue == False:
        rs.MessageBox("No Data Output",title='WARNING:')
    return

exportCheck()

I might do this:

for point in points:
    pt=rs.coerce3dpoint(point)
    file.write("{:.3f} {:.3f} {:.3f}\n".format(pt.X, pt.Y, pt.Z))
file.close()

--Mitch

Worked like a charm! Thank you, Mitch. Always impressed by your knowledge of rhino.

@Helvetosaur While I have you, I am trying to add an escapetest for the entirety of the code so if at any time the user decides to cancel the run, it will break from the code without throwing errors in the rhino python editor or continue looping through functions. Is this a possibility or will I have to add an escapetest within every loop?

These functions call my whole code:

DWLht = GetSummerLoadLine(0.0)
if Rhino.RhinoMath.IsValidDouble(DWLht):
    print "Summer Load Line:", DWLht, "m"

Water = WaterSrf()

weatherDeckht = CrossSectionalCrvs()

weatherDeck = SplitAllSelected(weatherDeckht)

GeometricQuery(weatherDeck,weatherDeckht)

PointPopulation(weatherDeckht)

exportCheck()

Yes, you need to to have the escape test on every loop if you want to exit as soon as the user hits esc. You would need to import scriptcontext at the top of the script to use the following escape test:

    for i in range(nnn):
        #do something
        if  scriptcontext.escape_test(False): break #or return or....

I suppose you could also use it in a while loop, haven’t tested it thoroughly though:

while not sc.escape_test(False):
    #continue to do your stuff...

–Mitch