0

I have problems with writing some python code. I will present the case and hope you can help me. From external softare I get an XML-file which contains information regarding a structure. Like the different points and lines are in this XML-file. Down here, there is a litlle bit of this XML file.

<container id="{XXXXXXX}" t="EP_DSG_Elements.EP_StructNode.1">
      <table id="XXXXXXX" t="EP_DSG_Elements.EP_StructNode.1" name="Node">
         <h>
            <h0 t="Name"/>
            <h1 t="Coord X"/>
            <h2 t="Coord Y"/>
            <h3 t="Coord Z"/></h>

         <obj id="1" nm="K1">
            <p0 v="K1"/>
            <p1 v="0"/>
            <p2 v="0"/>
            <p3 v="0"/></obj>
         
         <obj id="2" nm="K2">
            <p0 v="K2"/>
            <p1 v="0"/>
            <p2 v="0"/>
            <p3 v="3.6000000000000001"/></obj>
         
         <obj id="3" nm="K3">
            <p0 v="K3"/>
            <p1 v="0"/>
            <p2 v="0"/>
            <p3 v="7.2000000000000002"/></obj>

<container id="{XXX}" t="EP_DSG_Elements.EP_Beam.1">
      <table id="XXXX" t="EP_DSG_Elements.EP_Beam.1" name="Member 1D">
         <h>
            <h0 t="Name"/>
            <h1 t="Type"/>
            <h2 t="Beg. node"/>
            <h3 t="End node"/>
            <h4 t="Cross-section"/>
            <h5 t="FEM type"/>
            <h6 t="Member system-line at"/>
            <h7 t="ey"/>
            <h8 t="ez"/>
            <h9 t="Table of geometry"/></h>

         <obj id="1" nm="S1">
            <p0 v="S1"/>
            <p1 v="2" t="column (100)"/>
            <p2 i="1" n="K1"/>
            <p3 i="2" n="K2"/>
            <p4 i="3" n="CS3 - IPE300"/>
            <p5 v="0" t="standard"/>
            <p6 v="1" t="Centre"/>
            <p7 v="0"/>
            <p8 v="0"/>
            <p9 t="">
            <h>
            <h1 t="Node"/>
            <h2 t="Edge"/>
            </h>
            <row id="0">
            <p1 i="1" n="K1"/>
            <p2 v="0" t="Lijn"/>
            </row>
            <row id="1">
            <p1 i="2" n="K2"/>
            </row>
            </p9></obj>

The XML-file is automatically generated and the number of points is variable. The structure looks like this: Structure

I was able to sort out which lines were horizontal and which were vertical because I am only interested in the vertical ones.

So here is the problem. Because the number of lines and points are variable I want to work with a while loop which has the condition of the lentgh of the child (len(myroot[8][0])). As a result I would like to have a list for each vertical line. Those list have to include all the points that are located above the line. An example for line 9: [8,6,5] and for line 14: [10,11] Is there an easy way to do this?

This is what I got until now: First I made 3 lists which include respectively all X, Y and Z coordinates. I used this code:

List_points = []
List_All_Points_X = []
List_All_Points_Y = []
List_All_Points_Z = []
i=1
while i < len(myroot[7][0]):
    VarPointID = myroot[7][0][i].attrib
    PointID = str(VarPointID.get('id'))
    List_Points.append(int(KnoopID))

    VarXCoord = myroot[7][0][i][1].attrib
    XCoord = str(VarXCoord.get('v'))
    List_All_Points_X.append(int(XCoord))

    VarYCoord = myroot[7][0][i][2].attrib
    YCoord = str(VarYCoord.get('v'))
    List_All_Points_Y.append(int(YCoord))

    VarZCoord = myroot[7][0][i][3].attrib
    ZCoord = str(VarZCoord.get('v'))
    List_All_Points_Z.append(float(ZCoord))

    i = i+1

The I filtered all duplicates out and sorted them from low to high so I have an idea with the used coordinates are in the structure

for i in List_All_Points_X:
    if i not in List_Points_X:
        List_Points_X.append(i)

for i in List_All_Points_Y:
    if i not in List_Points_Y:
        List_Points_Y.append(i)

for i in List_All_Points_Z:
    if i not in List_Points_Z:
        List_Points_Z.append(i)

List_Points_X.sort(reverse=False)
List_Points_Y.sort(reverse=False)
List_Points_Z.sort(reverse=False)

But now I am stuck with creating multiple iterative lists for each line, Can someone help me with it or is there a better way to do all this?

