Example #1
0
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            int     H          = 0;
            int     W          = 0;
            GObject GContainer = new GObject();
            GObject GToDrag    = new GObject();

            GToDrag = GNetwork.GObjects[CurrObjDragIndx];
            double   d1     = 0;
            double   d2     = 0;
            TimeSpan DTDrag = new TimeSpan();

            DTDrag = DateTime.Now.Subtract(Tdown);
            if ((Dragging == true) && (DTDrag.Milliseconds > DragTimeMin))
            {
                if ((GNetwork.GObjects[CurrObjDragIndx].Type == "Line") &&
                    (GNetwork.FindContainerObject(e.X, e.Y, ref GContainer, true) > -1))
                {
                    //
                    //    What is the point of the line to link ?
                    //    The nearest to (Xdown,Ydown)
                    //
                    d1 = CommFnc.distance(Xdown, Ydown, GToDrag.x1, GToDrag.y1);
                    d2 = CommFnc.distance(Xdown, Ydown, GToDrag.x2, GToDrag.y2);
                    if (d1 <= d2)
                    {
                        GToDrag.x1   = (GContainer.x1 + GContainer.x2) / 2;
                        GToDrag.y1   = (GContainer.y1 + GContainer.y2) / 2;
                        GToDrag.Lnk1 = GContainer.Name;
                    }
                    else
                    {
                        GToDrag.x2   = (GContainer.x1 + GContainer.x2) / 2;
                        GToDrag.y2   = (GContainer.y1 + GContainer.y2) / 2;
                        GToDrag.Lnk2 = GContainer.Name;
                    }
                }
                else
                {
                    W          = GToDrag.x2 - GToDrag.x1;
                    H          = GToDrag.y2 - GToDrag.y1;
                    GToDrag.x1 = e.X;
                    GToDrag.y1 = e.Y;
                    GToDrag.x2 = e.X + W;
                    GToDrag.y2 = e.Y + H;
                    GNetwork.AdjustLinkedTo(GToDrag.Name);
                }
                Cursor.Current = Cursors.Default;
                Dragging       = false;
                this.Refresh();
            }
        }
Example #2
0
        public void MouseObject(MouseEventArgs e)
        {
            int     H          = 0;
            int     W          = 0;
            GObject GContainer = new GObject();
            GObject GToDrag    = new GObject();

            GToDrag = GNetwork.GObjects[CurrObjDragIndx];//取出該元件實體
            double   d1     = 0;
            double   d2     = 0;
            TimeSpan DTDrag = new TimeSpan();

            DTDrag = DateTime.Now.Subtract(Tdown);//計算時間間隔
            if ((Dragging == true) && (DTDrag.Milliseconds > DragTimeMin))
            {
                if ((GNetwork.GObjects[CurrObjDragIndx].Type == "Line") &&
                    (GNetwork.FindContainerObject(e.X, e.Y, ref GContainer, true) > -1))
                {
                    //
                    //    What is the point of the line to link ?
                    //    The nearest to (Xdown,Ydown)
                    //
                    d1 = CommFnc.distance(Xdown, Ydown, GToDrag.x1, GToDrag.y1);
                    d2 = CommFnc.distance(Xdown, Ydown, GToDrag.x2, GToDrag.y2);
                    if (d1 <= d2)
                    {
                        GToDrag.x1   = (GContainer.x1 + GContainer.x2) / 2;
                        GToDrag.y1   = (GContainer.y1 + GContainer.y2) / 2;
                        GToDrag.Lnk1 = GContainer.Name;
                    }
                    else
                    {
                        GToDrag.x2   = (GContainer.x1 + GContainer.x2) / 2;
                        GToDrag.y2   = (GContainer.y1 + GContainer.y2) / 2;
                        GToDrag.Lnk2 = GContainer.Name;
                    }
                }
                else
                {
                    W          = GToDrag.x2 - GToDrag.x1; //移動元件的位置計算
                    H          = GToDrag.y2 - GToDrag.y1; //移動元件的位置計算
                    GToDrag.x1 = e.X;                     //移動元件的位置計算
                    GToDrag.y1 = e.Y;                     //移動元件的位置計算
                    GToDrag.x2 = e.X + W;                 //移動元件的位置計算
                    GToDrag.y2 = e.Y + H;                 //移動元件的位置計算
                    GNetwork.AdjustLinkedTo(GToDrag.Name);
                }

                this.Refresh();//觸發重繪
            }
        }
