Author Topic: [AUTOCAD MEP] _AECPROPERTYRENUMBERDATA with Fence Selection  (Read 3669 times)

0 Members and 1 Guest are viewing this topic.

jtm2020hyo

  • Newt
  • Posts: 198
Is there a way to use _AECPROPERTYRENUMBERDATA ( propertyrenumberdata ) selecting multiple objects with Fence or similar?



 

Hanauer

  • Mosquito
  • Posts: 10
Re: [AUTOCAD MEP] _AECPROPERTYRENUMBERDATA with Fence Selection
« Reply #1 on: May 14, 2020, 01:06:22 PM »
One way that comes with AutoCAD MEP is unknown to me.
You can implement this through a plug-in and it is relatively easy.
Can reorder in order of selection of objects, by coordinates, in order that objects were inserted in the drawing, reorder one by one ( Implemented by MEP), or other ways. Implement as needed.

jtm2020hyo

  • Newt
  • Posts: 198
Re: [AUTOCAD MEP] _AECPROPERTYRENUMBERDATA with Fence Selection
« Reply #2 on: May 14, 2020, 07:14:47 PM »
One way that comes with AutoCAD MEP is unknown to me.
You can implement this through a plug-in and it is relatively easy.
Can reorder in order of selection of objects, by coordinates, in order that objects were inserted in the drawing, reorder one by one ( Implemented by MEP), or other ways. Implement as needed.


I was thinking in create another property set data with values incremental and another tag, but your method sounds interesting. Might explain us how to reinsert objects or reorde in order of selection of objects, please?

Hanauer

  • Mosquito
  • Posts: 10
Re: [AUTOCAD MEP] _AECPROPERTYRENUMBERDATA with Fence Selection
« Reply #3 on: May 15, 2020, 08:46:26 AM »
Basic sample for manipulate the AutoIncrementDataType of PropertySet using SortedDictionary.

In a windows form dialog:

Quote
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

// AutoCAD References     
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

using AcApp = Autodesk.AutoCAD.ApplicationServices.Application;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcException = Autodesk.AutoCAD.Runtime.Exception;
using ObjectId = Autodesk.AutoCAD.DatabaseServices.ObjectId;
using ObjectIdCollection = Autodesk.AutoCAD.DatabaseServices.ObjectIdCollection;

// AutoCAD ACA/MEP References     
using Autodesk.Aec.DatabaseServices;
using Autodesk.Aec.PropertyData.DatabaseServices;

using AecDb = Autodesk.Aec.DatabaseServices;
using AecPropDb = Autodesk.Aec.PropertyData.DatabaseServices;

namespace Gh.Aec.PropertyData
{
    public partial class GhAecPropertyDataSortForm : Form
    {         
        Document m_doc;
        Database m_db;
        GhAecPropertyDataSortSettings m_settings;

        public GhAecPropertyDataSortForm()
        {
            InitializeComponent();
            m_doc = AcApp.DocumentManager.MdiActiveDocument;           
            m_db = m_doc.Database;
            m_settings = new GhAecPropertyDataSortSettings(m_db);
            m_settings.Read();
            FillCombos();       
        }

        private void btnSort_Click(object sender, EventArgs e)
        {
            SortedDictionary<int, ObjectId> propSetIds = new SortedDictionary<int, ObjectId>();
            string propSetDefName = cmbPropertySet.SelectedItem.ToString();
            string propName = cmbProperty.SelectedItem.ToString();
            this.Hide();
            CollectPropertySetData(propSetIds, propSetDefName, propName);
            Sort(propSetIds, propSetDefName, propName);           
        }

        private void CollectPropertySetData(SortedDictionary<int, ObjectId> propSetIds, string propSetDefName, string propName)
        {
            ObjectId psdId = ObjectId.Null;
            AcDb.DBObject dbobj;
            PropertyValueUnitPair pvup = new PropertyValueUnitPair();
           
            psdId = GhAecPropertySetHelper.GetPropertySetDefinitionIdByName(propSetDefName, m_db);

            using (Transaction tr = m_db.TransactionManager.StartTransaction())
            {
                BlockTable bt = tr.GetObject(m_db.BlockTableId, OpenMode.ForRead, false) as BlockTable;
                BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead, false) as BlockTableRecord;

                foreach (ObjectId id in btr)
                {
                    try
                    {
                        dbobj = tr.GetObject(id, OpenMode.ForRead, false, false);
                        ObjectId psId = PropertyDataServices.GetPropertySet(dbobj, psdId);
                        PropertySet ps = tr.GetObject(psId, OpenMode.ForRead) as PropertySet;
                        pvup = GhAecPropertySetHelper.GetValueFromPropertySetId(propName, psId, m_db);
                        propSetIds.Add((int)pvup.Value, psId);
                    }
                    catch
                    {
                    }
                }
                tr.Commit();
            }
        }

