📄 cadjig.txt
字号:
这个周末主要的学习内容就是研究c# arx中拖动效果的显示,下面是代码和介绍:
1,实体绘制的拖动效果(多段线为例)
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
namespace MyPlineApp
{
public class MyPlineCmds
{
class PlineJig : EntityJig
{
//维护一个顶点列表,不一定需要,因为这会存储到polyline,不会相反地影响性能
Point3dCollection m_pts;
//用于存储新近的点的变量,不一定需要,但更容易使用
Point3d m_tempPoint;
Plane m_plane;
public PlineJig(Matrix3d ucs)
: base(new Polyline())
{
m_pts = new Point3dCollection();
Point3d origin = new Point3d(0, 0, 0); //创建一个临时plane来帮助计算
Vector3d normal = new Vector3d(0, 0, 1);
normal = normal.TransformBy(ucs); //法线变换到指定的UCS下
m_plane = new Plane(origin, normal);
//创建polyline,设置其属性为默认值,并添加虚构的顶点
Polyline pline = Entity as Polyline; //as把Entity转化为as后指定的实体类型
//设置实体的颜色,层等属性为其当前驻留的数据库的默认值,若实体不在数据库//中,就设置为当前数据库的值
pline.SetDatabaseDefaults();
pline.Normal = normal;
pline.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0);
}
//采样函数:用于在Drag的时候不断地采样
protected override SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptPointOptions jigOpts = new JigPromptPointOptions();
jigOpts.UserInputControls = (UserInputControls.Accept3dCoordinates |
UserInputControls.NullResponseAccepted |
UserInputControls.NoNegativeResponseAccepted);
if (m_pts.Count == 0) //请求第一个顶点
{
jigOpts.Message = "\nStart point of polyline: ";
} else if (m_pts.Count > 0) //对随后的点,顺次以前一点为基点,以实现Drag
{
jigOpts.BasePoint = m_pts[m_pts.Count - 1];
jigOpts.UseBasePoint = true;
jigOpts.Message = "\nPolyline vertex: ";
} else //应该不会发生的情况
return SamplerStatus.Cancel;
//得到这次采样的点
PromptPointResult res = prompts.AcquirePoint(jigOpts);
//检查采样点是否变化,这是为了减少闪动
if (m_tempPoint == res.Value) {
return SamplerStatus.NoChange;
} else if (res.Status == PromptStatus.OK) {
m_tempPoint = res.Value;
return SamplerStatus.OK;
}
return SamplerStatus.Cancel;
}
//更新函数:用采样得到的数据更新被拖动的实体
protected override bool Update()
{
//更新顶点,先要把点映射到指定的平面,注意把原来的虚构的顶点代替掉,
Polyline pline = Entity as Polyline;
pline.SetPointAt(pline.NumberOfVertices - 1, m_tempPoint.Convert2d(m_plane));
return true;
}
public Entity GetEntity()
{
return Entity;
}
//总是设置polyline为一个虚构的点,在完成Drag后,此点会被移除
public void AddLatestVertex()
{
//添加最近被选择的点到一个内部列表中,即m_pts,这个点已经
//被添加到polyline中了
m_pts.Add(m_tempPoint);
Polyline pline = Entity as Polyline;
//创建一个虚构的点,此点的值可是任意的值
pline.AddVertexAt(pline.NumberOfVertices, new Point2d(0, 0), 0, 0, 0);
}
public void RemoveLastVertex()//用于在完成Drag后,移除最后个虚构的点
{
Polyline pline = Entity as Polyline;
pline.RemoveVertexAt(m_pts.Count);
}
}
}
[CommandMethod("MYPOLY")]
public void MyPolyJig()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
//得到当前的UCS矩阵,并传给Jig
Matrix3d ucs = ed.CurrentUserCoordinateSystem;
//创建Jig对象
PlineJig jig = new PlineJig();
bool bSuccess = true, bComplete = false;
do //通过循环来直接设置polyline的顶点
{
PromptResult res = ed.Drag(jig); //拖动
//拖动成功后就添加得到的点
bSuccess = (res.Status == PromptStatus.OK);
if (bSuccess)
jig.AddLatestVertex();
//若没有输入了,表示拖动结束
bComplete = (res.Status == PromptStatus.None);
if (bComplete) //在添加polyline前,先清除最后一个点
jig.RemoveLastVertex();
} while (bSuccess && !bComplete);
//若JIG成功完成,添加这个polyline
if (bComplete) {
Database db = doc.Database;
Transaction tr = db.TransactionManager.StartTransaction();
using (tr) {
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId,
OpenMode.ForRead, false);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(
bt[BlockTableRecord.ModelSpace],
OpenMode.ForWrite, false);
btr.AppendEntity(jig.GetEntity());
tr.AddNewlyCreatedDBObject(jig.GetEntity(), true);
tr.Commit();
}
}
}
}
}
2.对实体集操作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using System.Collections.Specialized;
using Autodesk.AutoCAD.Geometry;
using DNA;
namespace ClassLibrary1
{
public class LineJigrawJig
{
Point3d startPoint, endPoint;
List<Line> lines = new List<Line>();
int rows, cols;
[CommandMethod("Test")]
public void Test()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Database db = HostApplicationServices.WorkingDatabase;
rows = ed.GetInteger("\n请输入网格行数:").Value;
cols = ed.GetInteger("\n请输入网格列数:").Value;
PromptPointResult pRes = ed.GetPoint("请选择第一点:");
startPoint = pRes.Value;
lines.Clear();
for (int i = 1; i < rows; i++)
{
Line line = new Line(startPoint, startPoint);
line.ColorIndex = 1;
lines.Add(line);
}
for (int j = 1; j < cols; j++)
{
Line line = new Line(startPoint, startPoint);
line.ColorIndex = 2;
lines.Add(line);
}
if (pRes.Status == PromptStatus.OK)
{
PromptResult resJig = ed.Drag(this);
if (resJig.Status == PromptStatus.OK)
{
Tools.AddEntities(lines.ToArray());
}
}
}
protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
{
foreach (Line line in lines)
{
draw.Geometry.Draw(line);
}
return true;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptPointOptions optJig = new JigPromptPointOptions("\n请指定第二点");
PromptPointResult resJigDis = prompts.AcquirePoint(optJig);
Point3d tempPt = resJigDis.Value;
if (resJigDis.Status == PromptStatus.Cancel)
{
return SamplerStatus.Cancel;
}
if (endPoint != tempPt)
{
endPoint = tempPt;
double xDist = (endPoint.X - startPoint.X) / cols;
double yDist = (endPoint.Y - startPoint.Y) / rows;
//画水平线
for (int i = 1; i < rows; i++)
{
Point3d pt1 = new Point3d(startPoint.X, startPoint.Y + yDist * i, 0);
Point3d pt2 = new Point3d(endPoint.X, startPoint.Y + yDist * i, 0);
lines[i].StartPoint = pt1;
lines[i].EndPoint = pt2;
}
//画竖直线
for (int i = 1; i < cols; i++)
{
Point3d pt1 = new Point3d(startPoint.X + xDist * i, startPoint.Y, 0);
Point3d pt2 = new Point3d(startPoint.X + xDist * i, endPoint.Y, 0);
lines[i + rows].StartPoint = pt1;
lines[i + rows].EndPoint = pt2;
}
return SamplerStatus.OK;
}
else
{
return SamplerStatus.NoChange;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -