这是我根据用户选择在dwg中选择块的代码,并在mtext的帮助下在dwg文件上打印块的详细信息但是mtext不工作它不会打印任何东西。它给了我例外。
[CommandMethod("LAT")]
public void ListAttributes()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Editor ed =
Application.DocumentManager.MdiActiveDocument.Editor;
Database db =
HostApplicationServices.WorkingDatabase;
Transaction tr =
db.TransactionManager.StartTransaction();
// Start the transaction
try
{
// Build a filter list so that only
// block references are selected
TypedValue[] filList = new TypedValue[1] {
new TypedValue((int)DxfCode.Start, "INSERT")
};
SelectionFilter filter =
new SelectionFilter(filList);
PromptSelectionOptions opts =
new PromptSelectionOptions();
opts.MessageForAdding = "Select block references: ";
PromptSelectionResult res =
ed.GetSelection(opts, filter);
// Do nothing if selection is unsuccessful
if (res.Status != PromptStatus.OK)
return;
SelectionSet selSet = res.Value;
ObjectId[] idArray = selSet.GetObjectIds();
foreach (ObjectId blkId in idArray)
{
BlockReference blkRef =
(BlockReference)tr.GetObject(blkId,
OpenMode.ForRead);
BlockTableRecord btr =
(BlockTableRecord)tr.GetObject(
blkRef.BlockTableRecord,
OpenMode.ForWrite
);
ed.WriteMessage(
"\nBlock: " + btr.Name
);
//btr.Dispose();
AttributeCollection attCol =
blkRef.AttributeCollection;
foreach (ObjectId attId in attCol)
{
MText mtext = new MText();
mtext.SetDatabaseDefaults();
mtext.Height = 2;
AttributeReference attRef =
(AttributeReference)tr.GetObject(attId,
OpenMode.ForRead);
string str =
////("\n Attribute Tag: "
//// + attRef.Tag
//// + "\n Attribute String: "
("\n" + attRef.TextString);
mtext.Contents = ("hELLO");
acDoc.Editor.WriteMessage("\n" + str);
btr.AppendEntity(mtext);
tr.AddNewlyCreatedDBObject(mtext, true);
}
}
tr.Commit();
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage(("Exception: " + ex.Message));
}
finally
{
tr.Dispose();
}
}
}
}
答案 0 :(得分:3)
你必须遍历模型空间并找到所有的孵化。 以下是执行此操作的方法之一:
[CommandMethod("FindAllHatches")]
public static void FindAllHatches()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
acDoc.Editor.WriteMessage("\nSearching for Hatches");
var db = acDoc.Database;
using (Transaction transaction = db.TransactionManager.StartTransaction())
{
ObjectId idModelSpace = SymbolUtilityServices.GetBlockModelSpaceId(db);
BlockTableRecord modelSpace =
transaction.GetObject(idModelSpace, OpenMode.ForRead) as
BlockTableRecord;
var sbReportText = new StringBuilder(); //usging System.Text
double fTotalArea = 0.0;
int nTotalHatches = 0;
foreach (var objId in modelSpace)
{
var entity = transaction.GetObject(objId, OpenMode.ForRead);
Hatch hatch = entity as Hatch;
if (hatch == null)
continue; //not hatch
nTotalHatches++;
fTotalArea += hatch.Area;
acDoc.Editor.WriteMessage("\nFound Hatch Area={0}", hatch.Area);
sbReportText.AppendFormat("Hatch Area={0}\n", hatch.Area);
}
if (nTotalHatches == 0)
return; //no hatches found
modelSpace.UpgradeOpen();
MText acMText = new MText();
acMText.SetDatabaseDefaults();
sbReportText.AppendFormat("Count = {0}, Total Area = {1}",
nTotalHatches, fTotalArea);
acMText.Contents = sbReportText.ToString();
modelSpace.AppendEntity(acMText);
transaction.AddNewlyCreatedDBObject(acMText, true);
transaction.Commit();
}
}
我的acBlkTblRec
modelSpace
来自我的例子。