        private void Sort(SortedDictionary<int, ObjectId> propSetIds, string propSetDefName, string propName)
        {
            bool updated = false;           
            int number = (int)nup1.Value;
            int incr = (int)nup2.Value;

            if (propSetIds.Count == 0)
            {
                m_doc.Editor.WriteMessage(String.Format("\nNot found property set name {0} attached to entities in the drawing.", propSetDefName));
                return;
            }

            // the transaction is the undo mark
            // one undo rollback all modifications bellow
            using (Transaction tr = m_db.TransactionManager.StartTransaction())
            {
                foreach (KeyValuePair<int, ObjectId> kvp in propSetIds)
                {
                    if (number != kvp.Key)                       
                        updated = GhAecPropertySetHelper.SetValueFromPropertySetId(propName, kvp.Value, number, tr);
                    number = number + incr;
                }
                tr.Commit();
            }           
        }

        private void FillCombos()
        {           
            List<string> lst = new List<string>();
            lst = GhAecPropertySetHelper.GetPropertySetDefinitionNamesWithAutoIncrementDataType(m_db);
            foreach (string name in lst)
                cmbPropertySet.Items.Add(name);
            if (cmbPropertySet.Items.Count > 0)
            {               
                if (m_settings.HasSettings())
                {
                   int index = cmbPropertySet.FindString(m_settings.PropertySetDefinitionName);
                   if (index > 0)
                       cmbPropertySet.SelectedIndex = index;
                   else
                       cmbPropertySet.SelectedIndex = 0;
                }
                else
                {
                    cmbPropertySet.SelectedIndex = 0;
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {           
            this.Close();           
        }

        private void cmbPropertySet_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbProperty.SelectedIndex = -1; // deseleciona os itens selecionados. Se tiver item selecionado clear() não remove ele
            cmbProperty.Items.Clear();
           
            List<string> lst = new List<string>();
            lst = GhAecPropertySetHelper.GetAutoIncrementPropertyDefinitionNames(cmbPropertySet.SelectedItem.ToString(), m_db);
            foreach (string name in lst)
                cmbProperty.Items.Add(name);

            if (cmbProperty.Items.Count > 0)
            {                             
                if (m_settings.HasSettings())
                {
                    int index = cmbProperty.FindString(m_settings.PropertyDefinitionAutoIncrementName);
                    if (index > 0)
                        cmbProperty.SelectedIndex = index;
                    else
                        cmbProperty.SelectedIndex = 0;
                }
                else
                {
                    cmbProperty.SelectedIndex = 0;
                }
            }
            if (cmbPropertySet.SelectedIndex >= 0)
                m_settings.PropertySetDefinitionName = cmbPropertySet.SelectedItem.ToString();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            SortedDictionary<int, ObjectId> propSetIds = new SortedDictionary<int, ObjectId>();
            string propSetDefName = cmbPropertySet.SelectedItem.ToString();
            string propName = cmbProperty.SelectedItem.ToString();
            this.Hide();
            CollectPropertySetData(propSetIds, propSetDefName, propName);
            Insert(propSetIds, propSetDefName, propName);           
        }

        private void Insert(SortedDictionary<int, ObjectId> propSetIds, string propSetDefName, string propName)
        {
            bool updated = false;
            int start = (int)nup1.Value;
            int incr = (int)nup2.Value;         

            if (propSetIds.Count == 0)
            {
                m_doc.Editor.WriteMessage(String.Format("\nNot found property set name {0} attached to entities in the drawing.", propSetDefName));
                return;
            }

            if (!propSetIds.ContainsKey(start))
            {
                m_doc.Editor.WriteMessage(String.Format("\nNot found {0} auto incremented integer in the property {1} of property set definition {2}.", start, propName, propSetDefName));
                return;
            }

            // the transaction is the undo mark
            // one undo rollback all modifications bellow
            using (Transaction tr = m_db.TransactionManager.StartTransaction())
            {
                foreach (KeyValuePair<int, ObjectId> kvp in propSetIds)
                {
                    if (kvp.Key >= start)
                        updated = GhAecPropertySetHelper.SetValueFromPropertySetId(propName, kvp.Value, kvp.Key + incr, tr);
                }
                tr.Commit();
            }   
        }

        private void MoveTo(SortedDictionary<int, ObjectId> propSetIds, string propSetDefName, string propName)
        {
            bool updated = false;
            int from = (int)nup1.Value;
            int to = (int)nup2.Value;

            if (propSetIds.Count == 0)
            {
                m_doc.Editor.WriteMessage(String.Format("\nNot found property set name {0} attached to entities in the drawing.", propSetDefName));
                return;
            }

            // the transaction is the undo mark
            // one undo rollback all modifications bellow
            using (Transaction tr = m_db.TransactionManager.StartTransaction())
            {
                updated = GhAecPropertySetHelper.SetValueFromPropertySetId(propName, propSetIds[from], to, tr);               
                while (propSetIds.ContainsKey(to) && to != from )               
                    updated = GhAecPropertySetHelper.SetValueFromPropertySetId(propName, propSetIds[to], ++to, tr);               
                tr.Commit();
            }
        }

        private void Swap(SortedDictionary<int, ObjectId> propSetIds, string propSetDefName, string propName)
        {
            bool updated = false;
            int number = (int)nup1.Value;
            int with = (int)nup2.Value;

            if (propSetIds.Count == 0)
            {
                m_doc.Editor.WriteMessage(String.Format("\nNot found property set name {0} attached to entities in the drawing.", propSetDefName));
                return;
            }

            if (!propSetIds.ContainsKey(number))
            {
                m_doc.Editor.WriteMessage(String.Format("\nNot found {0} auto incremented integer in the property {1} of property set definition {2}.", number, propName, propSetDefName));
                return;
            }

            if (!propSetIds.ContainsKey(with))
            {
                m_doc.Editor.WriteMessage(String.Format("\nNot found {0} auto incremented integer in the property {1} of property set definition {2}.", with, propName, propSetDefName));
                return;
            }

            // the transaction is the undo mark
            // one undo rollback all modifications bellow
            using (Transaction tr = m_db.TransactionManager.StartTransaction())
            {             
                updated = GhAecPropertySetHelper.SetValueFromPropertySetId(propName, propSetIds[number], with, tr);                   
                updated = GhAecPropertySetHelper.SetValueFromPropertySetId(propName, propSetIds[with], number, tr);                             
                tr.Commit();
            }
        }

        private void btnMoveTo_Click(object sender, EventArgs e)
        {
            SortedDictionary<int, ObjectId> propSetIds = new SortedDictionary<int, ObjectId>();
            string propSetDefName = cmbPropertySet.SelectedItem.ToString();
            string propName = cmbProperty.SelectedItem.ToString();
            this.Hide();
            CollectPropertySetData(propSetIds, propSetDefName, propName);
            MoveTo(propSetIds, propSetDefName, propName);
        }   

        private void btnSwap_Click(object sender, EventArgs e)
        {
            SortedDictionary<int, ObjectId> propSetIds = new SortedDictionary<int, ObjectId>();
            string propSetDefName = cmbPropertySet.SelectedItem.ToString();
            string propName = cmbProperty.SelectedItem.ToString();
            this.Hide();
            CollectPropertySetData(propSetIds, propSetDefName, propName);
            Swap(propSetIds, propSetDefName, propName);           
        }

        private void btnInsert_MouseHover(object sender, EventArgs e)
        {
            lbl1.Text = "Number";
            lbl2.Text = "Increment";
            lbl2.Visible = true;
            nup2.Visible = true;
        }
       
        private void btnSort_MouseHover(object sender, EventArgs e)
        {
            lbl1.Text = "Start number";
            lbl2.Visible = false;
            nup2.Visible = false;
        }

        private void btnMoveTo_MouseHover(object sender, EventArgs e)
        {
            lbl1.Text = "From number";
            lbl2.Text = "to number";
            lbl2.Visible = true;
            nup2.Visible = true;
        }       

        private void btnSwap_MouseHover(object sender, EventArgs e)
        {
            lbl1.Text = "Number";
            lbl2.Text = "with number";
            lbl2.Visible = true;
            nup2.Visible = true;
        }

        private void GhAecSortPropertyDataForm_Load(object sender, EventArgs e)
        {
           
        }

        private void cmbProperty_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbProperty.SelectedIndex >= 0)
                m_settings.PropertyDefinitionAutoIncrementName = cmbProperty.SelectedItem.ToString();
        }

        private void GhAecSortPropertyDataForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            m_settings.Write();

        }
    }
}


Helper methods:

Code: [Select]
public static class GhAecPropertySetHelper
{
       public static List<string> GetPropertySetDefinitionNamesWithAutoIncrementDataType(Database db)
       {
           List<string> lst = new List<string>();
           ObjectId psdId = ObjectId.Null;

           using (Transaction tr = db.TransactionManager.StartTransaction())
           {
               DictionaryPropertySetDefinitions psdDict = new DictionaryPropertySetDefinitions(db);
               ObjectIdCollection psdIds = psdDict.Records;

               foreach (ObjectId id in psdIds)
               {
                   PropertySetDefinition psdf = (PropertySetDefinition)tr.GetObject(id, OpenMode.ForRead, false, false);
                   PropertyDefinitionCollection pdc = psdf.Definitions;
                   foreach (PropertyDefinition pd in pdc)
                       if (pd.DataType == Autodesk.Aec.PropertyData.DataType.AutoIncrement)
                       {
                           lst.Add(psdf.Name);
                           break;
                       }
               }
               tr.Commit();
           }
           return lst;
       }

       public static List<string> GetAutoIncrementPropertyDefinitionNames(string propSetDefName, Database db)
       {
           List<string> lst = new List<string>();
           ObjectId psdId = ObjectId.Null;

           using (Transaction tr = db.TransactionManager.StartTransaction())
           {
               DictionaryPropertySetDefinitions psdDict = new DictionaryPropertySetDefinitions(db);

               if (psdDict.Has(propSetDefName, tr))
               {
                    // Assign the return variable the ObjectId of the property set
                    psdId = psdDict.GetAt(propSetDefName);
                    PropertySetDefinition psdf = (PropertySetDefinition)tr.GetObject(psdId, OpenMode.ForRead, false, false);

                    PropertyDefinitionCollection pdc = psdf.Definitions;
                    foreach (PropertyDefinition pd in pdc)
                        if (pd.DataType == Autodesk.Aec.PropertyData.DataType.AutoIncrement)
                            lst.Add(pd.Name);
               }         
               tr.Commit();
           }
           return lst;
       }

