找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 486|回复: 5

[ARX函数]:C# Arx

[复制链接]
发表于 2004-9-23 15:32:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
#region Using directives

using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using DBTransMan = Autodesk.AutoCAD.DatabaseServices.TransactionManager;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
#endregion

namespace ZHFArxLibrary
{
    public class Arx
    {
        private Database db;
        private DBTransMan tm;
        private Transaction myT;
        BlockTable bt;
        BlockTableRecord btr;
        public Arx()
        {
            db = HostApplicationServices.WorkingDatabase;
            tm = db.TransactionManager;

        }
        #region Line
        public Line AddLine(Point3d pt1, Point3d pt2)
        {
            Line line;
            try
            {
                Initialize();
                line = new Line(pt1, pt2);
                AddEntity(line);

            }
            finally
            {
                DisposeAll();
            }
            return line;
        }
        public Line AddLine(Point2d pt1, Point2d pt2)
        {
            Line line;
            line = AddLine(new Point3d(pt1.X, pt1.Y, 0), new Point3d(pt2.X, pt2.Y, 0));
            return line;
        }
        public Line AddLine(Point3d pt1, double x, double y)
        {
            Line line;
            Point3d pt2 = new Point3d();
            pt2.X = pt1.X + x;
            pt2.Y = pt1.Y + y;
            pt2.Z = pt1.Z;
            line = AddLine(pt1, pt2);
            return line;
        }
        public Line AddLineR(Point3d pt1, double angle, double length)
        {
            Line line;
            Point3d pt2 = new Point3d();
            pt2 = GetPointAR(pt1, angle, length);
            line = AddLine(pt1, pt2);
            return line;
        }
        #endregion
        
        #region Polyline
        public Polyline AddLWPolyline(Point2dCollection ptArr, double width)
        {
            Polyline pline = new Polyline();
            try
            {
                Initialize();
                for (int i = 0; i < ptArr.Count; i++)
                {
                    pline.AddVertexAt(i, ptArr, 0, width, width);
                }
                AddEntity(pline);
            }
            finally
            {
                DisposeAll();
            }
            return pline;
        }
        public Polyline AddLWPolyline(Point2d ptStart, Point2d ptEnd, double width)
        {
            Polyline pline;
            Point2dCollection ptArr = new Point2dCollection(); ;
            ptArr.Add(ptStart);
            ptArr.Add(ptEnd);
            pline = AddLWPolyline(ptArr, width);
            return pline;
        }
        public Polyline3d AddPolyline(Point3dCollection ptArr)
        {
            Polyline3d pline;
            try
            {
                Initialize();
                pline = new Polyline3d(Poly3dType.SimplePoly, ptArr, false);
                AddEntity(pline);
            }
            finally
            {
                DisposeAll();
            }
            return pline;

        }
        public Polyline3d AddPolyline(Point3d pt1, Point3d pt2)
        {
            Polyline3d pline;
            Point3dCollection ptArr = new Point3dCollection();
            ptArr.Add(pt1);
            ptArr.Add(pt2);
            pline = AddPolyline(ptArr);
            return pline;
        }
        public Polyline AddRectangle(Point2d pt1, Point2d pt2, double width)
        {
            Polyline pline;
            Point2dCollection ptArr = new Point2dCollection();
                   Point2d pt3 = new Point2d(pt2.X,pt1.Y);
                    Point2d pt4 = new Point2d(pt1.X, pt2.Y);
                    ptArr.Add(pt1);
                    ptArr.Add(pt3);
                    ptArr.Add(pt2);
                    ptArr.Add(pt4);
                    ptArr.Add(pt1);
                    pline = AddLWPolyline(ptArr, width);
                    

        
            return pline;
        }
        public Polyline AddRectangle(Point2d pt1, Point2d pt2)
        {
            return AddRectangle(pt1, pt2, 0);
        }
        public Polyline AddPolygon(Point2d center, int number, double radius, double width, double angle)
        {
            Polyline pline;
            Point2dCollection ptArr = new Point2dCollection();
            double ang;
            ang = 2 * Math.PI / number;
            Point2d pt2d = new Point2d();
            for (int i = 0; i <=number; i++)
            {

                pt2d.X = center.X + radius * Math.Cos(i * ang);
                pt2d.Y = center.Y + radius * Math.Sin(i * ang);
                ptArr.Add(pt2d);
            }
            pline = AddLWPolyline(ptArr, width);


            return pline;
        }
        public Polyline AddPolygon(Point2d center, int number, double radius)
        {
            return AddPolygon(center, number, radius, 0, 0);
        }
        public Polyline AddPolygon(Point2d center, int number, double radius, double width)
        {
            return AddPolygon(center, number, radius, width, 0);
        }
        #endregion
   
