namespace BlockJigApplication
{
class BlockJig : EntityJig
{
private Point3d _pos;
private Dictionary<ObjectId, AttInfo> _attInfo;
private Transaction _tr;
public BlockJig(Transaction tr, BlockReference br, Dictionary<ObjectId, AttInfo> attInfo) : base(br)
{
_pos = br.Position;
_attInfo = attInfo;
_tr = tr;
}
protected override bool Update()
{
BlockReference br = Entity as BlockReference;
br.Position = _pos;
if (br.AttributeCollection.Count != 0)
{
foreach (ObjectId id in br.AttributeCollection)
{
DBObject obj = _tr.GetObject(id, OpenMode.ForRead);
AttributeReference ar = obj as AttributeReference;
if (ar != null)
{
ar.UpgradeOpen();
AttInfo ai = _attInfo[ar.ObjectId];
ar.Position = ai.Position.TransformBy(br.BlockTransform);
if (ai.IsAligned)
ar.AlignmentPoint = ai.Alignment.TransformBy(br.BlockTransform);
if (ar.IsMTextAttribute)
ar.UpdateMTextAttribute();
}
}
}
return true;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptPointOptions opts
= new JigPromptPointOptions
(Constants
.vbLf + "Select insertion point:"); opts
.BasePoint = new Point3d
(0,
0,
0); opts.UserInputControls = UserInputControls.NoZeroResponseAccepted;
PromptPointResult ppr = prompts.AcquirePoint(opts);
if (_pos == ppr.Value)
return SamplerStatus.NoChange;
_pos = ppr.Value;
return SamplerStatus.OK;
}
public PromptStatus Run()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
PromptResult promptResult = ed.Drag(this);
return promptResult.Status;
}
}
public class Commands
{
[CommandMethod("BJ")]
static public void BlockJigCmd()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptStringOptions pso
= new PromptStringOptions
(Constants
.vbLf + "Enter block name: "); PromptResult pr = ed.GetString(pso);
if (pr.Status != PromptStatus.OK)
return;
Transaction tr = doc.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
if (!bt.Has(pr.StringResult))
{
ed.WriteMessage(Constants.vbLf + "Block \"" + pr.StringResult + "\" not found.");
return;
}
BlockTableRecord space = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt(pr.StringResult), OpenMode.ForRead);
BlockReference br
= new BlockReference
(new Point3d
(), btr
.ObjectId); space.AppendEntity(br);
tr.AddNewlyCreatedDBObject(br, true);
Dictionary
<ObjectId, AttInfo
> attInfo
= new Dictionary
<ObjectId, AttInfo
>();
if (btr.HasAttributeDefinitions)
{
foreach (ObjectId id in btr)
{
DBObject obj = tr.GetObject(id, OpenMode.ForRead);
AttributeDefinition ad = obj as AttributeDefinition;
if (ad != null && !ad.Constant)
{
AttributeReference ar
= new AttributeReference
(); ar.SetAttributeFromBlock(ad, br.BlockTransform);
//<<=====================================>>
[color=red]ar.Position = ad.Position.TransformBy(br.BlockTransform);
if (ad.Justify != AttachmentPoint.BaseLeft)
ar.AlignmentPoint = ad.AlignmentPoint.TransformBy(br.BlockTransform);
if (ar.IsMTextAttribute)
ar.UpdateMTextAttribute();
ar.TextString = ad.TextString;[/color]
//<<====================================>>
ObjectId arId = br.AttributeCollection.AppendAttribute(ar);
tr.AddNewlyCreatedDBObject(ar, true);
attInfo
.Add(arId,
new AttInfo
(ad
.Position, ad
.AlignmentPoint, ad
.Justify != AttachmentPoint
.BaseLeft)); }
}
}
BlockJig myJig
= new BlockJig
(tr, br, attInfo
); if (myJig.Run() != PromptStatus.OK)
return;
tr.Commit();
}
}
}
}