- UID
- 658062
- 积分
- 2147
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2008-10-22
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
/// <summary>
/// function to return increment string
/// </summary>
/// <param name="prefix"></param>
/// <param name="start"></param>
/// <param name="step"></param>
/// <returns></returns>
private static string TextInc(string prefix, string start, int step)
{
string result = "";
string head = "";
bool szero = false;
int leg = (prefix + start).Length;
if (start.StartsWith("0"))
{
szero = true;
int pos = start.LastIndexOf("0");
for (int n = 0; n <= pos; n++)
head = head + "0";
}
int num = Convert.ToInt32(start) + step;
if (szero)
{
result = String.Concat(prefix, head, num.ToString());
if (result.Length > leg)
{
if (head.Length > 1)
result = String.Concat(prefix, head.Remove(1, result.Length - leg), num.ToString());
else
result = String.Concat(prefix, num.ToString());
}
}
else
{
result = String.Concat(prefix, num.ToString());
}
return result;
}
/// <summary>
/// Command for autonumbering with text with prefix
/// </summary>
[CommandMethod("inm")]
public static void IncrementWithFrame()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
Matrix3d ucs = ed.CurrentUserCoordinateSystem;
// Get or discard prefix as string entered by the user
PromptStringOptions pso =
new PromptStringOptions("\n\tEnter prefix or press Enter w/o it: ");
pso.AllowSpaces = true;
pso.DefaultValue = "";
PromptResult res;
res = ed.GetString(pso);
if (res.Status != PromptStatus.OK) return;
string pfx = res.StringResult;
int pos = pfx.Length;
ed.WriteMessage("\nPrefix Entered:\t{0}", pfx);
// Get initial number as string entered by the user
pso = new PromptStringOptions("\nEnter initial number : ");
pso.UseDefaultValue = true;
pso.DefaultValue = "0001";
res = ed.GetString(pso);
if (res.Status != PromptStatus.OK)return;
string strnum = res.StringResult;
ed.WriteMessage("\ninitial number:\t{0}", strnum);
PromptIntegerOptions pio = new PromptIntegerOptions("");
pio.Message = "\nEnter an increment step: ";
// Restrict input to positive and non-negative values
pio.AllowZero = false;
pio.AllowNegative = false;
// Add default value
pio.DefaultValue = 1;
pio.AllowNone = true;
// Get step value entered by the user
PromptIntegerResult ires = ed.GetInteger(pio);
if (ires.Status != PromptStatus.OK) return;
int step = ires.Value;
ed.WriteMessage("\nStep entered\t{0}", step);
// Get gap value entered by the user
PromptDoubleOptions pdo =
new PromptDoubleOptions("\nEnter the gap size between text and frame : ");
pdo.AllowNone = true;
pdo.UseDefaultValue = true;
pdo.DefaultValue = db.Textsize / 2;
PromptDoubleResult dbres;
dbres = ed.GetDouble(pdo);
if (res.Status != PromptStatus.OK) return;
double gap = dbres.Value;
ed.WriteMessage("\nGap Entered\t{0}", gap);
// using a current textsize
double txtheight = db.Textsize;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
doc.TransactionManager.EnableGraphicsFlush(true);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
PromptPointOptions ppo = new PromptPointOptions("\n\tSpecify text insertion point (Enter to stop): ");
ppo.AllowNone = true;
PromptPointResult ptres;
int n = 0;
string txt = "";
// get text points multiple
do
{
Point3d pt = new Point3d();
ptres = ed.GetPoint(ppo);
if (ptres.Status == PromptStatus.OK)
{
pt = ptres.Value;
DBText otext = new DBText();
otext.Position = pt;
otext.VerticalMode = TextVerticalMode.TextVerticalMid;
otext.HorizontalMode = TextHorizontalMode.TextMid;
otext.AlignmentPoint = otext.Position;
otext.AdjustAlignment(db);
if (n == 0)
{
txt = TextInc(pfx, strnum, 0);
}
else
txt = TextInc(pfx, strnum, step);
otext.TextString = txt;
n += 1;
strnum = txt.TrimStart(pfx.ToCharArray());
btr.AppendEntity(otext);
tr.AddNewlyCreatedDBObject(otext, true);
tr.TransactionManager.QueueForGraphicsFlush();
Plane plan = otext.GetPlane();
Extents3d ext = otext.GeometricExtents;
Point3d bl = ext.MinPoint;
Point3d ur = ext.MaxPoint;
Point3d mp = new Point3d((bl.X + ur.X) / 2, (bl.Y + ur.Y) / 2, (bl.Z + ur.Z) / 2).TransformBy(ucs);
otext.Position = mp;
otext.AlignmentPoint = otext.Position;
otext.AdjustAlignment(db);
Point3d br = new Point3d(ur.X, bl.Y, bl.Z);
Point3d ul = new Point3d(bl.X, ur.Y, bl.Z);
bl = bl.Subtract(new Vector3d(gap, gap, 0)).TransformBy(ucs);
ur = ur.Subtract(new Vector3d(-gap, -gap, 0)).TransformBy(ucs);
br = br.Subtract(new Vector3d(-gap, gap, 0)).TransformBy(ucs);
ul = ul.Subtract(new Vector3d(gap, -gap, 0)).TransformBy(ucs);
Polyline poly = new Polyline();
poly.AddVertexAt(0, bl.Convert2d(plan), 0, 0, 0);
poly.AddVertexAt(0, br.Convert2d(plan), 0, 0, 0);
poly.AddVertexAt(0, ur.Convert2d(plan), 0, 0, 0);
poly.AddVertexAt(0, ul.Convert2d(plan), 0, 0, 0);
poly.Closed = true;
poly.ColorIndex = 1;
btr.AppendEntity(poly);
tr.AddNewlyCreatedDBObject(poly, true);
tr.TransactionManager.QueueForGraphicsFlush();
}
} while (ptres.Status == PromptStatus.OK);
doc.TransactionManager.FlushGraphics();
tr.Commit();
}
} |
|