autocad.net 画多段线_AutoCAD .NET 二次开发实例(4) 多段线等间距批量法线绘制

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Autodesk.AutoCAD.DatabaseServices; // (Database, DBPoint, Line, Spline)using Autodesk.AutoCAD.Geometry; //(Point3d, Line3d, Curve3d)using Autodesk.AutoCAD.ApplicationServices; // (Application, Document)using Autodesk.AutoCAD.Runtime; // (CommandMethodAttribute, RXObject, CommandFlag)using Autodesk.AutoCAD.EditorInput; //(Editor, PromptXOptions, PromptXResult)using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;

using Autodesk.AutoCAD.Colors;

namespace _05_多段线批量等间距法线绘制

{

public class Class1

{

// 获取当前活动文档 Document doc = Application.DocumentManager.MdiActiveDocument;

[CommandMethod("BCED")]

public void BatchCurveEquiDistance()

{

Database db = HostApplicationServices.WorkingDatabase;

Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

ed.WriteMessage("欢迎使用多段线等间距绘制法线程序\n");

// 单选图元 ObjectId curveId = this.SelectEntity("\n请选择一个多段线对象", typeof(Polyline));

// 判断是否选取了多段线对象 if (curveId != null)

{

using (Transaction trans = db.TransactionManager.StartTransaction())

{

Curve curEnt = trans.GetObject(curveId, OpenMode.ForRead) as Curve;

// 获取多段线终点 Point3d endPoint = curEnt.EndPoint;

// 多段线长度 double lineLength = curEnt.GetDistAtPoint(endPoint);

// 间隔距离 double dis = 10;

// 第一个点距离起点距离 double myDis = dis;

while (myDis < lineLength)

{

//获取距离起点 mydis 的多段线上的一个点 Point3d point = curEnt.GetPointAtDist(myDis);

//一阶导数 即切线向量 Vector3d curVector = curEnt.GetFirstDerivative(point);

//获取切线方向的垂线向量 (从切线得到法线) Vector3d curPervector = curVector.GetPerpendicularVector();

//向量转化成标准单位向量 curPervector = curPervector.GetNormal();

//起始点+ 单位向量*距离 就是距离起始点 距离向量长度的 一个点 Point3d newPoint = point.Add(curPervector * 10);

// 绘制直线 this.CreateLine(point, newPoint, 1);

// 间隔递增 myDis = myDis + dis;

}

trans.Commit();

}

}

}

/// /// 单选实体 /// /// /// /// public ObjectId SelectEntity(string message, Type enttype)

{

PromptEntityOptions opts = new PromptEntityOptions(message);

if (enttype != null)

{

opts.SetRejectMessage("选择图元类型错误,请重新选择" + enttype.ToString());

opts.AddAllowedClass(enttype, true);

}

PromptEntityResult res = doc.Editor.GetEntity(opts);

return res.ObjectId;

}

/// /// 创建直线 /// /// 直线起点 /// 直线终点 /// 直线颜色 /// 图层名称 public void CreateLine(Point3d strartPoint, Point3d endPoint, int Color)

{

// 获得数据库 Database db = doc.Database;

// 开启事务处理 using (Transaction trans = db.TransactionManager.StartTransaction())

{

// 以读模式打开块表 BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

// 以写模式打开当前块表记录 model空间 BlockTableRecord btr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

Line line = new Line(strartPoint, endPoint);

line.ColorIndex = Color;

// 将创建的直线对象添加到Model空间 并进行事务登记 btr.AppendEntity(line);

trans.AddNewlyCreatedDBObject(line, true);

// 提交事务处理 trans.Commit();

}

}

}

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值