Exemple #1
0
        public void RefreshContent()
        {
            _treeView.Nodes.Clear();

            _listViewEx.Items.Clear();

            if (ActiveDocument == null)
            {
                return;
            }

            ActiveDocument.DocumentEvents.OnChangeSelectSet +=
                new DocumentEventsSink_OnChangeSelectSetEventHandler(
                    DocumentEvents_OnChangeSelectSet);

            _listViewEx.Columns[1].Text = "Density (" + MaterialUtils.GetDocDensityUnits(ActiveDocument as Document) + ")";
            _listViewEx.Columns[2].Text = "Volume (" + MaterialUtils.GetDocVolumeUnits(ActiveDocument as Document) + ")";
            _listViewEx.Columns[3].Text = "Mass (" + MaterialUtils.GetDocMassUnits(ActiveDocument as Document) + ")";

            Dictionary <Material, MaterialUtils.MaterialGlobalInfos> materialInfos =
                MaterialUtils.GetMaterialInfos(ActiveDocument);

            if (materialInfos == null)
            {
                System.Windows.Forms.MessageBox.Show(
                    "Unable to parse material information...",
                    "Error parsing materials",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation);

                return;
            }

            TreeNode rootNode = _treeView.Nodes.Add("Assembly", ActiveDocument.DisplayName, 2, 2);

            foreach (Material material in materialInfos.Keys)
            {
                TreeNode materialNode = rootNode.Nodes.Add(material.Name);

                ListViewItem materialItem = _listViewEx.Items.Insert(
                    _listViewEx.Items.Count,
                    material.Name);

                MaterialUtils.MaterialInfos globalInfos = materialInfos[material].GlobalInfos;

                ListViewItem.ListViewSubItem subItem;

                double density = MaterialUtils.GetDocDensity(ActiveDocument as Document, globalInfos.Density);

                subItem     = materialItem.SubItems.Add(MaterialUtils.FormatVisible(density));
                subItem     = materialItem.SubItems.Add(MaterialUtils.FormatVisible(globalInfos.Volume));
                subItem     = materialItem.SubItems.Add(MaterialUtils.FormatVisible(globalInfos.Mass));
                subItem.Tag = globalInfos.DbMass;

                CustomControls.LabeledProgressBar ctrlGlobal =
                    new CustomControls.LabeledProgressBar(globalInfos.MassPercentage);

                _listViewEx.AddEmbeddedControl(ctrlGlobal, 4, materialItem.Index);

                List <ComponentOccurrence> occurrences =
                    materialInfos[material].BreakDownInfos.Keys.ToList <ComponentOccurrence>();

                materialItem.Tag = new ListViewItemTag(occurrences, materialNode);
                materialNode.Tag = new TreeNodeTag(occurrences, materialItem);

                foreach (ComponentOccurrence occurrence in occurrences)
                {
                    TreeNode occNode = materialNode.Nodes.Add(occurrence.Name, occurrence.Name, 3, 3);

                    ListViewItem occItem = _listViewEx.Items.Insert(
                        _listViewEx.Items.Count,
                        material.Name + " [" + occurrence.Name + "]");

                    MaterialUtils.MaterialInfos breakDownInfos =
                        materialInfos[material].BreakDownInfos[occurrence];

                    double dOcc = MaterialUtils.GetDocDensity(
                        ActiveDocument as Document,
                        breakDownInfos.Density);

                    subItem     = occItem.SubItems.Add(MaterialUtils.FormatVisible(dOcc));
                    subItem     = occItem.SubItems.Add(MaterialUtils.FormatVisible(breakDownInfos.Volume));
                    subItem     = occItem.SubItems.Add(MaterialUtils.FormatVisible(breakDownInfos.Mass));
                    subItem.Tag = breakDownInfos.DbMass;

                    CustomControls.LabeledProgressBar ctrlBreak =
                        new CustomControls.LabeledProgressBar(breakDownInfos.MassPercentage);

                    _listViewEx.AddEmbeddedControl(ctrlBreak, 4, occItem.Index);

                    occItem.Tag = new ListViewItemTag(occurrence, occNode);
                    occNode.Tag = new TreeNodeTag(occurrence, occItem);
                }
            }

            foreach (ListViewItem item in _listViewEx.Items)
            {
                item.Remove();
            }

            rootNode.Expand();
        }
