Пример #1
0
        //Creates a custom property in object contains number of floors and bars
        public void SaveToDB(ObjectId objectId, int floorNum, int numBars)
        {
            Document document = Application.DocumentManager.MdiActiveDocument;
            Database database = document.Database;

            using (Transaction transaction = database.TransactionManager.StartTransaction())
            {
                DBObject databaseObject = transaction.GetObject(objectId, OpenMode.ForRead);

                ObjectId extId = databaseObject.ExtensionDictionary;

                if (extId == ObjectId.Null)
                {
                    databaseObject.UpgradeOpen();
                    databaseObject.CreateExtensionDictionary();
                    extId = databaseObject.ExtensionDictionary;
                }

                DBDictionary dbExt = (DBDictionary)transaction.GetObject(extId, OpenMode.ForRead);

                if (!dbExt.Contains("CustomProp"))
                {
                    dbExt.UpgradeOpen();
                    Xrecord      xRec = new Xrecord();
                    ResultBuffer rb   = new ResultBuffer
                    {
                        new TypedValue((int)DxfCode.ExtendedDataAsciiString, floorNum.ToString()),
                        new TypedValue((int)DxfCode.ExtendedDataAsciiString, numBars.ToString())
                    };

                    xRec.Data = rb;
                    dbExt.SetAt("CustomProp", xRec);
                    transaction.AddNewlyCreatedDBObject(xRec, true);
                }

                transaction.Commit();
            }
        }
        public void Save()
        {
            if (WriteLock == true)
            {
                return;
            }
            if (BaseObjectId == ObjectId.Null)
            {
                return;
            }
            using (DocumentLock docLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
            {
                attrs.Clear();
                this.GetAttributes();
                if (attrs.ContainsKey("type"))
                {
                    attrs["type"] = this.ModelType();
                }
                else
                {
                    attrs.Add("type", this.ModelType());
                }
                if (attrs.ContainsKey("modelid"))
                {
                    attrs["modelid"] = Utils.ModelIdManager.toString(this.BaseModelId);
                }
                else
                {
                    attrs.Add("modelid", Utils.ModelIdManager.toString(this.BaseModelId));
                }
                Dictionary <String, String> pairs = attrs;
                Editor      ed    = Application.DocumentManager.MdiActiveDocument.Editor;
                Transaction trans = ed.Document.Database.TransactionManager.StartTransaction();
                try
                {
                    DBObject ent = trans.GetObject(BaseObjectId, OpenMode.ForRead);


                    if (ent.ExtensionDictionary.IsNull)
                    {
                        ent.UpgradeOpen();
                        ent.CreateExtensionDictionary();
                    }

                    DBDictionary extensionDict = (DBDictionary)trans.GetObject(ent.ExtensionDictionary, OpenMode.ForRead);
                    foreach (KeyValuePair <string, string> item in pairs)
                    {
                        String  key   = item.Key;
                        String  value = item.Value;
                        Xrecord myXrecord;
                        if (extensionDict.Contains(key))
                        {
                            ObjectId entryId = extensionDict.GetAt(key);
                            myXrecord = default(Xrecord);
                            extensionDict.UpgradeOpen();
                            myXrecord = (Xrecord)trans.GetObject(entryId, OpenMode.ForWrite);
                            ResultBuffer data = new ResultBuffer(new TypedValue((int)DxfCode.Text, value));
                            myXrecord.Data = data;
                        }
                        else
                        {
                            extensionDict.UpgradeOpen();
                            myXrecord = new Xrecord();
                            ResultBuffer data = new ResultBuffer(new TypedValue((int)DxfCode.Text, value));
                            myXrecord.Data = data;
                            extensionDict.SetAt(key, myXrecord);
                            trans.AddNewlyCreatedDBObject(myXrecord, true);
                        }
                    }
                    trans.Commit();
                }
                catch (System.Exception ex)
                {
                    ed.WriteMessage("a problem occured because " + ex.Message);
                }
                finally
                {
                    trans.Dispose();
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Save an object in the source object's extension dictionary.
        /// </summary>
        /// <typeparam name="T">The type of the object to store.</typeparam>
        /// <param name="source">The source object to write the object to.</param>
        /// <param name="key">A string that acts as the key in the extension dictionary.</param>
        /// <param name="data">The object to store.</param>
        /// <exception cref="System.Exception">Thrown when an AutoCAD error occurs.</exception>
        public static void SaveData <T>(this DBObject source, string key, T data)
        {
            Action <ResultBuffer> saveData = buffer =>
            {
                if (!source.IsWriteEnabled)
                {
                    source.UpgradeOpen();
                }

                if (source.ExtensionDictionary.IsNull)
                {
                    source.CreateExtensionDictionary();
                }

                Helpers.WrapInTransaction(source, tr =>
                {
                    var dict = (DBDictionary)tr.GetObject(source.ExtensionDictionary, OpenMode.ForWrite);

                    if (dict.Contains(key))
                    {
                        var xRecord  = (Xrecord)tr.GetObject(dict.GetAt(key), OpenMode.ForWrite);
                        xRecord.Data = buffer;
                    }
                    else
                    {
                        var xRecord  = new Xrecord();
                        xRecord.Data = buffer;
                        dict.SetAt(key, xRecord);
                        tr.AddNewlyCreatedDBObject(xRecord, true);
                    }
                });
            };

            Func <DxfCode, ResultBuffer> getResultBuffer = code => new ResultBuffer(new[] { new TypedValue((int)code, data) });

            try
            {
                // TODO: Add further types

                if (typeof(T) == typeof(bool))
                {
                    saveData(getResultBuffer(DxfCode.Bool));
                }
                else if (typeof(T) == typeof(double))
                {
                    saveData(getResultBuffer(DxfCode.Real));
                }
                else if (typeof(T) == typeof(byte))
                {
                    saveData(getResultBuffer(DxfCode.Int8));
                }
                else if (typeof(T) == typeof(char) ||
                         typeof(T) == typeof(short))
                {
                    saveData(getResultBuffer(DxfCode.Int16));
                }
                else if (typeof(T) == typeof(int))
                {
                    saveData(getResultBuffer(DxfCode.Int32));
                }
                else if (typeof(T) == typeof(long))
                {
                    saveData(getResultBuffer(DxfCode.Int32));
                }
                else if (typeof(T) == typeof(string))
                {
                    saveData(getResultBuffer(DxfCode.Text));
                }
                else
                {
                    saveData(new ResultBuffer(Helpers.Serialize(data)
                                              .Select(a => new TypedValue((int)DxfCode.BinaryChunk, a))
                                              .ToArray()));
                }
            }
            catch (Exception e)
            {
                throw Error.AutoCadException(e);
            }
        }