Set all line styles in an Enterprise Architect diagram automatically

With this script you can change set all the lines styles on a diagram at once, to your preferred style per type of connector.

In Enterprise Architect you can choose from no less then 9 different line styles for the connectors.

Line Styles

Unfortunately you can only choose from the first three to be the default line style for new connectors. Additionally you can also specify the default for Generalization to be Tree Style, but that is it.

For me that is not enough. I have my own habits when making UML diagrams. For most connector types I use Orthogonal – Square, but not for dependencies, use case relations and note links. For those I like the Direct style. The last exception are the control flow, state flow and object flows, for which I use Orthogonal – Rounded.

LineStylesDiagram

With this script I can set all the line styles on a diagram to the styles that I like.  All I need to do is right click on a diagram and choose Scripts|Set Line Styles

Set Linestyles menu option

And the script will set all the line styles to the defaults set in the script. If you would like the line styles to be set to your preferences automatically without having to run the script you can use EA-Matic version of this script.

You can set your own preferences in this section of the script

'*********EDIT BETWEEN HERE*************
' set here the menu name
menuDefaultLines = "&Set default linestyles"

' set here the default style to be used
defaultStyle = lsOrthogonalSquareTree

' set there the style to be used for each type of connector
function determineStyle(connector)
    dim connectorType
    connectorType = connector.Type
    select case connectorType
        case "ControlFlow", "StateFlow","ObjectFlow","InformationFlow"
            determineStyle = lsOrthogonalRoundedTree
        case "Generalization", "Realization", "Realisation"
            determineStyle = lsTreeVerticalTree
        case "UseCase", "Dependency","NoteLink"
            determineStyle = lsDirectMode
        case else
            determineStyle = defaultStyle
    end select
end function
'************AND HERE****************

Free download

[ecwid_product store_id=”6824265″ product_id=”56907450″]

Video

This script has been previously featured in the webinar Introduction to Scripting with Enterprise Architect presented by yours truly.

The script

option explicit

!INC Local Scripts.EAConstants-VBScript

' Script Name: DefaultLineStyles
' Author: Geert Bellekens
' Purpose: Allows to change the linestyles to their default
' Date: 27/04/2015
'
dim lsDirectMode, lsAutoRouteMode, lsCustomMode, lsTreeVerticalTree, lsTreeHorizontalTree, _
lsLateralHorizontalTree, lsLateralVerticalTree, lsOrthogonalSquareTree, lsOrthogonalRoundedTree

lsDirectMode = "1"
lsAutoRouteMode = "2" 
lsCustomMode = "3"
lsTreeVerticalTree = "V"
lsTreeHorizontalTree = "H"
lsLateralHorizontalTree = "LH"
lsLateralVerticalTree = "LC"
lsOrthogonalSquareTree = "OS"
lsOrthogonalRoundedTree = "OR"

dim defaultStyle
dim menuDefaultLines


'*********EDIT BETWEEN HERE*************


' set here the default style to be used
defaultStyle = lsOrthogonalSquareTree

' set there the style to be used for each type of connector
function determineStyle(connector)
	dim connectorType
	connectorType = connector.Type
	select case connectorType
		case "StateFlow","ObjectFlow","InformationFlow"
			determineStyle = lsOrthogonalRoundedTree
		case "Generalization", "Realization", "Realisation"
			determineStyle = lsTreeVerticalTree
		case "UseCase", "Dependency","NoteLink"
			determineStyle = lsDirectMode
		case else
			determineStyle = defaultStyle
	end select
end function
'************AND HERE****************


sub main
		dim diagram 
		dim diagramLink
		dim connector
		dim dirty
		dirty = false
		set diagram = Repository.GetCurrentDiagram
		'save the diagram first
		Repository.SaveDiagram diagram.DiagramID
		'then loop all diagramLinks
		if not diagram is nothing then
			for each diagramLink in diagram.DiagramLinks
				set connector = Repository.GetConnectorByID(diagramLink.ConnectorID)
				if not connector is nothing then
					'set the connectorstyle
					setConnectorStyle diagramLink, determineStyle(connector)
					'save the diagramlink
					diagramLink.Update
					dirty = true
				end if
			next
			'reload the diagram if we changed something
			if dirty then
				'reload the diagram to show the link style
				Repository.ReloadDiagram diagram.DiagramID
			end if
		end if
end sub

main


'gets the diagram link object
function getdiagramLinkForConnector(connector, diagram)
	dim diagramLink 
	set getdiagramLinkForConnector = nothing
	for each diagramLink in diagram.DiagramLinks
		if diagramLink.ConnectorID = connector.ConnectorID then
			set getdiagramLinkForConnector = diagramLink
			exit for
		end if
	next