Exemple #2
0
        private void ExcelExport()
        {
            try
            {
                SaveFileDialog sfd = new SaveFileDialog();

                sfd.Filter = "Excel Files (*.xls; *xlsx)|*.xlsx;*.xlsx";
                sfd.Title  = "Export to Excel format";

                if (sfd.ShowDialog() == DialogResult.Cancel)
                {
                    return;
                }

                Excel.Application app = new Excel.Application();
                Excel.Workbook    wb  = app.Workbooks.Add(1);
                Excel.Worksheet   ws  = (Excel.Worksheet)wb.Worksheets[1];

                ws.Cells[1, 1] = _listViewEx.Columns[0].Text;
                ws.Cells[1, 2] = _listViewEx.Columns[1].Text;
                ws.Cells[1, 3] = _listViewEx.Columns[2].Text;
                ws.Cells[1, 4] = _listViewEx.Columns[3].Text;
                ws.Cells[1, 5] = _listViewEx.Columns[4].Text;

                TreeNode rootNode = _treeView.Nodes[0];

                int rowIdx = 2;

                foreach (ListViewItem lvi in _listViewEx.Items)
                {
                    ListViewItemTag tag = lvi.Tag as ListViewItemTag;

                    if (tag.TreeNode.Parent == rootNode)
                    {
                        ++rowIdx;
                    }

                    int columIdx = 0;

                    foreach (ListViewItem.ListViewSubItem lvs in lvi.SubItems)
                    {
                        ws.Cells[rowIdx, ++columIdx] = lvs.Text;
                    }

                    CustomControls.LabeledProgressBar ctrl =
                        _listViewEx.GetEmbeddedControl(columIdx, lvi.Index) as
                        CustomControls.LabeledProgressBar;

                    if (ctrl != null)
                    {
                        ws.Cells[rowIdx, ++columIdx] = ctrl.LabelText;
                    }

                    ++rowIdx;
                }

                object objMissing = System.Reflection.Missing.Value;

                wb.SaveAs(sfd.FileName,
                          objMissing, objMissing, objMissing, objMissing, objMissing,
                          Excel.XlSaveAsAccessMode.xlExclusive,
                          objMissing, objMissing, objMissing, objMissing, objMissing);

                wb.Close(objMissing, objMissing, objMissing);

                app.Quit();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(
                    "Error during Excel export: " + ex.Message,
                    "Excel export error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation);
            }
        }
        public async Task RefreshContent()
        {
            try
            {
                _treeView.BeforeExpand +=
                    Handle_TreeView_BeforeExpand;

                _missingMaterials = new List <string>();

                List <Task> tasks = new List <Task>();

                bool connected = await ConnectPLM();

                long workspaceId = 0;

                if (connected)
                {
                    workspaceId = await GetWorkspaceIdAsync();
                }

                _treeView.Nodes.Clear();

                _listViewEx.Items.Clear();

                if (ActiveDocument == null)
                {
                    return;
                }

                ActiveDocument.DocumentEvents.OnChangeSelectSet +=
                    new DocumentEventsSink_OnChangeSelectSetEventHandler(
                        DocumentEvents_OnChangeSelectSet);

                _listViewEx.Columns[1].Text = "Density (" + MaterialUtils.GetDocDensityUnits(ActiveDocument as Document) + ")";
                _listViewEx.Columns[2].Text = "Volume (" + MaterialUtils.GetDocVolumeUnits(ActiveDocument as Document) + ")";
                _listViewEx.Columns[3].Text = "Mass (" + MaterialUtils.GetDocMassUnits(ActiveDocument as Document) + ")";

                Dictionary <Material, MaterialUtils.MaterialGlobalInfos> materialInfos =
                    MaterialUtils.GetMaterialInfos(ActiveDocument);

                if (materialInfos == null)
                {
                    System.Windows.Forms.MessageBox.Show(
                        "Unable to parse material information...",
                        "Error parsing materials",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Exclamation);

                    return;
                }

                TreeNode rootNode = _treeView.Nodes.Add("Assembly", ActiveDocument.DisplayName, 2, 2);

                foreach (Material material in materialInfos.Keys)
                {
                    TreeNode materialNode = rootNode.Nodes.Add(material.Name);

                    ListViewItem materialItem = _listViewEx.Items.Insert(
                        _listViewEx.Items.Count,
                        material.Name);

                    MaterialUtils.MaterialInfos globalInfos = materialInfos[material].GlobalInfos;

                    ListViewItem.ListViewSubItem subItem;

                    double density = MaterialUtils.GetDocDensity(
                        ActiveDocument as Document,
                        globalInfos.Density);

                    subItem     = materialItem.SubItems.Add(MaterialUtils.FormatVisible(density));
                    subItem     = materialItem.SubItems.Add(MaterialUtils.FormatVisible(globalInfos.Volume));
                    subItem     = materialItem.SubItems.Add(MaterialUtils.FormatVisible(globalInfos.Mass));
                    subItem.Tag = globalInfos.DbMass;

                    subItem = materialItem.SubItems.Add("... n/a ...");
                    subItem = materialItem.SubItems.Add("... n/a ...");

                    CustomControls.LabeledProgressBar ctrlGlobal =
                        new CustomControls.LabeledProgressBar(globalInfos.MassPercentage);

                    _listViewEx.AddEmbeddedControl(ctrlGlobal, 6, materialItem.Index);

                    List <ComponentOccurrence> occurrences =
                        materialInfos[material].BreakDownInfos.Keys.ToList <ComponentOccurrence>();

                    materialItem.Tag = new ListViewItemTag(occurrences, materialNode);
                    materialNode.Tag = new TreeNodeTag(occurrences, materialItem);

                    if (connected)
                    {
                        tasks.Add(GetPLMDataAsnyc(workspaceId, material.Name, materialItem));
                    }

                    foreach (ComponentOccurrence occurrence in occurrences)
                    {
                        TreeNode occNode = materialNode.Nodes.Add(
                            occurrence.Name,
                            occurrence.Name, 3, 3);

                        ListViewItem occItem = _listViewEx.Items.Insert(
                            _listViewEx.Items.Count,
                            material.Name + " [" + occurrence.Name + "]");

                        MaterialUtils.MaterialInfos breakDownInfos =
                            materialInfos[material].BreakDownInfos[occurrence];

                        double dOcc = MaterialUtils.GetDocDensity(
                            ActiveDocument as Document,
                            breakDownInfos.Density);

                        subItem     = occItem.SubItems.Add(MaterialUtils.FormatVisible(dOcc));
                        subItem     = occItem.SubItems.Add(MaterialUtils.FormatVisible(breakDownInfos.Volume));
                        subItem     = occItem.SubItems.Add(MaterialUtils.FormatVisible(breakDownInfos.Mass));
                        subItem.Tag = breakDownInfos.DbMass;

                        subItem = occItem.SubItems.Add("... n/a ...");
                        subItem = occItem.SubItems.Add("... n/a ...");

                        CustomControls.LabeledProgressBar ctrlBreak =
                            new CustomControls.LabeledProgressBar(breakDownInfos.MassPercentage);

                        _listViewEx.AddEmbeddedControl(ctrlBreak, 6, occItem.Index);

                        occItem.Tag = new ListViewItemTag(occurrence, occNode);
                        occNode.Tag = new TreeNodeTag(occurrence, occItem);

                        if (connected)
                        {
                            tasks.Add(GetPLMDataAsnyc(workspaceId, material.Name, occItem));
                        }
                    }
                }

                foreach (ListViewItem item in _listViewEx.Items)
                {
                    item.Remove();
                }

                await Task.WhenAll(tasks);

                _treeView.BeforeExpand -=
                    Handle_TreeView_BeforeExpand;

                rootNode.Expand();

                await CreateMissingMaterials(
                    workspaceId,
                    _missingMaterials);
            }
            catch (Exception ex)
            {
            }
        }