       public static bool SetValueFromPropertySetId(string propName, ObjectId propSetId, Object value, Database db)
       {
           bool findAny = false;           
           int propSetDefId = 0;
           using (Transaction trans = db.TransactionManager.StartTransaction())
           {
               try
               {
                   PropertySet propSet = (PropertySet)trans.GetObject(propSetId, OpenMode.ForWrite, false, false);
                   propSetDefId = propSet.PropertyNameToId(propName);                 

                   if ((propSet.IsWriteEnabled))
                   {
                        propSet.SetAt(propSetDefId, value);                     
                       findAny = true;
                   }   
               }
               catch (AcException)
               {
                   // most likely eKeyNotFound.
               }
               trans.Commit();
           }
           return findAny;
       }

       public static ObjectId GetPropertySetDefinitionIdByName(string propSetDefName, Database db)
       {
           // Create objectId to hold the property set objectId and             
           // set the default value equal to null. 
           ObjectId psdId = ObjectId.Null;               
           AcDb.TransactionManager tm = db.TransactionManager;
           using (Transaction trans = tm.StartTransaction())
           {
               // Create a Property Set Dictionary
               AecPropDb.DictionaryPropertySetDefinitions psdDict = new AecPropDb.DictionaryPropertySetDefinitions(db);
 
               // Check to see if the dictionary contains the name of the property set we are looking for
               if (psdDict.Has(propSetDefName, trans))
               {
                   // Assign the return variable the ObjectId of the property set
                   psdId = psdDict.GetAt(propSetDefName);
               } // End If Block
 
               trans.Commit();
           } // End Using Statement
 
           return psdId;
       }

}

jtm2020hyo

  • Newt
  • Posts: 198
Re: [AUTOCAD MEP] _AECPROPERTYRENUMBERDATA with Fence Selection
« Reply #4 on: May 16, 2020, 03:25:56 AM »
Looks awesome.

Thank for your help.

jtm2020hyo

  • Newt
  • Posts: 198
Re: [AUTOCAD MEP] _AECPROPERTYRENUMBERDATA with Fence Selection
« Reply #5 on: February 22, 2022, 08:54:43 PM »
Excuse me, how could I update this to NET framework 4.8?

HOSNEYALAA

  • Newt
  • Posts: 103
Re: [AUTOCAD MEP] _AECPROPERTYRENUMBERDATA with Fence Selection
« Reply #6 on: August 08, 2022, 07:40:13 AM »
HI @Hanauer

Is it possible
Share a full project

I couldn't arrange it like you did, I made mistakes


Thank you