[CommandMethod("TagMotor")]
public static void DOSE_TagMotor()
{
// Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// Get the current document editor
Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;
// Create a TypedValue array to define the filter criteria
TypedValue
[] ulTypValAr
= new TypedValue
[4]; ulTypValAr
.SetValue(new TypedValue
((int)DxfCode
.Operator,
"<and"),
0); ulTypValAr
.SetValue(new TypedValue
((int)DxfCode
.Start,
"MULTILEADER"),
1); ulTypValAr
.SetValue(new TypedValue
((int)DxfCode
.LayerName,
"*_UNIT"),
2); ulTypValAr
.SetValue(new TypedValue
((int)DxfCode
.Operator,
"and>"),
3);
// Assign the filter criteria to a SelectionFilter oject
SelectionFilter ulSelFtr
= new SelectionFilter
(ulTypValAr
);
// Request for objects to be selected in the drawing area
PromptSelectionResult ulSSPromptRes;
ulSSPromptRes = acDocEd.GetSelection(ulSelFtr);
if (ulSSPromptRes.Status == PromptStatus.OK)
{
SelectionSet ulSSet = ulSSPromptRes.Value;
// set MLeader to TGW MOTORS
SetTGWMotorMLeader(acDoc, acCurDb);
// Start iteration transaction
using (Transaction ulTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open block table for read
BlockTable ulBlkTbl;
ulBlkTbl = ulTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
// Open the block table record Model space for read
BlockTableRecord ulBlkTblRec;
ulBlkTblRec = ulTrans.GetObject(ulBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
// Step through each MLeader in selection set
foreach (ObjectId ulMlId in ulSSet.GetObjectIds())
{
MLeader ulMldr = ulTrans.GetObject(ulMlId, OpenMode.ForRead) as MLeader;
if (ulMldr.HasContent())
{
// Check if MLeader has MText
if (ulMldr.ContentType == ContentType.MTextContent)
{
String ulLayNam;
String UnitLabel;
ulLayNam = ulMldr.Layer.ToString();
int pos = 0;
UnitLabel = ulMldr.MText.Contents.ToString();
if (UnitLabel.Contains('\n'))
{
for (int n = 0; n < UnitLabel.Length; n++)
{
if (UnitLabel.ElementAt(n) == '\n')
{
pos = n;
break;
}
}// end for unitlabel length
}// end if "\n" exists
else
{
pos = UnitLabel.Length;
}// end if "\n" does not exist
using (Transaction lTrans = acCurDb.TransactionManager.StartTransaction())
{
string MtrLayNam;
if (ulLayNam.Contains("_TAG_UNIT") == true)
{
MtrLayNam = ulLayNam.Replace("_TAG_UNIT", "_TAG_MTR");
}// end if unit layer contains _TAG
else
{
MtrLayNam = ulLayNam.Replace("_UNIT", "_TAG_MTR");
}// end if unit layer does NOT contain _TAG
LayerTable lTbl;
lTbl = lTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;
if (lTbl.Has(MtrLayNam) == true)
{
acCurDb.Clayer = lTbl[MtrLayNam];
}// end if layer exists
else
{
LayerTableRecord lRec;
lTbl.UpgradeOpen();
lRec = lTrans.GetObject(lTbl[ulLayNam], OpenMode.ForRead).Clone() as LayerTableRecord;
lRec.Name = MtrLayNam;
lTbl.Add(lRec);
lTrans.AddNewlyCreatedDBObject(lRec, true);
acCurDb.Clayer = lTbl[MtrLayNam];
}// end clone layer if _Tag_Motor layer does not exist
lTrans.Commit();
}// end lTrans
// Getting and setting MLeader MText contents
MText MtrTag
= new MText
(); string tag = UnitLabel.Remove(pos, UnitLabel.Count() - pos);
MtrTag.Contents = tag;
Point3d ulPt1;
Point3d ulPt2;
ulPt1 = ulMldr.GetFirstVertex(0);
ulPt2 = ulMldr.GetLastVertex(0);
ulBlkTblRec.UpgradeOpen();
MLeader mtrLead
= new MLeader
(); int i = mtrLead.AddLeader();
mtrLead.MText = MtrTag;
mtrLead.TextAlignmentType = TextAlignmentType.LeftAlignment;
mtrLead.TextAttachmentType = TextAttachmentType.AttachmentMiddle;
mtrLead.AddLeaderLine(i);
mtrLead
.AddFirstVertex(i,
new Point3d
(ulPt1
[0], ulPt1
[1],
0)); mtrLead
.AddLastVertex(i,
new Point3d
(ulPt2
[0], ulPt2
[1],
0));
ulBlkTblRec.AppendEntity(mtrLead);
ulTrans.AddNewlyCreatedDBObject(mtrLead, true);
//}// end lTrans
//ulTrans.Commit();
}// end if multileader has MText content
}// end if multileader has content
//ulTrans.Commit();
}// end foreach object in selection set
ulTrans.Commit();
}// end ulTrans
}// end if selection prompt ok
}// end DOSE_TagMotor