RTV Tools

Friday, November 20, 2015

Read XML Files and Use In Revit with XML Support added to Bakery Package in DynamoBIM

XML files are everywhere. And in the BIM world, we have to deal with a range of different xml file schemas, such as BCF, Navisworks Clash Reports and Viewpoints, and so forth. Hiding inside these XMLs there is some very useful information. For example, BCF files often have Element IDs in the viewpoint.bcfv component, and Navisworks XML files often have point XYZ values. Can we easily get access to this information for use in Dynamo, and then in Revit?

Yes, we can! There were one or two ways to do this in Dynamo before, but here is my take on it...

Dynamo ships with IronPython, which in turn ships with an XML handler called ElementTree. I have created some basic nodes that give us access to ElementTree functions in Dynamo. Along the way, I learnt a bit about encoding and character sets. It turns out that Navisworks often inserts tricky characters into the XML (like the diameter symbol), so as a workaround (for now) I do a string encoding roundtrip to get rid of these problematic characters. In the same node, I create the ElementTree object: this is a special object that essentially represents structured information about the XML data. The initial import looks like this:

Once we have this ElementTree object in hand, we can start to do some interesting things, like:
Iterate through tree to get individual XML elements

 and Show a hierarchical representation:

With the individual elements, we can Get Attribute names and values, and the Get the children of those elements:

Obviously, you can immediately do some nice lookups against these lists in Dynamo, depending what information you want. However, on large XMLs this can be quite slow. Happily, ElementTree provides some basic XPATH support, which looks a bit like this:

With the XPATH support and an understanding of the xml hierarchy, I have created a node to do XPATH calls straight to the ElementTree object:

Now that we can 'snip' out useful information from the XML, we can do interesting things with it, like make some points:

When it comes to BCF, its a little bit more challenging. I haven't figured out how to unpack the bcfzip directly to memory (yet), so we have do that manual step first. Once we have a 'folder' from the BCFZIP, we can get the bcfv files from inside it and then get information from them, like this:


So, in the latest Bakery package are the nodes needed to read a variety of XML files, get information from them, and do some useful things with that information. It was a learning experience for me, and I hope its useful to you :)





8 comments:

  1. Hi Luke,
    Excellent nodes!
    I tried to recreate your workflow but the xpath field is not working properly. I can't find the xyz coordinates.
    Do I need to change something in xml files exported from Navis?
    Thanks!

    ReplyDelete
    Replies
    1. I tested it yesterday and it did work. What XPATH string are you using?

      Delete
    2. I reinstalled dynamo and its working now. Thanks.
      Have you tried to run a big xml file from a clash detection?
      Dynamo keeps loading for hours without any outcome (apparently crashes).

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Great Work!!, have you try it on dynamo 9.1? im getting null values

    ReplyDelete
  4. Hi...I'm trying to get the element id and/or guid number from Navisworks's xml file. What will be my xpath?

    ReplyDelete
  5. Luke,

    Thank you for your quick response. I duplicated your graph but it's not working. I'm not able to attach an image here but the error the first "List.FirsItem" node was reporting was the following.."Warning: List.FirstItem opearion failed. Index was out of range. Must be non-negative and less than the size of the collection. Parameter name:index". Any ideas?

    ReplyDelete