/// <summary> /// Adds the block attributes. /// </summary> /// <param name="blockRef">The block ref.</param> /// <param name="blockDefinition">The block definition.</param> /// <param name="attributeTagValue">The attribute tag value.</param> /// <param name="tm">The tm.</param> private static void AddBlockAttributes(BlockReference blockRef, BlockTableRecord blockDefinition, Hashtable attributeTagValue, TransactionManager tm) { PGA.MessengerManager.MessengerManager.AddLog("Start AddBlockAttributes"); var m = blockRef.BlockTransform; IEnumerator i; i = blockDefinition.GetEnumerator(); while (i.MoveNext()) { var obj = tm.GetObject((ObjectId)i.Current, OpenMode.ForRead, false); if (typeof(AttributeDefinition) == obj.GetType()) { var attDef = (AttributeDefinition)obj; if (!attDef.Constant) { var attRef = new AttributeReference(); attRef.SetAttributeFromBlock(attDef, blockRef.BlockTransform); if (attributeTagValue.ContainsKey(attRef.Tag)) { attRef.TextString = (string)attributeTagValue[attRef.Tag]; } attRef.Position = attDef.Position.TransformBy(m); blockRef.AttributeCollection.AppendAttribute(attRef); tm.AddNewlyCreatedDBObject(attRef, true); } } } PGA.MessengerManager.MessengerManager.AddLog("End AddBlockAttributes"); }
/// <summary> Adds Entities (text, Attribute Definitions etc)to the block definition. </summary> /// <param name="blockDefinitionObjectId"> <see cref="ObjectId"/> for the block definition object. </param> /// <param name="entities"> The entities to add to the block. </param> /// <returns> true if it succeeds, false if it fails. </returns> /// <exception cref="ArgumentNullException">The value of 'blockDefinitionObjectId' cannot be null. </exception> public static bool AddEntitiesToBlockDefinition <T>(ObjectId blockDefinitionObjectId, ICollection <T> entities) where T : Entity { if (blockDefinitionObjectId == null) { throw new ArgumentNullException("blockDefinitionObjectId"); } if (!blockDefinitionObjectId.IsValid) { return(false); } if (entities == null) { throw new ArgumentNullException("entities"); } if (entities.Count < 1) { return(false); } Xpos = 0; Ypos = 0; bool workedOk = false; Database database = blockDefinitionObjectId.Database; TransactionManager transactionManager = database.TransactionManager; using (Transaction transaction = transactionManager.StartTransaction()) { if (blockDefinitionObjectId.ObjectClass == (RXObject.GetClass(typeof(BlockTableRecord)))) { BlockTableRecord blockDefinition = (BlockTableRecord)transactionManager.GetObject(blockDefinitionObjectId, OpenMode.ForWrite, false); if (blockDefinition != null) { foreach (T entity in entities) { DBText text = entity as DBText; if (text != null) { text.Position = new Point3d(Xpos, Ypos, Zpos); incrementXY(); } else { MText mText = entity as MText; if (mText != null) { mText.Location = new Point3d(Xpos, Ypos, Zpos); incrementXY(); } } blockDefinition.AppendEntity(entity); // todo: vertical spacing ?? transactionManager.AddNewlyCreatedDBObject(entity, true); } workedOk = true; } } transaction.Commit(); } return(workedOk); }
/// <summary> Adds a collection of AutoCAD <see cref="Entity" />s to Model space. </summary> /// <exception cref="ArgumentNullException"> Thrown when one or more required arguments are null. </exception> /// <param name="entities"> The entities. </param> /// <param name="database"> The <see cref="Database" /> you are adding the <see cref="Entity" />s to. </param> /// <returns> a collection of ObjectId of the <see cref="Entity" />s you just added. </returns> public static ObjectIdCollection AddToModelSpace <T>(List <T> entities, Database database) where T : Entity { var objIdCollection = new ObjectIdCollection(); if (entities == null) { throw new ArgumentNullException("entities"); } if (database == null) { throw new ArgumentNullException("database"); } TransactionManager transactionManager = database.TransactionManager; using (Transaction transaction = transactionManager.StartTransaction()) { var blockTable = (BlockTable)transactionManager.GetObject(database.BlockTableId, OpenMode.ForRead, false); var modelSpace = (BlockTableRecord)transactionManager.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false); foreach (T entity in entities) { objIdCollection.Add(modelSpace.AppendEntity(entity)); var text = entity as DBText; if (text != null) { text.Position = new Point3d(Xpos, Ypos, Zpos); incrementXY(); } else { var mText = entity as MText; if (mText != null) { mText.Location = new Point3d(Xpos, Ypos, Zpos); incrementXY(); } } transactionManager.AddNewlyCreatedDBObject(entity, true); } transaction.Commit(); } return(objIdCollection); }
/// <summary> /// CustomToolBase override /// </summary> /// <param name="?"></param> public override bool ExecuteCallback() { Point3d ptStart = new Point3d(m_startX, m_startY, m_startZ); Point3d ptEnd = new Point3d(m_endX, m_endY, m_endZ); Database db = HostApplicationServices.WorkingDatabase; TransactionManager tm = db.TransactionManager; using (Transaction t = tm.StartTransaction()) { BlockTable bt = (BlockTable)t.GetObject(db.BlockTableId, OpenMode.ForRead); BlockTableRecord btr = (BlockTableRecord)t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); using (Line l = new Line(ptStart, ptEnd)) { btr.AppendEntity(l); tm.AddNewlyCreatedDBObject(l, true); } t.Commit(); } return(true); }