namespace MarksUtilities
{
public abstract class Mark
{
internal virtual Point3d GetInsertionPoint()
{
Point3d point = SelectionUtilities.GetPointFromUser();
return point;
}
internal abstract string GetMarkContent();
internal abstract void DrawMark(string content, Transaction tr, BlockTable bt, BlockTableRecord btr);
internal abstract void CreateBlockDescription(string blockName);
internal virtual void CreateBlockReference(RegMark markInstance, string blockName)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)bt[blockName].GetObject(OpenMode.ForRead);
BlockTableRecord curSpace_btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
BlockReference bRef = new BlockReference(markInstance.blockRefPosition, btr.ObjectId);
bRef.SetDatabaseDefaults();
bRef.Layer = markInstance.markLayer;
curSpace_btr.AppendEntity(bRef);
tr.AddNewlyCreatedDBObject(bRef, true);
if (btr.HasAttributeDefinitions)
{
foreach (ObjectId objID in btr)
{
DBObject dbObject = tr.GetObject(objID, OpenMode.ForRead);
if (dbObject is AttributeDefinition)
{
AttributeDefinition attDef = dbObject as AttributeDefinition;
if (attDef.Constant == false)
{
AttributeReference attRef = new AttributeReference();
attRef.SetAttributeFromBlock(attDef, bRef.BlockTransform);
attRef.Position = attDef.Position.TransformBy(bRef.BlockTransform);
attRef.TextString = attDef.TextString;
bRef.AttributeCollection.AppendAttribute(attRef);
tr.AddNewlyCreatedDBObject(attRef, true);
}
}
}
}
tr.Commit();
}
}
internal virtual void CreateBlockReference(WTMark markInstance, string blockName)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)bt[blockName].GetObject(OpenMode.ForRead);
BlockTableRecord curSpace_btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
BlockReference bRef = new BlockReference(markInstance.blockRefPosition, btr.ObjectId);
bRef.SetDatabaseDefaults();
bRef.Layer = markInstance.markLayer;
curSpace_btr.AppendEntity(bRef);
tr.AddNewlyCreatedDBObject(bRef, true);
if (btr.HasAttributeDefinitions)
{
foreach (ObjectId objID in btr)
{
DBObject dbObject = tr.GetObject(objID, OpenMode.ForRead);
if (dbObject is AttributeDefinition)
{
AttributeDefinition attDef = dbObject as AttributeDefinition;
if (attDef.Constant == false)
{
AttributeReference attRef = new AttributeReference();
attRef.SetAttributeFromBlock(attDef, bRef.BlockTransform);
attRef.Position = attDef.Position.TransformBy(bRef.BlockTransform);
attRef.TextString = attDef.TextString;
bRef.AttributeCollection.AppendAttribute(attRef);
tr.AddNewlyCreatedDBObject(attRef, true);
}
}
}
}
tr.Commit();
}
}
}
public class Initiator
{
[CommandMethod("insRegMark")]
public static void InsertItem()
{
RegMark regMark = new RegMark();
LayerCreator.CreateLayerSet();
regMark.CreateBlockDescription(regMark.blockName);
regMark.CreateBlockReference(regMark, regMark.blockName);
}
}
public class RegMark : Mark
{
public readonly string markContent;
public readonly string markLayer;
public readonly Point3d blockRefPosition;
public string blockName;
public RegMark()
{
markContent = GetMarkContent();
markLayer = "-10-ItemMarks";
blockRefPosition = base.GetInsertionPoint();
blockName = "ItemMark";
}
internal override Point3d GetInsertionPoint()
{
return base.GetInsertionPoint();
}
internal override string GetMarkContent()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
PromptStringOptions pso1 = new PromptStringOptions("\nSet item number: ");
PromptResult itemNumber = doc.Editor.GetString(pso1);
string content = itemNumber.StringResult;
return content;
}
internal override void DrawMark(string itemNumber, Transaction tr, BlockTable bt, BlockTableRecord btr)
{
Circle circle = new Circle();
circle.Radius = 8;
circle.LineWeight = LineWeight.LineWeight060;
circle.Layer = "-10-ItemMarks";
btr.AppendEntity(circle);
tr.AddNewlyCreatedDBObject(circle, true);
using (AttributeDefinition itemNo = new AttributeDefinition())
{
itemNo.SetDatabaseDefaults(bt.Database);
itemNo.Verifiable = true;
itemNo.Prompt = "Item Number";
itemNo.Tag = "ItemNumber";
itemNo.TextString = itemNumber;
itemNo.WidthFactor = 0.8;
itemNo.Height = 0.75 * circle.Radius;
itemNo.Justify = AttachmentPoint.BaseFit;
itemNo.Position = new Point3d(-0.75 * circle.Radius, -0.375*circle.Radius, 0);
itemNo.AlignmentPoint = new Point3d(0.75 * circle.Radius, -0.375 * circle.Radius, 0);
itemNo.AdjustAlignment(bt.Database);
itemNo.Layer = "0";
btr.AppendEntity(itemNo);
}
}
internal override void CreateBlockDescription(string blockName)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
if (bt.Has(blockName) == false)
{
BlockTableRecord btr = new BlockTableRecord();
btr.Name = blockName;
btr.Explodable = false;
btr.Origin = new Point3d(0, 0, 0);
bt.UpgradeOpen();
bt.Add(btr);
tr.AddNewlyCreatedDBObject(btr, true);
DrawMark(markContent, tr, bt, btr);
}
tr.Commit();
}
}
internal override void CreateBlockReference(RegMark regMark, string blockName)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)bt[blockName].GetObject(OpenMode.ForRead);
BlockTableRecord curSpace_btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
BlockReference bRef = new BlockReference(regMark.blockRefPosition, btr.ObjectId);
bRef.SetDatabaseDefaults();
bRef.Layer = regMark.markLayer;
curSpace_btr.AppendEntity(bRef);
tr.AddNewlyCreatedDBObject(bRef, true);
if (btr.HasAttributeDefinitions)
{
foreach (ObjectId objID in btr)
{
DBObject dbObject = tr.GetObject(objID, OpenMode.ForRead);
if (dbObject is AttributeDefinition)
{
AttributeDefinition attDef = dbObject as AttributeDefinition;
if (attDef.Constant == false)
{
AttributeReference attRef = new AttributeReference();
attRef.SetAttributeFromBlock(attDef, bRef.BlockTransform);
attRef.Position = attDef.Position.TransformBy(bRef.BlockTransform);
attRef.TextString = attDef.TextString;
bRef.AttributeCollection.AppendAttribute(attRef);
tr.AddNewlyCreatedDBObject(attRef, true);
}
}
}
}
tr.Commit();
}
}
}