        public void Initialize()
        {
            myT = tm.StartTransaction();
            bt = (BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForRead, false);
            btr = (BlockTableRecord)tm.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false);
        }
        public void DisposeAll()
        {
            bt.Close();
            btr.Close();
            myT.Dispose();
        }
        public Point3d GetPointAR(Point3d pt1, double angle, double length)
        {
            Point3d pt2 = new Point3d();
            pt2.X = pt1.X + length * Math.Cos(angle);
            pt2.Y = pt1.Y + length * Math.Sin(angle);
            pt2.Z = pt1.Z;
            return pt2;
        }
        public Point3d GetPoint(Point3d pt, string prompt)
        {
            double[] d = new double[3];
            d[0] = pt.X;
            d[1] = pt.Y;
            d[2] = pt.Z;
            Array a = (Array)((AcadApplication)Application.AcadApplication).ActiveDocument.Utility.GetPoint(d, prompt);
            Point3d point = new Point3d();
            point.X = (double)a.GetValue(0);
            point.Y = (double)a.GetValue(1);
            point.Z = (double)a.GetValue(2);
            return point;
        }
        public Point3d GetPoint(string prompt)
        {
            Array a = (Array)((AcadApplication)Application.AcadApplication).ActiveDocument.Utility.GetPoint(Type.Missing, prompt);
            Point3d point = new Point3d();
            point.X = (double)a.GetValue(0);
            point.Y = (double)a.GetValue(1);
            point.Z = (double)a.GetValue(2);
            return point;
        }
        public double GetReal(string prompt)
        {
            double d = ((AcadApplication)Application.AcadApplication).ActiveDocument.Utility.GetReal(prompt);
            return d;
        }
        public double[] Point3dToObject(Point3d pt)
        {
            double[] d = new double[3];
            d[0] = pt.X;
            d[1] = pt.Y;
            d[2] = pt.Z;
            return d;
        }
        public double AngleFromAxis(Point3d pt1, Point3d pt2)
        {
            double angle = ((AcadApplication)Application.AcadApplication).ActiveDocument.Utility.AngleFromXAxis(Point3dToObject(pt1), Point3dToObject(pt2));
            return angle;
        }
        public Point3d GetCenterOf3Pt(Point3d pt1, Point3d pt2, Point3d pt3, out double radius)
        {
            double xysm, xyse, xy;
            Point3d center;
            xy = pt1.X * pt1.X + pt1.Y * pt1.Y;
            xyse = xy - pt3.X * pt3.X - pt3.Y * pt3.Y;
            xysm = xy - pt2.X * pt2.X - pt2.Y * pt2.Y;
            xy = (pt1.X - pt2.X) * (pt1.Y - pt3.Y) - (pt1.X - pt3.X) * (pt1.Y - pt2.Y);
            if (Math.Abs(xy) < 0.000001)
            {
                CommandLinePrompts.Message("\n所输入的参数无法创建图形!");
                radius = 0;
                Environment.Exit(0);
            }
            else
            {
                center.X = (xysm * (pt1.Y - pt3.Y) - xyse * (pt1.Y - pt2.Y)) / (2 * xy);
                center.Y = (xyse * (pt1.X - pt2.X) - xysm * (pt1.X - pt3.X)) / (2 * xy);
                center.Z = 0;
                radius = Math.Sqrt((pt1.X - center.X) * (pt1.X - center.X) + ((pt1.Y - center.Y) * (pt1.Y - center.Y)));
                if (radius < 0.000001)
                {
                    CommandLinePrompts.Message("半径过小!");
                    Environment.Exit(0);
                }

            }
            return center;

        }
        public Point3d GetMidPoint(Point3d pt1, Point3d pt2)
        {
            Point3d pt = new Point3d();
            pt.X = (pt1.X + pt2.X) / 2;
            pt.Y = (pt1.Y + pt2.Y) / 2;
            pt.Z = 0;
            return pt;
        }
        public void AddEntity(Entity entity)
        {
            btr.AppendEntity(entity);
            tm.AddNewlyCreatedDBObject(entity, true);
            myT.Commit();
        }

    }
}
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
发表于 2004-9-23 15:40:28 | 显示全部楼层
楼上不能解释一下各部分的意义。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2004-9-24 17:15:31 | 显示全部楼层
没有学过C#,不过对用C#在cad下编程倒是蛮感兴趣的
要学的太多了,我是忙不过来了
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2004-9-25 14:35:31 | 显示全部楼层
不知用C++能不能编译
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2004-9-25 14:53:06 | 显示全部楼层
ObjectArx2005下有三个.Net的例子,调用的是托管类,和一般意义上的Arx有区别,功能要弱一些,但是做界面简单,各位有兴趣的话可以研究一下
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2004-9-26 11:37:59 | 显示全部楼层
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-9-22 03:41 , Processed in 0.367828 second(s), 42 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表