end function

'actually sets the connector style
function setConnectorStyle(diagramLink, connectorStyle)
	'split the style into its parts
	dim styleparts
	dim styleString
	styleString = diagramLink.Style
	styleparts = Split(styleString,";")
	dim stylePart
	dim mode
	dim modeIndex
	modeIndex = -1
	dim tree
	dim treeIndex
	treeIndex = -1
	mode = ""
	tree = ""
	dim i
	'find if Mode and Tree are already defined
	for i = 0 to Ubound(styleparts) -1 
		stylePart = styleparts(i)
		if Instr(stylepart,"Mode=") > 0 then
			modeIndex = i
		elseif Instr(stylepart,"TREE=") > 0 then
			treeIndex = i
		end if
	next
	'these connectorstyles use mode=3 and the tree
	if  connectorStyle = lsTreeVerticalTree or _
		connectorStyle = lsTreeHorizontalTree or _
		connectorStyle = lsLateralHorizontalTree or _
		connectorStyle = lsLateralVerticalTree or _
		connectorStyle = lsOrthogonalSquareTree or _
		connectorStyle = lsOrthogonalRoundedTree then
		mode = "3"
		tree = connectorStyle
	else
		mode = connectorStyle
	end if
	'set the mode value
	if modeIndex >= 0 then
		styleparts(modeIndex) = "Mode=" & mode
		diagramLink.Style = join(styleparts,";")
	else
		diagramLink.Style = "Mode=" & mode& ";"& diagramLink.Style
	end if
	'set the tree value
	if treeIndex >= 0 then
		if len(tree) > 0 then
			styleparts(treeIndex) = "TREE=" & tree
			diagramLink.Style = join(styleparts,";")
		else
			'remove tree part
			diagramLink.Style = replace(diagramLink.Style,styleparts(treeIndex)&";" , "")
		end if
	else
		diagramLink.Style = diagramLink.Style & "TREE=" & tree & ";"
	end if
end function

function getConnectorStyle(diagramLink)
	'split the style
	dim styleparts
	styleparts = Split(diagramLink.Style,";")
	dim stylePart
	dim mode
	dim tree
	mode = ""
	tree = ""
	for each stylepart in styleparts
		if Instr(stylepart,"Mode=") > 0 then
			mode = right(stylepart, 1)
		elseif Instr(stylepart,"TREE=") > 0 then
			tree = replace(stylepart, "TREE=", "")
		end if
	next
	if tree <> "" then
		getConnectorStyle = tree
	else
		getConnectorStyle = mode
	end if
end function

Configure your own default line styles in Enterprise Architect with EA-Matic

With EA-Matic you can develop add-ins for Enterprise Architect using the built-in scripting feature of EA.

With this EA-Matic script you can configure your own default line-styles depending on the type of connector.

In Enterprise Architect you can choose from no less then 9 different line style for the connectors.

Line Styles

Unfortunately you can only choose from the first three to be the default line style for new connectors.

line style default options

Additionally you can also specify the default for Generalization to be Tree Style.

For me that is not enough. I have my own habits when making UML diagrams. For most connector types I use Orthogonal – Square, but not for dependencies, use case relations and note links. For those I like the Direct style. The last exception are the control flow, state flow and object flows, for which I use Orthogonal – Rounded.

LineStylesDiagram

This EA-Matic script gives me the control I want. Not only does it set the default line style for new connectors, with the context menu I can reset all connectors on a diagram to my own default styles.

Free download

[ecwid_product store_id=”6824265″ product_id=”48249914″]

The code

In order to define your own preferred style you can edit the indicated part in the script

'*********EDIT BETWEEN HERE*************
' set here the menu name
menuDefaultLines = "&Set default linestyles"

' set here the default style to be used
defaultStyle = lsOrthogonalSquareTree

' set there the style to be used for each type of connector
function determineStyle(connector)
    dim connectorType
    connectorType = connector.Type
    select case connectorType
        case "ControlFlow", "StateFlow","ObjectFlow","InformationFlow"
            determineStyle = lsOrthogonalRoundedTree
        case "Generalization", "Realization", "Realisation"
            determineStyle = lsTreeVerticalTree
        case "UseCase", "Dependency","NoteLink"
            determineStyle = lsDirectMode
        case else
            determineStyle = defaultStyle
    end select
end function
'************AND HERE****************

This script uses 3 EA events. The first one is the EA_OnPostNewConnector which gets triggered by EA the moment you create  a new connector.

