예제 #1
0
 // Add/remove template objects to/from the selected line
 private void buttonEditTemplateSelection_Click(object sender = null, EventArgs e = null)
 {
     // If not a line was selected
     if (!isEditSelection_ && (treeTemplateObjects.SelectedNode == null || treeTemplateObjects.SelectedNode.Nodes.Count == 0))
     {
         return;
     }
     if (isEditSelection_)
     {
         TreeNode line = treeTemplateObjects.Nodes[iSelectedSet_];
         line.Nodes.Clear();
         var               selection     = modelRenderer_.getSelection();
         string            chart         = listBoxTemplateCharts.SelectedItem.ToString();
         List <ISelection> objects       = charts_.selection[chart];
         Lines             selLine       = null;
         int               iSelectedLine = 0;
         bool              isFound       = false;
         foreach (ISelection item in objects)
         {
             selLine = (Lines)item;
             if (selLine.lineName_ == line.Text)
             {
                 isFound = true;
                 break;
             }
             ++iSelectedLine;
         }
         if (isFound == false)
         {
             return;
         }
         if (selection.Count < 1)
         {
             charts_.selection[chart].RemoveAt(iSelectedLine);
             line.Remove();
         }
         else
         {
             selLine.nodeNames_.Clear();
             foreach (string item in selection)
             {
                 line.Nodes.Add(item);
                 selLine.nodeNames_.Add(item);
             }
         }
         iSelectedSet_ = -1;
     }
     else
     {
         iSelectedSet_ = treeTemplateObjects.SelectedNode.Index;
         modelRenderer_.clearSelection();
         // Select all the nodes in the set
         foreach (TreeNode item in treeTemplateObjects.SelectedNode.Nodes)
         {
             string[] selectionInfo = item.Text.Split(selectionDelimiter_);
             modelRenderer_.select(selectionInfo[0], selectionInfo[1], false);
         }
         modelRenderer_.draw();
     }
     // Invert the states of the controls
     buttonAddTemplateObject.Enabled    = isEditSelection_;
     buttonRemoveTemplateObject.Enabled = isEditSelection_;
     listBoxTemplateCharts.Enabled      = isEditSelection_;
     isEditSelection_ = !isEditSelection_;
 }
        // Processing all the data
        private void buttonProcess_Click(object sender = null, EventArgs e = null)
        {
            string templateName      = textBoxNameExcel.Text;
            string templateDirectory = textBoxDirectoryExcel.Text;

            if (String.IsNullOrEmpty(templateName))
            {
                MessageBox.Show("Filename to save is not specified", "Empty filename", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (String.IsNullOrEmpty(templateDirectory))
            {
                MessageBox.Show("Directory to save is not specified", "Empty directory name", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            ExcelObject excelResult = new ExcelObject(excelTemplate_, templateDirectory, templateName);

            // Checking the project, template and selected signals
            if (!project.isProjectOpened() || !excelTemplate_.isOpened())
            {
                return;
            }
            // Resolving dependencies
            resolveDependencies();
            // Retrieve selected frequencies
            int nSingleFrequency = singleFrequencyIndices_.Count;

            ChartPosition.lastRow = 0;
            // Error handling
            errorMessage_ = null;
            int iError = 0;
            // Create a map to convert directions into labels
            Dictionary <ChartDirection, string> mapDirections = new Dictionary <ChartDirection, string>();

            mapDirections[ChartDirection.X] = "X";
            mapDirections[ChartDirection.Y] = "Y";
            mapDirections[ChartDirection.Z] = "Z";
            // Creating series
            double signData      = 1.0;
            string inversionInfo = "No";

            if (checkBoxInverseResults.Checked)
            {
                signData      = -1.0;
                inversionInfo = "Yes";
            }
            // Creating the header
            excelResult.addHeaderInfo($"Date: {DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")}");
            excelResult.addHeaderInfo($"Inversion: {inversionInfo}");
            excelResult.addHeaderInfo($"Template: {templateDirectory + "\\" + templateName + ".xlsx"}");
            ChartPosition.lastRow += 1;
            foreach (string chart in listBoxTemplateCharts.Items)
            {
                // Nodes selection
                List <ISelection> selectedObjects = charts_.selection[chart];
                // Type and direction
                ChartTypes     type      = charts_.type[chart];
                ChartDirection direction = charts_.direction[chart];
                SignalUnits    units     = charts_.units[chart];
                // Error handling
                if (type == ChartTypes.UNKNOWN || selectedObjects.Count == 0)
                {
                    continue;
                }
                if (direction == ChartDirection.UNKNOWN)
                {
                    throwError(ref iError, "The direction for '" + chart + "' is not specified");
                    continue;
                }
                if (units == SignalUnits.UNKNOWN)
                {
                    throwError(ref iError, "The units for '" + chart + "' are not specified");
                    continue;
                }
                // Norm
                double         norm = charts_.normalization[chart];
                ChartDirection axis = charts_.axis[chart];
                int            indX = 0, indY = 1;
                if (charts_.swapAxes[chart])
                {
                    indX = 1;
                    indY = 0;
                }
                string infoUnits = comboBoxTemplateUnits.Items[(int)units].ToString();
                // Frequency response function: real and imaginary parts
                if ((type == ChartTypes.REAL_FRF || type == ChartTypes.IMAG_FRF) && project.signals_.Count != 0)
                {
                    List <string> chartNodes = new List <string>();
                    foreach (ISelection item in selectedObjects)
                    {
                        chartNodes.Add((string)item.retrieveSelection());
                    }
                    int iType = (int)type - 1;
                    foreach (string node in chartNodes) // Node
                    {
                        // Check if there is an appropriate signal
                        if (!project.signals_.ContainsKey(node) || !project.signals_[node].ContainsKey(direction))
                        {
                            throwError(ref iError, "The chosen signals do not contain the node '" + node + "'");
                            continue;
                        }
                        Response response = project.signals_[node][direction][0];
                        // Slice data by the selected index
                        if (!response.data.ContainsKey(units) || response.data[units] == null)
                        {
                            continue;
                        }
                        double[,] refFullData = response.data[units];
                        double[,] data        = new double[nSingleFrequency, 2];
                        int iSelected;
                        for (int i = 0; i != nSingleFrequency; ++i)
                        {
                            iSelected     = singleFrequencyIndices_[i];
                            data[i, indX] = response.frequency[iSelected];
                            data[i, indY] = refFullData[iSelected, iType] * signData;
                        }
                        string ptrNode = "т. " + node.Split(selectionDelimiter_)[1];
                        string info    = $" - {mapDirections[direction]} ({infoUnits})";
                        excelResult.addSeries(chart, data, ptrNode, info);
                    }
                }
                // Modeset
                else if (type == ChartTypes.MODESET && project.signals_.Count != 0)
                {
                    if (axis == ChartDirection.UNKNOWN)
                    {
                        throwError(ref iError, "The coordinate axis for '" + chart + "' is not specified");
                        continue;
                    }
                    if (indexSingleResonanceFrequency_ < 0)
                    {
                        throwError(ref iError, "The resonance frequency is not chosen");
                        continue;
                    }
                    // For each line
                    foreach (ISelection item in selectedObjects)
                    {
                        Lines         currentLine        = (Lines)item;
                        string        nameLine           = currentLine.lineName_;
                        List <string> lineNodes          = (List <string>)currentLine.retrieveSelection();
                        List <double> coordinates        = new List <double>();
                        List <double> values             = new List <double>();
                        double        resonanceFrequency = -1.0;
                        foreach (string node in lineNodes)
                        {
                            // Check if there is an appropriate signal
                            if (!project.signals_.ContainsKey(node) || !project.signals_[node].ContainsKey(direction))
                            {
                                throwError(ref iError, "The chosen signals do not contain the node '" + node + "'");
                                continue;
                            }
                            // Retreiving the coordinate along the choosen axis
                            string[] selectionInfo = node.Split(selectionDelimiter_);
                            uint     indNode       = modelRenderer_.componentSet_.mapNodeNames[selectionInfo[0]][selectionInfo[1]];
                            double[,] componentCoordinates = (double[, ])modelRenderer_.componentSet_.nodeCoordinates[selectionInfo[0]];
                            int tInd = (int)axis - 1;
                            coordinates.Add(componentCoordinates[indNode, tInd]);
                            // Retrieving the function value
                            Response response = project.signals_[node][direction][0];
                            if (!response.data.ContainsKey(units) || response.data[units] == null)
                            {
                                continue;
                            }
                            double[,] refFullData = response.data[units];
                            values.Add(refFullData[indexSingleResonanceFrequency_, 1]); // Imaginary part of the signal
                            if (resonanceFrequency < 0)
                            {
                                resonanceFrequency = response.frequency[indexSingleResonanceFrequency_];
                            }
                        }
                        if (values.Count > 0)
                        {
                            int nNodes = coordinates.Count;
                            double[,] data = new double[nNodes, 2];
                            for (int i = 0; i != nNodes; ++i)
                            {
                                data[i, indX] = coordinates[i] / norm;
                                data[i, indY] = values[i] * signData;
                            }
                            string frequencyInfo = null;
                            if (resonanceFrequency > 0)
                            {
                                frequencyInfo = $" - {resonanceFrequency.ToString("F3", CultureInfo.InvariantCulture)} Гц ({infoUnits})";
                            }
                            excelResult.addSeries(chart, data, nameLine, frequencyInfo);
                        }
                    }
                }
                // Multi-FRF
                else if ((type == ChartTypes.MULTI_REAL_FRF || type == ChartTypes.MULTI_IMAG_FRF) && project.multiSignals_.Count != 0)
                {
                    List <string> chartNodes = new List <string>();
                    int           iType      = 0;
                    if (type == ChartTypes.MULTI_IMAG_FRF)
                    {
                        iType = 1;
                    }
                    foreach (ISelection item in selectedObjects)
                    {
                        chartNodes.Add((string)item.retrieveSelection());
                    }
                    foreach (string node in chartNodes) // Node
                    {
                        // Check if there is an appropriate signal
                        if (!project.multiSignals_.ContainsKey(node) || !project.multiSignals_[node].ContainsKey(direction))
                        {
                            throwError(ref iError, "The chosen signals do not contain the node '" + node + "'");
                            continue;
                        }
                        var dirNodeSignals = project.multiSignals_[node][direction];
                        foreach (Response response in dirNodeSignals)
                        {
                            // Slice data by the selected index
                            if (!response.data.ContainsKey(units) || response.data[units] == null)
                            {
                                continue;
                            }
                            double[,] refFullData = response.data[units];
                            List <int> indices  = multiFrequencyIndices_[mapResponses_[response.path]];
                            int        nIndices = indices.Count;
                            double[,] data = new double[nIndices, 2];
                            int iSelected;
                            for (int i = 0; i != nIndices; ++i)
                            {
                                iSelected     = indices[i];
                                data[i, indX] = response.frequency[iSelected];
                                data[i, indY] = refFullData[iSelected, iType] * signData;
                            }
                            // Retrieving force value
                            if (data.GetLength(0) > 0)
                            {
                                string force     = $"𝐹 = {getForceValue(response.path)} Н";
                                string infoChart = $" - {mapDirections[direction]} ({infoUnits})";
                                string infoNode  = $"{node}";
                                excelResult.addSeries(chart, data, force, infoChart, infoNode);
                            }
                        }
                    }
                }
                // Frequency function
                else if ((type == ChartTypes.REAL_FREQUENCY || type == ChartTypes.IMAG_FREQUENCY) && project.multiSignals_.Count != 0)
                {
                    List <string> chartNodes = new List <string>();
                    foreach (ISelection item in selectedObjects)
                    {
                        chartNodes.Add((string)item.retrieveSelection());
                    }
                    foreach (string node in chartNodes) // Node
                    {
                        // Check if there is an appropriate signal
                        if (!project.multiSignals_.ContainsKey(node) || !project.multiSignals_[node].ContainsKey(direction))
                        {
                            throwError(ref iError, "The chosen signals do not contain the node '" + node + "'");
                            continue;
                        }
                        var dirNodeSignals = project.multiSignals_[node][direction];
                        double[,] data = new double[dirNodeSignals.Count, 2];
                        int k = 0;
                        foreach (Response response in dirNodeSignals)
                        {
                            if (!response.data.ContainsKey(units) || response.data[units] == null)
                            {
                                continue;
                            }
                            string label = mapResponses_[response.path];
                            int    indexMultiResonance  = multiResonanceFrequencyIndices_[label];
                            double resonanceFrequency   = multiFrequency_[label][indexMultiResonance];
                            Tuple <double, double> pair = response.evaluateResonanceFrequency(type, units, resonanceFrequency);
                            if (pair != null)
                            {
                                data[k, indX] = pair.Item1;
                                data[k, indY] = pair.Item2;
                            }
                            else
                            {
                                double realPart = response.data[units][indexMultiResonance, 0];
                                double imagPart = response.data[units][indexMultiResonance, 1];
                                data[k, indX] = resonanceFrequency;
                                data[k, indY] = Math.Sqrt(Math.Pow(realPart, 2.0) + Math.Pow(imagPart, 2.0));
                            }
                            ++k;
                        }
                        if (data.GetLength(0) > 0)
                        {
                            string ptrNode   = "т. " + node.Split(selectionDelimiter_)[1];
                            string infoChart = $" - {mapDirections[direction]} ({infoUnits})";
                            excelResult.addSeries(chart, data, ptrNode, infoChart);
                        }
                    }
                }
            }
            ChartPosition.lastRow = 0;
            // Saving and opening the results
            excelResult.save();
            excelResult.open();
            if (iError == 0)
            {
                setStatus("The results were successfully processed");
            }
            else
            {
                showErrors(iError);
            }
        }
        public void read(string fileName, Func <string, string, bool> checkNode, char nodeNameDelimiter, ref int indAvailableLine)
        {
            indAvailableLine = 0;
            // Reading another instance
            IFormatter formatter = new BinaryFormatter();

            using (Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                ChartsData    another       = (ChartsData)formatter.Deserialize(stream);
                List <string> anotherCharts = new List <string>(another.type.Keys);
                // Copying fields
                string[] selectionInfo;
                bool     isLineCorrect;
                foreach (string chart in anotherCharts)
                {
                    if (!type.ContainsKey(chart))
                    {
                        continue;
                    }
                    // Properties
                    type[chart]          = another.type[chart];
                    units[chart]         = another.units[chart];
                    direction[chart]     = another.direction[chart];
                    normalization[chart] = another.normalization[chart];
                    axis[chart]          = another.axis[chart];
                    swapAxes[chart]      = another.swapAxes[chart];
                    dependency[chart]    = another.dependency[chart];
                    // Selection
                    List <ISelection> checkedSelection = new List <ISelection>();
                    foreach (ISelection item in another.selection[chart])
                    {
                        // Nodes
                        if (item.GetType() == typeof(Nodes))
                        {
                            string fullName = (string)item.retrieveSelection();
                            selectionInfo = fullName.Split(nodeNameDelimiter);
                            if (checkNode(selectionInfo[0], selectionInfo[1]))
                            {
                                checkedSelection.Add(item);
                            }
                        }
                        // Lines
                        if (item.GetType() == typeof(Lines))
                        {
                            List <string> nodes = (List <string>)item.retrieveSelection();
                            isLineCorrect = true;
                            Lines line    = (Lines)item;
                            int   indLine = int.Parse(line.lineName_.Replace("Line", ""));
                            if (indLine > indAvailableLine)
                            {
                                indAvailableLine = indLine;
                            }
                            foreach (string fullName in nodes)
                            {
                                selectionInfo = fullName.Split(nodeNameDelimiter);
                                // If the current geometry contains all the specified nodes
                                if (!checkNode(selectionInfo[0], selectionInfo[1]))
                                {
                                    isLineCorrect = false;
                                    break;
                                }
                            }
                            if (isLineCorrect)
                            {
                                checkedSelection.Add(item);
                            }
                        }
                    }
                    selection[chart] = checkedSelection;
                }
            }
            if (indAvailableLine > 0)
            {
                indAvailableLine += 1;
            }
        }
예제 #4
0
        // -- Template  --

        private void listBoxTemplateCharts_SelectedIndexChanged(object sender = null, EventArgs e = null)
        {
            string     chart = listBoxTemplateCharts.SelectedItem.ToString();
            ChartTypes type  = charts_.type[chart];

            // Copy template objects
            if (!buttonCopyTemplateObjects.Enabled && buttonCopyTemplateObjects.Tag != null)
            {
                buttonCopyTemplateObjects.Enabled = true;
                string baseChart = buttonCopyTemplateObjects.Tag.ToString();
                buttonCopyTemplateObjects.Tag = null;
                if (baseChart.Equals(chart))
                {
                    return;
                }
                listBoxTemplateCharts.SelectedItem = baseChart;
                ChartTypes baseType = charts_.type[baseChart];
                if (charts_.selection[baseChart].Count == 0 || baseType == ChartTypes.UNKNOWN || type == ChartTypes.UNKNOWN)
                {
                    return;
                }
                convertSelection(chart, baseChart, type, baseType);
                type  = baseType;
                chart = baseChart;
            }
            string masterChart = charts_.dependency[chart];

            if (masterChart == null)
            {
                masterChart = chart;
            }
            comboBoxTemplateType.SelectedIndex      = (int)type;
            comboBoxTemplateUnits.SelectedIndex     = (int)charts_.units[masterChart];
            comboBoxTemplateDirection.SelectedIndex = (int)charts_.direction[masterChart];
            comboBoxTemplateAxis.SelectedIndex      = (int)charts_.axis[masterChart];
            numericTemplateNormalization.Value      = (decimal)charts_.normalization[masterChart];
            checkBoxSwapAxes.Checked = charts_.swapAxes[masterChart];
            treeTemplateObjects.Nodes.Clear();
            // Check if the type is defined
            if (type == ChartTypes.UNKNOWN)
            {
                return;
            }
            // Check if the selected signal is dependent
            setDependencyEnabled();
            if (charts_.dependency[chart] != null)
            {
                return;
            }
            // Nodes
            if (isNodeType(type))
            {
                List <ISelection> objects = charts_.selection[chart];
                foreach (ISelection item in objects)
                {
                    treeTemplateObjects.Nodes.Add((string)item.retrieveSelection());
                }
            }
            // Lines
            if (isLineType(type))
            {
                List <ISelection> objects = charts_.selection[chart];
                foreach (ISelection item in objects)
                {
                    Lines    line   = (Lines)item;
                    TreeNode parent = treeTemplateObjects.Nodes.Add(line.lineName_);
                    foreach (string node in line.nodeNames_)
                    {
                        parent.Nodes.Add(node);
                    }
                }
            }
        }