Book a Demo

Author Topic: Script for anyone who wants it:Sets/gets user specific properties  (Read 2425 times)

Jayson

  • EA User
  • **
  • Posts: 363
  • Karma: +1/-0
    • View Profile
Okie dokie

So a lot of the same wonderful usual suspects have helped me a lot with my quest to develop a new MDG.
So, I have created a class that can be used to get and set the value of user specific properties with no muss or fuss.
For the uninitiated, these are properties that belong to a stereotyped element and have different values in each diagram they appear.

For more information on how to create them and what they do, please see the bottom of the page link below.

I hope someone finds this useful as it is my first chance to give something back to the forum that has helped me so much.
Btw, sniggering at my utter lack of coding skills is TOTALLY allowed. I manage an architecture team, not write code!  :P

Cheers

Jays  :)

https://sparxsystems.com/enterprise_architect_user_guide/14.0/modeling_tools/query_methods.html


' use this class to set and get the user specific properties (stereotyped as diagram properties in a stereotype
' To use it, follow the following steps:
'
' 1. Create an instance of the class.
' 2. Call SetStyleString and pass the diagram.StyleEx string
'
' From there, you can either:
' a) Get the value of properties by calling GetProperty, or
' b) Set the value of properties by calling SetProperty (as many times as needed) and then
'    set the value of diagram.StyleEx = StyleString()
Class DiagramStyle
   
   Private m_styleString
   
   Public Default Function Init()
      Set Init = Me
      
   End Function
   
   Public Sub SetStyleString(style)
      m_styleString = style
      
   End Sub
   
   Public Function StyleString()
      StyleString = m_styleString
   End Function
   
   ' Returns the value of an Object Property (either 1 or 0)
   ' Returns 2 if the object or property were not found
   Public Function GetProperty(objectDUID, propertyName)
   
      dim regExp
      dim matchPattern
      dim objString
      dim PropString
      dim rxMatch
      
      Set regExp = new RegExp
      regExp.IgnoreCase = true
      regExp.Global = false
      
      ' Find the object in the style string
      matchPattern = "OPTIONS_" & objectDUID & "[^;]*;"
      regExp.Pattern = matchPattern
      Set rxMatch = regExp.Execute(m_StyleString)
      
      If(rxMatch.Count <>1) Then
         GetProperty = 2   ' No match found
         Exit Function
      End If
      
      objString = rxMatch.Item(0)
      
      ' Find the property in the object string
      matchPattern = propertyName & "=[10]"
      regExp.Pattern = matchPattern
      Set rxMatch = regExp.Execute(objString)
      
      If(rxMatch.Count <>1) Then
         GetProperty = 2   ' No match found
         Exit Function
      End If
      
      propString = rxMatch.Item(0)

      ' Return the value of the property
      If(Right(propString,1) = 1) Then
         GetProperty = 1
      Else
         GetProperty = 0
      End If
      
   End Function
   
   ' Sets the value of a Object Property where the propertyValue is a string with value "0" or "1"
   ' Returns True if successful, False if unsuccessful
   Public Function SetProperty( objectDUID, propertyName, propertyValue)
   
      dim matchPattern
      dim oldObjString
      dim newObjString
      dim oldPropString
      dim newPropString
      dim rxMatches
      dim rxMatch
      dim regExp
      dim oldStyleString
      dim newStyleString
      
      Set regExp = new RegExp
      regExp.IgnoreCase = true
      regExp.Global= false
      
      oldStyleString = m_styleString
      newStyleString = m_styleString
      
      ' Check property value is valid (can only be 1 or 0)
      propertyValue = Trim(propertyValue)
      
      Select Case propertyValue
      Case "0":
      Case "1":
      Case Else
         SetProperty = false
         Exit Function
         
      End Select
      
      ' Find the matching object
      matchPattern = "OPTIONS_" & objectDUID & "[^;]*;"
      regExp.Pattern = matchPattern
      Set rxMatch = regExp.Execute(newStyleString)
      
      If(rxMatch.Count <>1) Then
         SetProperty = false   ' No match found
         Exit Function
      End If
      
      oldObjString = rxMatch.Item(0)
      newObjString= oldObjString
   
      ' Find the matching property
      newPropString = propertyName & "=" & propertyValue
      matchPattern = propertyName & "=[10]"
      
      regExp.Pattern = matchPattern
      Set rxMatch = regExp.Execute(newObjString)
         
      If(rxMatch.Count <>1) Then
         SetProperty = false ' No match found
         Exit Function
      End If
      
      oldPropString = rxMatch.Item(0)
      
      'Replace the property in the object string
      regExp.Pattern = oldPropString
      newObjString = regExp.Replace(newObjString, newPropString)
      
         
      'Session.Output "Old Object = " & oldObjString & " New Object = " & newObjString & " Old Prop = " & oldPropString & " New Prop = " & newPropString
         
      ' Replace object in style string
      regExp.Pattern = oldObjString
      newStyleString = regExp.Replace(newStyleString, newObjString)
      m_styleString = newStyleString
      
   End Function
   
End Class