- UID
- 658062
- 积分
- 2147
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2008-10-22
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
#region Imports
// Microsoft
using System;
using System.Collections.Generic;
using System.Text;
// Autodesk
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
#endregion
[assembly: CommandClass(typeof(MyTextPlugin.TextCommands))]
namespace MyTextPlugin
{
public class TextCommands
{
[CommandMethod("NumPrefix")]
static public void NumberingWithPrefix()
{
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
try
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
PromptStringOptions pso = new PromptStringOptions("\nEnter a Prefix : ");
pso.AllowSpaces = true;
pso.UseDefaultValue = true;
pso.DefaultValue = "AB";
PromptResult res;
string pfx = string.Empty;
res = ed.GetString(pso);
if (res.Status == PromptStatus.OK)
{
pfx = res.StringResult;
}
if (res.Status == PromptStatus.None)
{
pfx = "AB";
}
int num = new Int32();
PromptIntegerOptions pio = new PromptIntegerOptions("");
pio.Message = "\nEnter a Starting Number: ";
// Restrict input to positive and non-negative or non-zero values
pio.AllowZero = false;
pio.AllowNegative = false;
// Add default value
pio.DefaultValue = 1;
// Allow user the empty input by press Enter key
pio.AllowNone = true;
// Get the value entered by the user
PromptIntegerResult ires = ed.GetInteger(pio);
if (ires.Status == PromptStatus.OK)
{
num = ires.Value;
}
if (ires.Status == PromptStatus.None)
{
num = 1;
}
int inc = new Int32();
pio = new PromptIntegerOptions("");
pio.Message = "\nEnter an Increment Number by: ";
// Restrict input to positive and non-negative or non-zero values
pio.AllowZero = false;
pio.AllowNegative = false;
// Add default value
pio.DefaultValue = 1;
// Allow user the empty input by press Enter key
pio.AllowNone = true;
// Get the value entered by the user
ires = ed.GetInteger(pio);
if (ires.Status == PromptStatus.OK)
{
inc = ires.Value;
}
if (ires.Status == PromptStatus.None)
{
inc = 1;
}
double hgt = new double();
PromptDoubleOptions pdo = new PromptDoubleOptions("\nEnter a Text Height: ");
pdo.AllowNone = true;
// Restrict input to positive and non-negative or non-zero values
pdo.AllowZero = false;
pdo.AllowNegative = false;
pdo.UseDefaultValue = true;
pdo.DefaultValue = 1.0; // may be db.Textsize or other default
pdo.Message = "\nEnter a text height: ";
PromptDoubleResult dres;
dres = ed.GetDouble(pdo);
if (dres.Status == PromptStatus.None)
{
hgt = 1.0;
}
if (dres.Status == PromptStatus.OK)
{
hgt = dres.Value;
}
BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
Point3d pt = new Point3d();
Point3d npt = new Point3d();
bool first = GetFirstPoint(ed, "Select text location: ", out pt);
DBText txt = new DBText();
txt.SetDatabaseDefaults();
txt.Position = pt;
txt.Height = hgt;
txt.TextString = pfx + num.ToString();
//Set justification to middle center:
txt.VerticalMode = TextVerticalMode.TextVerticalMid;
txt.HorizontalMode = TextHorizontalMode.TextMid;
txt.AlignmentPoint = pt;
btr.AppendEntity(txt);
tr.AddNewlyCreatedDBObject(txt, true);
tr.TransactionManager.QueueForGraphicsFlush();
num += inc;
while (GetNextPoint(ed, "Select text location (or press Enter to Exit): ", pt, out npt))
{
txt = new DBText();
txt.SetDatabaseDefaults();
txt.Position = npt;
txt.Height = hgt;
txt.TextString = pfx + num.ToString();
//Set justification to middle center:
txt.VerticalMode = TextVerticalMode.TextVerticalMid;
txt.HorizontalMode = TextHorizontalMode.TextMid;
txt.AlignmentPoint = npt;
btr.AppendEntity(txt);
tr.AddNewlyCreatedDBObject(txt, true);
tr.TransactionManager.QueueForGraphicsFlush();
pt = npt;
num += inc;
}
tr.Commit();
}
}
catch (System.Exception ex)
{
ed.WriteMessage("Error: ==>\n{0}\nTrace: ==>\n{1}", ex.Message, ex.StackTrace);
}
finally
{
//do whatever you need
}
}
/// <summary>
/// Custom getpoint function without base point
/// </summary>
/// <param name="ed">document editor</param>
/// <param name="pt">Point3d, by reference</param>
/// <returns>Return true if success</returns>
public static bool GetFirstPoint(Editor ed, string msg, out Point3d pt)
{
pt = new Point3d();
PromptPointOptions opt = new PromptPointOptions("\n" + msg);
PromptPointResult res = ed.GetPoint(opt);
if (res.Status == PromptStatus.OK)
{
pt = res.Value;
return true;
}
else
{
return false;
}
}
/// <summary>
/// Custom getpoint function with base point
/// </summary>
/// <param name="ed">document editor</param>
/// <param name="msg">string, user prompt</param>
/// <param name="frompt">Point3d, by value</param>
/// <param name="pt">Point3d, by reference</param>
/// <returns>Return true if succes</returns>
public static bool GetNextPoint(Editor ed, string msg, Point3d frompt, out Point3d pt)
{
pt = new Point3d();
PromptPointOptions opt = new PromptPointOptions("\n" + msg);
opt.UseBasePoint = false;
opt.UseDashedLine = false;
opt.AllowNone = true;
opt.BasePoint = frompt;
PromptPointResult res = ed.GetPoint(opt);
if (res.Status == PromptStatus.OK)
{
pt = res.Value;
return true;
}
else
{
return false;
}
}
}
} |
|