Esempio n. 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();
        }
        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)
            {
            }
        }