Importing json into Revit using the API:

Importing json into Revit using the API:

J450NP13
Advisor Advisor
10,737 Views
22 Replies
Message 1 of 23

Importing json into Revit using the API:

J450NP13
Advisor
Advisor

So I have the CustomExporterAdnMeshJson solution.  I got it working in 2021.

 

Is there a way to import json the same way?  if so is there an example.

 

 

 

 

10,738 Views
22 Replies
Replies (22)
Message 2 of 23

J450NP13
Advisor
Advisor

I found the newer examples.  The CreateTrianglesTopography example shows a json getting imported.  So this is awesome.

 

But, I notice in that json there is an object called "Facets"  I am assuming these are faces.  But why is there 3 values in square brackets?

 

"Facets":[[0,25,3],[0,20,25],[0,11,20],[0,1,11],[1,9,11],[1,8,9],[2,18,56],[2,6,18],[4,3,24],[3,23,24],[25,23,3],[4,24,7],[5,18,6],

 

Can I format the json like the below example I used in Unity?  I am not familiar with the three values in brackets.

 

"StoredTris":[14415,14414,14413,14412,14411,14410,14409,14408,14407,14406,14405,14404,14403,14402,14401,14400,14399

 

0 Likes
Message 3 of 23

jeremy_tammik
Alumni
Alumni

The facets refer to triangles. Each triangle has three vertices. Each vertex is represented by an index into a list of vertex coordinates.  In the first example, each triangle is bracketed into its own individual sublist. In the second, apparently not. In both cases, you can check and confirm whether this assessment it true by examining the source code yourself. Don't trust me and my guesswork, for heaven's sake.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 4 of 23

J450NP13
Advisor
Advisor

@jeremy_tammik 

 

Does the json have to be formatted like that with the brackets?  I see that the json is generated....I want to use my own json.  Is this for any type of model geometry or just topo?  Can I stop the generation of this topo json and make it use a json to import into Revit?

 

The second example is my json out of unity.  

 

Will Revit read any json file that is in the same directory as the DLL's for the Sample?  I'd like to do an import dialog.

0 Likes
Message 5 of 23

jeremy_tammik
Alumni
Alumni

The add-in is a source code sample. You can reprogram and adapt it to fit your needs in any way you like.

 

Revit itself does not read the JSON and has no interest in its formatting in any way whatsoever.

 

The add-in does all that. If you prefer to read JSON formatted in the Unity convention, go ahead and do so.

 

The same applies to the location of the JSON file. It can be placed wherever you like. You just need to adapt the add-in code to pick it up from your desired location.

 

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 6 of 23

J450NP13
Advisor
Advisor

@jeremy_tammik 

 

Thank you.

 

Do you have any Suggestions on how to get started?  

0 Likes
Message 7 of 23

jeremy_tammik
Alumni
Alumni

Fork the Github repository, clone it to your development environment, and adapt it to your needs.

 

If your enhancements are generic and universally useful, please also submit a pull request for integration into the main branch.

 

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 8 of 23

J450NP13
Advisor
Advisor

@jeremy_tammik 

 

I am getting an error about the mxjsonlength being exceeded in Revit.  How can I give it a max value?  Since there's not a web.config file?

 

Anyone else experience this?

0 Likes
Message 9 of 23

jeremy_tammik
Alumni
Alumni

I have never heard of mxjsonlength, and I very much doubt that Revit has ever heard of it either.

 

What component is causing this error? Which line of code? Maybe this "error" is an exception? Maybe it is thrown by a library that is referenced by your add-in? Maybe you can discover more details and relevant information by debugging your add-in?

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 10 of 23

J450NP13
Advisor
Advisor

@jeremy_tammik 

 

I'm getting it in Revit.  Review the image I attached.  its maxjsonlength,  Sorry not mx.

 

I've been trying to debug it, but Revit doesn't give me a line.  And nothing is getting thrown in VS when built.

0 Likes
Message 11 of 23

jeremy_tammik
Alumni
Alumni

Wow. Well, I am surprised, and you have me totally stumped. I have no idea how you are achieving this.

 

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 12 of 23

J450NP13
Advisor
Advisor

All I am doing is trying to read my json file and not the one provided in the example.

0 Likes
Message 13 of 23

jeremy_tammik
Alumni
Alumni

And how exactly are you trying to read it? What add-in are you using? What code?

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 14 of 23

J450NP13
Advisor
Advisor

@jeremy_tammik 

 

I am using the Revit SDK samples, specifically the "CreateTrianglesTopography" example.

 

I replaced the json file data with my own json data and formatted the same way besides bracketing the 3 facets.

 

 

0 Likes
Message 15 of 23

RPTHOMAS108
Mentor
Mentor

JavaScriptSerializer.MaxJsonLength Property (System.Web.Script.Serialization) | Microsoft Docs

 

Being an Integer it can be any number from 0 to 2147483647 I suppose (Int32.Max).

 

The default is 2097152.

0 Likes
Message 16 of 23

J450NP13
Advisor
Advisor

@RPTHOMAS108 

 

I tried giving it that max and it throws an error saying:

 

Cannot convert object of type 'System.Int32' to type 'System.Collections.Generic.IList`1[System.Int32]'

 

0 Likes
Message 17 of 23

RPTHOMAS108
Mentor
Mentor

You did this:

serializer.MaxJsonLength = Int32.MaxValue;

Crazy in a way you should need to process a string that large, probably get the actual length (the file has a content with a number of characters). The variable likely exists to set a memory buffer if so should be considered carefully in terms of performance.

 

By doing the above you probably solved one problem and moved onto the next (that isn't clear in your response). I'm guessing it is expecting:

public IList<IList<int>> Facets { get; set; }

not

public IList<int> Facets { get; set; }

 

As is the code and the brackets are significant in this respect:

[1,2,3,4] List(of Integer)

[[1,2,3],[1,2,3],[1,2,3]] List(of List(of Integer)) perhaps?

 

 

0 Likes
Message 18 of 23

J450NP13
Advisor
Advisor

Yeah I wondering if I needed to have the facets in the brackets.  Not sure how I can do that out of unity.  

I have a solid json serialization system in unity but the triangles are just as you said, a list of ints.

 

It won’t even read a json file exported from Revit using the json exporter example.

 

0 Likes
Message 19 of 23

RPTHOMAS108
Mentor
Mentor

I think that is a relatively easy problem to solve since they are groups of three either way.

 

You just need to match for what serializer is expecting and convert to other form later:

 

 

 Dim Points As New List(Of List(Of Integer)) 'The new list into Revit topo
        Dim InputPoints As List(Of Integer) 'Flat list
        Dim J As Integer = 0
        Dim CurSet As New List(Of Integer)
        For i = 0 To InputPoints.Count - 1
            CurSet.Add(InputPoints(i))
            If J = 2 Then
                J = 0
                Points.Add(CurSet)
                CurSet = New List(Of Integer)
            Else
                J += 1
            End If
        Next

 

 

You can also do the opposite of this above and in many different ways (that is just the first way that comes to mind).

 

If unity is fixed output then change what serializer is expecting on Revit side Json is same format or it is Jim not Json.

0 Likes
Message 20 of 23

mastjaso
Advocate
Advocate

In all honesty I often run into issues with .NET's built in JSON serializer. I don't actually know if it will help in this case, but often the Newtonsoft.Json package on NuGet will work more reliably / quickly / intuitively.