Esempio n. 1
0
        public void CreateEmployee()
        {
            Database    db    = HostApplicationServices.WorkingDatabase;
            Transaction trans = db.TransactionManager.StartTransaction();

            try
            {
                BlockTable       bt  = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForWrite));
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                //Create the block reference...use the return from CreateEmployeeDefinition directly!
                BlockReference br = new BlockReference(new Point3d(10, 10, 0), CreateEmployeeDefinition());
                btr.AppendEntity(br);                    //Add the reference to ModelSpace
                trans.AddNewlyCreatedDBObject(br, true); //Let the transaction know about it

                //Create the custom per-employee data
                Xrecord xRec = new Xrecord();
                //We want to add 'Name', 'Salary' and 'Division' information.  Here is how:
                xRec.Data = new ResultBuffer(
                    new TypedValue((int)DxfCode.Text, "Earnest Shackleton"),
                    new TypedValue((int)DxfCode.Real, 72000),
                    new TypedValue((int)DxfCode.Text, "Sales"));

                //Next, we need to add this data to the 'Extension Dictionary' of the employee.
                br.CreateExtensionDictionary();
                DBDictionary brExtDict = (DBDictionary)trans.GetObject(br.ExtensionDictionary, OpenMode.ForWrite, false);
                brExtDict.SetAt("EmployeeData", xRec);                 //Set our XRecord in the dictionary at 'EmployeeData'.
                trans.AddNewlyCreatedDBObject(xRec, true);

                trans.Commit();
            }
            finally
            {
                trans.Dispose();
            }
        }
Esempio n. 2
0
        public void CreateInsert()
        {
            var db = HostApplicationServices.WorkingDatabase;

            using (var trans = db.TransactionManager.StartTransaction())
            {
                var bt    = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                var blkId = bt.Has("EmployeeBlock") ?
                            bt["EmployeeBlock"] : CreateBlkDefinition();

                var ins    = new BlockReference(Point3d.Origin, blkId);
                var curBtr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                curBtr.AppendEntity(ins);
                trans.AddNewlyCreatedDBObject(ins, true);

                ins.CreateExtensionDictionary();
                var extDict = trans.GetObject(ins.ExtensionDictionary, OpenMode.ForWrite) as DBDictionary;

                var data = new ResultBuffer(
                    new TypedValue((int)DxfCode.Text, "Earest Shackleton"),
                    new TypedValue((int)DxfCode.Real, 72000),
                    new TypedValue((int)DxfCode.Text, "Sales"));
                CreateXRecWithData(trans, extDict, "EmploeeInfo", data);

                trans.Commit();
            }
        }
Esempio n. 3
0
        // This function creates a new BlockReference to the "EmployeeBlock" object,
        // and adds it to ModelSpace.
        private ObjectId CreateEmployee(string name, string division, double salary, Point3d pos)
        {
            // get the current working database
            Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                BlockTable       bt  = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForWrite));
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                // Create the block reference...use the return from CreateEmployeeDefinition directly!
                BlockReference br = new BlockReference(pos, CreateEmployeeDefinition());

                AttributeReference attRef = new AttributeReference();
                // Iterate the employee block and find the attribute definition
                BlockTableRecord empBtr = (BlockTableRecord)trans.GetObject(bt["EmployeeBlock"], OpenMode.ForRead);
                foreach (ObjectId id in empBtr)
                {
                    Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead, false);
                    // Use it to open the current object!
                    if (ent is AttributeDefinition)  // We use .NET's RunTimeTypeInformation (RTTI) to establish type.
                    {
                        // Set the properties from the attribute definition on our attribute reference
                        AttributeDefinition attDef = ((AttributeDefinition)(ent));
                        attRef.SetPropertiesFrom(attDef);
                        attRef.Position   = new Point3d(attDef.Position.X + br.Position.X, attDef.Position.Y + br.Position.Y, attDef.Position.Z + br.Position.Z);
                        attRef.Height     = attDef.Height;
                        attRef.Rotation   = attDef.Rotation;
                        attRef.Tag        = attDef.Tag;
                        attRef.TextString = name;
                    }
                }
                // Add the reference to ModelSpace
                btr.AppendEntity(br);
                // Add the attribute reference to the block reference
                br.AttributeCollection.AppendAttribute(attRef);
                // let the transaction know
                trans.AddNewlyCreatedDBObject(attRef, true);
                trans.AddNewlyCreatedDBObject(br, true);

                // Create the custom per-employee data
                Xrecord xRec = new Xrecord();
                // We want to add 'Name', 'Salary' and 'Division' information.  Here is how:
                xRec.Data = new ResultBuffer(
                    new TypedValue((int)DxfCode.Text, name),
                    new TypedValue((int)DxfCode.Real, salary),
                    new TypedValue((int)DxfCode.Text, division));

                // Next, we need to add this data to the 'Extension Dictionary' of the employee.
                br.CreateExtensionDictionary();
                DBDictionary brExtDict = (DBDictionary)trans.GetObject(br.ExtensionDictionary, OpenMode.ForWrite, false);
                brExtDict.SetAt("EmployeeData", xRec); //Set our XRecord in the dictionary at 'EmployeeData'.
                trans.AddNewlyCreatedDBObject(xRec, true);

                ObjectId retId = br.ObjectId;
                trans.Commit();

                return(retId);
            }
        }
