The dynamic simulation implementation of C # for two times of Visio development

Source: Internet
Author: User
Tags foreach requires sleep

Visio Two development can achieve a lot of project scenarios, such as electrical circuit analysis, distribution network analysis, flowchart, etc., now because of the project needs, but also know more than one application, hydraulic transmission simulation. The project effect diagram looks like this:


View artwork (larger)

Dynamic simulation, in fact, is to simulate the actual line of the trend, to achieve the display of animation. My Visio projects were mostly based on static graphics, and there was not much dynamic presentation. The original distribution network power line analysis, strictly also static, because basically is a one-time power and no power lines to draw out. and dynamic simulation requires the slow animation to show the direction of the line and color changes.

such as the piston motion simulation, requires not to stop the animation of the case, can be the cycle of changes. 


This article describes how to achieve line progression, color changes, and dynamic simulation of specific graphics such as pistons.

First of all, to realize the dynamic simulation effect, we must first analyze the topology network order and level of the whole drawing, so that we can know the correct line direction and the order of animation changes. For example, in the distribution network circuit diagram, it must be the power supply, through the wire or equipment. Pass the power supply to achieve the continuity of the circuit. In the hydraulic circuit, starting from the fuel tank, after a series of equipment, and finally back to the tank.

To achieve the animation of the above image on a Visio drawing, the most important mystery is to use the following code:


 System.Windows.Forms.Application.DoEvents();
 Thread.Sleep(50);


In many cases, we may not be familiar with the function of this DoEvents function. In fact, we can understand that it is an active trigger event, let the message flow advance into the processing flow, so that we can see the graphics update effect on the Visio drawing.

The entire process of graphical analysis is divided into three steps:

1) Perform a simple topology analysis to maintain the relationship around the device to the database for analysis.

2) According to the database structure, analyze the device relationship and obtain a list of device hierarchy of the topology network.

3) According to the different equipment types and the current state of the drawings, the equipment is properly drawn and animated.

 

