CAD.NET API一日一练(4)初识多段线
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
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
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, 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,0,0,0);
}
for (int i = 0; i <= count-1; i++)
{
pl2.SetBulgeAt(i, -bulges);
}
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
}
}
感谢每天的分享,学习了 继续。。。。。。 请教:如何定义“多段线的顺时针还是逆时针”?
Mark,学习了!同问“顺时针”、“逆时针”指什么? netload出了问题,错误信息
令: netload
无法加载程序集。错误详细信息: System.BadImageFormatException:
未能加载文件或程序集“file:///D:\MyProgram\VS2010\testCAD\testCAD\bin\Debug\testCAD.dll”或它的
某一个依赖项。生成此程序集的运行时比当前加载的运行时新,无法加载此程序集。
文件名:“file:///D:\MyProgram\VS2010\testCAD\testCAD\bin\Debug\testCAD.dll”
在 System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase,
Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark,
Boolean throwOnFileNotFound, Boolean forIntrospection)
在 System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase,
Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark,
Boolean throwOnFileNotFound, Boolean forIntrospection)
在 System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence
assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
在 System.Reflection.Assembly.InternalLoadFrom(String assemblyFile, Evidence
securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm,
Boolean forIntrospection, StackCrawlMark& stackMark)
在 System.Reflection.Assembly.LoadFrom(String assemblyFile)
在 Autodesk.AutoCAD.Runtime.ExtensionLoader.Load(String fileName)
在 loadmgd()
警告: 程序集绑定日志记录被关闭。
要启用程序集绑定失败日志记录,请将注册表值 (DWORD)设置为 1。
注意: 会有一些与程序集绑定失败日志记录关联的性能损失。
要关闭此功能,请移除注册表值 。
yanglin112 发表于 2011-11-3 13:54 static/image/common/back.gif
Mark,学习了!同问“顺时针”、“逆时针”指什么?
其实就是线的倒向吧,
就是多段线是顺时针画出来的
还是逆时针画出来的 Student 发表于 2011-10-27 18:37 static/image/common/back.gif
请教:如何定义“多段线的顺时针还是逆时针”?
其实就是线的倒向吧,
就是多段线是顺时针画出来的
还是逆时针画出来的 好贴,一定支持。
页:
[1]