- 积分
- 2784
- 明经币
- 个
- 注册时间
- 2012-6-4
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
using System;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
[assembly: CommandClass(typeof(qian2014531.qian2014531a))]
namespace qian2014531
{
class qian2014531a
{
[CommandMethod("sf")]
public void qianpp()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityResult ent = ed.GetEntity("选择实体");
Entity entity = null;
using (Transaction transaction = db.TransactionManager.StartTransaction())
{
entity = (Entity)transaction.GetObject(ent.ObjectId, OpenMode.ForRead, true);
transaction.Commit();
}
// 拾取点,指定偏移的方向“点 ”
PromptPointResult pPtRes;
PromptPointOptions pPtOpts = new PromptPointOptions("");
pPtOpts.Message = "\nEnter the start point of the line: ";
pPtRes = doc.Editor.GetPoint(pPtOpts);
Point3d DirectOffsetPt = pPtRes.Value;
double offsetDist = 10;
Curve mycurve = entity as Curve;
Point3d offPtNew = new Point3d(DirectOffsetPt.X, DirectOffsetPt.Y, 0);
//这里开始判断偏移的方向
Point3d pt1 = mycurve.GetClosestPointTo(offPtNew, false);
pt1 = new Point3d(pt1.X, pt1.Y, 0);
Vector3d vec1 = offPtNew - pt1;
Vector3d vec2 = mycurve.GetFirstDerivative(pt1);
Vector3d vec3 = vec1.CrossProduct(vec2);
if (vec3.Z < 0)
{
offsetDist = -offsetDist;
}
if (mycurve.GetType() == typeof(Line))
{
offsetDist = -offsetDist;
}
//结束,这里开始判断偏移的方向
using (Transaction acTrans = db.TransactionManager.StartTransaction())
{
// 以读模式打开Block 表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
// 以写模式打开块表记录模型空间
BlockTableRecord acBlkTblRec;
acBlkTblRec =acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],OpenMode.ForWrite) as BlockTableRecord;
DBObjectCollection objCol = new DBObjectCollection();
objCol = mycurve.GetOffsetCurves(offsetDist);
// 遍历得到的新对象
foreach (Curve acEnt in objCol)
{
// 添加每个对象
acBlkTblRec.AppendEntity(acEnt);
acTrans.AddNewlyCreatedDBObject(acEnt, true);
}
acTrans.Commit();
}
}
}
}
|
|