wjklxywjy 发表于 2019-12-27 23:13:15

C#中如何实现先选择的pickfirst选择集按下ESC键清除选择集并且触发对Combobox

C#中如何实现先选择的pickfirst选择集按下ESC键清除选择集并且触发对Combobox控制内容列表清空,如下图所示:

雪山飞狐_lzh 发表于 2019-12-28 01:35:58

在Pycad里试了下,下面的代码是可行的
_esc = False
_num = 0

def pickfirst_added(ed, e):
    if e.Flags == aced.SelectionFlags.PickfirstSet:
      global _esc, _num
      if _esc:
            _esc = False
            _num -= (e.Selection.Count + e.AddedObjects.Count)
      else:
            _num = e.Selection.Count + e.AddedObjects.Count
      ed.WriteMessage("\n%s" % _num)

def pickfirst_removed(ed, e):
    if e.Flags == aced.SelectionFlags.PickfirstSet:
      global _num
      _num = e.Selection.Count - e.RemovedObjects.Count
      ed.WriteMessage("\n%s" % _num)

def on_message(sender, e):
    m = e.Message
    if (m.message == 0x0100) and (int(m.wParam) & 65535 == 27):
      global _esc
      _esc = True

@command()
def showpickfirstinfo(doc):
    ed = doc.Editor#type: aced.Editor
    ed.SelectionAdded += pickfirst_added
    ed.SelectionRemoved += pickfirst_removed
    acap.Core.Application.PreTranslateMessage += on_message

@command()
def unshowpickfirstinfo(doc):
    ed = doc.Editor#type: aced.Editor
    ed.SelectionAdded -= pickfirst_added
    ed.SelectionRemoved -= pickfirst_removed
    acap.Core.Application.PreTranslateMessage -= on_message

雪山飞狐_lzh 发表于 2019-12-28 12:52:26

本帖最后由 雪山飞狐_lzh 于 2019-12-28 22:00 编辑

好吧 感觉我想多了。。。ImpliedSelectionChanged事件就可以 奇怪我昨天这样写怎么总不行?
def pickfirst_changed(sender, e):
    #事件的触发者即当前文档
    ed = sender.Editor#type: aced.Editor
    #获取PickFirst选择集
    res = ed.SelectImplied()
    if res.Status == aced.PromptStatus.OK:
      ed.WriteMessage(str(res.Value.Count))
    else:
      ed.WriteMessage("0")

def doc_created(sender, e):
    #新文档创建后,绑定ImpliedSelectionChanged事件
    e.Document.ImpliedSelectionChanged += pickfirst_changed

@command()
def showpickfirstinfo(doc):
    #获取文档集合
    docs = acap.Application.DocumentManager
    #绑定DocumentCreated事件
    docs.DocumentCreated += doc_created
    for d in docs:
      #遍历已有文档,绑定ImpliedSelectionChanged事件
      d.ImpliedSelectionChanged += pickfirst_changed

@command()
def unshowpickfirstinfo(doc):
    docs = acap.Application.DocumentManager
    docs.DocumentCreated -= doc_created
    for d in docs:
      d.ImpliedSelectionChanged -= pickfirst_changed

wjklxywjy 发表于 2019-12-28 21:09:12

用C#来写,这个不太明白

雪山飞狐_lzh 发表于 2019-12-28 22:01:08

简单注释了一下。。。话说我觉得pycad的代码和C#相差不大
页: [1]
查看完整版本: C#中如何实现先选择的pickfirst选择集按下ESC键清除选择集并且触发对Combobox