- 积分
- 24557
- 明经币
- 个
- 注册时间
- 2004-3-17
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
楼主 |
发表于 2010-6-9 20:32:00
|
显示全部楼层
本帖最后由 作者 于 2010-7-16 21:13:43 编辑
感觉直接用Ge库的曲线应该也可以做,改写代码如下
-
- /// <summary>
- /// 为优化多段线倒角
- /// </summary>
- /// <param name="polyline">优化多段线</param>
- /// <param name="index">顶点索引号</param>
- /// <param name="radius">倒角半径</param>
- /// <param name="isFillet">倒角类型</param>
- public static void ChamferAt(this Polyline polyline, int index, double radius, bool isFillet)
- {
- if (index < 1 || index > polyline.NumberOfVertices - 2)
- throw new System.Exception("错误的索引号");
- if (polyline.GetSegmentType(index - 1) != SegmentType.Line || polyline.GetSegmentType(index) != SegmentType.Line)
- throw new System.Exception("非直线段不能倒角");
- //获取当前索引号的前后两段直线,并组合为Ge复合曲线
- Curve3d[] c3ds =
- new Curve3d[]
- {
- polyline.GetLineSegmentAt(index - 1),
- polyline.GetLineSegmentAt(index)
- };
- var cc3d = new CompositeCurve3d(c3ds);
- //试倒直角
- //子曲线的个数有三种情况:
- //1、=3时倒角方向正确
- //2、=2时倒角方向相反
- //3、=0或为直线时失败
- c3ds =
- cc3d.GetTrimmedOffset
- (
- radius,
- Vector3d.ZAxis,
- OffsetCurveExtensionType.Chamfer
- );
- if (c3ds.Length > 0 && c3ds[0] is CompositeCurve3d)
- {
- var newcc3d = c3ds[0] as CompositeCurve3d;
- c3ds = newcc3d.GetCurves();
- if (c3ds.Length == 3)
- {
- c3ds =
- cc3d.GetTrimmedOffset
- (
- -radius,
- Vector3d.ZAxis,
- OffsetCurveExtensionType.Chamfer
- );
- if (c3ds.Length == 0 || c3ds[0] is LineSegment3d)
- {
- throw new System.Exception("倒角半径过大");
- }
- }
- else if (c3ds.Length == 2)
- {
- radius = -radius;
- }
- }
- else
- {
- throw new System.Exception("倒角半径过大");
- }
- //GetTrimmedOffset会生成倒角+偏移,故先反方向倒角,再倒回
- c3ds =
- cc3d.GetTrimmedOffset
- (
- -radius,
- Vector3d.ZAxis,
- OffsetCurveExtensionType.Extend
- );
- OffsetCurveExtensionType type =
- isFillet ?
- OffsetCurveExtensionType.Fillet : OffsetCurveExtensionType.Chamfer;
- c3ds =
- c3ds[0].GetTrimmedOffset
- (
- radius,
- Vector3d.ZAxis,
- type
- );
- //将结果Ge曲线转为Db曲线,并将相关的数值反映到原曲线
- Polyline plTemp = c3ds[0].ToCurve() as Polyline;
- polyline.RemoveVertexAt(index);
- polyline.AddVertexAt(index, plTemp.GetPoint2dAt(1), plTemp.GetBulgeAt(1), 0, 0);
- polyline.AddVertexAt(index + 1, plTemp.GetPoint2dAt(2), 0, 0, 0);
- }
-
-
- [CommandMethod("CTest")]
- public void test()
- {
- var db = HostApplicationServices.WorkingDatabase;
- var doc = Application.DocumentManager.GetDocument(db);
- var ed = doc.Editor;
- var resEnt = ed.GetEntity("\n请选择优化多段线:");
- var resInt = ed.GetInteger("\n请输入索引号:");
- bool isFillet = false;
- PromptDoubleOptions opt = new PromptDoubleOptions("\n请输入倒角半径:");
- opt.Keywords.Add("F");
- var resDbl = ed.GetDouble(opt);
- if (resDbl.Status == PromptStatus.Keyword)
- {
- isFillet = true;
- resDbl = ed.GetDouble(opt);
- }
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- Polyline pl = resEnt.ObjectId.GetObject(OpenMode.ForWrite) as Polyline;
- ChamferAt(pl, resInt.Value, resDbl.Value, isFillet);
- tr.Commit();
- }
- }
命令行的输入:
命令: ctest
请选择优化多段线:
请输入索引号: 3
请输入倒角半径 [F]: f
请输入倒角半径 [F]: 5
命令: ctest
请选择优化多段线:
请输入索引号: 2
请输入倒角半径 [F]: 5
倒角效果:
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
x
|