Esempio n. 4
0
        public ObjectId CreateEmployee(string name, string division, double salary, Point3d position)
        {
            var      db    = HostApplicationServices.WorkingDatabase;
            var      blkId = CreateBlkDefinition();
            ObjectId idIns = ObjectId.Null;

            using (var trans = db.TransactionManager.StartTransaction())
            {
                var ins    = new BlockReference(position, blkId);
                var curBtr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                idIns = curBtr.AppendEntity(ins);
                trans.AddNewlyCreatedDBObject(ins, true);

                var empBlk = trans.GetObject(blkId, OpenMode.ForRead) as BlockTableRecord;
                foreach (var id in empBlk)
                {
                    if (trans.GetObject(id, OpenMode.ForRead, false) is AttributeDefinition attDef)
                    {
                        var attRef = new AttributeReference();
                        attRef.SetPropertiesFrom(attDef);
                        attRef.Position       = position;
                        attRef.Height         = attDef.Height;
                        attRef.Rotation       = attDef.Rotation;
                        attRef.Tag            = attDef.Tag;
                        attRef.TextString     = name;
                        attRef.HorizontalMode = attDef.HorizontalMode;
                        attRef.VerticalMode   = attDef.VerticalMode;
                        attRef.AlignmentPoint = position;
                        attRef.AdjustAlignment(db);

                        ins.AttributeCollection.AppendAttribute(attRef);
                        trans.AddNewlyCreatedDBObject(attRef, true);
                    }
                }

                ins.CreateExtensionDictionary();
                var extDict = trans.GetObject(ins.ExtensionDictionary, OpenMode.ForWrite) as DBDictionary;

                var data = new ResultBuffer(
                    new TypedValue((int)DxfCode.Text, name),
                    new TypedValue((int)DxfCode.Real, salary),
                    new TypedValue((int)DxfCode.Text, division));
                CreateXRecWithData(trans, extDict, "EmploeeInfo", data);

                trans.Commit();
            }

            return(idIns);
        }
Esempio n. 5
0
 public static void Set(ObjectId id, string key, object value)
 {
     try {
         if (id.IsErased == false & id.IsEffectivelyErased == false)
         {
             using (World.Docu.LockDocument()) {
                 using (Database db = World.Docu.Database) {
                     using (Transaction tr = db.TransactionManager.StartTransaction()) {
                         using (BlockReference br = (BlockReference)tr.GetObject(id, OpenMode.ForWrite)) {
                             if (br.ExtensionDictionary == ObjectId.Null)
                             {
                                 br.CreateExtensionDictionary();
                             }
                             using (DBDictionary dict = (DBDictionary)tr.GetObject(br.ExtensionDictionary, OpenMode.ForWrite, false)) {
                                 DxfCode code = AcadIO.DxfHelper.GetFromObject(value);
                                 if (dict.Contains(key))
                                 {
                                     Xrecord      xRec   = (Xrecord)tr.GetObject(dict.GetAt(key), OpenMode.ForWrite);
                                     ResultBuffer resBuf = new ResultBuffer(new TypedValue(Convert.ToInt32(code), value));
                                     xRec.Data = resBuf;
                                 }
                                 else
                                 {
                                     Xrecord      xRec   = new Xrecord();
                                     ResultBuffer resBuf = new ResultBuffer(new TypedValue(Convert.ToInt32(code), value));
                                     xRec.Data = resBuf;
                                     dict.SetAt(key, xRec);
                                     tr.AddNewlyCreatedDBObject(xRec, true);
                                 }
                             }
                         }
                         tr.Commit();
                     }
                 }
             }
         }
     }
     catch (System.Exception ex) {
         Err.Log(ex);
     }
 }
