Author Topic: Access properties directly from DynamicBlockReferencePropertyCollection  (Read 1601 times)

0 Members and 1 Guest are viewing this topic.

mohnston

  • Bull Frog
  • Posts: 305
  • CAD Programmer
Is there another way of accessing DynamicBlockReferenceProperty other than "foreach" or "for" loop on the DynamicBlockReferencePropertyCollection?
I'm working with some dynamic blocks with enormous numbers of dynamic properties and it's causing the function to take a long time.

I am setting the props in one transaction with the thought that this would speed things up slightly.

This function works fine. It's just slow with these unusually dynamic blocks.

Code: [Select]
        public static bool SetDynamicProperties(
            ObjectId BlockRefOID,
            Hashtable PropValue)
        {
            bool res = false;
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            using (DocumentLock dl = doc.LockDocument())
            {
                using (Transaction tr = doc.TransactionManager.StartTransaction())
                {
                    BlockReference br = tr.GetObject(BlockRefOID, OpenMode.ForRead) as BlockReference;
                    if (br.IsDynamicBlock)
                    {
                        DynamicBlockReferencePropertyCollection pc1 =
                            br.DynamicBlockReferencePropertyCollection;
                        foreach (DynamicBlockReferenceProperty dprop in pc1)
                        {
                            // does propName match prop from incoming hashtable
                            string propName = dprop.PropertyName;
                            object val = PropValue[propName];
                            //System.Windows.Forms.MessageBox.Show("Dynamic property " + propName +
                            //    " has a PropertyTypeCode of " + dprop.PropertyTypeCode.ToString());
                           
                            if (PropValue.ContainsKey(propName))
                            {
                                if (val != null)
                                {
                                    short ptc = dprop.PropertyTypeCode;
                                    switch (ptc)
                                    {
                                        case DynPropTypeCode_Double:
                                            dprop.Value = Convert.ToDouble(val);
                                            break;
                                        case DynPropTypeCode_Origin:
                                            // don't know what value type = Point?
                                            break;
                                        case DynPropTypeCode_Visibility:
                                            dprop.Value = val.ToString();
                                            break;
                                        default:
                                            try
                                            {
                                                dprop.Value = Convert.ToDouble(val);
                                            }
                                            catch (System.Exception)
                                            {
                                                try
                                                {
                                                    dprop.Value = val.ToString();
                                                }
                                                catch
                                                {
                                                }
                                            }
                                            break;
                                    }
                                }
                            }
                        }
                        tr.Commit();
                    }
                }
            }
            return res;
        }

There isn't a LINQ solution is there?
It's amazing what you can do when you don't know what you can't do.
CAD Programming Solutions