AutoCAD 命令统计魔幻球的实现过程--(4)

简介:


这是这个系列的最后一篇,制作一个AutoCAD插件来获取AutoCAD的命令使用情况,并上传到云端服务。首先我使用AutoCAD.net plugin wizard创建一个AutoCAD 插件,这个向导会帮助我添加一些AutoCAD相关的引用。另外因为我要用到前面自定义的数据模型,在之前为了在这个项目中重用,我已经把他独立成一个单独的项目,现在在这个AutoCAD插件项目中添加到这个数据模型项目的引用。

 

本文连接:http://www.cnblogs.com/junqilian/archive/2013/03/25/2980097.html

 

在AutoCAD中获取命令的使用情况可以使用AutoCAD的一个非常简单的API,即监听Document 的CommandEnded事件。看下面的代码,首先定义了一个字典用于存储命令使用统计信息,然后定义了一个AutoCAD自定义命令,在这个命令中捕捉Document的Ended事件。在Document——Ended事件中,可以通过e.GobalCommandName取到执行成功的AutoCAD命令名字,添加到字典中。

 

private Dictionary<stringint> _commandHitDic = new Dictionary<stringint>();        
[
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);
            }
        }

 

下面就是把字典中的统计信息上传到云端。前面的文章中已经结束了云端REST服务的实现,AutoCAD插件作为客户端只要发送REST请求即可。这里我使用了一个比较流行的类库RestSharp,废话不多说了,看代码:

        [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<stringint
> 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);
            }
        }

 
 
 
 

 

好了,整个过程也不复杂,总的来说就是通过REST和云端通信,把AutoCAD本地的信息上传到云端,进而可以在其他终端(浏览器,甚至手机)来做处理。这只是个小例子,也许你会有更实用的想法,不妨动手试试吧。

作者: 峻祁连
邮箱:junqilian@163.com 
出处: http://junqilian.cnblogs.com 
转载请保留此信息。



本文转自峻祁连. Moving to Cloud/Mobile博客园博客,原文链接:http://www.cnblogs.com/junqilian/archive/2013/03/25/2980097.html ,如需转载请自行联系原作者
目录
打赏
0
0
0
0
23
分享
相关文章
图像去雨-雨线清除-图像处理-(计算机作业附代码)
图像去雨-雨线清除-图像处理-(计算机作业附代码)
用Python print画一条龙,有眼睛,会动,彩色的,还会喷火那种
古有 div画条, console画龙。 今有我 Python print 画 战龙, 一条目光凶猛,霸气红色,爱运动,能战斗的霸王龙。 上面的都是产品说的,我是研发, 所以,大家懂的,从产品到设计, 从设计到实现, 每一步都是有差距的。
1905 0
用Python print画一条龙,有眼睛,会动,彩色的,还会喷火那种
HMI-33-【运动模式】补上油量表和水温表
上一篇,以为是做了一个收尾,写了灯光控制面板和底部的信息栏,但是,有位眼见的小伙伴`江山壹角`,直接不给我面子,说我的水温表和油量表不会动。截图位置,我记仇哈。
巧用千寻位置GNSS软件| 直线放样有技巧
日常测量作业中,直线放样是对设计好的直线进行放样,其中包括直线的里程,左右偏距及设计直线范围内的高程控制。本文将介绍如何运用千寻位置GNSS软件完成日常的直线放样。
C语言——函数的综合运用。自定义函数,gotoxy清屏函数与HideCursor隐藏光标,防闪屏,共同制作打飞机游戏。
①在变量中,我们必须进行定义赋值初始化后,才能在程序中使用,所以需要一个“地方”,整理好这些变量,在程序中整洁一些void startup() //数据初始化②打出画面中所显示的所需代码,也给一个函数void show() //显示画面③在游戏运行中,飞机的移动,发射等操作必须使用键盘,此时就要进行输入判断,给出相应的函数,实行不同的命令 ,其中分为两种,一种是程序运行时对输入做出变化,另一种是不变化void updateWithoutInput() //与用户输入无关的更新。
jmeter如何进行一个简单的测试(超级详细,有图有文字,闭着眼都能成功)
jmeter如何进行一个简单的测试(超级详细,有图有文字,闭着眼都能成功)
155 0
jmeter如何进行一个简单的测试(超级详细,有图有文字,闭着眼都能成功)
Java锤子剪刀布大家应该都会玩“锤子剪刀布”的游戏: 现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。
Java锤子剪刀布大家应该都会玩“锤子剪刀布”的游戏: 现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。
176 0
Halcon标定系列(3):我个人总结的“眼在手外“和“眼在手上”的心得笔记
Halcon标定系列(3):我个人总结的“眼在手外“和“眼在手上”的心得笔记
3115 0
Halcon标定系列(3):我个人总结的“眼在手外“和“眼在手上”的心得笔记
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等