Board index FlightGear Development Nasal

UFO object placement export to arbitrary file location

Nasal is the scripting language of FlightGear.

UFO object placement export to arbitrary file location

Postby openflight » Sun Jul 12, 2020 4:56 am

Split off from the topic Lack of documentation (?)


I am trying to write the object placement data to a text file. I am editing ufo.nas, getting this far: (Flight Gear v 1.0 Nasal version .. well there is fgcommand(savexml) why should this not work? Basically I need the file read writing code for Nasal

Code snippet:

Code: Select all
# Added code to print data and save to a file Openflight 12 Jul 2020

    print("Rule Printed");
    print("Selected data below");
    print(selected);
    fgcommand("save",selected);



OUTPUT:

Rule Printed
Selected data below


# w130n30/w123n37/942050.stg
OBJECT_SHARED Models/Airport/radar.xml -122.49545370 37.50848779 7.4856 0.0
OBJECT_SHARED Models/Airport/radar.xml -122.49533997 37.50845175 7.3988 0.0
OBJECT_SHARED Models/Airport/radar.xml -122.49534076 37.50845201 7.3994 0.0
OBJECT_SHARED Models/Airport/radar.xml -122.49534076 37.50845201 7.3994 0.0
OBJECT_SHARED Models/Airport/radar.xml -122.49534187 37.50845080 7.3993 0.0
Last edited by Johan G on Sun Jul 12, 2020 2:05 pm, edited 1 time in total.
Reason: Split off from the topic "Lack of documentation (?)"
openflight
 
Posts: 487
Joined: Fri Sep 16, 2011 12:14 pm
Version: 1 2 2018
OS: Linux Mint 19.3

Re: Lack of documentation (?)

Postby openflight » Sun Jul 12, 2020 6:25 am

viewtopic.php?t=32055&p=311004

Some io stuff here

http://wiki.flightgear.org/Nasal_library#printf.28.29


Code: Select all
var path = string.normpath(getprop("/sim/fg-home") ~ '/Export/parse_markdown()-test.html');

var file = io.open(path, "w");

var html = "<!DOCTYPE html>\n\n<html>\n\n<head>\n\t<meta charset=\"UTF-8\">\n\t<title>parse_markdown() test generated by Nasal</title>\n</head>\n\n<body>\n\t<pre>" ~ parsed ~ "</pre>\n</body>\n\n</html>";

io.write(file, html);
io.close(file);
print("Done, file ready for viewing (" ~ path ~ ")");



Console works:
var file = io.open("text1.txt", "w");
io.write(file,"some text");
io.close(file);
Last edited by openflight on Sun Jul 12, 2020 6:41 am, edited 1 time in total.
openflight
 
Posts: 487
Joined: Fri Sep 16, 2011 12:14 pm
Version: 1 2 2018
OS: Linux Mint 19.3

Re: Lack of documentation (?)

Postby D-ECHO » Sun Jul 12, 2020 6:36 am

@openflight: Have you looked at http://wiki.flightgear.org/Nasal_library/io ?

also, these posts might be better off split of into another topic, as it seems the thread is supposed to be about documentation (?)
D-ECHO
 
Posts: 2460
Joined: Sat May 09, 2015 1:31 pm
Pronouns: Bea (she/her)
Version: next

Re: Lack of documentation (?)

Postby openflight » Sun Jul 12, 2020 1:55 pm

D-ECHO, yes that is exactly what I was looking for!

After much searching, I had to resort to a forum query, why is it not listed on the Nasal page? Anyway thanks!

http://wiki.flightgear.org/Nasal_scripting_language
openflight
 
Posts: 487
Joined: Fri Sep 16, 2011 12:14 pm
Version: 1 2 2018
OS: Linux Mint 19.3

Re: UFO object placement export to arbitrary file location

Postby openflight » Sun Jul 12, 2020 2:39 pm

Thanks for the split. So here is the problem: The UFO object placement works nicely in version 1.0 and 2018 as well, which is good.
I know I am working with 1.0 for the moment, but getting a bit more familiar with the nasal code would be a good thing in either case.

The existing code prints the following data to the console, where I have to right click, select and copy. (something like this)

# w130n30/w123n37/942050.stg
OBJECT_SHARED Models/Airport/radar.xml -122.49545370 37.50848779 7.4856 0.0
OBJECT_SHARED Models/Airport/radar.xml -122.49533997 37.50845175 7.3988 0.0

How do I print this to a file, say object_exports.txt ?

The following code prints to the console, so modify this to write to file also, that is, write another function e.g. var write_model_data...

Code: Select all
var print_model_data = func(prop) {
   print("\n\n------------------------ Selected Object -----------2--------------\n");
   var elev = prop.getNode("elevation-ft").getValue();
   printf("Path:         %s", prop.getNode("path").getValue());
   printf("Latitude:     %.8f deg", prop.getNode("latitude-deg").getValue());
   printf("Longitude:    %.8f deg", prop.getNode("longitude-deg").getValue());
   printf("Altitude ASL: %.4f m (%.4f ft)", elev * FT2M, elev);
   printf("Heading:      %.1f deg", prop.getNode("heading-deg").getValue());
   printf("Pitch:        %.1f deg", prop.getNode("pitch-deg").getValue());
   printf("Roll:         %.1f deg", prop.getNode("roll-deg").getValue());
}

# interface functions -----------------------------------------------------------------------------

var print_data = func {
   var rule = "\n----------------------------------------------3--------------------\n";
   print("\n\n");
   print_ufo_data();

   var data = modelmgr.get_data();

   var selected = data.getChild("model", 0);
   if (selected == nil) {
      print(rule);
      return;
   }

     print_model_data(selected);
      print(rule);

openflight
 
Posts: 487
Joined: Fri Sep 16, 2011 12:14 pm
Version: 1 2 2018
OS: Linux Mint 19.3

Re: UFO object placement export to arbitrary file location

Postby openflight » Sun Jul 12, 2020 2:41 pm

I will probably figure it our eventually, but if anyone has the time to help, it would be nice. :)
openflight
 
Posts: 487
Joined: Fri Sep 16, 2011 12:14 pm
Version: 1 2 2018
OS: Linux Mint 19.3

Re: UFO object placement export to arbitrary file location

Postby openflight » Sun Jul 12, 2020 4:40 pm

Got it to write to file, but only writes one line.

If I get an answer I will continue otherwise I will go the manual route:

OBJECT_SHARED Models/Airport/ils.xml -122.35724735 37.61354418 -0.1371 0.0


Code Amended:

Code: Select all
# Open file for printing  test1.txt
    var file = io.open("text2.txt", "w");

   # group all objects of a bucket
   var bucket = {};
   foreach (var m; data.getChildren("model")) {
      var stg = m.getNode("stg-path").getValue();
      var obj = m.getNode("object-line").getValue();
      if (contains(bucket, stg))
         append(bucket[stg], obj);
      else
         bucket[stg] = [obj];
   }
   foreach (var key; keys(bucket)) {
      print("\n# ", key);
        print("n key");
      foreach (var obj; bucket[key])
          print(obj);
    #Write the data to file
            io.write(file,obj);
            print(bucket[key]);
   }
   print(rule);
# close file
io.close(file);
}
openflight
 
