c# CAD二次开发 模拟CAD移动图形, 通过圆现在注记,改变图形颜色

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);

                ent.ColorIndex = 1;
                DBText textEntity = ent as DBText;
                string sTxt = textEntity.TextString;
            }
            trans.Commit();
        }
    }
    /// 
    /// 通过选择圆来选择
    /// 
    [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 ids = new List();
        if (psr.Status == PromptStatus.OK)
        {
            //遍历每个圆,将每个
            SelectionSet sset = psr.Value;
            List 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);
    }
    /// 
    /// 通过顶点选择集
    /// 
    /// 
    /// 
    private List getpoint(SelectionSet sset)
    {
        List points = new List();
        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;
    }
    /// 
    /// 更新图形颜色
    /// 
    /// 
    private void changecolor1(List 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();
        }
    }
    /// 
    /// 动态交互,移动图形
    /// 
    [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 entlist = new List();
        //把选中的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);
    }
    /// 
    /// 将选择的对象转成Entity
    /// 
    /// 图形列表
    /// 
    private List getentity(ObjectId[] ids)
    {
        Database db = HostApplicationServices.WorkingDatabase;
        List enlist = new List();
        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;
    }
    /// 
    /// 改变图形对象的颜色
    /// 
    /// 图形对象
    /// 颜色值
    private void lowcolor(List 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();
        }
    }
}

}

你可能感兴趣的:(c#,linq,开发语言)