private void instance_DoubleClick(NNodeViewEventArgs args)
        {
            UDNGroup shape = args.Node as UDNGroup;

            if (shape == null)
            {
                return;
            }

            MessageBox.Show(shape.Name + "  " + shape.UDPorts.Count, "Instance clicked:~~~", MessageBoxButtons.OK, MessageBoxIcon.None);
            args.Handled = true;

            editPorts = new EditPorts(shape);
            editPorts.Show();
        }
        private void treeView2_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            TreeNode node = treeView2.SelectedNode;

            MessageBox.Show(string.Format("You selected: {0}", node.Text));

            UDNGroup instance = CreateInstance(node.Text, BaseModels[node.Text].Ports, "None");

            nDrawingDocument.ActiveLayer.AddChild(instance);
            instanceList.Add(instance);

            setInstancesPos(instanceList, inPortList, outPortList);

            nDrawingDocument.SizeToContent();

            treeView2.ExpandAll();
        }
        private UDNGroup CreateGlobalPort(string name, PortType type)
        {
            int width  = 10;
            int height = 15;

            UDNGroup group = new UDNGroup();

            group.Name = name;

            NShape port = new NPolygonShape(new NPointF[] { new NPointF(0, 0),
                                                            new NPointF((int)(width * 1.5), 0),
                                                            new NPointF((int)(width * 1.5 + 10), (int)(height / 2)),
                                                            new NPointF((int)(width * 1.5), (int)(height)),
                                                            new NPointF(0, (int)(height)) });

            group.Shapes.AddChild(port);

            port.Name = name;

            port.CreateShapeElements(ShapeElementsMask.Ports);

            NDynamicPort portInner;

            if (type == PortType.IN)
            {
                portInner = new NDynamicPort(new NContentAlignment(50, 0), DynamicPortGlueMode.GlueToContour);
            }
            else
            {
                portInner = new NDynamicPort(new NContentAlignment(-50, 0), DynamicPortGlueMode.GlueToContour);
            }
            portInner.Name = name;
            port.Ports.AddChild(portInner);

            NTextShape nodeName;

            if (type == PortType.IN)
            {
                nodeName = new NTextShape(name, -(name.Length * 8), 0, name.Length * 8, height);
            }
            else
            {
                nodeName = new NTextShape(name, port.Bounds.Width, 0, name.Length * 8, height);
            }
            nodeName.Style.TextStyle           = new NTextStyle();
            nodeName.Style.TextStyle.FontStyle = new NFontStyle(new Font("Arial", 9));
            if (type == PortType.IN)
            {
                nodeName.Style.TextStyle.StringFormatStyle.HorzAlign = Nevron.HorzAlign.Right;
            }
            else
            {
                nodeName.Style.TextStyle.StringFormatStyle.HorzAlign = Nevron.HorzAlign.Left;
            }

            group.Shapes.AddChild(nodeName);

            group.UpdateModelBounds();

            return(group);
        }
        private UDNGroup CreateInstance(string name, List <Port> ports, string id)
        {
            int instanceWidth  = 50;
            int instanceHeight = 50;

            int InputMaxSize  = 10;
            int InputCnt      = 0;
            int OutputMaxSize = 10;
            int OutputCnt     = 0;

            int offsetWidth   = 9;
            int offsetHeight  = 30;
            int widthPadding  = 10;
            int heightPadding = 10;

            int    textWidth  = 30;
            int    textHeight = 15;
            double textOffset = 1.5;

            int curInPtCnt  = 0;
            int curOutPtCnt = 0;

            UDNGroup group = new UDNGroup();

            group.Name       = name;
            group.UDFullName = name;
            group.UDPorts    = ports;

            // find max input/output port size
            for (int i = 0; i < ports.Count; i++)
            {
                if (ports[i].Type == PortType.IN)
                {
                    InputCnt += 1;
                    if (InputMaxSize < ports[i].Name.Length)
                    {
                        InputMaxSize = ports[i].Name.Length;
                    }
                }
                else
                {
                    OutputCnt += 1;
                    if (OutputMaxSize < ports[i].Name.Length)
                    {
                        OutputMaxSize = ports[i].Name.Length;
                    }
                }
            }

            instanceWidth  = (InputMaxSize * offsetWidth) + (OutputMaxSize * offsetWidth) + widthPadding;
            instanceHeight = (InputCnt > OutputCnt ? InputCnt : OutputCnt) * offsetHeight + heightPadding;

            textWidth = instanceWidth;

            // Add Instance
            NRectangleShape node       = new NRectangleShape(0, 0, (int)instanceWidth, (int)instanceHeight);
            NAbilities      protection = node.Protection;

            protection.InplaceEdit = true;
            node.Protection        = protection;
            node.Name = name;

            group.Shapes.AddChild(node);

            NTextShape nodeName = new NTextShape(name, 0, -15, textWidth, textHeight);

            nodeName.Style.TextStyle           = new NTextStyle();
            nodeName.Style.TextStyle.FontStyle = new NFontStyle(new Font("Arial", 9));
            protection             = nodeName.Protection;
            protection.InplaceEdit = true;
            nodeName.Protection    = protection;
            group.Shapes.AddChild(nodeName);

            // Add Port

            for (int i = 0; i < ports.Count; i++)
            {
                NShape port = createPort(ports[i].Name, ports[i].Type);
                protection             = port.Protection;
                protection.InplaceEdit = true;
                port.Protection        = protection;
                group.Shapes.AddChild(port);
                if (ports[i].Type == PortType.IN)
                {
                    curInPtCnt   += 1;
                    port.Location = new NPointF(-port.Bounds.Width / 2, (node.Bounds.Height / (InputCnt + 1)) * curInPtCnt);

                    NTextShape portName = new NTextShape(ports[i].Name,
                                                         port.Bounds.Width / 2, (node.Bounds.Height / (InputCnt + 1)) * curInPtCnt,
                                                         ports[i].Name.Length * 9, (int)(port.Bounds.Height * textOffset));
                    portName.Style.TextStyle           = new NTextStyle();
                    portName.Style.TextStyle.FontStyle = new NFontStyle(new Font("Arial", 9));
                    portName.Style.TextStyle.StringFormatStyle.HorzAlign = Nevron.HorzAlign.Left;
                    protection             = portName.Protection;
                    protection.InplaceEdit = true;
                    portName.Protection    = protection;
                    group.Shapes.AddChild(portName);
                }
                else
                {
                    curOutPtCnt  += 1;
                    port.Location = new NPointF((-port.Bounds.Width / 2) + node.Bounds.Width, (node.Bounds.Height / (OutputCnt + 1)) * curOutPtCnt);

                    NTextShape portName = new NTextShape(ports[i].Name,
                                                         node.Bounds.Width - (port.Bounds.Width / 2) - (ports[i].Name.Length * 9), (node.Bounds.Height / (OutputCnt + 1)) * curOutPtCnt,
                                                         ports[i].Name.Length * 9, (int)(port.Bounds.Height * textOffset));
                    portName.Style.TextStyle           = new NTextStyle();
                    portName.Style.TextStyle.FontStyle = new NFontStyle(new Font("Arial", 9));
                    portName.Style.TextStyle.StringFormatStyle.HorzAlign = Nevron.HorzAlign.Right;
                    protection             = portName.Protection;
                    protection.InplaceEdit = true;
                    portName.Protection    = protection;
                    group.Shapes.AddChild(portName);
                }



                port.CreateShapeElements(ShapeElementsMask.Ports);

                NDynamicPort portInner;
                if (ports[i].Type == PortType.IN)
                {
                    portInner      = new NDynamicPort(new NContentAlignment(-50, 0), DynamicPortGlueMode.GlueToContour);
                    portInner.Name = ports[i].Name;
                }
                else
                {
                    portInner      = new NDynamicPort(new NContentAlignment(50, 0), DynamicPortGlueMode.GlueToContour);
                    portInner.Name = ports[i].Name;
                }
                port.Ports.AddChild(portInner);
            }

            group.UpdateModelBounds();

            return(group);
        }
        private void Update_instance(Module mod)
        {
            Module _module = (Module)mod.ShallowCopy();

            inPortList.Clear();
            outPortList.Clear();
            instanceList.Clear();
            objMDict.Clear();

            if (_module.Name != OutmostModelName)
            {
                _module.Name = string.Copy(ModulePool[_module.Type].Name);
            }

            // draw global port
            for (int i = 0; i < _module.Ports.Count; i++)
            {
                UDNGroup gPort = CreateGlobalPort(_module.Ports[i].Name, _module.Ports[i].Type);
                nDrawingDocument.ActiveLayer.AddChild(gPort);
                if (_module.Ports[i].Type == PortType.IN)
                {
                    inPortList.Add(gPort);
                }
                else
                {
                    outPortList.Add(gPort);
                }
                objMDict[_module.Name, _module.Ports[i].Name] = gPort;
            }


            for (int i = 0; i < _module.Instances.Count; i++)
            {
                string   key      = _module.Instances[i].Name;
                UDNGroup instance = CreateInstance(key, ModulePool[_module.Instances[i].Type].Ports, _module.Instances[i].Id);
                // [!@#$] maybe need Deepcopy of ports
                //instance.UDPorts = ModulePool[_module.Instances[i].Type].Ports;
                nDrawingDocument.ActiveLayer.AddChild(instance);
                instanceList.Add(instance);
                for (int j = 0; j < ModulePool[_module.Instances[i].Type].Ports.Count; j++)
                {
                    objMDict[key, ModulePool[_module.Instances[i].Type].Ports[j].Name] = instance;
                }
                instance.DoubleClick += new NodeViewEventHandler(instance_DoubleClick);
            }

            setInstancesPos(instanceList, inPortList, outPortList);


            NRoutableConnector routableConnector;

            for (int i = 0; i < _module.Instances.Count; i++)
            {
                for (int j = 0; j < _module.Instances[i].Couplings.Count; j++)
                {
                    routableConnector                   = new NRoutableConnector(RoutableConnectorType.DynamicHV, RerouteAutomatically.Always);
                    routableConnector.Name              = "name";
                    routableConnector.StyleSheetName    = NDR.NameConnectorsStyleSheet;
                    routableConnector.Style.StrokeStyle = new NStrokeStyle(1, Color.Blue);
                    nDrawingDocument.ActiveLayer.AddChild(routableConnector);

                    var cou = _module.Instances[i].Couplings[j];

                    var sourIns = objMDict[cou.From, cou.FPort];
                    var destIns = objMDict[cou.To, cou.TPort];

                    routableConnector.StartPlug.Connect(((NShape)(sourIns.Shapes.GetChildByName(cou.FPort, 0))).Ports.GetChildByName(cou.FPort, 0) as NPort);
                    routableConnector.EndPlug.Connect(((NShape)(destIns.Shapes.GetChildByName(cou.TPort, 0))).Ports.GetChildByName(cou.TPort, 0) as NPort);
                    routableConnector.DoubleClick += new NodeViewEventHandler(routableConnector_DoubleClick);
                    routableConnector.Reroute();
                }
            }


            nDrawingDocument.SizeToContent();

            _module = null;
        }
        private void InitDocument()
        {
            // draw global port
            for (int i = 0; i < OutmostModel.Ports.Count; i++)
            {
                UDNGroup gPort = CreateGlobalPort(OutmostModel.Ports[i].Name, OutmostModel.Ports[i].Type);
                nDrawingDocument.ActiveLayer.AddChild(gPort);
                if (OutmostModel.Ports[i].Type == PortType.IN)
                {
                    inPortList.Add(gPort);
                }
                else
                {
                    outPortList.Add(gPort);
                }
                objMDict[OutmostModel.Name, OutmostModel.Ports[i].Name] = gPort;
            }


            for (int i = 0; i < OutmostModel.Instances.Count; i++)
            {
                string   key      = OutmostModel.Instances[i].Name;
                UDNGroup instance = CreateInstance(key, ModulePool[OutmostModel.Instances[i].Type].Ports, OutmostModel.Instances[i].Id);
                nDrawingDocument.ActiveLayer.AddChild(instance);
                instanceList.Add(instance);
                //objDict[key] = instance;
                for (int j = 0; j < ModulePool[OutmostModel.Instances[i].Type].Ports.Count; j++)
                {
                    objMDict[key, ModulePool[OutmostModel.Instances[i].Type].Ports[j].Name] = instance;
                }

                instance.DoubleClick += new NodeViewEventHandler(instance_DoubleClick);
            }

            setInstancesPos(instanceList, inPortList, outPortList);


            NRoutableConnector routableConnector;

            for (int i = 0; i < OutmostModel.Instances.Count; i++)
            {
                for (int j = 0; j < OutmostModel.Instances[i].Couplings.Count; j++)
                {
                    routableConnector = new NRoutableConnector(RoutableConnectorType.DynamicHV, RerouteAutomatically.Always);
                    routableConnector.StyleSheetName    = NDR.NameConnectorsStyleSheet;
                    routableConnector.Style.StrokeStyle = new NStrokeStyle(1, Color.Blue);
                    nDrawingDocument.ActiveLayer.AddChild(routableConnector);

                    var cou = OutmostModel.Instances[i].Couplings[j];

                    var sourIns = objMDict[cou.From, cou.FPort];
                    var destIns = objMDict[cou.To, cou.TPort];

                    routableConnector.StartPlug.Connect(((NShape)(sourIns.Shapes.GetChildByName(cou.FPort, 0))).Ports.GetChildByName(cou.FPort, 0) as NPort);
                    routableConnector.EndPlug.Connect(((NShape)(destIns.Shapes.GetChildByName(cou.TPort, 0))).Ports.GetChildByName(cou.TPort, 0) as NPort);
                    routableConnector.DoubleClick += new NodeViewEventHandler(routableConnector_DoubleClick);

                    routableConnector.Reroute();
                }
            }


            nDrawingDocument.SizeToContent();
        }
 public EditPorts(UDNGroup _shape)
 {
     InitializeComponent();
     ep_shape = _shape;
     EditPorts_init();
 }