'the event called by EA
function EA_OnPostNewConnector(Info)
    'get the connector id from the Info
    dim connectorID
    connectorID = Info.Get("ConnectorID")
    dim connector 
    set connector = Repository.GetConnectorByID(connectorID)
    'get the current diagram
    dim diagram
    set diagram = Repository.GetCurrentDiagram()
    if not diagram is nothing then
        'first save the diagram
        Repository.SaveDiagram diagram.DiagramID
        'get the diagramlink for the connector
        dim diagramLink
        set diagramLink = getdiagramLinkForConnector(connector, diagram)
        if not diagramLink is nothing then
            'set the connectorstyle
            setConnectorStyle diagramLink, determineStyle(connector)
            'save the diagramlink
            diagramLink.Update
            'reload the diagram to show the link style
            Repository.ReloadDiagram diagram.DiagramID
        end if
    end if
end function

The next even we use is the EA_GetMenuItems so we can show a menu option in the context menu of a diagram.

'Tell EA what the menu options should be
function EA_GetMenuItems(MenuLocation, MenuName)
    if MenuName = "" and MenuLocation = "Diagram" then
        'Menu Header
        EA_GetMenuItems = menuDefaultLines
    end if 
end function

The last EA event is the EA_MenuClick, triggered when the user actually clicks on the menu item.

'react to user clicking a menu option
function EA_MenuClick(MenuLocation, MenuName, ItemName)
    if ItemName = menuDefaultLines then
        dim diagram 
        dim diagramLink
        dim connector
        dim dirty
        dirty = false
        set diagram = Repository.GetCurrentDiagram
        'save the diagram first
        Repository.SaveDiagram diagram.DiagramID
        'then loop all diagramLinks
        if not diagram is nothing then
            for each diagramLink in diagram.DiagramLinks
                set connector = Repository.GetConnectorByID(diagramLink.ConnectorID)
                if not connector is nothing then
                    'set the connectorstyle
                    setConnectorStyle diagramLink, determineStyle(connector)
                    'save the diagramlink
                    diagramLink.Update
                    dirty = true
                end if
            next
            'reload the diagram if we changed something
            if dirty then
                'reload the diagram to show the link style
                Repository.ReloadDiagram diagram.DiagramID
            end if
        end if
    end if
end function

Then we have a small helper function to get the diagramLink object from the diagram

'gets the diagram link object
function getdiagramLinkForConnector(connector, diagram)
    dim diagramLink 
    set getdiagramLinkForConnector = nothing
    for each diagramLink in diagram.DiagramLinks
        if diagramLink.ConnectorID = connector.ConnectorID then
            set getdiagramLinkForConnector = diagramLink
            exit for
        end if
    next
end function

And last but not least the method that will actually set the connector style.

'actually sets the connector style
function setConnectorStyle(diagramLink, connectorStyle)
    'split the style into its parts
    dim styleparts
    dim styleString
    styleString = diagramLink.Style
    styleparts = Split(styleString,";")
    dim stylePart
    dim mode
    dim modeIndex
    modeIndex = -1
    dim tree
    dim treeIndex
    treeIndex = -1
    mode = ""
    tree = ""
    dim i
    'find if Mode and Tree are already defined
    for i = 0 to Ubound(styleparts) -1 
        stylePart = styleparts(i)
        if Instr(stylepart,"Mode=") > 0 then
            modeIndex = i
        elseif Instr(stylepart,"TREE=") > 0 then
            treeIndex = i
        end if
    next
    'these connectorstyles use mode=3 and the tree
    if  connectorStyle = lsTreeVerticalTree or _
        connectorStyle = lsTreeHorizontalTree or _
        connectorStyle = lsLateralHorizontalTree or _
        connectorStyle = lsLateralVerticalTree or _
        connectorStyle = lsOrthogonalSquareTree or _
        connectorStyle = lsOrthogonalRoundedTree then
        mode = "3"
        tree = connectorStyle
    else
        mode = connectorStyle
    end if
    'set the mode value
    if modeIndex >= 0 then
        styleparts(modeIndex) = "Mode=" & mode
        diagramLink.Style = join(styleparts,";")
    else
        diagramLink.Style = "Mode=" & mode& ";"& diagramLink.Style
    end if
    'set the tree value
    if treeIndex >= 0 then
        if len(tree) > 0 then
            styleparts(treeIndex) = "TREE=" & tree
            diagramLink.Style = join(styleparts,";")
        else
            'remove tree part
            diagramLink.Style = replace(diagramLink.Style,styleparts(treeIndex)&";" , "")
        end if
    else
        diagramLink.Style = diagramLink.Style & "TREE=" & tree & ";"
    end if
end function