c# CAD二次开发 模拟CAD移动图形, 通过圆现在注记,改变图形颜色
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Application = Autodesk.AutoCAD.ApplicationServices.Application;
using System.Windows.Forms;
namespace _7_属性查找
{
public class Class1
{
///
/// 通过文本和扩展属性进行搜索
///
[CommandMethod(“yesee”)]
public void selectdemo()
{
Form1selecte form = new Form1selecte();
form.Show();
}
///
/// 更新图形颜色
///
///
private void changecolor(SelectionSet sset)
{
ObjectId[] ids = sset.GetObjectIds();
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
for (int i = 0; i < ids.Length; i++)
{
Entity ent = (Entity)ids[i].GetObject(OpenMode.ForWrite);文章来源:https://www.toymoban.com/news/detail-465587.html
ent.ColorIndex = 1;
DBText textEntity = ent as DBText;
string sTxt = textEntity.TextString;
}
trans.Commit();
}
}
/// <summary>
/// 通过选择圆来选择
/// </summary>
[CommandMethod("yese1")]
public void selectdemo1()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
//通过过滤器选择圆
TypedValue[] values = new TypedValue[]
{
new TypedValue((int)DxfCode.Start,"circle")//选择图形为圆
};
SelectionFilter filter = new SelectionFilter(values);
PromptSelectionResult psr = ed.GetSelection(filter);
List<ObjectId> ids = new List<ObjectId>();
if (psr.Status == PromptStatus.OK)
{
//遍历每个圆,将每个
SelectionSet sset = psr.Value;
List<Point3d> points = this.getpoint(sset);
for (int i = 0; i < points.Count; i++)
{
PromptSelectionResult ss1 = ed.SelectCrossingWindow(points.ElementAt(i), points.ElementAt(i));
ids.AddRange(ss1.Value.GetObjectIds());
}
}
this.changecolor1(ids);
}
/// <summary>
/// 通过顶点选择集
/// </summary>
/// <param name="sset"></param>
/// <returns></returns>
private List<Point3d> getpoint(SelectionSet sset)
{
List<Point3d> points = new List<Point3d>();
ObjectId[] ids = sset.GetObjectIds();
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
for (int i = 0; i < ids.Length; i++)
{
Entity ent = (Entity)ids[i].GetObject(OpenMode.ForWrite);
Point3d creter = ((Circle)ent).Center;
double radius = ((Circle)ent).Radius;
points.Add(new Point3d(creter.X + radius, creter.Y, creter.Z));
}
trans.Commit();
}
return points;
}
/// <summary>
/// 更新图形颜色
/// </summary>
/// <param name="sset"></param>
private void changecolor1(List<ObjectId> ids)
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
for (int i = 0; i < ids.Count; i++)
{
Entity ent = (Entity)ids[i].GetObject(OpenMode.ForWrite);
ent.ColorIndex = 3;
}
trans.Commit();
}
}
/// <summary>
/// 动态交互,移动图形
/// </summary>
[CommandMethod("yemove", CommandFlags.UsePickSet)]
public void yemove()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
//选择在命令前的图形
PromptSelectionResult psr = ed.SelectImplied();
//如果执行命令前没有选择对象,则执行选择
if (psr.Status != PromptStatus.OK)
{
psr = ed.GetSelection();
}
if (psr.Status != PromptStatus.OK) return;
//获取用户指定的基础点
Point3d pointbase = new Point3d(0, 0, 0);
PromptPointOptions ppo = new PromptPointOptions("\n 指定基点或[位移(D)]<位移>:");
ppo.AllowNone = true;
PromptPointResult ppr = ed.GetPoint(ppo);
//判断用户指定基础点的输入
if (ppr.Status == PromptStatus.Cancel) return;
if (ppr.Status == PromptStatus.OK) pointbase = ppr.Value;
//获取选择图形的图形对象
List<Entity> entlist = new List<Entity>();
//把选中的ID赋给ids
ObjectId[] ids = psr.Value.GetObjectIds();
entlist = this.getentity(ids);
//改变源对象的颜色
this.lowcolor(entlist, 211);
//声明一个移动类
MoveJig movejig = new MoveJig(entlist, pointbase);
PromptResult pr = ed.Drag(movejig);
}
/// <summary>
/// 将选择的对象转成Entity
/// </summary>
/// <param name="ids">图形列表</param>
/// <returns></returns>
private List<Entity> getentity(ObjectId[] ids)
{
Database db = HostApplicationServices.WorkingDatabase;
List<Entity> enlist = new List<Entity>();
using (Transaction trans = db.TransactionManager.StartTransaction())
{
for (int i = 0; i < ids.Length; i++)
{
Entity ent = (Entity)ids[i].GetObject(OpenMode.ForWrite);
enlist.Add(ent);
}
}
return enlist;
}
/// <summary>
/// 改变图形对象的颜色
/// </summary>
/// <param name="enlist">图形对象</param>
/// <param name="colorindex">颜色值</param>
private void lowcolor(List<Entity> enlist, int colorindex)
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
for (int i = 0; i < enlist.Count; i++)
{
Entity ent = (Entity)enlist[i].ObjectId.GetObject(OpenMode.ForWrite);
ent.ColorIndex = colorindex;
}
trans.Commit();
}
}
}
}文章来源地址https://www.toymoban.com/news/detail-465587.html
到了这里,关于c# CAD二次开发 模拟CAD移动图形, 通过圆现在注记,改变图形颜色的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!