Author Topic: Scripting - Locate Assoc. Class' source class  (Read 9410 times)

David Rains (bioform)

  • EA User
  • **
  • Posts: 84
  • Karma: +0/-0
    • View Profile
Re: Scripting - Locate Assoc. Class' source class
« Reply #15 on: January 13, 2011, 08:05:43 pm »
For completness here is the full code for the VBScript
=======================================
option explicit

'Process the newly created wording connector

'Assumptions:
'      - a valid sbvrWording connector (conWording) is selected in the current diagram
'            - the connector stores the associated class ID in PDATA1 (access via .MiscData(#-1)
'            - the connector has the necessary tag values (e.g., verb_phrase_forward/reverse)
'            - the connector associations a new class with 1 or 2 <sbvrTerm> classes (unary or binary)
'            - the newassociation class has NOT been stereotyped yet
'      - the associated term(s) have been previously named
'      - user will supply a forward and reverse verb phrase when prompted

'Example Data:
'      - Term Names = A, B
'      - verb phrase forward = precedes, reverse = follows

'Success Outcome:
'      - associated clase name will be derived and assigned (e.g., A precedes B)
'      - associated class will be assigned the correct stereotype (e.g., sbvrFactType)
'      - wording connector will display 'precedes' in the .... label
'

'TO DO LIST
'      - add error checking

sub sbvrProcessWordingConnector()
      
      ' Get the currently selected connector in the diagram to work on
      dim theDiagram as EA.Diagram
      set theDiagram = Repository.GetCurrentDiagram

      dim theConnector as EA.Connector
      set theConnector = theDiagram.SelectedConnector
      
      on error resume next
      if theConnector is nothing or theConnector.Stereotype <> "sbvrWording" or theConnector.MiscData(0) = "" then 'PDATA# is (#-1)
            ' No item selected in thediagram, or the item selected was not expected
            MsgBox( "This script requires a sbvrWording connector be selected in the current diagram." & chr(10) & _
                  "Please try again." )
            on error goto 0            'Turn Error checking back on
      else
            on error goto 0            'Turn Error checking back on
            'Msgbox( "Success")
                  
            dim supplierElement as EA.Element
                  supplierElement = Repository.GetElementByID(theConnector.SupplierID)   'ERROR OCCURS HERE!!            
            dim clientElement as EA.Element
                  clientElement =Repository.GetElementByID(theConnector.ClientID)

            msgbox("Supplier and Client IDs: " & supplierID & " <" & supplierElement.Name & ">, " & clientID & " <" & clientElement.Name & ">")
      end if

      set theConnector = nothing
      set theDiagram = nothing

end sub

sbvrProcessWordingConnector
=======================================

David Rains (bioform)

  • EA User
  • **
  • Posts: 84
  • Karma: +0/-0
    • View Profile
Re: Scripting - Locate Assoc. Class' source class
« Reply #16 on: January 14, 2011, 10:20:17 am »
Okay I just could not let go of this and just scrapped the whoile thing and started from scratch... oif course things worked fine now?!!

anyway here is the working solution.... and this thread can be closed!
====================
option explicit

sub main()

      dim currentDiagram as EA.Diagram
      set currentDiagram = Repository.GetCurrentDiagram
      
      dim currentConnector as EA.Connector
      set currentConnector = currentDiagram.SelectedConnector
      
      on error resume next
      if currentConnector is nothing or currentConnector.Stereotype <> "sbvrWording" or currentConnector.MiscData(0) = "" then 'PDATA# is (#-1)
            ' No item selected in thediagram, or the item selected was not expected
            MsgBox( "This script requires a sbvrWording connector be selected in the current diagram." & chr(10) & _
                  "Please try again." )
            on error goto 0            'Turn Error checking back on
      else
            on error goto 0            'Turn Error checking back on
            msgbox "Connector Name, ID, and GUID: " & currentConnector.Name & ", " & currentConnector.ConnectorID & ", " & currentConnector.ConnectorGUID ,vbOKOnly

            dim sourceElement as EA.Element
            set sourceElement = Repository.GetElementByID(currentConnector.SupplierID)
            'set elementSelected = Repository.GetTreeSelectedObject
                  msgbox "Source Element Name, ID, and GUID: " & sourceElement.Name & ", " & sourceElement.ElementID & ", " & sourceElement.ElementGUID ,vbOKOnly
            dim clientElement as EA.Element
            set clientElement = Repository.GetElementByID(currentConnector.ClientID)
            'set elementSelected = Repository.GetTreeSelectedObject
                  msgbox "Client Element Name, ID, and GUID: " & clientElement.Name & ", " & clientElement.ElementID & ", " & clientElement.ElementGUID ,vbOKOnly

            dim assocc_classElement as EA.Element
            set assocc_classElement = Repository.GetElementByID(currentConnector.MiscData(0))
            'set elementSelected = Repository.GetTreeSelectedObject
                  msgbox "Assoc. Class Element Name, ID, and GUID: " & assocc_classElement.Name & ", " & assocc_classElement.ElementID & ", " & assocc_classElement.ElementGUID ,vbOKOnly

            set sourceElement = nothing
            set clientElement = nothing
            set assocc_classElement = nothing

      end if

      set currentConnector = nothing
      set currentDiagram = nothing
end sub
main
====================

mrf

  • EA User
  • **
  • Posts: 311
  • Karma: +0/-0
    • View Profile
Re: Scripting - Locate Assoc. Class' source class
« Reply #17 on: January 14, 2011, 11:32:22 am »
Glad to hear you got everthing working. There will be improved support for Association Classes through the automation interface in version 9.

Quote
I re-read the snippet about GetElementByID and this seems to refer NOT to the EA assigned object ID, but to a local value of a collection?

I'm not sure if I'm reading into this wrong (having not seen the source of your quote), but I don't think that statement is accurate.

An object ID is unique within the context of a repository, but not globally - which is why objects both have IDs (repository unique identifier) and GUIDs (globally unique identifier).

If you import/export between repositories you will notice that the IDs change but the GUIDs do not. As a result, I would not expect GetElementByID to fail if the element ID you are using comes from the current repository.
Best Regards,

Michael

[email protected]
"It is more complicated than you think." - RFC 1925, Section 2.8