public class testing
{
[CommandMethod("Library", CommandFlags.Session)]
public void Library()
{
string sBlock;
string sFile;
Document doc;
Document docBlock;
ObjectId libBlockid;
doc = acadApp.DocumentManager.MdiActiveDocument;
string sPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
OpenFileDialog openFileDia
= new OpenFileDialog
(); openFileDia.InitialDirectory = sPath;
openFileDia.Filter = "Drawing (*.dwg)|*.dwg|All files (*.*)|*.*";
openFileDia.Multiselect = false;
openFileDia.RestoreDirectory = true;
if (openFileDia.ShowDialog() != DialogResult.OK) return;
sFile = openFileDia.FileName;
sBlock = Path.GetFileNameWithoutExtension(sFile);
docBlock = acadApp.DocumentManager.Open(sFile, true);
acadApp.DocumentManager.MdiActiveDocument = docBlock;
//Using a keyword prompt leaves the user free to zoom around the drawing and inspect it further if the initial view isn't enough
PromptKeywordOptions pko
= new PromptKeywordOptions
("\nPress ENTER to insert the drawing " + sBlock
+ " as a block?\nPress ESCAPE to cancel"); pko.AllowNone = true;
pko.AllowArbitraryInput = true;
if (docBlock.Editor.GetKeywords(pko).Status == PromptStatus.Cancel)
{
docBlock.CloseAndDiscard();
return;
}
using (doc.LockDocument())
{
Database db = doc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
if (bt.Has(sBlock))
libBlockid = bt[sBlock];
else
libBlockid = db.Insert(sBlock, docBlock.Database, true);
tr.Commit();
}
}
acadApp.DocumentManager.MdiActiveDocument = doc;
docBlock.CloseAndDiscard();
using (doc.LockDocument())
{
if (libBlockid == ObjectId.Null) return; // JigNew.InsertBlockJiggy.Insert(libBlockid, false, "", scalefactor);
#region code to set up Keans jig
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
BlockTableRecord space = (BlockTableRecord)tr.GetObject(doc.Database.CurrentSpaceId, OpenMode.ForWrite);
BlockReference br =
new BlockReference
(new Point3d
(), libBlockid
); space.AppendEntity(br);
tr.AddNewlyCreatedDBObject(br, true);
Dictionary<ObjectId, AttInfo> attInfo =
new Dictionary
<ObjectId, AttInfo
>(); BlockTableRecord btr = (BlockTableRecord)tr.GetObject(libBlockid, OpenMode.ForRead);
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);
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;
ObjectId arId =
br.AttributeCollection.AppendAttribute(ar);
tr.AddNewlyCreatedDBObject(ar, true);
// Initialize our dictionary with the ObjectId of
// the attribute reference + attribute definition info
attInfo.Add(
arId,
ad.Position,
ad.AlignmentPoint,
ad.Justify != AttachmentPoint.BaseLeft
)
);
}
}
}
#endregion
// Run the jig
BlockJig myJig
= new BlockJig
(tr, br, attInfo
);
if (myJig.Run() != PromptStatus.OK)
return;
tr.Commit();
}
}
//throw new NotImplementedException();
//IdleEvents.Remove(acadApp_Idle); <------------- not neccessary, Tony's class takes care of this.
}//end Library
#region A jig borrowed from Kean for the purpose of playing here
class AttInfo
{
private Point3d _pos;
private Point3d _aln;
private bool _aligned;
public AttInfo(Point3d pos, Point3d aln, bool aligned)
{
_pos = pos;
_aln = aln;
_aligned = aligned;
}
public Point3d Position
{
set { _pos = value; }
get { return _pos; }
}
public Point3d Alignment
{
set { _aln = value; }
get { return _aln; }
}
public bool IsAligned
{
set { _aligned = value; }
get { return _aligned; }
}
}
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;
// Apply block transform to att def position
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
("\nSelect 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 =
acadApp.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
PromptResult promptResult = ed.Drag(this);
return promptResult.Status;
}
}
#endregion