1

I have a tool I am converting to an ArcMap add-in. The problem is that the tool created a report and now the add-in report only contains the header row. I can't get the complete message box to fire at the end of the script either so I think the xls is not being saved.

class button2(object):
    import xlwt
    from xlwt import easyxf
    import os
    """Implementation for SecondTime_addin.button2 (Button)"""
    def __init__(self):
        self.enabled = False
        self.checked = False
    def onClick(self):
        global selectedLayer
        global selectedLayerPath
        self.mxd=arcpy.mapping.MapDocument('current')
        red_with_white_font = xlwt.easyxf("pattern: pattern solid, fore_color red; font: color-index white, bold on;")
        reportName=os.path.abspath(os.path.join(selectedLayerPath, os.pardir))+"\\DistanceElevationReport.xls"
        fileName=reportName
        #pythonaddins.MessageBox(reportName,"What Up?",0)
        distanceReport=xlwt.Workbook()
        worksheet=distanceReport.add_sheet("Distance Report")
        worksheet.write(0,0,"Feature OID")
        worksheet.write(0,1,"Distance To")
        worksheet.write(0,2,"Point Id")
        worksheet.write(0,3,"Elevation")
        worksheet.write(0,4,"Code")
        distanceReport.save(fileName)
        ##point features to get elevation from
        where=""""CODE" LIKE 'BEND' OR "CODE" LIKE 'CLOSURE'  OR "CODE" LIKE 'FLN' OR "CODE" LIKE 'NIP' OR "CODE" LIKE 'RED' OR "CODE" LIKE 'TAP' OR "CODE" LIKE 'TEE' OR  "CODE" LIKE 'VLV' OR "CODE" LIKE 'WELD' OR "CODE" LIKE 'PL_TOPO'"""
        #lists and dicts for values
        allThePoints=arcpy.mapping.ListLayers(self.mxd, selectedLayer)[0]
        spatialRef=arcpy.SpatialReference(6578)
        def funCenNAme():
            with arcpy.da.SearchCursor("PL_PIPELINE_LN","CENTERLINE_NAME") as sc:
                for row in sc:
                    cenName = row[0]
                    return cenName
        cen=funCenNAme()
        #pythonaddins.MessageBox(allThePoints.name,"What Up?",0)
        where2= """CENTERLINE_NAME = '{0}'""".format(cen)
        arcpy.SelectLayerByAttribute_management("PL_PIPELINE_LN","CLEAR_SELECTION")
        i=1
        with arcpy.da.UpdateCursor("PL_PIPELINE_LN",["SHAPE@","OBJECTID"],where2,spatial_reference=spatialRef) as sc:
            for row in sc:
                ##access geometry and get into vertex
                geometry=row[0]
                objId=row[1]
                pts=geometry.getPart(0)
                pointArray=arcpy.Array()
                for point in pts:
                    i+=1
                    pointDict={}
                    newPoint=arcpy.Point()
                    ##iterate through the survey shape file store values to point dictionary
                    with arcpy.da.SearchCursor(allThePoints.name,["SHAPE@","CODE","ELEV","FID"],where,spatial_reference=spatialRef) as sc2:
                        for row2 in sc2:
                            pointGeometry= row2[0]
                            code=row2[1]
                            elev=row2[2]
                            oid=row2[3]
                            shpPoint=pointGeometry.getPart(0)
                            ##get distance to vertex
                            dist=sqrt((point.X-shpPoint.X)**2+(point.Y-shpPoint.Y)**2)
                            ##print(dist)
                            ##add values to point dictionary
                            pointDict[oid]=dist,elev,code,oid

                    print point.X
                    print point.Y
                    ##print minDist[1][1]
                    minDist=(min(pointDict.items(), key=lambda x:x[1]))
                    newPoint.X=point.X
                    newPoint.Y=point.Y
                    newPoint.Z = minDist[1][1]
                    pointArray.add(newPoint)
                    if round(minDist[1][0],4)>.300:
                        worksheet.write(i,0,objId)
                        #arcpy.AddMessage("OID For Feature IS {0} And the vertex nummber is {1}".format(objId,i))
                        worksheet.write(i,1,round(minDist[1][0],4))
                        #arcpy.AddMessage("""FID of elevation point {}""".format(str(minDist[1][3]).encode('utf8')))
                        worksheet.write(i,2,minDist[1][3])
                        #arcpy.AddMessage("""Elevation: {}""".format(str(minDist[1][1]).encode('utf8')))
                        worksheet.write(i,3,float(minDist[1][1]))
                        #arcpy.AddMessage("""Distance to point {}""".format(str(round(minDist[1][0],4)).encode('utf8')))
                        #arcpy.AddMessage(minDist[1][2])
                        worksheet.write(i,4,minDist[1][2])
                    else:
                        worksheet.write(i,0,objId)
                        worksheet.write(i,1,round(minDist[1][0],4))
                        worksheet.write(i,2,minDist[1][3])
                        worksheet.write(i,3,float(minDist[1][1]))
                        worksheet.write(i,4,minDist[1][2])

                newGeometry=arcpy.Polyline(pointArray,None,True)
                #pythonaddins.MessageBox(newGeometry.centroid.X,"title box",0)
                print newGeometry.centroid.X
                row=newGeometry,objId
                sc.updateRow(row)

        distanceReport.save(fileName)
        pythonaddins.MessageBox("Tool Run Complete, Check the Report in the Parent Folder for Details","title box",0)
0

I amended the code to write to test file just fine, I am wondering if there is a list of python packages that work with the esri pythonaddin library?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.