Example #3
0
        public int FindContainerObject(int X, int Y, ref GObject GContainer, bool NoLineFlag)
        {
            GObject CurrObj;
            bool    inside = false;
            int     retval = -1;
            double  m      = 0;
            double  q      = 0;
            double  d      = 0;
            double  dmax   = 0;
            double  e      = 0;

            for (int i = 0; i < Nobj; i++)
            {
                CurrObj = GObjects[i];
                if (CurrObj.Type != "Line")
                {
                    //
                    //   module of (x2,y2) is always > (x1,y1)
                    //
                    inside = (X >= CurrObj.x1) && (Y >= CurrObj.y1);
                    inside = inside && (X <= CurrObj.x2);
                    inside = inside && (Y <= CurrObj.y2);
                }
                else
                {
                    //
                    //   due to mobile links can be (x2,y2) > or < di (x1,y1)
                    //   in this case we don't consider the container
                    //   but only if we are next to the line
                    //
                    m      = (double)(CurrObj.y2 - CurrObj.y1) / (CurrObj.x2 - CurrObj.x1);
                    q      = (double)CurrObj.y1 - m * CurrObj.x1;
                    d      = System.Math.Abs(Y - m * X - q) / System.Math.Sqrt(1 + m * m);
                    dmax   = CommFnc.module(X, Y);
                    e      = d / dmax;
                    inside = (e < PrcLineDist);
                    if (NoLineFlag == true)
                    {
                        inside = false;
                    }
                }
                if (inside == true)
                {
                    GContainer = CurrObj;
                    retval     = i;
                }
            }
            return(retval);
        }
Example #4
0
        private void saveToFileToolStripMenuItem_Click(object sender, EventArgs e)//儲存檔案
        {
            string SaveFileName = "";
            string ErrSaving    = "";

            SaveFileName = CommFnc.AssignFileToSave();
            if (GNetwork.SaveFile(SaveFileName, ref ErrSaving) == true)
            {
                toolStripStatusLabel1.Text = "File saved.";
            }
            else
            {
                toolStripStatusLabel1.Text = ErrSaving;
            }
        }
Example #5
0
        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string LoadFileName = "";
            string ErrLoading   = "";

            LoadFileName = CommFnc.FindFileToOpen();
            GNetwork.Clear();
            if (GNetwork.LoadFile(LoadFileName, ref ErrLoading) == true)
            {
                toolStripStatusLabel1.Text = "File loaded in memory.";
            }
            else
            {
                toolStripStatusLabel1.Text = ErrLoading;
            }
        }
Example #6
0
        public bool LoadFile(string FileFullPath, ref string sErrFileMsg)
        {
            string  sVariable = "";
            string  sValue    = "";
            string  cFirst    = "";
            int     i         = 0;
            int     iLine     = 0;
            GObject NewGObj   = new GObject();

            sErrFileMsg = "";
            try
            {
                using (StreamReader sr = new StreamReader(FileFullPath))
                {
                    String sLine = "";
                    while (sLine != "end network file.")
                    {
                        sLine = sr.ReadLine();
                        iLine++;
                        if (sLine == "end object.")
                        {
                            sLine = sr.ReadLine();
                            iLine++;
                        }
                        if (sLine == "")
                        {
                            cFirst = "*";
                        }
                        else
                        {
                            cFirst = sLine.Substring(0, 1);
                        }
                        if (cFirst == "*")
                        {
                            // null o *-beginning lines are like comments
                        }
                        else
                        {
                            while ((sLine != "end object.") && (sLine != "end network file."))
                            {
                                CommFnc.RowDecode(sLine, ref sVariable, ref sValue);
                                switch (sVariable)
                                {
                                case "object":
                                    NewGObj.Name = sValue;
                                    break;

                                case "type":
                                    NewGObj.Type = sValue;
                                    break;

                                case "lnk1":
                                    NewGObj.Lnk1 = sValue;
                                    break;

                                case "lnk2":
                                    NewGObj.Lnk2 = sValue;
                                    break;

                                case "x1":
                                    NewGObj.x1 = System.Convert.ToInt32(sValue);
                                    break;

                                case "x2":
                                    NewGObj.x2 = System.Convert.ToInt32(sValue);
                                    break;

                                case "y1":
                                    NewGObj.y1 = System.Convert.ToInt32(sValue);
                                    break;

                                case "y2":
                                    NewGObj.y2 = System.Convert.ToInt32(sValue);
                                    break;

                                default:
                                    break;
                                }
                                sLine = sr.ReadLine();
                                iLine++;
                            }
                            if (sLine == "end object.")
                            {
                                GObjects[i] = new GObject(NewGObj.Name, NewGObj.Type,
                                                          NewGObj.x1, NewGObj.y1, NewGObj.Lnk1, NewGObj.x2, NewGObj.y2, NewGObj.Lnk2);
                                i++;
                                CurrObjIndx = i;
                            }
                        }
                    }
                }
                return(true);
            }
            catch (Exception e)
            {
                sErrFileMsg = "Error reading file : " + e.Message + " line = " + i.ToString() + "\n";
                return(false);
            }
        }