By Daniel Du
This is the last part of this topic. In this post, I will introduce how I created the AutoCAD plugin to monitor the AutoCAD command usage and upload it to cloud.
firstly, I created an AutoCAD plugin project with the help of AutoCAD.net plugin wizard. AutoCAD related references are added automatically by the wizard.
To use my custom data model, I need to add referenced to my data model project, which is created in part 1. It is simple to monitor the AutoCAD command with Document Events in AutoCAD API, for example CommandEnded wraps the AcEditorReactor.commandEnded() ObjectARX function, it indicates that a command has complete. So firstly register the Document.CommandEnded event:
private Dictionary<string, int> _commandHitDic = new Dictionary<string, int>();
[CommandMethod("ACV_MonitorCommandEvents")]
public void MonitorCommandEvents_Method()
{
SubscribeToDoc(Application.DocumentManager.MdiActiveDocument);
GetEditor().WriteMessage("Magic ball is listening, keep working... ");
}
public void SubscribeToDoc(Document doc)
{
doc.CommandEnded += new CommandEventHandler(doc_CommandEnded);
}
void doc_CommandEnded(object sender, CommandEventArgs e)
{
string commandName = e.GlobalCommandName;
// filter out the custom commands of this application
if (commandName.StartsWith("ACV"))
{
return;
}
AddCommandHit(commandName);
}
private void AddCommandHit(string commandName)
{
if (_commandHitDic.Keys.Contains<string>(commandName))
{
_commandHitDic[commandName]++;
}
else
{
_commandHitDic.Add(commandName, 1);
}
}
By this code snippet, I am listening to the CommandEnded event and put the hit number of command into a a dictionary.
Next step is to upload the command statistics up to the RESTful service on Windows Azure from the dictionary. To send REST request, I am using a popular library RestSharp here, you may want to read Stephen’s post about usage of RestSharp here.
[CommandMethod("ACV_UpdateToCloud")]
public void UpdateToCloud()
{
UserCommandsHit usrCmdsHit = null;
RestClient client = new RestClient(BASE_URL);
client.AddHandler("application/json", new JsonDeserializer());
usrCmdsHit = GetUserCommadsHitByUserName(_userName, client);
//CommandHit record with this username is not found in cloud
//Add one record for this user.
if (usrCmdsHit == null)
{
//add user command hit record
usrCmdsHit = BuildNewUserCommandsHitFromDictionary(_userName,_commandHitDic);
//POST to add new
AddNewToCloud(usrCmdsHit, client);
}
else
{
//update the user command hit with dictionary
usrCmdsHit = UpdateUserCommandsHitFromDictionary(usrCmdsHit,_commandHitDic);
//PUT to update
UpdateToCloud(usrCmdsHit, client);
}
GetEditor().WriteMessage("\n Your command usage statastic has been updated to cloud succesfully.");
GetEditor().WriteMessage("\n Keep working or open http://acadcommandwebviewer.cloudapp.net/ with a modern broswerto view the magic ball ;) ");
GetEditor().WriteMessage("\n Chrome/Firefox are recommended. ");
System.Diagnostics.Process.Start("http://acadcommandwebviewer.cloudapp.net/");
}
private UserCommandsHit GetUserCommadsHitByUserName(
string userName,
RestClient client)
{
UserCommandsHit usrCmdsHit = null;
RestRequest reqGet = new RestRequest();
reqGet.Resource = "api/AcadCommands";
reqGet.Method = Method.GET;
reqGet.RequestFormat = DataFormat.Json;
reqGet.AddHeader("Accept", "Application/json");
reqGet.JsonSerializer = new RestSharp.Serializers.JsonSerializer();
reqGet.AddParameter("username", userName);
var respGet = client.Execute<UserCommandsHit>(reqGet);
if (respGet.StatusCode == System.Net.HttpStatusCode.OK)
{
if (respGet.Data != null)
{
usrCmdsHit = respGet.Data;
}
else
{
usrCmdsHit = Newtonsoft.Json.JsonConvert
.DeserializeObject<UserCommandsHit>(respGet.Content);
}
}
return usrCmdsHit;
}
private UserCommandsHit BuildNewUserCommandsHitFromDictionary(
string userName,
Dictionary<string, int> commandHitDic)
{
UserCommandsHit usrCmdsHit = new UserCommandsHit();
usrCmdsHit.UserName = userName;
List<CommandHit> list = new List<CommandHit>();
foreach (var cmdName in commandHitDic.Keys)
{
CommandHit ch = new CommandHit
{
CommandName = cmdName,
HitNumber = commandHitDic[cmdName]
};
list.Add(ch);
}
usrCmdsHit.CommandHits = list;
return usrCmdsHit;
}
private UserCommandsHit UpdateUserCommandsHitFromDictionary(
UserCommandsHit usrCmdsHit,
Dictionary<string,int> commandHitDic)
{
foreach (var cmdName in commandHitDic.Keys)
{
int count = usrCmdsHit.CommandHits
.Where<CommandHit>(p => p.CommandName == cmdName)
.Count<CommandHit>();
if (count == 0)
{
CommandHit ch = new CommandHit
{
CommandName = cmdName,
HitNumber = commandHitDic[cmdName]
};
usrCmdsHit.CommandHits.Add(ch);
}
else
{
CommandHit ch = usrCmdsHit.CommandHits
.First<CommandHit>(p => p.CommandName == cmdName);
ch.HitNumber += commandHitDic[cmdName];
}
}
return usrCmdsHit;
}
private void AddNewToCloud(UserCommandsHit usrCmdsHit, RestClient client)
{
RestRequest reqPost = new RestRequest();
reqPost.Resource = "api/AcadCommands";
reqPost.Method = Method.POST;
reqPost.RequestFormat = DataFormat.Json;
reqPost.AddHeader("Content-Type", "application/json");
reqPost.JsonSerializer = new RestSharp.Serializers.JsonSerializer();
reqPost.AddBody(usrCmdsHit);
var respPost = client.Execute<UserCommandsHit>(reqPost);
if (respPost.StatusCode == System.Net.HttpStatusCode.Created)
{
_commandHitDic.Clear();
GetEditor().WriteMessage("\nUpdate to cloud successfully.");
}
else
{
GetEditor().WriteMessage("\n Error:" + respPost.StatusCode);
GetEditor().WriteMessage("\n" + respPost.Content);
}
}
private void UpdateToCloud(UserCommandsHit usrCmdsHit, RestClient client)
{
RestRequest reqPut = new RestRequest();
reqPut.Resource = "api/AcadCommands/" + usrCmdsHit.Id ;
reqPut.Method = Method.PUT;
reqPut.RequestFormat = DataFormat.Json;
reqPut.AddHeader("Content-Type", "application/json");
reqPut.JsonSerializer = new JsonSerializer();
reqPut.AddBody(usrCmdsHit);
var respPut = client.Execute<UserCommandsHit>(reqPut);
if (respPut.StatusCode == System.Net.HttpStatusCode.OK)
{
_commandHitDic.Clear();
GetEditor().WriteMessage("\nUpdate to cloud successfully.");
}
else
{
GetEditor().WriteMessage("\n Error:" + respPut.StatusCode);
GetEditor().WriteMessage("\n" + respPut.Content);
}
}
Okay, with that I am wrapping up this post, hopefully you got the idea what I did, and hope it is helpful for you.
I'd like to try out a working version. Is this something you'd share?
Posted by: EyesonElysium | 04/13/2016 at 08:46 PM