79
« on: July 08, 2012, 04:34:37 PM »
[CommandMethod("testMleaderAttribute", "tma", CommandFlags.Modal)]
public void MleaderAttribute()
{
object clay = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("clayer");
object ccolor = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("cecolor");
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
try
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
ObjectId blkid = ObjectId.Null;
if (!bt.Has("MleaderBlock"))// may have any name
{
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("clayer", "0");
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("cecolor", "bylayer");
BlockTableRecord lbtr = new BlockTableRecord();
lbtr.Name = "MleaderBlock";
lbtr.Origin = new Point3d(0, 0, 0);
lbtr.BlockScaling = BlockScaling.Uniform;
lbtr.Explodable = true;
lbtr.Units = UnitsValue.Inches;
// add BlockTableRecord to BlocTable
bt.UpgradeOpen();
blkid = bt.Add(lbtr);
tr.AddNewlyCreatedDBObject(lbtr, true);
// add objects to block
// add circle
Circle circle = new Circle(new Point3d(0, 0, 0), Vector3d.ZAxis, 3);
lbtr.AppendEntity(circle);
tr.AddNewlyCreatedDBObject(circle, true);
// add attribute
AttributeDefinition attr = new AttributeDefinition();
attr.Tag = "NUMBER";// may have any name
attr.Prompt = "Axis number:";
attr.TextString = "1";
//attr.TextStyle = db.Textstyle;//<-- A2009
attr.TextStyleId = db.Textstyle;//<-- A2010
attr.Height = 1.0;
attr.Position = new Point3d(0, 0, 0);
attr.HorizontalMode = TextHorizontalMode.TextCenter;
attr.VerticalMode = TextVerticalMode.TextVerticalMid;
attr.LockPositionInBlock = true;
attr.AdjustAlignment(db);
lbtr.AppendEntity(attr);
tr.AddNewlyCreatedDBObject(attr, true);
// commented adding the mtext
//MText mText = new MText();
//mText.Contents = "test";
//Point3d mpt = new Point3d(0, 0, 0);
//mText.Location = mpt;
//mText.Attachment = AttachmentPoint.MiddleCenter;
//lbtr.AppendEntity(mText);
//tr.AddNewlyCreatedDBObject(mText, true);
}
blkid = bt["MleaderBlock"];
MLeader leader = new MLeader();
// set content type to Block
leader.ContentType = ContentType.BlockContent;
leader.BlockContentId = blkid;
// check if desired arrowblock exist
if (!bt.Has("_NONE"))
{
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("DIMBLK", "_NONE");
}
leader.ArrowSymbolId = bt["_NONE"];
leader.ArrowSize = 0;
leader.DoglegLength = 0;
leader.LeaderLineType = LeaderType.StraightLeader;
// you can set coordinates using Getpoint instead
Point3d lpt = new Point3d(0, 0, 0);
Point3d npt = new Point3d(5, 5, 0);
leader.AddLeaderLine(lpt);
leader.SetVertex(0, 1, npt);
btr.AppendEntity(leader);
tr.AddNewlyCreatedDBObject(leader, true);
// leader insereted, go to attribute updating
if (!leader.IsWriteEnabled) leader.UpgradeOpen();
blkid = leader.BlockContentId;
BlockTableRecord mbtr = (BlockTableRecord)tr.GetObject(blkid, OpenMode.ForRead);
foreach (ObjectId atid in mbtr)
{
DBObject obj = tr.GetObject(atid, OpenMode.ForRead, false) as DBObject;
if (atid.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(AttributeDefinition))))
{
AttributeDefinition attdef = (AttributeDefinition)obj as AttributeDefinition;
if (attdef.Tag == "NUMBER") // check your existing tag
{
AttributeReference attref = leader.GetBlockAttribute(attdef.ObjectId);
if (!attref.IsWriteEnabled) attref.UpgradeOpen();
// add new attribute value
attref.TextString = ed.GetString("\nEnter the number: ").StringResult;
leader.SetBlockAttribute(attdef.ObjectId, attref);
}
}
}
leader.RecordGraphicsModified(true);
tr.Commit();
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("clayer", clay);
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("cecolor", ccolor);
}
}
~'J'~