AutoCAD2008, the polyline drawing with the serrated edge.
But i used the lisp, which with the function "grread". Lisp work smooth.
Here is my C# code:
using System;
using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Colors;
namespace CsMgd1
{
/// <summary>
/// 绘制板负筋
/// </summary>
public class MyClass : DrawJig
{
private Polyline entGangJin;
private Point2d startPt, endPt;
[CommandMethod("tt")]
public void BanFuJin()
{
CreateLayer("REIN", 1);
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("\n画板负筋 作者:carrot1983 2011年7月23日");
bool flag = true;
while (flag)
{
PromptPointOptions pointOpt = new PromptPointOptions("\n指定第一点<退出>: ");
pointOpt.AllowNone = true; //允许空输入
PromptPointResult pointRes = ed.GetPoint(pointOpt);
if (pointRes.Status != PromptStatus.OK)
{
flag = false;
}
else
{
// 取得起点
startPt = new Point2d(pointRes.Value.X, pointRes.Value.Y);
// 创建多段线
Point2d[] pt = new Point2d[4];
pt[0] = startPt;
pt[1] = startPt;
pt[2] = startPt;
pt[3] = startPt;
Point2dCollection pts = new Point2dCollection(pt);
int n = pts.Count;
entGangJin = new Polyline();
for (int i = 0; i < n; i++)
entGangJin.AddVertexAt(i, pts[i], 0, 100 * 0.3, 100 * 0.3);
entGangJin.Layer = "REIN";
// 提交事务处理
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
// 开始拖拽.
PromptResult resJig = ed.Drag(this);
if (resJig.Status == PromptStatus.OK)
{
btr.AppendEntity(entGangJin);
trans.AddNewlyCreatedDBObject(entGangJin, true);
trans.Commit();
}
}
}
}
}
protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
{
draw.Geometry.Draw(entGangJin);
return true;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
Database db = HostApplicationServices.WorkingDatabase;
JigPromptPointOptions optJigPoint = new JigPromptPointOptions("\n指定第二点:");
optJigPoint.Cursor = CursorType.Crosshair;
optJigPoint.BasePoint = new Point3d(startPt.X, startPt.Y, 0.0);
optJigPoint.UseBasePoint = true;
//
// 用AcquirePoint函数得到用户输入的点.
PromptPointResult resJigPoint = prompts.AcquirePoint(optJigPoint);
Point2d curPt = new Point2d(resJigPoint.Value.X, resJigPoint.Value.Y);
if (curPt != endPt)
{
// 重新设置多段线参数--------------------------------------------
// 线的起点 startPt 终点 curPt
Point2d pt1, pt2, pt3, pt4;
double ang = startPt.GetVectorTo(curPt).Angle;
pt1 = PolarPoint(startPt, (ang - Math.PI * 0.5), 100 * 2);
pt2 = startPt;
pt3 = curPt;
pt4 = PolarPoint(curPt, (ang - Math.PI * 0.5), 100 * 2);
entGangJin.SetPointAt(0, pt1);
entGangJin.SetPointAt(1, pt2);
entGangJin.SetPointAt(2, pt3);
entGangJin.SetPointAt(3, pt4);
// 保存当前点
endPt = curPt;
return SamplerStatus.OK;
}
else
return SamplerStatus.NoChange;
}
// 创建图层
public static void CreateLayer(string layerName, short colorIndex)
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForWrite);
ObjectId layerId;
if (lt.Has(layerName) == false)
{
LayerTableRecord ltr = new LayerTableRecord();
//图层名
ltr.Name = layerName;
//颜色
Color layerColor = Color.FromColorIndex(ColorMethod.ByColor, colorIndex);
ltr.Color = layerColor;
//图层打印
ltr.IsPlottable = true;
//线型
LinetypeTable tt = (LinetypeTable)trans.GetObject(db.LinetypeTableId, OpenMode.ForRead);
LinetypeTableRecord ttr;
try
{
db.LoadLineTypeFile("Continuous", "acadiso.lin");
}
catch
{
// 此处无需操作.
}
ttr = (LinetypeTableRecord)trans.GetObject(tt["Continuous"], OpenMode.ForRead);
ltr.LinetypeObjectId = ttr.ObjectId;
layerId = lt.Add(ltr);
trans.AddNewlyCreatedDBObject(ltr, true);
db.Clayer = layerId;
}
trans.Commit();
}
}
// 获取与给定点指定角度和距离的点.
public static Point2d PolarPoint(Point2d basePt, double angle, double distance)
{
double[] pt = new double[2];
pt[0] = basePt[0] + distance * Math.Cos(angle);
pt[1] = basePt[1] + distance * Math.Sin(angle);
Point2d point = new Point2d(pt[0], pt[1]);
return point;
}
// 将图形对象加入到模型空间的函数.
public static ObjectId AppendEntity(Entity ent)
{
Database db = HostApplicationServices.WorkingDatabase;
ObjectId entId;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
entId = btr.AppendEntity(ent);
trans.AddNewlyCreatedDBObject(ent, true);
trans.Commit();
}
return entId;
}
}
}