- UID
- 658062
- 积分
- 2147
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2008-10-22
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
原帖地址:
http://blog.sina.com.cn/s/blog_4cb57a380101f5gt.html
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Geometry;
- namespace DotNetARX
- {
- public static class EntTools
- {
- public static void Move(this ObjectId id, Point3d sourcePt, Point3d targetPt)
- {
- //构建用于移动实体的函数
- Vector3d vector = targetPt.GetVectorTo(sourcePt);//获得一个从起点到终点的向量vector
- Matrix3d mt = Matrix3d.Displacement(vector);//构建移动矩阵
- //displacement是matrix3d的一个函数(方法),用于构建一个以vector为向量的矩阵
- //以写的方式打开ID表示的实体对象
- Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
- ent.TransformBy(mt);//对实体实施移动
- //tranformby是entity的一个函数(方法),用于对矩阵进行操作,移动、旋转、赋值等
- //pubilc virtual void transformby(matrix3d transform)
- ent.DowngradeOpen();//为防止错误,切换实体为读的状态
- }
- public static ObjectId Copy(this ObjectId id, Point3d sourcePt, Point3d targetPt)
- {
- //构建用于复制实体的矩阵
- Vector3d vector = targetPt.GetVectorTo(sourcePt);//获得一个从起点到终点的向量vector
- Matrix3d mt = Matrix3d.Displacement(vector);//构建移动矩阵
- //displacement是matrix3d的一个函数(方法),用于构建一个以vector为向量的矩阵
- //获取ID表示的实体对象
- Entity ent = (Entity)id.GetObject(OpenMode.ForRead);
- Entity entCopy = ent.GetTransformedCopy(mt);//获取实体的复制件
- //将复制的实体对象添加到模型空间
- ObjectId copyId = id.Database.AddToModelSpace(entCopy);
- return copyId;//返回复制的实体的ObjectId
- }
- public static void Rotate(this ObjectId id,Point3d basePt,double angle)
- {
- Matrix3d mt = Matrix3d.Rotation(angle, Vector3d.ZAxis, basePt);
- //public static Matrix3d Rotation(double angle,Vertor3d axis,Point3d center)
- //angle旋转角度,axis旋转轴,center旋转中心
- Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
- ent.TransformBy(mt);
- ent.DowngradeOpen();
- }
- public static void Scale(this ObjectId id, Point3d basePt, double scaleFactor)
- {
- Matrix3d mt = Matrix3d.Scaling(scaleFactor, basePt);
- //public static Matrix3d Scaling(double scaleAll,Point3d center)
- //scaleALL缩放比例,center缩放中心
- Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
- ent.TransformBy(mt);
- ent.DowngradeOpen();
- }
- public static ObjectId Mirror(this ObjectId id, Point3d mirrorPt1, Point3d mirrorPt2, bool eraseSourceObjec)
- {
- Line3d miLine = new Line3d(mirrorPt1, mirrorPt2);
- Matrix3d mt = Matrix3d.Mirroring(miLine);
- ObjectId mirrorId = id;
- Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
- //如果删除源对象,则直线对源对象实行镜像交换
- if (eraseSourceObjec == true)
- ent.TransformBy(mt);
- else
- {
- Entity entCopy = ent.GetTransformedCopy(mt);
- mirrorId = id.Database.AddToModelSpace(entCopy);
- }
- return mirrorId;
- }
- public static ObjectIdCollection Offset(this ObjectId id,double dis)
- {
- ObjectIdCollection ids= new ObjectIdCollection();
- Curve cur = id.GetObject(OpenMode.ForWrite) as Curve;
- if (cur != null)
- {
- try
- {
- //获取偏移对象的对象集合
- DBObjectCollection offsetCurves = cur.GetOffsetCurves(dis);
- //将对象集合类型转换为实体类的数组,以方便加入实体的操作
- Entity [] offsetEnts = new Entity[offsetCurves.Count];
- offsetCurves.CopyTo(offsetEnts, 0);
- //将偏移的对象加入到数据库
- ids = id.Database.AddToModelSpace(offsetEnts);
- }
- catch
- {
- Application.ShowAlertDialog("无法偏移");
- }
- }
- else
- Application.ShowAlertDialog("无法偏移");
- return ids;
-
- }
- }
- }
偏移时用到的addtomodelspace:
- public static ObjectIdCollection AddToModelSpace(this Database db, params Entity[] ents)
- {
- ObjectIdCollection ids = new ObjectIdCollection();
- var trans = db.TransactionManager;
- BlockTableRecord btr = (BlockTableRecord)trans.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite);
- foreach (var ent in ents)
- {
- ids.Add(btr.AppendEntity(ent));
- trans.AddNewlyCreatedDBObject(ent, true);
- }
- btr.DowngradeOpen();
- return ids;
- }
|
|