@@ -674,8 +674,9 @@ def addSubcomponents(self, items, iA=[0], iB=[-1]):
674674 self .subcomponents = items # dict(enumerate(items))
675675 for item in items :
676676 if isinstance (item , list ):
677- for subitem in item :
678- subitem .part_of = self
677+ for branch in item :
678+ for subitem in branch :
679+ subitem .part_of = self
679680 else :
680681 item .part_of = self
681682
@@ -739,6 +740,53 @@ def findEnd(self, object):
739740 return end
740741
741742
743+ def getSubcomponent (self , index ):
744+ '''Returns the subcomponent of the edge corresponding to the provided
745+ index. An index with multiple entries can be used to refer to parallel
746+ subcomponents.
747+
748+ Parameters
749+ ----------
750+ index: list
751+ The index of the subcomponent requested to be returned. Examples:
752+ [2]: return the third subcomponent in the series (assuming there
753+ are no parallel subcomponents).
754+ [2,1,0]: Return the first (or only) object along the second
755+ parallel string at the third serial position. Same as [2,1].
756+ [1,0,2]: Return the third object along the first paralle string
757+ at the first serial position.
758+ '''
759+
760+ if np .isscalar (index ):
761+ index = [index ] # put into a common list format if not already
762+
763+ if len (index ) == 2 : # assume length 2 is for a parallel branch with
764+ index .append (0 ) # with just one edge object, so add that 0 index.
765+
766+ # Only one index means the simple case without a parallel string here
767+ if len (index ) == 1 :
768+ if isinstance (self .subcomponents [index [0 ]], list ):
769+ raise Exception ('There is a parallel string at the requested index.' )
770+
771+ object = self .subcomponents [index [0 ]]
772+
773+ # Three indices means an object along a parallel string
774+ elif len (index ) == 3 :
775+ if not isinstance (self .subcomponents [index [0 ]], list ):
776+ raise Exception ('There is not a parallel string at the requested index.' )
777+ if len (self .subcomponents [index [0 ]]) < index [1 ]+ 1 :
778+ raise Exception ('The number of parallel strings is less than the requested index.' )
779+ if len (self .subcomponents [index [0 ]][index [1 ]]) < index [2 ]+ 1 :
780+ raise Exception ('The number of objects along the parallel string is less than the requested index.' )
781+
782+ object = self .subcomponents [index [0 ]][index [1 ]][index [2 ]]
783+
784+ else : # other options are not yet supported
785+ raise Exception ('Index must be length 1 or 3.' )
786+
787+ return object
788+
789+
742790 def setEndPosition (self , r , end ):
743791 '''Set the position of an end of the edge. This method should only be
744792 called by a Node's setPosition method if the edge end is attached to
@@ -1117,6 +1165,8 @@ def assemble(items):
11171165 # note: this requires the end objects to be edges
11181166
11191167 elif isinstance (subitem , Edge ): # if the subitem is just one edge
1168+ print ("THIS CASE SHOULDN'T HAPPEN - the list should be nested more" )
1169+ breakpoint ()
11201170 if i > 0 and isinstance (items [i - 1 ], Node ): # attach to previous node
11211171 items [i - 1 ].attach (subitem , end = 'a' )
11221172 if i < n - 1 and isinstance (items [i + 1 ], Node ): # attach to next node
@@ -1219,18 +1269,22 @@ def rotationMatrix(x3,x2,x1):
12191269 n0 = Node (id = 'n0' )
12201270 e1 = Edge (id = 'e1' )
12211271 n1 = Node (id = 'n1' )
1222- e2a = Edge (id = 'e2a' )
1223- e2b = Edge (id = 'e2b' )
1272+ e2a1 = Edge (id = 'e2a' )
1273+ e2a2 = Node (id = 'e2a' )
1274+ e2a3 = Edge (id = 'e2a' )
1275+ e2b = Edge (id = 'e2b' )
12241276
12251277
1226- thelist = [e0 , n0 , e1 , n1 , [e2a , e2b ]]
1278+ thelist = [e0 , n0 , e1 , n1 , [[ e2a1 , e2a2 , e2a3 ], [ e2b ] ]]
12271279
12281280 E = Edge (id = 'big edge' )
12291281
12301282 E .addSubcomponents (thelist )
12311283
1232- # ----- try joining two nodes -----
1284+ s = E . getSubcomponent ([ 4 , 0 , 2 ])
12331285
1286+ # ----- try joining two nodes -----
1287+ """
12341288 A = Node(id='Node A')
12351289 B = Node(id='Node B')
12361290 A.join(B)
@@ -1323,5 +1377,9 @@ def rotationMatrix(x3,x2,x1):
13231377 n1.setPosition(r=[0,0,0], theta=0)
13241378 #print(n1.attachments)
13251379 #print(e1.attached_to)
1326-
1380+ """
13271381
1382+
1383+
1384+
1385+
0 commit comments