Update Infraworks 360 data using JavaScript and a CSV file

You can add or update data for any object within Infraworks 360 using a simple CSV file saved from Excel and a little bit of Javascript coding. Here’s how….

In this simple example, I have a CSV file which has four (4) columns and six (6) rows. The first row doesn’t really count because it’s the header information for the columns. Everything below that is where the magic happens.

IW_JS_001

You can copy/paste the code below to the Scripting dialog. To open the Scripting dialog box click on CREATE AND MANAGE YOURMODEL then click SCRIPTS.IW_JS_002

IW_JS_003

My Javascript code looks like this.

function UpdateBldgData() {
  var db = app.ActiveModelDb;
  var sset = app.ActiveSelectionSet;
  if (sset.QueryCount() < 1) {
    alert("Please select one or more buildings and try again.");
    return;
  }
  var tableName = "BUILDINGS";
  var filter = sset.GetFilter(db.TableIndex("BUILDINGS"));
  var table = db.Table("BUILDINGS");
  var extent = table.QueryExtent(filter);

  table.StartQuery(filter);
  table.BeginWriteBatch();
  
  var read;
  var write = table.GetWriteRow();
  while (read = table.Next()) {
    var coords = file.ReadFile("e:/temp/AU 2016.csv");
    var coordsa = coords.split("\n");
    for (var i = 0; i < coordsa.length; i++) {
      var txta = coordsa[i].split(",");
      if (txta.length < 4) // Number of columns in the CSV file
        continue;
      // If the name of the building is euqal to the record
      // found in the CSV file then...
      if (read.NAME == (txta[0])) { // 0 is the first column - A
        write.USER_DATA = (txta[1]); // Owner - Description
        // (2nd column - B)
        write.DESCRIPTION = (txta[2]); // Update the description
        // (3rd column - C)
        write.ROOF_HEIGHT = (txta[3] * 3.6576); // Update the roof
        // height based on number of floors
        // (4th column - D)
        table.UpdateFeature(write, read.ID); // Update the model
        write.Invalidate();
      }
    }
  }
  table.CommitWriteBatch();
  table.EndQuery();
  app.InvalidateTileCache(db.TableIndex(tableName), extent);
  return true;
}

UpdateBldgData();

And works like this…

  • You must have (in this case) one or more buildings selected to update properties about the buildings
  • There must be a unique identifier for each building that corresponds to the values in the first column. In this case I’m using the building names as a way to check against the information in the CSV file.
  • If one of the selected building’s name is equal to one of the values in the building name column in the CSV file (cells A2 thru A6), then the code proceeds to update the following information
    • The building usage type (the USER DATA property in this example) from cells B2 thru B6
    • The owner (the DESCRIPTION property for this example) from cells C2 thru C6 and finally
    • The roof height (based on the number of floors in the CSV file) from cells D2 thru D6

If you have more data (I.E. more columns) you’ll need to adjust the code accordingly to either include or exclude certain data that pertains to your model.

To run the code, with the SCRIPTING dialog box open, select the buildings you want to update then click the START SCRIPT button. After a few seconds or less (depending on how much data you have to update) the model will be updated. My code took just 0.169 seconds to complete.

IW_JS_004

The final result looks something like this…

IW_JS_005

The highlighted row below is the data that was pulled from the CSV file for this particular building.

IW_JS_006

If you have more data that you’d like to populate your model with but you don’t have enough space in the properties palette well, there’s a solution for that – create a custom im.schema.json file.

This entry was posted in Infraworks. Bookmark the permalink.

4 Responses to Update Infraworks 360 data using JavaScript and a CSV file

  1. This is really Cool!
    It works perfect for me, when i am trying to update default properties.
    But i have created some custom properties (im.schema.json) for my coverage areas and pipelines.
    I am not able to update my custom properties?
    Is it because I am not allowed or am i doing something wrong?

    Like

    • Shruti Harve says:

      Hi Anders, Did you figure this out? I am getting a reference error? Did you get a error like that?
      [ERROR] line: 22 : ReferenceError: Can’t find variable: i

      [ERROR] ReferenceError: Can’t find variable: i
      Script evaluation: 0.043 seconds

      What could I be doing wrong!

      Thanks so much!

      Like

  2. Shruti Harve says:

    This is very cool indeed, but I am getting an error when trying to run the code-Please see below?
    [ERROR] line: 22 : ReferenceError: Can’t find variable: i

    [ERROR] ReferenceError: Can’t find variable: i
    Script evaluation: 0.043 seconds

    What could I be doing wrong? Thanks so much in advance for your help!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s