这是我可以在dwg file
中打印属性的程序,但问题是mtext
打印的值如下:
项目:534Phase:1Zone:A(0530)
项目:534阶段:1区域:A(0520)等等,但我希望输出像
项目:534
阶段1
区:一
(0530)
(0520)。
[CommandMethod("ATT")]
public void ListAttributes()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Editor ed = acDoc.Editor;
Database db = acDoc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
// Start the transaction
try
{
// Build a filter list so that only
// block references with attributes are selected
TypedValue[] filList = new TypedValue[2] { new TypedValue((int)DxfCode.Start, "INSERT"), new TypedValue((int)DxfCode.HasSubentities, 1) };
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();
PromptPointResult ppr;
PromptPointOptions ppo = new PromptPointOptions("");
ppo.Message = "\n Select the place for print output:";
//get the coordinates from user
ppr = ed.GetPoint(ppo);
if (ppr.Status != PromptStatus.OK)
return;
Point3d startPoint = ppr.Value.TransformBy(ed.CurrentUserCoordinateSystem);
Vector3d disp = new Vector3d(0.0, -2.0 * db.Textsize, 0.0);
HashSet<string> attValues = new HashSet<string>();
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);
var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
AttributeCollection attCol = blkRef.AttributeCollection;
foreach (ObjectId attId in attCol)
{
AttributeReference attRef = (AttributeReference)tr.GetObject(attId, OpenMode.ForRead);
string str = (attRef.TextString);
//ed.WriteMessage("\n" + str);
if (attValues.Contains(str))
continue;
if (btr.Name == "NAL-SCRTAG")
{
MText mtext = new MText();
mtext.Location = startPoint;
string file = acDoc.Name;
string str1 = Path.GetFileNameWithoutExtension(file);
Match match = Regex.Match(str1, @"^(\w+-[CSDWM]\d+[A-Z]-.)$");
var split = str1.Split('-');
string code = split.First();
string phase = new string(split.ElementAt(1).Skip(1).Take(1).ToArray());
string zone = new string(split.ElementAt(1).Skip(2).Take(1).ToArray());
mtext.Contents = ("Project:" + code + "Phase:" + phase + "Zone:" + zone + "(" + str + ")");
//ed.WriteMessage(text);
curSpace.AppendEntity(mtext);
tr.AddNewlyCreatedDBObject(mtext, true);
db.TransactionManager.QueueForGraphicsFlush();
attValues.Add(str);
startPoint += disp;
}
}
}
tr.Commit();
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage(("Exception: " + ex.Message));
}
}
}
答案 0 :(得分:0)
如果我不误解你要做的事,你应该分开不同的任务:
在AutoCAD中将文本打印为MText
[CommandMethod("ATT")]
public void ListAttributes()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Editor ed = acDoc.Editor;
Database db = acDoc.Database;
// build the begining of the text from the filename
string filename = Path.GetFileNameWithoutExtension(acDoc.Name);
string pattern = @"^(?<code>\w+)-[CSDWM](?<phase>\d+)(?<zone>[A-Z])-\w+$";
// pattern description:
// ^(?<code>\w+) 'code' named group: one or more word characters at the begining
// - one hyphen (literal)
// [CSDWM] one alphabetic character (C, S, D, W or M)
// (?<phase>\d+) 'phase' named group: one or more digits
// (?<zone>[A-Z]) 'zone' named group: one alphabetic character
// - one hyphen (literal)
// \w+$ one or more world characters at the end
Match match = Regex.Match(filename, pattern);
if (!match.Success)
{
ed.WriteMessage("\n The document file name does not match the pattern.");
return;
}
var groups = match.Groups;
string text = $"Project:{groups["code"]} Phase:{groups["phase"]} Zone:{groups["zone"]}";
// Build a filter list so that only "NAL-SCRTAG"
// block references are selected
TypedValue[] filList = new TypedValue[2] { new TypedValue(0, "INSERT"), new TypedValue(2, "NAL-SCRTAG") };
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;
//get the coordinates from user
PromptPointOptions ppo = new PromptPointOptions("\n Select the place for print output: ");
PromptPointResult ppr = ed.GetPoint(ppo);
if (ppr.Status != PromptStatus.OK)
return;
Point3d startPoint = ppr.Value.TransformBy(ed.CurrentUserCoordinateSystem);
// Start the transaction
using (Transaction tr = db.TransactionManager.StartTransaction())
{
try
{
// use a HashSet to avoid duplicated values
HashSet<string> attValues = new HashSet<string>();
// add the attributes values at the end of the text
foreach (ObjectId blkId in res.Value.GetObjectIds())
{
BlockReference blkRef = (BlockReference)tr.GetObject(blkId, OpenMode.ForRead);
foreach (ObjectId attId in blkRef.AttributeCollection)
{
AttributeReference attRef = (AttributeReference)tr.GetObject(attId, OpenMode.ForRead);
string str = (attRef.TextString);
if (attValues.Add(str)) // returns false if attValues already contains str
{
text += $" ({str})"; // adds the attribute text string at the end of the text
}
}
}
// add the mText to the current space
var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
MText mtext = new MText();
mtext.Location = startPoint;
mtext.Contents = text;
curSpace.AppendEntity(mtext);
tr.AddNewlyCreatedDBObject(mtext, true);
tr.Commit();
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage(("Exception: " + ex.Message));
}
}
}