Py学习  »  Python

ArcGIS Pro 二分式标注,VB或python

GISAI • 3 月前 • 180 次点击  

# 设计人:闫磊

# 编制日期:2012-03-25


def strlen(s):

    """

    计算字符串长度(考虑中文字符)

    中文字符计为2,英文字符计为1

    """

    length = 0

    for char in s:

        # 如果字符的ASCII码值大于255或小于0,视为中文字符

        if ord(char) > 255:

            length += 2

        else:

            length += 1

    return length


def myFind(DZM, NAME):

    """

    格式化输出函数

    根据DZM和NAME的长度差异进行居中处理

    """

    a = strlen(DZM)

    b = strlen(NAME)


    if a > b:

        return f"{DZM}\n{NAME}"

    else:

        # 计算两边需要添加的空格数

        spaces_count = (b - a) // 2

        spaces = " " * spaces_count

        return f"{spaces}{DZM}{spaces}\n{NAME}"


def FindLabel([省], [省级码]):

    """

    主函数,调用myFind进行标签查找和格式化

    """

    return myFind([省], [省级码])


VB的代码

'设计人:闫磊

'----------FUNCTION STRLEN(STR)----------

FUNCTION strlen(str)

    dim p_len

    p_len=0

    strlen=0

    p_len=len(str)


    FOR xx=1 to p_len


        IF asc(mid(str,xx,1))<0 then

            strlen=int(strlen) + 2

        ELSE

            strlen=int(strlen) + 1

        END if


    NEXT


END function


FUNCTION myFind ( DZM, NAME )

    a=strlen(dzm) 

    b=strlen(NAME)


    IF a>b then 

       myFind ="" & DZM & "" &  vbnewline & NAME

    ELSE

        str= space((b-a)/2)

        myFind ="" & str & DZM & str & "" & vbnewline & NAME

    END if


END Function



'编制日期:2012-03-25

Function FindLabel ( [DLBM],[DLMC]  )

  FindLabel =myFind([DLBM],[DLMC])

End Function



Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/192251