public static EA.Diagram CreateDiagram(EA.Package package, string diagramName, string diagramType)
 {
     EA.Diagram diagram = package.Diagrams.AddNew(diagramName, diagramType);
     diagram.ShowDetails = 0;
     diagram.Update();
     package.Update();
     return diagram;
 }
        public static bool ActivityIsSimple = true; // Create Activity from function in the simple form
        public static bool CreateDefaultElementsForActivity(EA.Repository rep, 
                                EA.Diagram dia, EA.Element act)
        {
            // create init node
            CreateInitFinalNode(rep, dia, act, 100, @"l=350;r=370;t=70;b=90;");
            act.Elements.Refresh();
            dia.DiagramObjects.Refresh();
            dia.Update();
            rep.ReloadDiagram(dia.DiagramID);

            return true;
        }
 public static bool UpdateAttribute(EA.Repository rep, EA.Attribute a)
 {
     // no classifier defined
     if (a.ClassifierID == 0)
     {
         // find type from type_name
         int id = GetTypeId(rep, a.Type);
         if (id > 0)
         {
             a.ClassifierID = id;
             bool error = a.Update();
             if (!error)
             {
                 MessageBox.Show(@"Error write Attribute", a.GetLastError());
                 return false;
             }
         }
     }
     return true;
 }
        public static void ChangeClassNameToSynonyms(EA.Repository rep, EA.Element el)
        {
            if (el.Type.Equals("Class"))
            {
                // check if property 'Syynonym' exists
                foreach (EA.TaggedValue tag in el.TaggedValues)
                {
                    if (tag.Name == "typeSynonyms")
                    {
                        if (tag.Value != el.Name)
                        {
                            el.Name = tag.Value;
                            el.Update();
                            break;
                        }
                    }
                }
                foreach (EA.Element elNested in el.Elements)
                {
                    ChangeClassNameToSynonyms(rep, elNested);
                }

            }
        }
        //--------------------------------------------------------------------------------------------------------------
        // setLineStyleForLink  Set line style for a digram link
        //--------------------------------------------------------------------------------------------------------------
        // linestyle
        // LH = "Line Style: Lateral Horizontal";
        // LV = "Line Style: Lateral Vertical";
        // TH  = "Line Style: Tree Horizontal";
        // TV = "Line Style: Tree Vertical";
        // OS = "Line Style: Orthogonal Square";
        // OR =              Orthogonal Round
        // A =               Automatic
        // D =               Direct
        // C =               Customer
        // B =               Bezier
        // NO=               make nothing
        public static void SetLineStyleForDiagramLink(string lineStyle, EA.DiagramLink link)
        {
            #pragma warning disable RECS0012
            lineStyle = lineStyle + "  "; 
            if (lineStyle.Substring(0, 2).ToUpper() == "NO") return;
            if (lineStyle.Substring(0,2) == "TH") lineStyle = "H ";
            if (lineStyle.Substring(0,2) == "TV") lineStyle = "V ";
            if (lineStyle.Substring(0,1) == "D") link.Style = "Mode=1;EOID=A36C0F5C;SOID=3ECFB522;Color=-1;LWidth=0;";
            else if (lineStyle.Substring(0,1) == "A") link.Style = "Mode=2;EOID=A36C0F5C;SOID=3ECFB522;Color=-1;LWidth=0;";
            else if (lineStyle.Substring(0,1) == "C") link.Style = "Mode=3;EOID=A36C0F5C;SOID=3ECFB522;Color=-1;LWidth=0;";
            else if (lineStyle.Substring(0,1) == "B") link.Style = "Mode=8;EOID=61B36ED5;SOID=08967F1E;Color=-1;LWidth=0;";
            else
            {
            link.Style = "Mode=3;EOID=A36C0F5C;SOID=3ECFB522;Color=-1;LWidth=0;TREE=" +
                                    lineStyle.Trim() + ";";

            }
            link.Update();
            #pragma warning restore RECS0012
        }
        public static void VisualizePortForDiagramobject(int pos, EA.Diagram dia, EA.DiagramObject diaObjSource, EA.Element port, 
            EA.Element interf, string portBoundTo = "right")
        {
            // check if port already exists
            foreach (EA.DiagramObject diaObjPort in dia.DiagramObjects)
            {
                if (diaObjPort.ElementID == port.ElementID) return;
            }

            // visualize ports
            int length = 12;
            int leftPort;
            int rightPort;
            // calculate target position of port
            if (portBoundTo == "right" || portBoundTo == "")
            {
                leftPort = diaObjSource.right - length / 2;
                rightPort = leftPort + length;
            }
            else
            {
                leftPort = diaObjSource.left - length / 2;
                rightPort = leftPort + length;

            }

            int top = diaObjSource.top;
                       

            int topPort = top - 35 - pos * 20 ;
            int bottomPort = topPort - length;

            // diagram object can't host port (not tall enough)
            // make diagram object taller to host all ports
            if (bottomPort <= diaObjSource.bottom)
            {
                diaObjSource.bottom = diaObjSource.bottom - 30; 
                diaObjSource.Update();
            }

            string position = "l=" + leftPort + ";r=" + rightPort + ";t=" + topPort + ";b=" + bottomPort + ";";
            var diaObjectPort = (EA.DiagramObject)dia.DiagramObjects.AddNew(position, "");
            if (port.Type.Equals("Port"))
            {
                // not showing label
                //diaObject.Style = "LBL=CX=97:CY=13:OX=45:OY=0:HDN=1:BLD=0:ITA=0:UND=0:CLR=-1:ALN=0:ALT=0:ROT=0;";
                diaObjectPort.Style = "LBL=CX=200:CY=12:OX=23:OY=1:HDN=0:BLD=0:ITA=0:UND=0:CLR=-1:ALN=0:ALT=0:ROT=0;";
            }
            else
            {

                // not showing label
                diaObjectPort.Style = "LBL=CX=97:CY=13:OX=39:OY=3:HDN=0:BLD=0:ITA=0:UND=0:CLR=-1:ALN=0:ALT=0:ROT=0;";
            }
            diaObjectPort.ElementID = port.ElementID;


            diaObjectPort.Update();

            if (interf == null) return;

            // visualize interface
            var diaObject2 = (EA.DiagramObject)dia.DiagramObjects.AddNew(position, "");
            dia.Update();
            diaObject2.Style = "LBL=CX=69:CY=13:OX=45:OY=0:HDN=0:BLD=0:ITA=0:UND=0:CLR=-1:ALN=0:ALT=0:ROT=0;";
            diaObject2.ElementID = interf.ElementID;
            diaObject2.Update();

        }
        public static void VisualizePortForDiagramobject(int pos, EA.Diagram dia, EA.DiagramObject diaObjSource, EA.Element port, EA.Element interf)
        {
            // check if port already exists
            foreach (EA.DiagramObject diaObj in dia.DiagramObjects)
            {
                if (diaObj.ElementID == port.ElementID) return;
            }

            // visualize ports
            int length = 6;
            // calculate target position
            int left = diaObjSource.right - length / 2;
            int right = left + length;
            int top = diaObjSource.top;

            top = top - 10 - pos * 10;
            int bottom = top - length;
            string position = "l=" + left + ";r=" + right + ";t=" + top + ";b=" + bottom + ";";
            var diaObject = (EA.DiagramObject)dia.DiagramObjects.AddNew(position, "");
            dia.Update();
            if (port.Type.Equals("Port"))
            {
                // not showing label
                diaObject.Style = "LBL=CX=97:CY=13:OX=0:OY=0:HDN=1:BLD=0:ITA=0:UND=0:CLR=-1:ALN=0:ALT=0:ROT=0;";
            }
            else
            {

                // not showing label
                diaObject.Style = "LBL=CX=97:CY=13:OX=39:OY=3:HDN=0:BLD=0:ITA=0:UND=0:CLR=-1:ALN=0:ALT=0:ROT=0;";
            }
            diaObject.ElementID = port.ElementID;


            diaObject.Update();

            if (interf == null) return;

            // visualize interface
            var diaObject2 = (EA.DiagramObject)dia.DiagramObjects.AddNew(position, "");
            dia.Update();
            diaObject.Style = "LBL=CX=69:CY=13:OX=-69:OY=0:HDN=0:BLD=0:ITA=0:UND=0:CLR=-1:ALN=0:ALT=0:ROT=0;";
            diaObject2.ElementID = interf.ElementID;
            diaObject2.Update();

        }
        //-------------------------------------------------------------------------------------------------
        // get Parameter from operation
        // visualize them on diagram / activity
        //-------------------------------------------------------------------------------------------------
        public static bool UpdateParameterFromOperation(EA.Repository rep, EA.Element act, EA.Method m)
        {
            if (m == null) return false;
            if (act.Locked) return false;
            if (!act.Type.Equals("Activity")) return false;

            EA.Element parTrgt = null;


            ///////////////////////////////////////////////////////////////////////////////////
            // return code
            string parName = "Return";
            int methodReturnTypId;

            // is type defined?
            if ((m.ClassifierID != "0") & (m.ClassifierID != ""))
            {
                methodReturnTypId = Convert.ToInt32(m.ClassifierID);
            }
            
            // type is only defined as text
            else
            {
                methodReturnTypId = Convert.ToInt32(Util.GetTypeId(rep, m.ReturnType));
            }

            bool withActivityReturnParameter = false;
            if (withActivityReturnParameter)
            {
                parTrgt.ClassifierID = methodReturnTypId;
                // create an return Parameter for Activity (in fact an element with properties)
                parTrgt = Util.GetParameterFromActivity(rep, null, act, true);
                if (parTrgt == null)
                {
                    parTrgt = (EA.Element)act.EmbeddedElements.AddNew(parName, "Parameter");
                }
                else { parTrgt.Name = parName; }


                parTrgt.Alias = "return:" + m.ReturnType;
                parTrgt.ClassifierID = parTrgt.ClassifierID;

                parTrgt.Update();
                // update properties for return value
                var par = new Param(rep, parTrgt);
                par.SetParameterProperties("direction", "out");
                par.Save();
                par = null;
            }
            // returnType for activity
            act.ClassfierID = methodReturnTypId;
            act.Name = m.Name;

            // use stereotype of operation as stereotype for activity
            act.StereotypeEx = m.StereotypeEx;
            act.Update();
            act.EmbeddedElements.Refresh();

            // over all parameters
            string guids = "";
            foreach (EA.Parameter parSrc in m.Parameters)
            {
                // create an Parameter for Activity (in fact an element with properties)
                // - New if the parameter don't exists
                // - Update if the parameter exists
                // -- Update according to the parameter position

                //string direction = " [" + parSrc.Kind + "]";
                string direction = "";
                string prefixTyp = "";
                if (parSrc.IsConst) prefixTyp = " const";
                var postfixName = "";
                if (parSrc.Kind.Contains("out")) postfixName = "*";
                parName = parSrc.Position + ":" + parSrc.Name + postfixName + prefixTyp + direction;

                // check if parameter already exists (last parameter = false)
                parTrgt = Util.GetParameterFromActivity(rep, parSrc, act);



                // parameter doesn't exists
                if (parTrgt == null)
                {
                    parTrgt = (EA.Element)act.EmbeddedElements.AddNew(parName, "Parameter");
                }
                else
                {
                    parTrgt.Name = parName;
                }
                guids = guids + parTrgt.ElementGUID;

                // is type defined?
                if ((parSrc.ClassifierID != "0") & (parSrc.ClassifierID != ""))
                {
                    parTrgt.ClassifierID = Convert.ToInt32(parSrc.ClassifierID);
                }
                // type is only defined as text
                else
                {   // try to find classifier
                    parTrgt.ClassifierID = Convert.ToInt32(Util.GetTypeId(rep, parSrc.Type));
                    // use type in name (no classifier found)
                    if (parTrgt.ClassifierID == 0) parTrgt.Name = parTrgt.Name + ":" + parSrc.Type;
                }

                parTrgt.Notes = parSrc.Notes;
                parTrgt.Alias = "par_" + parSrc.Position + ":" + parSrc.Type;

                // update properties for parameter
                var par = new Param(rep, parTrgt);
                par.SetParameterProperties("direction", parSrc.Kind);
                if (parSrc.IsConst)  par.SetParameterProperties("constant", "true");
                par.Save();
                parTrgt.Update();
               


            }
            act.EmbeddedElements.Refresh();
            // delete all unused parameter
            for (short i = (short)(act.EmbeddedElements.Count - 1); i >= 0; --i)
            {
                var embeddedEl = (EA.Element)act.EmbeddedElements.GetAt(i);
                if (embeddedEl.Type.Equals("ActivityParameter"))
                {
                    if (! (guids.Contains(embeddedEl.ElementGUID) )) {
                        act.EmbeddedElements.Delete(i);
                        }
                }
            }
            act.EmbeddedElements.Refresh();

            return true;
        }
        //------------------------------------------------------------------------------
        // Create default Elements for Statemachine
        //------------------------------------------------------------------------------
        //
        // init
        // state 'State1'
        // final
        // transition from init to 'State1'

        public static bool CreateDefaultElementsForStateDiagram(EA.Repository rep, EA.Diagram dia, EA.Element stateChart)
        {

            // check if init and final node are available
            bool init = false;
            bool final = false;
            foreach (EA.Element node in stateChart.Elements)
            {
                init |= node.Type == "StateNode" & node.Subtype == 100;
                final |= node.Type == "StateNode" & node.Subtype == 101;
            }
            EA.Element initNode = null;
            if (!init)
            {
                initNode = (EA.Element)stateChart.Elements.AddNew("", "StateNode");
                initNode.Subtype = 3;
                initNode.ParentID = stateChart.ElementID;
                initNode.Update();
                if (dia != null)
                {
                    Util.AddSequenceNumber(rep, dia);
                    var initDiaNode = (EA.DiagramObject)dia.DiagramObjects.AddNew("l=295;r=315;t=125;b=135;", "");
                    initDiaNode.Sequence = 1;
                    initDiaNode.ElementID = initNode.ElementID;
                    initDiaNode.Update();
                    Util.SetSequenceNumber(rep, dia, initDiaNode, "1");
                }

            }
            EA.Element finalNode = null;
            // create final node
            if (!final)
            {
                finalNode = (EA.Element)stateChart.Elements.AddNew("", "StateNode");
                finalNode.Subtype = 4;
                finalNode.ParentID = stateChart.ElementID;
                finalNode.Update();
                if (dia != null)
                {
                    Util.AddSequenceNumber(rep, dia);
                    var finalDiaNode = (EA.DiagramObject)dia.DiagramObjects.AddNew("l=285;r=305;t=745;b=765;", "");
                    finalDiaNode.Sequence = 1;
                    finalDiaNode.ElementID = finalNode.ElementID;
                    finalDiaNode.Update();
                    Util.SetSequenceNumber(rep, dia, finalDiaNode, "1");
                }
            }
            // create state node
            var stateNode = (EA.Element)stateChart.Elements.AddNew("", "State");
            stateNode.Subtype = 0;// state
            stateNode.Name = "State1";
            stateNode.ParentID = stateChart.ElementID;
            stateNode.Update();
            if (dia != null)
            {
                Util.AddSequenceNumber(rep, dia);
                string pos = "l=300;r=400;t=-400;b=-470";
                var stateDiaNode = (EA.DiagramObject)dia.DiagramObjects.AddNew(pos, "");
                stateDiaNode.Sequence = 1;
                stateDiaNode.ElementID = stateNode.ElementID;
                stateDiaNode.Update();
                Util.SetSequenceNumber(rep, dia, stateDiaNode, "1");

                // draw a transition
                var con = (EA.Connector)finalNode.Connectors.AddNew("", "StateFlow");
                con.SupplierID = stateNode.ElementID;
                con.ClientID = initNode.ElementID;
                con.Update();
                finalNode.Connectors.Refresh();
            }


            stateChart.Elements.Refresh();
            dia.DiagramObjects.Refresh();
            dia.Update();
            rep.ReloadDiagram(dia.DiagramID);

            return true;
        }