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 my CSV file I have 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.

AKN_02

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

AKN_SCRIPTSa

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
    1. The building usage type (the USER DATA property in this example) from cells B2 thru B6
    2. The owner (the DESCRIPTION property for this example) from cells C2 thru C6 and finally
    3. 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.

IW_START_SCRIPT

The final result looks something like this…

IW_CSV_UPDATE.png

This entry was posted in Infraworks and tagged , , . Bookmark the permalink.

1 Response to Update Infraworks 360 data using JavaScript and a CSV file

  1. Pingback: Create Custom Properties in Infraworks 360 | The BVH Blog

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