The approximate code is as follows:


Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> private void PowerCutAnalyze(Visio.Application app)
        {
            #region Get the operating device and judge whether the drawing has equipment
            Visio.Shape shapeSelected = null;
            Try
            {
                Visio.Window wndVisio = app.ActiveWindow;
                If (wndVisio.Selection.Count == 1)
                {
                    shapeSelected = wndVisio.Selection.get_Item16(1);
                }
            }
            Catch { ;

            If (!VisioUtility.HasShapeInWindow(VisWindow))
            {
                MessageUtil.ShowWarning("There is no device on the drawing, this operation cannot be performed");
                Return;
            }
            #endregion

            app.UndoEnabled = false;
            List<string> list = new List<string>();
            String message = "";
            List = powerCutBLL.RunPowerCutAnalyzing(app, shapeSelected, ref message);
            app.UndoEnabled = true;

            If (message != "")
            {
                MessageUtil.ShowError(message);
                Return;
            }

            If (list.Count > 0)
            {
                AnalyzeShapeIdList.Clear();
                Foreach (string shapeStrID in list)
                {
                    AnalyzeShapeIdList.Add(Convert.ToInt32(shapeStrID));
                }
                RunColorChanging(app);
            }
            Else
            {
                MessageUtil.ShowWarning("Please check if the line is connected correctly.");
            }
        }



The code for the line color change and the animation display section is as follows:


Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> /// <summary>
        /// According to the analyzed device ID, the device color change animation display
        /// </summary>
        /// <param name="visApp"></param>
        Private void RunColorChanging(Visio.Application visApp)
        {
            Visio.Cell cell = visApp.ActiveDocument.Pages[1].PageSheet.get_Cells("Scratch.A1");
            Int intValue = Convert.ToInt32(VisioUtility.FormulaStringToString(cell.Formula));
            If (intValue == 1)
            {
                cell.Formula = "0";
            }
            Else
            {
                cell.Formula = "1";
                isMovie = !isMovie;
            }
...................

            Int sequence = 1;
            Foreach (int shapeId in AnalyzeShapeIdList)
            {
                Visio.Shape shape = VisDocument.Pages[1].Shapes.get_ItemFromID(shapeId);
                If (shape != null)
                {
                    If (intValue == 0)
                    {
                        shape.Text = sequence++.ToString("D2");//string.Format("{0}({1})", sequence++, shape.ID);//

                        VisioUtility.SetShapeLineColor(shape, VisDefaultColors.visDarkGreen);//Powered (green)
                        System.Windows.Forms.Application.DoEvents();
                        Thread.Sleep(500 * minFlowValue);
                    }
                    Else
                    {
                        shape.Text = "";
                        VisioUtility.SetShapeLineColor(shape, VisDefaultColors.visBlack);//No power (black)
                        System.Windows.Forms.Application.DoEvents();
                    }
                    
                    String equipType = VisioUtility.GetShapeCellValue(shape, "device type");
                    If (!string.IsNullOrEmpty(equipType))
                    {
                        #region Single acting, double acting
                        If (equipType == "single acting" || equipType == "double acting")
                        {
                            String minValue = "Width*0.25";
                            String maxValue = "Width*0.75";
                            String cellName = "Controls.Row_1.X";
                            Try
                            {
                                If (shape.get_CellExistsU(cellName, (short)VisExistsFlags.visExistsAnywhere) != 0)
                                {
                                    Short i = shape.get_CellsRowIndex(cellName);
                                    Visio.Cell typeCell = shape.get_CellsSRC((short)VisSectionIndices.visSectionControls, i, (short)VisCellIndices.visCtlX);
                                    If (intValue == 0)
                                    {
                                        ThreadParameterInfo param = new ThreadParameterInfo();
                                        param.Cell = typeCell;
                                        param.ScratchCell = cell;

                                        Thread thread = new Thread(new ParameterizedThreadStart(HuoSaiMoving));
                                        thread.Start(param);
                                    }
                                    Else
                                    {
                                        typeCell.Formula = VisioUtility.StringToFormulaForString(minValue);
                                        System.Windows.Forms.Application.DoEvents();
                                        //Thread.Sleep(500 * minFlowValue);
                                    }
                                }
                            }
                            Catch (Exception ex)
                            {
                                LogHelper.Error(ex);
                            }
                        }
                        #endregion
                    }
                }
                
            }
        }


Among them, we noticed that when the piston moves, it is processed by a separate thread, as shown below.


Thread thread = new Thread(new ParameterizedThreadStart(HuoSaiMoving));
thread.Start(param);



Piston movement is after the line is connected, and continues to loop for animation display, because it is a separate thread for processing operations, through the judgment of the logo to achieve the animation stop control, the specific processing of the piston animation effect code is as follows:


Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> private void HuoSaiMoving(object obj)
        {
            ThreadParameterInfo objParam = obj as ThreadParameterInfo;
            Visio.Cell scratchCell = objParam.ScratchCell;
            Visio.Cell typeCell = objParam.Cell;
            Int intValue = Convert.ToInt32(VisioUtility.FormulaStringToString(scratchCell.Formula));
            While (intValue == 1 && isMovie)
            {
                String minValue = "Width*0.25";
                String maxValue = "Width*0.75";
                //Visio.Cell typeCell = objCell as Visio.Cell;
                If (typeCell != null)
                {
                    String currentValue = "";
                    //increase
                    For (int k = 1; k <= 10; k++)
                    {
                        currentValue = string.Format("Width*0.25 + Width*{0}", 0.05 * k);
                        typeCell.Formula = VisioUtility.StringToFormulaForString(currentValue);
                        System.Windows.Forms.Application.DoEvents();
                        Thread.Sleep(50);
                    }
                    //cut back
                    For (int k = 1; k <= 10; k++)
                    {
                        currentValue = string.Format("Width*0.75 - Width*{0}", 0.05 * k);
                        typeCell.Formula = VisioUtility.StringToFormulaForString(currentValue);
                        System.Windows.Forms.Application.DoEvents();
                        Thread.Sleep(50);
                    }
                }
                intValue = Convert.ToInt32(VisioUtility.FormulaStringToString(scratchCell.Formula));
            }
        }




Visio application is high and low, the code map is difficult to adjust; not seeking a blockbuster, but seeking subtle.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.