Esempio n. 6
0
        public void CreateEmployee()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor   ed = Application.DocumentManager.MdiActiveDocument.Editor;

            using (Transaction trans = db.TransactionManager.StartTransaction())
                try
                {
                    BlockTable       bt  = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForWrite));
                    BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                    //Create the block reference...use the return from CreateEmployeeDefinition directly!
                    BlockReference br = new BlockReference(new Point3d(10, 10, 0), CreateEmployeeDefinition());
                    btr.AppendEntity(br);                    //Add the reference to ModelSpace
                    trans.AddNewlyCreatedDBObject(br, true); //Let the transaction know about it

                    //Create the custom per-employee data
                    Xrecord xRec = new Xrecord();
                    //We want to add 'Name', 'Salary' and 'Division' information.  Here is how:
                    xRec.Data = new ResultBuffer(
                        new TypedValue((int)DxfCode.Text, "Earnest Shackleton"),
                        new TypedValue((int)DxfCode.Real, 72000),
                        new TypedValue((int)DxfCode.Text, "Sales"));

                    //Next, we need to add this data to the 'Extension Dictionary' of the employee.
                    br.CreateExtensionDictionary();
                    DBDictionary brExtDict = (DBDictionary)trans.GetObject(br.ExtensionDictionary, OpenMode.ForWrite, false);
                    brExtDict.SetAt("EmployeeData", xRec); //Set our XRecord in the dictionary at 'EmployeeData'.
                    trans.AddNewlyCreatedDBObject(xRec, true);

                    trans.Commit();
                }
                catch (System.Exception ex)
                {
                    ed.WriteMessage("Error Creating Employee Block: " + ex.Message);
                }
            // No 'finally' block is required to clean up the transaction
            // since it was declared inside a 'using' statement.
        }
Esempio n. 7
0
        //This function creates a new BlockReference to the "EmployeeBlock" object,
        //and adds it to ModelSpace.
        public static ObjectId CreateEmployee(String name, String division, Double salary, Point3d pos)
        {
            Database    db    = HostApplicationServices.WorkingDatabase;
            Transaction trans = db.TransactionManager.StartTransaction();

            try
            {
                BlockTable       bt  = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                //Create the block reference...use the return from CreateEmployeeDefinition directly!
                BlockReference br = new BlockReference(pos, CreateEmployeeDefinition());
                // create a new attribute reference
                AttributeReference attRef = new AttributeReference();

                //Iterate the employee block and find the attribute definition
                BlockTableRecord empBtr = (BlockTableRecord)trans.GetObject(bt["EmployeeBlock"], OpenMode.ForRead);

                foreach (ObjectId id in empBtr)
                {
                    Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead, false);                          //Use it to open the current object!
                    if (ent.GetType().FullName.Equals("Autodesk.AutoCAD.DatabaseServices.AttributeDefinition")) //We use .NET//s RTTI to establish type.
                    {
                        //Set the properties from the attribute definition on our attribute reference
                        AttributeDefinition attDef = (AttributeDefinition)ent;
                        attRef.SetPropertiesFrom(attDef);
                        attRef.Position = new Point3d(attDef.Position.X + br.Position.X,
                                                      attDef.Position.Y + br.Position.Y,
                                                      attDef.Position.Z + br.Position.Z);

                        attRef.Height     = attDef.Height;
                        attRef.Rotation   = attDef.Rotation;
                        attRef.Tag        = attDef.Tag;
                        attRef.TextString = name;
                    }
                }
                btr.AppendEntity(br);                 //Add the reference to ModelSpace

                //Add the attribute reference to the block reference
                br.AttributeCollection.AppendAttribute(attRef);

                //let the transaction know
                trans.AddNewlyCreatedDBObject(attRef, true);
                trans.AddNewlyCreatedDBObject(br, true);                 //Let the transaction know about it

                //Create the custom per-employee data
                Xrecord xRec = new Xrecord();
                //We want to add //Name//, //Salary// and //Division// information.  Here is how:
                xRec.Data = new ResultBuffer(
                    new TypedValue((int)DxfCode.Text, name),
                    new TypedValue((int)DxfCode.Real, salary),
                    new TypedValue((int)DxfCode.Text, division));

                //Next, we need to add this data to the //Extension Dictionary// of the employee.
                br.CreateExtensionDictionary();
                DBDictionary brExtDict = (DBDictionary)trans.GetObject(br.ExtensionDictionary, OpenMode.ForWrite, false);
                brExtDict.SetAt("EmployeeData", xRec);                 //Set our XRecord in the dictionary at //EmployeeData//.
                trans.AddNewlyCreatedDBObject(xRec, true);

                trans.Commit();

                //return the objectId of the employee block reference
                return(br.ObjectId);
            }
            finally
            {
                trans.Dispose();
            }
        }