Posts: 487
Joined: Fri Sep 16, 2011 12:14 pm
Version: 1 2 2018
OS: Linux Mint 19.3

Re: Lack of documentation (?)

Postby legoboyvdlp » Sun Jul 12, 2020 4:46 pm

openflight wrote in Sun Jul 12, 2020 1:55 pm:
After much searching, I had to resort to a forum query, why is it not listed on the Nasal page? Anyway thanks!


Actually, it is :

Image
User avatar
legoboyvdlp
 
Posts: 7981
Joined: Sat Jul 26, 2014 2:28 am
Location: Northern Ireland
Callsign: G-LEGO
Version: next
OS: Windows 10 HP

Re: UFO object placement export to arbitrary file location

Postby openflight » Mon Jul 13, 2020 1:38 am

What it the URL please?
openflight
 
Posts: 487
Joined: Fri Sep 16, 2011 12:14 pm
Version: 1 2 2018
OS: Linux Mint 19.3

Re: UFO object placement export to arbitrary file location

Postby Johan G » Mon Jul 13, 2020 1:49 am

Low-level flying — It's all fun and games till someone looses an engine. (Paraphrased from a YouTube video)
Improving the Dassault Mirage F1 (Wiki, Forum, GitLab. Work in slow progress)
Some YouTube videos
Johan G
Moderator
 
Posts: 6629
Joined: Fri Aug 06, 2010 6:33 pm
Location: Sweden
Callsign: SE-JG
IRC name: Johan_G
Version: 2020.3.4
OS: Windows 10, 64 bit

Re: UFO object placement export to arbitrary file location

Postby openflight » Wed Jul 15, 2020 2:34 pm

Thanks, well I wrote a Python script to extract the object information from the exported xml file and it works fine. Come to think of it, I could have used a later version and got the clipboard data from there. (version 1 does not do clipboard)
openflight
 
Posts: 487
Joined: Fri Sep 16, 2011 12:14 pm
Version: 1 2 2018
OS: Linux Mint 19.3


Return to Nasal

Who is online

Users browsing this forum: No registered users and 2 guests