CAD.NET API一日一练(2)回顾用户交互,了解对实体的操作
本帖最后由 mrhvslisp 于 2011-10-23 21:42 编辑using System;
using System.Collections.Generic;
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 supercopy
public static void SuperCopy()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction trans = doc.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt,OpenMode.ForWrite);
PromptSelectionResult result = ed.GetSelection();
ObjectIdCollection idcoll = new ObjectIdCollection();
if (result.Status == PromptStatus.OK)
{
SelectionSet ss = result.Value;
foreach (SelectedObject obj in ss)
{
idcoll.Add(obj.ObjectId);
}
}
PromptPointOptions ptoptions = new PromptPointOptions("");
ptoptions.AllowArbitraryInput = false;
ptoptions.Message = "\n请选择起点";
PromptPointResult ptresult = ed.GetPoint(ptoptions);
Point3dCollection ptcoll = new Point3dCollection();
if(ptresult.Status==PromptStatus .OK)
{
Point3d pt1 = new Point3d(ptresult .Value.X,ptresult.Value.Y,0);
ptcoll.Add(pt1);
}
else if (ptresult.Status == PromptStatus.Cancel)
{
return;
}
ptoptions.Message = "\n请选择终点";
PromptPointResult pt2result = ed.GetPoint (ptoptions);
if (pt2result.Status == PromptStatus.OK)
{
Point3d pt2 = new Point3d(pt2result.Value.X, pt2result.Value.Y,0);
ptcoll.Add(pt2);
}
else if (ptresult.Status == PromptStatus.Cancel)
{
return;
}
Vector3d vec = new Vector3d(ptcoll.X-ptcoll.X,ptcoll.Y-ptcoll.Y,0);
PromptDistanceOptions distanceoptions = new PromptDistanceOptions("");
distanceoptions.Message = "\n请输入复制间隔";
distanceoptions.AllowNone = true;
distanceoptions.DefaultValue = 8;
distanceoptions.AllowArbitraryInput = false;
PromptDoubleResult distresult = ed.GetDistance(distanceoptions);
if (distresult.Status == PromptStatus.OK | distresult.Status == PromptStatus.None)
{
double length = distresult.Value;
int i = Convert.ToInt32(Math.Floor(vec.Length / length));
vec = vec / i;
for (int m = 1; m <= i; m++)
{
foreach (ObjectId id in idcoll)
{
Entity entity = trans.GetObject(id, OpenMode.ForWrite) as Entity;
Matrix3d mt = Matrix3d.Displacement(vec*m);
Entity entcopy = entity.GetTransformedCopy(mt);
btr.AppendEntity(entcopy);
trans.AddNewlyCreatedDBObject(entcopy, true);
}
}
}
ed.UpdateScreen();
trans.Commit();
}
}
#endregion
}
}
加精了?
哈哈
感谢版主 嗯 不错,看过~ 学习中,希望能进步 大多数都是C#的代码,VB.NET的怎么就那么少呢?看来还是C#有人气啊! 看来c #人气旺啊
好贴,一定支持。
页:
[1]