Esempio n. 8
0
        // This function creates a new BlockReference to the "EmployeeBlock" object,
        // and adds it to ModelSpace.
        private ObjectId CreateEmployee(string name, string division, double salary, Point3d pos)
        {
            // get the current working database
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForWrite));
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                // Create the block reference...use the return from CreateEmployeeDefinition directly!
                BlockReference br = new BlockReference(pos, CreateEmployeeDefinition());

                AttributeReference attRef = new AttributeReference();
                // Iterate the employee block and find the attribute definition
                BlockTableRecord empBtr = (BlockTableRecord)trans.GetObject(bt["EmployeeBlock"], OpenMode.ForRead);
                foreach (ObjectId id in empBtr)
                {
                    Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead, false);
                    // Use it to open the current object!
                    if (ent is AttributeDefinition)  // We use .NET's RunTimeTypeInformation (RTTI) to establish type.
                    {
                        // Set the properties from the attribute definition on our attribute reference
                        AttributeDefinition attDef = ((AttributeDefinition)(ent));
                        attRef.SetPropertiesFrom(attDef);
                        attRef.Position = new Point3d(attDef.Position.X + br.Position.X, attDef.Position.Y + br.Position.Y, attDef.Position.Z + br.Position.Z);
                        attRef.Height = attDef.Height;
                        attRef.Rotation = attDef.Rotation;
                        attRef.Tag = attDef.Tag;
                        attRef.TextString = name;
                    }
                }
                // Add the reference to ModelSpace
                btr.AppendEntity(br);
                // Add the attribute reference to the block reference
                br.AttributeCollection.AppendAttribute(attRef);
                // let the transaction know
                trans.AddNewlyCreatedDBObject(attRef, true);
                trans.AddNewlyCreatedDBObject(br, true);

                // Create the custom per-employee data
                Xrecord xRec = new Xrecord();
                // We want to add 'Name', 'Salary' and 'Division' information.  Here is how:
                xRec.Data = new ResultBuffer(
                  new TypedValue((int)DxfCode.Text, name),
                  new TypedValue((int)DxfCode.Real, salary),
                  new TypedValue((int)DxfCode.Text, division));

                // Next, we need to add this data to the 'Extension Dictionary' of the employee.
                br.CreateExtensionDictionary();
                DBDictionary brExtDict = (DBDictionary)trans.GetObject(br.ExtensionDictionary, OpenMode.ForWrite, false);
                brExtDict.SetAt("EmployeeData", xRec); //Set our XRecord in the dictionary at 'EmployeeData'.
                trans.AddNewlyCreatedDBObject(xRec, true);

                ObjectId retId = br.ObjectId;
                trans.Commit();

                return retId;
            }
        }
