使用Python实现ArcGIS中的自动刷新

2024-10-16 17:21:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为ArcMap创建一个“自动刷新”工具,以刷新数据帧。我相信版本10有一个附加组件,你可以为此下载。。不过,我们现在运行的是10.1版本,没有这样的工具。在

编辑wxPython的计时器应该可以工作,但是在arc中使用wx是很棘手的。下面是当前的代码:

import arcpy
import pythonaddins
import os
import sys
sMyPath = os.path.dirname(__file__)
sys.path.insert(0, sMyPath)

WATCHER = None

class WxExtensionClass(object):
    """Implementation for Refresher_addin.extension (Extension)"""
    _wxApp = None
    def __init__(self):
        # For performance considerations, please remove all unused methods in this class.
        self.enabled = True
    def startup(self):
        from wx import PySimpleApp
        self._wxApp = PySimpleApp()
        self._wxApp.MainLoop()
        global WATCHER
        WATCHER = watcherDialog()


class RefreshButton(object):
    """Implementation for Refresher_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        if not WATCHER.timer.IsRunning():
            WATCHER.timer.Start(5000)
        else:
            WATCHER.timer.Stop()

class watcherDialog(wx.Frame):
    '''Frame subclass, just used as a timer event.'''
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "timer_event")
        #set up timer
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.onTimer, self.timer)

    def onTimer(self, event):
        localtime = time.asctime( time.localtime(time.time()) )
        print "Refresh at :", localtime
        arcpy.RefreshActiveView()

    app = wx.App(False)

你会注意到里面有PySimpleApp的东西。我从塞德霍姆的演讲中得到的。我想知道我是否误解了什么。我应该为扩展创建一个完全独立的加载项吗?然后,用我需要的代码创建我的工具栏/工具栏加载项?我这么问是因为我没有看到下面代码中引用的PySimpleApp,或者在startup override方法中没有从wx导入任何内容。。。我认为这是必要的/这一切的重点。我非常感谢你的帮助。请告诉我你在我的代码中看到了什么。在


Tags: 代码importselfnoneeventtimeinitdef
2条回答

您可以使用RefreshTOCRefreshActiveView方法。只需添加一个timer

您不能以您正在尝试的方式执行此操作,因为time.sleep将阻止并锁定整个应用程序。ArcGIS中的Python插件是非常新的东西,还有很多功能还没有实现。其中之一是某种更新或计时器事件,就像在.NET和ArcObjects中遇到的一样。你可以考虑用线程。线程以及线程。事件在这样的情况下,但是与线程无关的内容在Python加载项环境中不起作用。至少我不能让它工作。所以我在这种情况下所做的就是使用wxPython和Timer类。如果加载项设置正确,下面的代码将有效。在

import time
import os, sys
import wx
import arcpy

mp = os.path.dirname(__file__)
sys.path.append(mp)

WATCHER = None

class LibLoader1(object):
    """Extension Implementation"""
    def __init__(self):
        self.enabled = True

    def startup(self):
        global WATCHER
        WATCHER = watcherDialog()

class ButtonClass5(object):
    """Button Implementation"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        if not WATCHER.timer.IsRunning():
            WATCHER.timer.Start(5000)
        else:
            WATCHER.timer.Stop()

class watcherDialog(wx.Frame):
    '''Frame subclass, just used as a timer event.'''
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "timer_event")
        #set up timer
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.onTimer, self.timer)

    def onTimer(self, event):
        localtime = time.asctime( time.localtime(time.time()) )
        print "Refresh at :", localtime
        arcpy.RefreshActiveView()

    app = wx.App(False)

制作一个扩展插件,包含一个工具栏和一个button类。如上所示,重写扩展的startup方法。它将创建一个带有计时器的Frame子类的实例。然后,无论何时单击工具栏上的按钮,计时器都将打开或关闭。计时器参数以毫秒为单位,因此显示的代码将每5秒刷新一次。在

您可以阅读有关在addinshere中使用wxPython的更多信息。特别要注意MCederholm的帖子,比如说打印声明不起作用。在

编辑

代码使用addin扩展类的startup方法重写。这个方法应该在Arcmap启动时运行,但是从您的注释来看,这个启动方法在启动时无法运行。如果你不能正确地创建你的外接程序,这是可能的,但是在我的测试中,它对我很有效。如果继续获取“AttributeError:'NoneType'object没有属性'timer'”,请更改按钮类的onClick方法,如下所示:

^{pr2}$

前3行检查以确保WATCHER变量已设置为watcherDialog的实例,并且没有仍然设置为None。不知道为什么你的启动方法没有运行,但希望这能帮你解决问题。在

相关问题 更多 >