1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.Windows; using Autodesk.AutoCAD.Geometry; namespace AcadDemo { public class RibbonDemo { Autodesk.Windows.RibbonTab _ctxTab = null; [CommandMethod("CtxTabUponSelect")] public void CtxTabUponSelect() { Document doc = Application.DocumentManager.MdiActiveDocument;
doc.ImpliedSelectionChanged +=new EventHandler(doc_ImpliedSelectionChanged); }
void doc_ImpliedSelectionChanged(object sender, EventArgs e) { Document doc = Application.DocumentManager.MdiActiveDocument;
PromptSelectionResult psr = doc.Editor.SelectImplied();
if (psr.Value == null) { HideTab(); return; }
foreach (SelectedObject selObj in psr.Value) { if (selObj.ObjectId.ObjectClass.DxfName.ToLower()!= "circle") { HideTab(); return; } } if (_ctxTab == null || !_ctxTab.IsVisible) { Autodesk.AutoCAD.ApplicationServices.Application.Idle +=new EventHandler(OnIdle); } }
void OnIdle(object sender, EventArgs e) {
if (Autodesk.Windows.ComponentManager.Ribbon != null) {
if (_ctxTab == null) CreateCtxTab();
if (!_ctxTab.IsVisible) { Autodesk.Windows.RibbonControl ribbonCtrl =Autodesk.Windows.ComponentManager.Ribbon;
ribbonCtrl.ShowContextualTab(_ctxTab, false, true);
_ctxTab.IsActive = true; }
if (!_ctxTab.IsActive) _ctxTab.IsActive = true; } }
void CreateCtxTab() { Autodesk.Windows.RibbonControl ribbonCtrl =Autodesk.Windows.ComponentManager.Ribbon;
_ctxTab = new Autodesk.Windows.RibbonTab();
_ctxTab.Name = "MyTab";
_ctxTab.Id = "MY_CTX_TAB_ID";
_ctxTab.IsVisible = true;
_ctxTab.Title = _ctxTab.Name;
_ctxTab.IsContextualTab = true; ribbonCtrl.Tabs.Add(_ctxTab);
}
void HideTab() { Autodesk.AutoCAD.ApplicationServices.Application.Idle-= new EventHandler(OnIdle); if (_ctxTab != null) { Autodesk.Windows.RibbonControl ribbonCtrl=Autodesk.Windows.ComponentManager.Ribbon;
ribbonCtrl.HideContextualTab(_ctxTab);
_ctxTab.IsVisible = false;
_ctxTab = null;
} } } }
|