Esempio n. 9
0
        public void CreateEmployee()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            using (Transaction trans = db.TransactionManager.StartTransaction())
                try
                {
                    BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForWrite));
                    BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                    //Create the block reference...use the return from CreateEmployeeDefinition directly!
                    BlockReference br = new BlockReference(new Point3d(10, 10, 0), CreateEmployeeDefinition());
                    btr.AppendEntity(br); //Add the reference to ModelSpace
                    trans.AddNewlyCreatedDBObject(br, true); //Let the transaction know about it

                    //Create the custom per-employee data
                    Xrecord xRec = new Xrecord();
                    //We want to add 'Name', 'Salary' and 'Division' information.  Here is how:
                    xRec.Data = new ResultBuffer(
                        new TypedValue((int)DxfCode.Text, "Earnest Shackleton"),
                        new TypedValue((int)DxfCode.Real, 72000),
                        new TypedValue((int)DxfCode.Text, "Sales"));

                    //Next, we need to add this data to the 'Extension Dictionary' of the employee.
                    br.CreateExtensionDictionary();
                    DBDictionary brExtDict = (DBDictionary)trans.GetObject(br.ExtensionDictionary, OpenMode.ForWrite, false);
                    brExtDict.SetAt("EmployeeData", xRec); //Set our XRecord in the dictionary at 'EmployeeData'.
                    trans.AddNewlyCreatedDBObject(xRec, true);

                    trans.Commit();
                }
                catch (System.Exception ex)
                {
                    ed.WriteMessage("Error Creating Employee Block: " + ex.Message);
                }
            // No 'finally' block is required to clean up the transaction
            // since it was declared inside a 'using' statement.
        }
Esempio n. 10
0
        //This function creates a new BlockReference to the "EmployeeBlock" object,
        //and adds it to ModelSpace.
        public static ObjectId CreateEmployee(String name, String division, Double salary, Point3d pos)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                //Create the block reference...use the return from CreateEmployeeDefinition directly!
                BlockReference br = new BlockReference(pos, CreateEmployeeDefinition());
                // create a new attribute reference
                AttributeReference attRef = new AttributeReference();

                //Iterate the employee block and find the attribute definition
                BlockTableRecord empBtr = (BlockTableRecord)trans.GetObject(bt["EmployeeBlock"], OpenMode.ForRead);

                foreach (ObjectId id in empBtr)
                {
                    Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead, false); //Use it to open the current object!
                    if (ent.GetType().FullName.Equals("Autodesk.AutoCAD.DatabaseServices.AttributeDefinition"))  //We use .NET//s RTTI to establish type.
                    {
                        //Set the properties from the attribute definition on our attribute reference
                        AttributeDefinition attDef = (AttributeDefinition)ent;
                        attRef.SetPropertiesFrom(attDef);
                        attRef.Position = new Point3d(attDef.Position.X + br.Position.X,
                        attDef.Position.Y + br.Position.Y,
                        attDef.Position.Z + br.Position.Z);

                        attRef.Height = attDef.Height;
                        attRef.Rotation = attDef.Rotation;
                        attRef.Tag = attDef.Tag;
                        attRef.TextString = name;
                    }
                }
                btr.AppendEntity(br); //Add the reference to ModelSpace

                //Add the attribute reference to the block reference
                br.AttributeCollection.AppendAttribute(attRef);

                //let the transaction know
                trans.AddNewlyCreatedDBObject(attRef, true);
                trans.AddNewlyCreatedDBObject(br, true); //Let the transaction know about it

                //Create the custom per-employee data
                Xrecord xRec = new Xrecord();
                //We want to add //Name//, //Salary// and //Division// information.  Here is how:
                xRec.Data = new ResultBuffer(
                    new TypedValue((int)DxfCode.Text, name),
                    new TypedValue((int)DxfCode.Real, salary),
                    new TypedValue((int)DxfCode.Text, division));

                //Next, we need to add this data to the //Extension Dictionary// of the employee.
                br.CreateExtensionDictionary();
                DBDictionary brExtDict = (DBDictionary)trans.GetObject(br.ExtensionDictionary, OpenMode.ForWrite, false);
                brExtDict.SetAt("EmployeeData", xRec); //Set our XRecord in the dictionary at //EmployeeData//.
                trans.AddNewlyCreatedDBObject(xRec, true);

                trans.Commit();

                //return the objectId of the employee block reference
                return br.ObjectId;
            }
        }