Wednesday 29 April 2015

python - ArcGIS 10.1 Custom Toolbar issue with addin wizard


I am having some issues. In ArcGIS, I created a custom toolbar (with the python addin wizard) to do the following: 1. Button to connect to feature class, 2-5. A series of comboboxes to view, edit data in a field in the feature class. I want each of the combo box classes to directly connect to the field. And be able to write new data from the boxes into the fields.


Example: Combobox 1 to connect to field 1. So that when I deploy this a user can edit data without doing this the traditional way. Its a way to have inexperienced users edit data. I am new to python and willing to pay for the time required to help me as I am out of ideas. And ESRI doesn't have a solution either.


import arcpy
import pythonaddins
from arcpy import env


class ButtonClass1(object):
"""Implementation for QuickTableEdit_addin.button (Button)"""

def __init__(self):
self.enabled = True
self.checked = False
def onClick(self):
print("Button1 Clicked")
env.workspace = "C:/data"
arcpy.CreateTable_management("C:/data", "test_table.dbf","template.dbf")
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
dbf_Table = arcpy.mapping.TableView(r"C:\data\test_table.dbf")

arcpy.mapping.AddTableView(df, dbf_Table)
arcpy.RefreshTOC()
pass

class ComboBoxClass2(object):
"""Implementation for QuickTableEdit_addin.combobox_1 (ComboBox)"""
def __init__(self):
self.items = [1,2]
self.editable = True
self.enabled = True

self.dropdownWidth = 'WWWWWW'
self.width = 'WWWWWW'
def onSelChange(self, selection):
global cb1
cb1 = self.value
pass
def onEditChange(self, text):
pass
def onFocus(self, focused):
pass

def onEnter(self):
pass
def refresh(self):
pass

Answer



Yeah, ESRI documentation is poor regarding how each function works. It took me a while to figure most of it out. Just trial and error, unfortunately.


The onSelChange function grabs the selection from self.items. onEditChange creates a text variable with whatever is typed into the combobox. So, everytime you type something in, it changes. So if you type in "NEW", the text variable will be "N", "NE", and then "NEW", as it is typed. The onFocus function does something whenever the combobox is selected, or has focus. In your case you would probably need to read from the field and populate self.items list with whatever items are in the field. The onEnter function does something whenever the ENTER button is hit, your case would probably be an update cursor.


Let me know if this helps, or if you need more clarification.


EDIT: I posted some of my code below. Hope it helps.


class CallTypeComboBoxClass(object):

"""Implementation for Custom_Tools_addin.combobox_3 (ComboBox)"""
def __init__(self):
self.items = []
self.editable = True
self.enabled = False
self.dropdownWidth = 'WWWWWWWWWWWWWW'
self.width = 'WWWWWWW'
def onSelChange(self, selection):
global typ_selection
typ_selection = selection

def onEditChange(self, text):
global new_call
new_call = text
def onFocus(self, focused):
if focused:
self.items = []
# Add all items from 'CALL_TYPE' field to combo box
with arcpy.da.SearchCursor(call_type_table, ("CALL_TYPE")) as cursor:
for row in cursor:
self.items.append(row[0])

def onEnter(self):
# if text is not already in combobox and has length greater than 0
if new_call not in self.items and len(new_call) > 0:
cursor = arcpy.da.InsertCursor(call_type_table, ("CALL_TYPE"))
cursor.insertRow((new_call,))
del cursor
def refresh(self):
pass

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...