Example #1
0
        private static Dictionary <AddinManagerAssembly, List <ICADExCommand> > DeserializeAssemblies(
            AssemblyInfos amInfos)
        {
            var nodesInfo = new Dictionary <AddinManagerAssembly, List <ICADExCommand> >(new AssemblyComparer());

            //
            if (amInfos != null)
            {
                foreach (string assemblyPath in amInfos.AssemblyPaths)
                {
                    if (File.Exists(assemblyPath))
                    {
                        // 将每一个程序集中的外部命令提取出来
                        List <ICADExCommand> m = ExCommandFinder.RetriveExternalCommandsFromAssembly(assemblyPath);
                        if (m.Any())
                        {
                            Assembly             ass        = m[0].GetType().Assembly;
                            AddinManagerAssembly amAssembly = new AddinManagerAssembly(assemblyPath, ass);
                            nodesInfo.Add(amAssembly, m);
                        }
                    }
                }
            }
            return(nodesInfo);
        }
Example #2
0
        private void buttonLoad_Click(object sender, EventArgs e)
        {
            string[] dllPaths          = ChooseOpenDll("Choose an Addin file");
            bool     hasNewMethodAdded = false;

            //
            if (dllPaths != null)
            {
                foreach (string dllPath in dllPaths)
                {
                    if (string.IsNullOrEmpty(dllPath))
                    {
                        continue;
                    }
                    //
                    var methods = ExCommandFinder.RetriveExternalCommandsFromAssembly(dllPath);
                    if (methods.Any())
                    {
                        // 更新 Dictionary
                        AddMethodsInOneAssembly(dllPath, methods);
                        hasNewMethodAdded = true;
                    }
                }
            }

            if (hasNewMethodAdded)
            {
                // 刷新界面
                RefreshTreeView(_nodesInfo);
            }
        }
Example #3
0
        private void button_Reload_Click(object sender, EventArgs e)
        {
            TreeNode nd    = treeView1.SelectedNode;
            TreeNode ndAss = null;

            if (nd == null)
            {
                return;
            }
            //
            if (nd.Level == 0) // 移除程序集
            {
                ndAss = nd;
            }
            else if (nd.Level == 1)// 移除某个方法所对应的程序集
            {
                ndAss = nd.Parent;
            }
            AddinManagerAssembly mtd = ndAss.Tag as AddinManagerAssembly;
            string assFullPath       = mtd.Path;

            // 重新加载此程序集
            if (!string.IsNullOrEmpty(assFullPath))
            {
                bool hasNewMethodAdded = false;
                //
                var methods = ExCommandFinder.RetriveExternalCommandsFromAssembly(assFullPath);
                if (methods.Any())
                {
                    // 更新 Dictionary
                    AddMethodsInOneAssembly(assFullPath, methods);
                    hasNewMethodAdded = true;
                }

                if (hasNewMethodAdded)
                {
                    // 刷新界面
                    RefreshTreeView(_nodesInfo);
                }
            }
        }
Example #4
0
        private static ExternalCommandResult Execute(object exCommandIns, SelectionSet impliedSelection, ref string errorMessage, ref IList <ObjectId> errorSet)
        {
            eZcad.DocumentModifier.LineFeedInCommandLine();

            ExternalCommandResult res = ExternalCommandResult.Failed;

            try
            {
                var mtd = ExCommandFinder.FindExCommandMethod(exCommandIns.GetType());
                if (mtd != null)
                {
                    // 先将焦点交给 AutoCAD 主界面
                    Application.MainWindow.Focus();
                    // ---------------------------- 执行命令 ----------------------------------------
                    object[] paras = new object[] { impliedSelection, errorMessage, errorSet };
                    res = (ExternalCommandResult)mtd.Invoke(exCommandIns, paras);
                    // 将 ref 参数的值提取出来
                    errorMessage = paras[1] as string;
                    errorSet     = paras[2] as IList <ObjectId>;
                }

                // 执行操作
                // 如果在执行 Execute()方法时发现某个程序集不存在,则通过AssemblyResolve 事件手动进行加载
                // 所以,所有引用的 zengfy 自定义程序集,都必须在  Execute() 方法中调用至少一次,以解决在Form.Show()时,出现不能找到或加载前面缺失的程序集B的问题。

                // 如果不想通过 AssemblyResolve 来加载缺失的程序集的话,可以在 AddinManager 中自行设计代码,手动在 Execute() 方法之前将要引用的程序集从临时文件夹中通过 Assembly.LoadFile() 进行加载即可。
                // var exCommand = exCommandIns as ICADExCommand;
                // res = exCommand.Execute(impliedSelection, errorMessage: ref errorMessage, elementSet: ref errorSet);
            }
            catch (Exception ex)
            {
                if (string.IsNullOrEmpty(errorMessage))
                {
                    errorMessage = GetDebugMessage(ex); // ex.Message;
                }
                else
                {
                    errorMessage = errorMessage + "\n\r--------------------------------------------\n\r"
                                   + GetDebugMessage(ex); // ex.Message;
                }
                res = ExternalCommandResult.Failed;
            }

            // 对命令的结果进行处理
            switch (res)
            {
            case ExternalCommandResult.Failed:
            {
                // 选择出错的单元格
                if (errorSet != null)
                {
                    StringBuilder errorIds = new StringBuilder();
                    foreach (var id in errorSet)
                    {
                        errorIds.AppendLine(id.ObjectClass.Name);
                    }
                    errorMessage += "\r\n出错对象:\r\n " + errorIds.ToString();
                }
                // MessageBox.Show(errorMessage, @"外部命令执行出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                break;
            }

            case ExternalCommandResult.Cancelled:
            {
                // 由于没有在CAD中没有事务或者回滚,所以直接结束就可以了。
                break;
            }

            case ExternalCommandResult.Succeeded:
            {
                break;
            }
            }
            return(res);
        }