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”.

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 through the “NETLOAD” command, and type “ChangePLName” to start command)

// 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() { }
    }

    //Start of Command Class
    public class MyCommands
    {
        //List of product list names
        public List<string> plNames = new List<string>();

        //Start of "Change Product List Name" command
        [CommandMethod("ChangePLName")]
        public void MyCommand()
        {
            //Get document, database, and editor
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            //New windows form instance
            Form1 form1 = new Form1();

            //Object Id Colletion
            ObjectIdCollection oIdC;

            //List of Fabrication Items
            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)
            {
                //Add selection Object IDs to Object ID Collection
                oIdC = new ObjectIdCollection(res.Value.GetObjectIds());
                if (oIdC.Count > 0)
                {
                    try
                    {
                        //iterate through Object Id's
                        foreach (ObjectId id in oIdC)
                        {
                            //Start Transaction
                            using (Transaction tr = doc.Database.TransactionManager.StartTransaction())
                            {
                                //Get entity object
                                Entity ent = tr.GetObject(id, OpenMode.ForRead) as Entity;
                                if (ent != null)
                                {
                                    //Get fabrication job item
                                    Item item = Job.GetFabricationItemFromACADHandle(ent.Handle.ToString());
                                    if (item != null)
                                    {
                                        //Get item's product list object
                                        ItemProductList ipl = item.ProductList as ItemProductList;
                                        if (ipl != null)
                                        {
                                            //If item has product list, add item to list of selected items
                                            selectedItems.Add(item);

                                            //iterate through product list data rows and add to form drop down if unique
                                            foreach (ItemProductListDataRow ipdr in ipl.Rows)
                                            {
                                                if (!form1.cmbNames.Items.Contains(ipdr.Name))
                                                {
                                                    form1.cmbNames.Items.Add(ipdr.Name);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch { }

                    //Show windows form
                    Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(null, form1, false);

                    //If user presses ok, change to product list name specified
                    if (form1.ok == 1)
                    {
                        //List of items to update UI
                        List<Item> itemUpdatedList = new List<Item>();

                        //Iterate through Selected Items with Product List
                        foreach (Item item in selectedItems)
                        {
                            //Get items Product list object
                            ItemProductList ipl = item.ProductList as ItemProductList;
                            if (ipl != null)
                            {
                                //Product list row counter
                                int ct = 0;

                                //Iterate through Product list
                                foreach (ItemProductListDataRow ipdr in ipl.Rows)
                                {
                                    //If the product list line entry name = user selected name,
                                    //load that product list entry and break the loop
                                    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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s