动态链接运行AutoCAD08后,可以实现功能,实现几次之后出现致命错误,或CAD死机。求解!!!感激不尽。

解决方案 »

  1.   

    using System;
    using System.Text;
    using System.Collections;
    using Autodesk.AutoCAD.Runtime;
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.Geometry;
    using Autodesk.AutoCAD.ApplicationServices;
    using Autodesk.AutoCAD.EditorInput;
    using Autodesk.AutoCAD.Colors;
    using DBTransMan = Autodesk.AutoCAD.DatabaseServices.TransactionManager;
    using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;[assembly: CommandClass(typeof(CsMgdAcad1.Commands))]namespace CsMgdAcad1
    {
        /// <summary>
        /// Summary description for Commands.
        /// </summary>
        public class Commands
        {
            public double zuobiaox1, zuobiaoy1, zuobiaox2, zuobiaoy2;
            public double i1, i2, i3, i4, i5, i6;
            public double x1, y1, x2, y2;
            public ObjectId ida;
            public String u1, u2;
            public int u3;
            public LineWeight u4;
            public double x3, y3, x4, y4;
            public ObjectId idb;        public static void Erase(ObjectId id)
            {
                Database db = id.OriginalDatabase;
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    Entity ent = (Entity)trans.GetObject(id, OpenMode.ForWrite);
                    ent.Erase();
                    trans.Commit();
                    
                }
            }
            public void CreateLine(double o1, double o2, double o3, double o4, String SelectedLayer, String SelectedType, int SelectedColor, LineWeight SelectedWeight)//导入最终确定的最远的两点坐标(z默认等于0)
            {
                Database DataBase = HostApplicationServices.WorkingDatabase;
                Editor Editor = Autodesk.AutoCAD.ApplicationServices. Application.DocumentManager.MdiActiveDocument.Editor;
                using (Transaction Transaction = DataBase.TransactionManager.StartTransaction())
                {
                    Line LineEntity = new Line(new Point3d(o1, o2, 0), new Point3d(o3, o4, 0));
                    LineEntity.Layer = SelectedLayer;
                    LineEntity.Linetype = SelectedType;
                    LineEntity.ColorIndex = SelectedColor;
                    LineEntity.LineWeight = SelectedWeight;
                    BlockTable BlockTable = (BlockTable)Transaction.GetObject(DataBase.BlockTableId, OpenMode.ForRead, false, false);
                    BlockTableRecord CurrentSpace = (BlockTableRecord)Transaction.GetObject(DataBase.CurrentSpaceId, OpenMode.ForWrite, false, false);
                    CurrentSpace.AppendEntity(LineEntity);
                    Transaction.AddNewlyCreatedDBObject(LineEntity, true);
                    Transaction.Commit();
                }
                
            }
            public void DaoruZuobiao(double x1,double y1,double x2, double y2, double x3, double y3,  double x4, double y4)
            {
                //获取五个数
                double juli12, juli13, juli14, juli23, juli24, juli34, xa, ya, xb, yb;
                juli12 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
                juli13 = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3);
                juli14 = (x1 - x4) * (x1 - x4) + (y1 - y4) * (y1 - y4);
                juli23 = (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3);
                juli24 = (x2 - x4) * (x2 - x4) + (y2 - y4) * (y2 - y4);
                juli34 = (x3 - x4) * (x3 - x4) + (y3 - y4) * (y3 - y4);            i1 = juli12;
                i2 = juli13;
                i3 = juli14;
                i4 = juli23;
                i5 = juli24;
                i6 = juli34;            double[] shuzu = new double[] { juli12, juli13, juli14, juli23, juli24, juli34 };            //调用Sort方法进行排序            
                double[] temp = Sort(shuzu);
                //最大距离为数组第一个数        
                double Max = temp[0];
                if (Math.Abs(Max - juli12) < 0.01)
                {
                    xa = x1;
                    ya = y1;
                    xb = x2;
                    yb = y2;
                    zuobiaox1 = xa;
                    zuobiaox2 = xb;
                    zuobiaoy1 = ya;
                    zuobiaoy2 = yb;
                }
                else if (Math.Abs(Max - juli13) < 0.01)
                {
                    xa = x1;
                    ya = y1;
                    xb = x3;
                    yb = y3;
                    zuobiaox1 = xa;
                    zuobiaox2 = xb;
                    zuobiaoy1 = ya;
                    zuobiaoy2 = yb;
                }
                else if (Math.Abs(Max - juli14) < 0.01)
                {
                    xa = x1;
                    ya = y1;
                    xb = x4;
                    yb = y4;
                    zuobiaox1 = xa;
                    zuobiaox2 = xb;
                    zuobiaoy1 = ya;
                    zuobiaoy2 = yb;
                }
                else if (Math.Abs(Max - juli23) < 0.01)
                {
                    xa = x2;
                    ya = y2;
                    xb = x3;
                    yb = y3;
                    zuobiaox1 = xa;
                    zuobiaox2 = xb;
                    zuobiaoy1 = ya;
                    zuobiaoy2 = yb;
                }
                else if (Math.Abs(Max - juli24) < 0.01)
                {
                    xa = x2;
                    ya = y2;
                    xb = x4;
                    yb = y4;                zuobiaox1 = xa;
                    zuobiaox2 = xb;
                    zuobiaoy1 = ya;
                    zuobiaoy2 = yb;
                }
                else if (Math.Abs(Max - juli34) < 0.01)
                {
                    xa = x3;
                    ya = y3;
                    xb = x4;
                    yb = y4;
                    zuobiaox1 = xa;
                    zuobiaox2 = xb;
                    zuobiaoy1 = ya;
                    zuobiaoy2 = yb;
                }
            }        
            //对任意数组进行排序的方法        
            public static double[] Sort(double[] number)
            {
                double[] t = number;
                double temp = 0;
                for (int i = 0; i < t.Length; i++)
                {
                    for (int j = i + 1; j < t.Length; j++)
                    {
                        if (t[i] < t[j])
                        {
                            temp = t[i];
                            t[i] = t[j];
                            t[j] = temp;
                        }
                    }
                }
                return t;
            }
            public void SelectFirstline()
            {
                Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
                Database DataBase = HostApplicationServices.WorkingDatabase;         
                using (Transaction tr = DataBase.TransactionManager.StartTransaction())
                {
                    PromptEntityOptions peo = new PromptEntityOptions("选择一侧图幅中的一条线");
                    ed.WriteMessage("\n");
                    PromptEntityResult per = ed.GetEntity(peo);
                    if (per.Status == Autodesk.AutoCAD.EditorInput.PromptStatus.OK)
                    {
                        Entity ent = tr.GetObject(per.ObjectId, OpenMode.ForRead) as Entity;
                        Point3d pickPt = new Point3d(0, 0, 0);
                        Point3dCollection snapPts = new Point3dCollection();
                        IntegerCollection geomIds = new IntegerCollection();
                        geomIds.Add(0);
                        ent.GetObjectSnapPoints(ObjectSnapModes.ModeEnd, 0, pickPt, pickPt, Matrix3d.Identity, snapPts, geomIds);                    u1 = ent.Layer;
                        u2 = ent.Linetype;
                        u3 = ent.ColorIndex;
                        u4 = ent.LineWeight;                    ida = per.ObjectId;//获得ID                    x1 = snapPts[0].X;
                        y1 = snapPts[0].Y;
                        x2 = snapPts[1].X;
                        y2 = snapPts[1].Y;                    tr.Commit();
                    }
                }
                
            }
      

  2.   

    public void SelectSecondline()
            {
                Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
                Database DataBase = HostApplicationServices.WorkingDatabase;
                    
                using (Transaction tr = DataBase.TransactionManager.StartTransaction())
                {
                    PromptEntityOptions peo = new PromptEntityOptions("选择另一侧图幅中的一条线");
                    ed.WriteMessage("\n");
                    PromptEntityResult per = ed.GetEntity(peo);
                    if (per.Status == Autodesk.AutoCAD.EditorInput.PromptStatus.OK)
                    {
                        Entity ent = tr.GetObject(per.ObjectId, OpenMode.ForRead) as Entity;
                        Point3d pickPt = new Point3d(0, 0, 0);
                        Point3dCollection snapPts = new Point3dCollection();
                        IntegerCollection geomIds = new IntegerCollection();
                        geomIds.Add(0);
                        ent.GetObjectSnapPoints(ObjectSnapModes.ModeEnd, 0, pickPt, pickPt, Matrix3d.Identity, snapPts, geomIds);                    idb = per.ObjectId;                    x3 = snapPts[0].X;
                        y3 = snapPts[0].Y;
                        x4 = snapPts[1].X;
                        y4 = snapPts[1].Y;                    tr.Commit();
                    }
                }
            }        public Commands()
            {
                //
                // TODO: Add constructor logic here
                //
            }        // Define Command "AsdkCmd1"
            [CommandMethod("1")]        public void shixian()//主函数,逐一实现各个类的功能
            {
                Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;            SelectFirstline();
                SelectSecondline();            DaoruZuobiao(x1,y1,x2,y2,x3,y3,x4,y4);            double k1 = i1;
                double k2 = i2;
                double k3 = i3;
                double k4 = i4;
                double k5 = i5;
                double k6 = i6;            if (x3 == x4 &&x4 == y3 && y3 == y4)
                {
                    ed.WriteMessage("您未点击第二条直线……");
                    return;
                }
                else if (k1 * k2 * k3 * k4 * k5 == 0 && k1 * k2 * k3 * k4 * k6 == 0 && k1 * k2 * k3 * k5 * k6 == 0 && k1 * k2 * k4 * k5 * k6 == 0 && k1 * k3 * k4 * k5 * k6 == 0 && k2 * k3 * k4 * k5 * k6 == 0)
                {
                    ed.WriteMessage("\n你可能两次点的是同一条直线……");
                    return;
                }
                else
                {
                    CreateLine(zuobiaox1,zuobiaoy1,zuobiaox2,zuobiaoy2,u1, u2, u3, u4);//调用函数在模型空间中加入直线。
                    Erase(ida);
                    Erase(idb);            }        }
        }
    }