JSON to DWG

JSON to DWG

fieldguy
Advisor Advisor
19,304 Views
2 Replies
Message 1 of 3

JSON to DWG

fieldguy
Advisor
Advisor

Cross posted in Map 3D Developer 

I have some spatial databases and I am able to connect in visual studio with the SQLClient and read point coordinates and create autocad entities.

I also have access to arcgis/rest/services and would like to create query urls and parse the JSON results to create autocad entities.

Can anyone provide an example, or documentation to get me started?  A google search returns several DWG to JSON posts, but I want to go the other way.

I have access to FME but that is a last resort.

0 Likes
Accepted solutions (1)
19,305 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor

Based on your short description, "JSON to DWG" would mean that your app (Acad addin) calls a servcies that returns data (likely, geometry data) that can be used to buld AutoCAD entities, such as line, polylines, arcs. circles.... most likely closed polylines, since you get data from ARCGIS services. The data from the services would be in Jason format.

So, what you'll do something like:


1. Use either WebClient (older technology), or HttpClient (latest technology) to call the services to get the data in Json format;
2. Then use one of the available Json utility (Json.NET from NewtonSoft is most popular to be used in client side/desktop app) to desearize the json string into a a custom class, which you create according to the Json data structure.
3. Once you have all data in your own custom data model, do whatever you want to create Acad entities.

 

You can search HpptClient on the net for some code example.

 

For example, assune a service call returns Json data, representing a circle, like this:
{
'Radius': 20.0,
'CenterX': 1.0,
'CenterY': 1.0,
'CenterZ': 0.0
}

Then you can create a custom class like this

public class GisCircle
{
   public double Radius{set;get;}
   public double CenterX {set;get;}
   public double CenterY {set;get;}
   public double CenterZ {set;get;}
   
   public Point3d Center { get {return new Point3d(CenterX, CenterY,CenterZ);}}
}

 In the data access code, you use HttpClient to access the services to get data, like this:

public async Task<GisCircle> GetCircle(string queryUrl)
{
    GisCircle circle=null;

    var client =  new HttpClient();
    client.BaseAddress=new Uri("https://....../");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    var response = await client.GetAsync(queryUrl);
    if (response.IsSuccessStateCode)
    (
        circle=await response.Content.ReadAsAsync<GisCircle>();
    )

    return circle;
}

Obviously the example is extremely simplified. The Json data from the ARCGIS services could be quite complicated. But the process would be the same, or very similar. And you very likely need to use Json.NET library to parse the json string into more complicated custom data classes/models.

I assume that once you are able to translate the Json data into your own data structure/model, you should be able to use it in whatever way in the process of ccreating corresponding Acad entities.

So, you would focus on how to call the service with HttpClient and translate the Json data into your own data model. As long as you correctly collect the required geometry information from the returned json data, it would not a an issue to create Acad entities.

 

At my work, most our CAD applications need to have access to backend enterprise database for varieties of data. In the past, the access is direct connection to the database (using ADO.NET, SQL Server). Now the company is a huge global one, so all the database accesses have been through backend services (REST services, all data returned in Json format). The process of accessing data from backend services and then do something in our Acad add-in apps would not be much different from what you want to do.

 

Hope this helps a bit.

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 3

fieldguy
Advisor
Advisor
Accepted solution

I used the short description because getting the JSON data from the REST service is pretty easy.  The JSON data in this case is large - I would recommend using a small data set if possible.

I struggled with the data somewhat before choosing XML - IMO it's easier to see / navigate the data structure.

I ended up using WebClient (System.Net),  JsonReaderWriterFactory.CreateJsonReader from System.Runtime.Serialization.Json, System.Xml, and System.Xml.Linq.

There are several examples on the web.

Thanks again!

When I am finished I will change my requests to get the response in XML format - that should save a few steps.

0 Likes