// This function returns the ObjectId for the BlockTableRecord called "EmployeeBlock", // creating it if necessary. The block contains three entities - circle, text // and ellipse. private ObjectId CreateEmployeeDefinition() { ObjectId newBtrId = new ObjectId(); //The return value for this function Database db = HostApplicationServices.WorkingDatabase; //save some space using (Transaction trans = db.TransactionManager.StartTransaction()) //begin the transaction { // Now, access the database and obtain a reference to the BlockTable BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite); if ((bt.Has("EmployeeBlock"))) { newBtrId = bt["EmployeeBlock"]; } else { Point3d center = new Point3d(0, 0, 0); // Declare and define the entities we want to add: // Circle: Circle circle = new Circle(center, Vector3d.ZAxis, 2); // Attribute Definition AttributeDefinition text = new AttributeDefinition(center, "NoName", "Name:", "Enter Name", db.Textstyle); text.ColorIndex = 2; // Ellipse: Ellipse ellipse = new Ellipse(center, Vector3d.ZAxis, new Vector3d(3, 0, 0), 0.5, 0, 0); // Next, create a layer with the helper function, and assign the layer to our entities. ObjectId empId = CreateLayer(); text.LayerId = empId; circle.LayerId = empId; ellipse.LayerId = empId; // Set the color for each entity irrespective of the layer's color. text.ColorIndex = 2; circle.ColorIndex = 1; ellipse.ColorIndex = 3; // Create a new block definition called EmployeeBlock BlockTableRecord newBtr = new BlockTableRecord(); newBtr.Name = "EmployeeBlock"; newBtrId = bt.Add(newBtr); //Add the block, and set the id as the return value of our function trans.AddNewlyCreatedDBObject(newBtr, true); //Let the transaction know about any object/entity you add to the database! newBtr.AppendEntity(circle); //Append our entities... newBtr.AppendEntity(text); newBtr.AppendEntity(ellipse); trans.AddNewlyCreatedDBObject(circle, true); //Again, let the transaction know about our newly added entities. trans.AddNewlyCreatedDBObject(text, true); trans.AddNewlyCreatedDBObject(ellipse, true); } trans.Commit(); //All done, no errors? Go ahead and commit! } return newBtrId; }
//This function returns the ObjectId for the BlockTableRecord called "EmployeeBlock", //creating it if necessary. The block contains three entities - circle, text //and ellipse. public ObjectId CreateEmployeeDefinition() { ObjectId newBtrId = new ObjectId(); //The return value for this function Database db = HostApplicationServices.WorkingDatabase; //save some space // The "using" keyword used below automatically calls "Dispose" // on the "trans" object. using (Transaction trans = db.TransactionManager.StartTransaction()) { //Now, drill into the database and obtain a reference to the BlockTable BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite); if ((bt.Has("EmployeeBlock"))) { newBtrId = bt["EmployeeBlock"]; } else { Point3d center = new Point3d(10, 10, 0); // convenient declaration... // Declare and define the entities we want to add: //Circle: Circle circle = new Circle(center, Vector3d.ZAxis, 2); //Text: MText text = new MText(); text.Contents = "Earnest Shackleton"; text.Location = center; //Ellipse: Ellipse ellipse = new Ellipse(center, Vector3d.ZAxis, new Vector3d(3, 0, 0), 0.5, 0, 0); //Next, create a layer with the helper function, and assign //the layer to our entities. ObjectId empId = CreateLayer(); text.LayerId = empId; circle.LayerId = empId; ellipse.LayerId = empId; //Set the color for each entity irrespective of the layer's color. text.ColorIndex = 2; circle.ColorIndex = 1; ellipse.ColorIndex = 3; //Create a new block definition called EmployeeBlock BlockTableRecord newBtr = new BlockTableRecord(); newBtr.Name = "EmployeeBlock"; newBtrId = bt.Add(newBtr); //Add the block, and set the id as the return value of our function trans.AddNewlyCreatedDBObject(newBtr, true); //Let the transaction know about any object/entity you add to the database! newBtr.AppendEntity(circle); //Append our entities... newBtr.AppendEntity(text); newBtr.AppendEntity(ellipse); trans.AddNewlyCreatedDBObject(circle, true); //Again, let the transaction know about our newly added entities. trans.AddNewlyCreatedDBObject(text, true); trans.AddNewlyCreatedDBObject(ellipse, true); } trans.Commit(); //All done, no errors? Go ahead and commit! } return newBtrId; }