EDIT The output of the file would be to generate new xml files which loaded into a program will look like this: Orginal structure --> New structure To create the new structure I have to delete a line and move the points above the line down. The horizontal lines will move automatically when the coordinates of the points are changed. But because the number of lines and points are not known in the beginning I need to make it iterative. I created a list which contains all line numbers which are vertical so I know which lines I have to delete each time. But the problem is with moving the dots. I want to make a list for each line that included all points horizontaly above that line. So to create multiple lists without doing it iterative and just doing it manually I want it to look like this:

Line1=[]
Line2=[]
Line3=[]
Line4=[]
Line5=[]
Line6=[]
Line7=[]
Line8=[]
Line9=[]
Line10=[]

The I will check all the X-coordinates of all the points and make a list of all points that are in the same vertical space. The I will sort this list so the first member of the list is the lowest point and the last member of the list is the highest point.

ListFirstRow=[1,2,3,4]
ListSecondRow=[5,6,7,8]
ListThirdRow=[9,10,11,12]

SortedListFirstRow=[1,2,3,4]
SortedListSecondRow=[7,8,6,5]
SortedListThirdRow=[12,9,10,11]

When I delete the line I can find in the XML file the beginning and the and point of that line. I will look both points up in the list containg the vertical space points and check for their indeces. Than I can just iterati throug that list with the starting index the one of the end point of the line and make those changes.

So lets say I want to create the structure where line 1 is removed. Then I check in the XML File the start and end point of line 1.

<container id="{XXX}" t="EP_DSG_Elements.EP_Beam.1">
      <table id="XXXX" t="EP_DSG_Elements.EP_Beam.1" name="Member 1D">
         <h>
            <h0 t="Name"/>
            <h1 t="Type"/>
            <h2 t="Beg. node"/>
            <h3 t="End node"/>

         <obj id="1" nm="S1">
            <p0 v="S1"/>
            <p1 v="2" t="column (100)"/>
            <p2 i="1" n="K1"/>
            <p3 i="2" n="K2"/>

The start and end point are K1 and K2. I can now check in the list SortedListFirstRow=[1,2,3,4] which point is located the highest. In this case point 2 or K2 is the one the highest, I look for the index with SortedListFirstRow.index(2) and it equals 1. So know I can make a while loop like this:

i = SortedListFirstRow.index(2)
while i < len(SortedListFirstRow)

    VarZCoord = myroot[7][0][i][3].attrib
    ZCoord = str(VarZCoord.get('v'))
    New_Value = float(ZCoord)-1
    myroot[7][0][i][3].clear()
    myroot[7][0][i][3].set('v', New_Value)
    

Like this I can change the coordinates of the points which have to move a little bit down by one in this example

I hope I have made it clearer, already thank you all of you for responding to my question!

Thanks in advance, Niels

nielssvdk
  • 1
  • 1
  • Can you show a more minimal example, and also what you want to get as result? – mkrieger1 May 03 '22 at 07:25
  • 1
    If as I suspect you want to dynamically create variables, don't do it [use a dictionary](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables). – mozway May 03 '22 at 07:28
  • Soory for the complex example, I am just trying to edit the XML file of the orginal structure and make some alterations, like removing the line and moving the points above the line a bit down. I thoufg that it would be the easiest to make a list for every line with the points above the line. Then I could just iteratze through the list and make those alterations and save it as a new XML file. I know it would work if I write al these list to different text files but the I end up with perhaps 20 or even more text files and I hoped that there was a more easy way – nielssvdk May 03 '22 at 08:47
  • I lookad at using a dictionary but I dont know how to implement it in this case, I originally wanted to just write multiple varibales with an incremented number, for example var1, var2, ... but I can't make those lists at the beginning of the code because I don't know how many lists I need to make. I know this works for creating text files, I just made a string with an variable that increases each time it loops and then I just created a txt file with the name of the string. So later in the code when I want to alter the xml file, I know what the name of the files are to get the wanted variables – nielssvdk May 03 '22 at 08:55
  • Could you edit your question to include a dummy example of the output you want? That would make it a lot easier to understand where you want to end up and help you with the easiest way there. – doggie May 03 '22 at 08:58
  • Sure i'll edit it right away – nielssvdk May 03 '22 at 08:59

1 Answers1

0

I was succesfull with using a dictionary. Thanks for the help!

nielssvdk
  • 1
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 04 '22 at 12:31