- 积分
- 2124
- 明经币
- 个
- 注册时间
- 2011-4-12
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.Linq;
- using System.Text;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.EditorInput;
- namespace Base
- {
- public class Class1
- {
- //判断多段线的顺时针还是逆时针
- #region clockwise
- [CommandMethod("clockwise")]
- public void clockwise()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Editor ed = doc.Editor;
- Database db = doc.Database;
- PromptEntityOptions options = new PromptEntityOptions("");
- options.Message = "\n请选择多段线";
- options.SetRejectMessage("\n所选择实体不是多段线,请重新选择");
- options.AddAllowedClass(typeof(Polyline), true);
- PromptEntityResult result = ed.GetEntity(options);
- int i = 1;
- if (result.Status == PromptStatus.OK)
- {
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- Polyline pl = trans.GetObject(result.ObjectId, OpenMode.ForRead) as Polyline;
- Vector3d vec1 = pl.GetPoint3dAt(0).GetAsVector();
- Vector3d vec2 = pl.GetPoint3dAt(1).GetAsVector();
- Vector3d vec = vec1.CrossProduct(vec2);
- if (vec.Z > 0)
- {
- ed.WriteMessage("\n逆时针");
- }
- else if (vec.Z < 0)
- {
- ed.WriteMessage("\n顺时针");
- }
- else
- {
- while (vec.Z == 0 & i + 1 <= (Convert.ToInt32(pl.EndParam)))
- {
- vec1 = pl.GetPoint3dAt(i).GetAsVector();
- vec2 = pl.GetPoint3dAt(i + 1).GetAsVector();
- vec = vec1.CrossProduct(vec2);
- if (vec.Z > 0)
- {
- ed.WriteMessage("\n逆时针");
- }
- else if (vec.Z < 0)
- {
- ed.WriteMessage("\n顺时针");
- }
- i++;
- }
- if (vec.Z == 0 & i == (Convert.ToInt32(pl.EndParam)))
- {
- ed.WriteMessage("\n该多段线通过原点且斜率恒定,无顺逆之分");
- }
- }
- }
- }
- }
- //多段线的反向
- #region plreverse
- [CommandMethod("plreverse")]
- public void plreverse()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Editor ed = doc.Editor;
- Database db = doc.Database;
- PromptEntityOptions options = new PromptEntityOptions("");
- options.Message = "\n请选择多段线";
- options.SetRejectMessage("\n所选择实体不是多段线,请重新选择");
- options.AddAllowedClass(typeof(Polyline), true);
- PromptEntityResult result = ed.GetEntity(options);
- if (result.Status == PromptStatus.OK)
- {
- 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);
- Polyline pl2 = new Polyline();
- btr.AppendEntity(pl2);
- trans.AddNewlyCreatedDBObject(pl2,true);
- Polyline pl = trans.GetObject(result.ObjectId,OpenMode.ForWrite)as Polyline;
- int count=Convert .ToInt32(pl.EndParam);
- Point2dCollection ptcoll = new Point2dCollection();
- DoubleCollection bulges = new DoubleCollection();
- for (int i = 0; i <= count; i++)
- {
- ptcoll.Add(pl.GetPoint2dAt(i));
- bulges.Add(pl.GetBulgeAt(i));
- }
- for (int i =0; i <= count; i++)
- {
- pl2.AddVertexAt(i,ptcoll[count-i],0,0,0);
- }
- for (int i = 0; i <= count-1; i++)
- {
- pl2.SetBulgeAt(i, -bulges[count - i - 1]);
- }
-
- pl2.Draw();
- /* for(int i=0;i<=count;i++)
- {
- ed.WriteMessage("pl1的第{0}点凸度为{1},pl2的第{3}点凸度为{4}",i,pl.GetBulgeAt(i),count-i,pl2.GetBulgeAt(count-i));
- }*/
-
- //pl.Erase();
- trans.Commit();
- }
- }
- }
- #endregion
- }
- }
|
|