解析ICD文件生成虚端子表


ICD文件

<?xml version="1.0" encoding="UTF-8"?>
<SCL xmlns="http://www.iec.ch/61850/2003/SCL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.iec.ch/61850/2003/SCL SCL.xsd" xmlns:ext="http://nari-relays.com" xmlns:csg="http://www.csg.cn">
  <Header id="PCS-222ILA-DG-N" version="V1.03" toolID="NRRConfig" nameStructure="IEDName"/>
  <Communication>
    <SubNetwork name="SubNetwork_Processbus" type="8-GOOSE">
      <BitRate unit="b/s" multiplier="M">10</BitRate>
      <ConnectedAP iedName="TEMPLATE" apName="G1">
        <GSE ldInst="RPIT" cbName="gocb0">
          <Address>
            <P type="MAC-Address">01-0C-CD-01-00-01</P>
            <P type="VLAN-ID">000</P>
            <P type="VLAN-PRIORITY">4</P>
            <P type="APPID">3001</P>
          </Address>
          <MinTime unit="s" multiplier="m">2</MinTime>
          <MaxTime unit="s" multiplier="m">5000</MaxTime>
        </GSE>
        <GSE ldInst="RPIT" cbName="gocb1">
          <Address>
            <P type="MAC-Address">01-0C-CD-01-00-02</P>
            <P type="VLAN-ID">000</P>
            <P type="VLAN-PRIORITY">4</P>
            <P type="APPID">3002</P>
          </Address>
          <MinTime unit="s" multiplier="m">2</MinTime>
          <MaxTime unit="s" multiplier="m">5000</MaxTime>
        </GSE>
        <GSE ldInst="RPIT" cbName="gocb2">

一、解析Python代码

# 解析ICD文件生成虚端子表
# 20240805
# Cao Jinhao
# pip install lxml


import os
from lxml import etree


def find_all_nodes(node, node_type):
    ln_nodes = []
    if node.tag.endswith(node_type):
        ln_nodes.append(node)


    for child in node:
        ln_nodes.extend(find_all_nodes(child, node_type))
    return ln_nodes


inputs = []
py_dir = os.path.split(os.path.realpath(__file__))[0]
#print(py_dir)

count = 0


for file_name in os.listdir(py_dir):
    if file_name.endswith(".icd"):
        #print(file_name)


        temp = ["IED设备名称,虚端子类型,虚端子数据属性,虚端子定义,ICD文件行号"]


        file_path = os.path.join(py_dir, file_name)
        icd_tree = etree.parse(file_path)


        icd_root = icd_tree.getroot()
        #print(icd_root)
        ln_nodes = find_all_nodes(icd_root, "LN")
        #print(ln_nodes)
        ied_node = find_all_nodes(icd_root, "IED")


        for ln in ln_nodes:
            prefix = ln.get("prefix")
            #print(prefix)
            if prefix == None:
                prefix = ""


            lnclass = ln.get("lnClass")
            inst = ln.get("inst")


            in_out = "GOOSE输出"
            if "GOIN" in str(prefix):
                in_out = "GOOSE输入"


            for doi in find_all_nodes(ln, "DOI"):
                #input_node = prefix + u"\u200c" + lnclass + u"\u200c" + inst
                #print(ied_node[0].get("name"))


                #input_node = ied_node[0].get("name") + "," + in_out + "," + ln.getparent().get("inst") + "/" + prefix + u"\u200c" + lnclass + u"\u200c" + inst
                input_node = file_name.replace(".icd", "") + "," + in_out + "," + ln.getparent().get("inst") + "/" + prefix + lnclass + inst
                break_dai = False


                for dai in find_all_nodes(doi, "DAI"):
                    if break_dai == True:
                        break


                    for val in find_all_nodes(dai, "Val"):
                        if val.text == doi.get("desc"):
                            #input_node = input_node + u"\u200c" + val.text


                            input_node = input_node + "." + doi.get("name") + "." + doi.getchildren()[0].get("name") + "," + val.text + "," + str(dai.sourceline)
                            temp.append(input_node)
                            #print(temp)


                            break_dai = True
                            break


        temp = [i for i in temp if not "LD0" in i.split(',')[2]]


        #with open(ied_node[0].get("name") + ".csv", "w", encoding="utf-8") as file:
        with open(os.path.join(py_dir, "虚端子表导出结果", file_name.replace(".icd", "") + ".csv"), "w", encoding="utf-8") as file:
            for item in temp:
                file.write(item + "\n")


        #inputs.append(temp)
        #break


        #count = count + 1
        #if count == 3:
            #break


print("虚端子表导出完成")





二、虚端子表

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hanssjtuer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值