Posts Changing Fabrication Item Product List Entries Through .NET
Post
Cancel

Changing Fabrication Item Product List Entries Through .NET

A few months ago there was an occasion where a colleague needed to swap out some clamps that were product listed for a different size. The simplest method would have been to just do a swap in, but the problem with that method was that the clamps had unique spool names and custom data that would be overwritten if a swap in was performed. This made me decide to look into changing a Job Item product list line entry through the Fabrication .NET API. For more information about getting started with Fabrications Items through the .NET API see the post [Accessing Fabrication Items Through .NET]/posts/accessing-fabricaton-items-through-net/.

The code below has the user select some fabrication objects, then displays a dialog with a drop down of all unique Product List names within the user selection. The user then selects a name from the drop down and hits “OK”. The selected items that have that product list name entry are then updated. It should be noted that the items will not be dynamically stretched/relocated like they would if you used “revdesign” to updated items with Design Line, so you would need to go back through and make any adjustments where needed. The benefit would be the fact that unlike Design Line, all of the user imputed data like “Spool Name” will not be overwritten.

Click here to download the AutoCAD/Fabrication 2017 .dll
Load using the “NETLOAD” command, and type “ChangePLName” to start command

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// SDaniels HOB 01.08.2017
using System.Collections.Generic;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.Fabrication;

namespace ChangeProductListName
{
    //This class needed to load the Fabrication .dll
    //See "Accessing Fabrication Items Through .NET" post for more info
    public class MyPlugin : IExtensionApplication
    {
        void IExtensionApplication.Initialize()
        {
            System.Reflection.Assembly.LoadFrom("C:\\Program Files\\Autodesk\\Fabrication 2017\\CADmep\\FabricationAPI.dll");
        }

        void IExtensionApplication.Terminate() { }
    }

    public class MyCommands
    {
        public List<string> plNames = new List<string>();

        [CommandMethod("ChangePLName")]
        public void MyCommand()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            Form1 form1 = new Form1();

            ObjectIdCollection oIdC;

            List<Item> selectedItems = new List<Item>();

            //Get selection based on fabrication parts
            TypedValue[] typArray = new TypedValue[1];
            typArray.SetValue(new TypedValue((int)DxfCode.Start, "MAPS_SOLID"), 0);
            SelectionFilter sf = new SelectionFilter(typArray);
            PromptSelectionResult res = ed.GetSelection();

            //If user presses enter, iterate through selection and compose a list for the drop down of unique Product list names
            if (res.Status == PromptStatus.OK)
            {
                oIdC = new ObjectIdCollection(res.Value.GetObjectIds());
                if (oIdC.Count > 0)
                {
                    try
                    {
                        foreach (ObjectId id in oIdC)
                        {
                            using (Transaction tr = doc.Database.TransactionManager.StartTransaction())
                            {
                                Entity ent = tr.GetObject(id, OpenMode.ForRead) as Entity;
                                if (ent != null)
                                {
                                    Item item = Job.GetFabricationItemFromACADHandle(ent.Handle.ToString());
                                    if (item != null)
                                    {
                                        ItemProductList ipl = item.ProductList as ItemProductList;
                                        if (ipl != null)
                                        {
                                            selectedItems.Add(item);

                                            foreach (ItemProductListDataRow ipdr in ipl.Rows)
                                            {
                                                if (!form1.cmbNames.Items.Contains(ipdr.Name))
                                                {
                                                    form1.cmbNames.Items.Add(ipdr.Name);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch { }
                    
                    Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(null, form1, false);

                    if (form1.ok == 1)
                    {
                        List<Item> itemUpdatedList = new List<Item>();

                        foreach (Item item in selectedItems)
                        {
                            ItemProductList ipl = item.ProductList as ItemProductList;
                            if (ipl != null)
                            {
                                int ct = 0;

                                foreach (ItemProductListDataRow ipdr in ipl.Rows)
                                {
                                    if (ipdr.Name == form1.cmbNames.Text)
                                    {
                                        itemUpdatedList.Add(item);
                                        item.LoadProductListEntry(ct);
                                        item.Update();
                                        break;
                                    }
                                    ct++;
                                }
                            }
                        }
                        //Update the item UI view
                        //Without this, the item won't depict the updated productlist entry unless double clicked on.
                        if (itemUpdatedList.Count > 0)
                            Autodesk.Fabrication.UI.UIApplication.UpdateView(itemUpdatedList);
                    }
                }
            }
        }
    }
}

Thank you for taking the time to read this post and please leave a comment below with any questions or feedback.

This post is licensed under CC BY 4.0 by the author.