本帖最后由 作者 于 2007-8-25 9:26:50 编辑
这是我自已写的,原版文件在这里,如果使用中有什么问题,请与我联系 // (C) Copyright 2002-2005 by Autodesk, Inc. // // Permission to use, copy, modify, and distribute this software in // object code form for any purpose and without fee is hereby granted, // provided that the above copyright notice appears in all copies and // that both that copyright notice and the limited warranty and // restricted rights notice below appear in all supporting // documentation. // // AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS. // AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF // MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC. // DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE // UNINTERRUPTED OR ERROR FREE. // // Use, duplication, or disclosure by the U.S. Government is subject to // restrictions set forth in FAR 52.227-19 (Commercial Computer // Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) // (Rights in Technical Data and Computer Software), as applicable. // //using System ; using Autodesk.AutoCAD.Runtime ; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.ApplicationServices; [assembly: CommandClass(typeof(Caiqs.Cutility))] //蔡全胜 QQ 361865648 实体镜像,阵列,旋转通用类库 namespace Caiqs { /// <summary> /// Summary description for CQSClass. /// </summary> public class Cutility { static public Database db; static public Editor ed; public Cutility() { // // TODO: Add constructor logic here // } [CommandMethod("Comm")] static public void comm() { return; } //通过Entity对实体进行矩阵matrix3D的变换 static public void Transform(Entity acadEntity,Matrix3d matrix3D) { try { if (acadEntity.IsWriteEnabled==true) {acadEntity.TransformBy(matrix3D);} else { Transform(acadEntity.ObjectId ,matrix3D); } } catch (Exception e) { throw(e); } return; } //通过Entity实体数组进行矩阵matrix3D的变换 static public void Transform(Entity[] acadEntity,Matrix3d matrix3D) { int i; for(i=0;i<acadEntity.Length;i++) { if (acadEntity.IsWriteEnabled==true) {acadEntity.TransformBy(matrix3D);} else {Transform(acadEntity.ObjectId ,matrix3D);} } return; } //通过实体id对实体进行矩阵matrix3D的变换 static public void Transform(ObjectId objectId,Matrix3d matrix3D) { Transaction tm; db = HostApplicationServices.WorkingDatabase; tm = db.TransactionManager.StartTransaction(); try { Entity acadEntity = (Entity)tm.GetObject(objectId, OpenMode.ForWrite); acadEntity.TransformBy(matrix3D); tm.Commit();} finally { tm.Dispose(); } return; } // //实体克窿 static public Entity Clone(Entity acadEntity) { Transaction tm; db = HostApplicationServices.WorkingDatabase; tm = db.TransactionManager.StartTransaction(); Entity retuEnt; try { BlockTable bt=(BlockTable)tm.GetObject(db.BlockTableId,OpenMode.ForRead,true); BlockTableRecord btr=(BlockTableRecord)tm.GetObject(db.CurrentSpaceId,OpenMode.ForWrite,true); if (acadEntity.IsReadEnabled==true) {retuEnt=(Entity)acadEntity.Clone();} else { Entity Ent = (Entity)tm.GetObject(acadEntity.ObjectId, OpenMode.ForRead); retuEnt=(Entity)Ent.Clone(); } btr.AppendEntity(retuEnt); tm.AddNewlyCreatedDBObject(retuEnt,true); tm.Commit();} finally { tm.Dispose(); } return retuEnt; } //实体数组克隆 static public Entity[] Clone(Entity[] acadEntity) { int i; Entity[] retuEnt=new Entity[acadEntity.Length]; for(i=0;i<acadEntity.Length;i++) { retuEnt=Clone(acadEntity); } return retuEnt; } //通过实体id对实体进行矩阵matrix3D的变换并复制实体 static public Entity Ctransform(ObjectId objectId,Matrix3d matrix3D) { Transaction tm; db = HostApplicationServices.WorkingDatabase; tm = db.TransactionManager.StartTransaction(); Entity retuEnt; try { BlockTable bt=(BlockTable)tm.GetObject(db.BlockTableId,OpenMode.ForRead,true); Entity acadEntity = (Entity)tm.GetObject(objectId, OpenMode.ForRead); BlockTableRecord btr=(BlockTableRecord)tm.GetObject(db.CurrentSpaceId,OpenMode.ForWrite,true); retuEnt=(Entity)acadEntity.Clone(); btr.AppendEntity(retuEnt); tm.AddNewlyCreatedDBObject(retuEnt,true); Transform(retuEnt.ObjectId,matrix3D); tm.Commit();} finally { tm.Dispose(); } return retuEnt; } //通过实体id数组对实体进行矩阵matrix3D的变换并复制实体 static public Entity[] Ctransform(ObjectId[] objectId,Matrix3d matrix3D) { // db = HostApplicationServices.WorkingDatabase; // tm = db.TransactionManager.StartTransaction(); // BlockTable bt=(BlockTable)tm.GetObject(db.BlockTableId,OpenMode.ForRead,true); // BlockTableRecord btr=(BlockTableRecord)tm.GetObject(db.CurrentSpaceId,OpenMode.ForWrite,true); // //Entity acadEntity = (Entity)tm.GetObject(objectId, OpenMode.ForRead); // int i; Entity[] retuEnt=new Entity[objectId.Length]; // for(i=0;i<objectId.Length;i++) // { // Entity acadEntity = (Entity)tm.GetObject(objectId, OpenMode.ForRead ); // retuEnt=(Entity)acadEntity.Clone(); // btr.AppendEntity(retuEnt); // tm.AddNewlyCreatedDBObject(retuEnt,true); // } // tm.Commit(); // tm.Dispose(); retuEnt=Clone(IdarraytoEntity(objectId)); Transform(retuEnt,matrix3D); return retuEnt; } //Id转成实体 static public Entity IdtoEntity(ObjectId objectId) { Transaction tm; db = HostApplicationServices.WorkingDatabase; tm = db.TransactionManager.StartTransaction(); Entity acadEntity = (Entity)tm.GetObject(objectId, OpenMode.ForRead ); tm.Commit(); tm.Dispose(); return acadEntity; } //Id数组转成实体数组 static public Entity[] IdarraytoEntity(ObjectId[] objectId) { Transaction tm; db = HostApplicationServices.WorkingDatabase; tm = db.TransactionManager.StartTransaction(); Entity[] retuEnt=new Entity[objectId.Length]; int i; for (i=0;i<objectId.Length;i++) { retuEnt = (Entity)tm.GetObject(objectId, OpenMode.ForRead ); } tm.Commit(); tm.Dispose(); return retuEnt; } //对id数组进行矩阵matrix3D的变换 static public void Transform(ObjectId[] idArray,Matrix3d matrix3D) { Transaction tm; db = HostApplicationServices.WorkingDatabase; tm = db.TransactionManager.StartTransaction(); int i; for(i=0;i<idArray.Length;i++) { Entity acadEntity = (Entity)tm.GetObject(idArray, OpenMode.ForWrite); acadEntity.TransformBy(matrix3D); } tm.Commit(); tm.Dispose(); return; } //单个实体加入数据库 static public void Apptodb(Entity acadEntity) { Transaction tm; db = HostApplicationServices.WorkingDatabase; tm = db.TransactionManager.StartTransaction(); ed=Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; ed.WriteMessage(acadEntity.IsNewObject.ToString()); try { BlockTable bt=(BlockTable)tm.GetObject(db.BlockTableId,OpenMode.ForRead,true); BlockTableRecord btr=(BlockTableRecord)tm.GetObject(db.CurrentSpaceId,OpenMode.ForWrite,true); btr.AppendEntity(acadEntity); tm.AddNewlyCreatedDBObject(acadEntity,true); tm.Commit(); tm.Dispose(); } catch (Exception e) { throw(e); } return; } //实体数组加入到数据库 static public void Apptodb(Entity[] acadEntity) { Transaction tm; db = HostApplicationServices.WorkingDatabase; tm = db.TransactionManager.StartTransaction(); BlockTable bt=(BlockTable)tm.GetObject(db.BlockTableId,OpenMode.ForRead,true); BlockTableRecord btr=(BlockTableRecord)tm.GetObject(db.CurrentSpaceId,OpenMode.ForWrite,true); int i; for(i=0;i<acadEntity.Length;i++) { btr.AppendEntity(acadEntity); tm.AddNewlyCreatedDBObject(acadEntity,true); } tm.Commit(); tm.Dispose(); return; } [CommandMethod("Ccreat")] static public void Ccreat() { //db = HostApplicationServices.WorkingDatabase; ed=Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; try { Line ln1=new Line(new Point3d(0,0,0),new Point3d(50,50,50)); Move(ln1,new Point3d(50,50,0),new Point3d(0,0,0)); ed.WriteMessage("\n移动1完成"); Mirror(ln1,new Point3d(50,50,0),new Point3d(0,0,0)); ed.WriteMessage("\n镜像1完成"); Rotate(ln1,new Point3d(50,50,0),3.14159/2.0); ed.WriteMessage("\n旋转完成"); Apptodb(ln1); ed.WriteMessage("\n加入数据库完成"); Copymove(ln1,new Point3d(0,0,0),new Point3d(50,50,0)); ed.WriteMessage("\n复制完成"); Mirror(ln1,new Point3d(50,50,0),new Point3d(0,0,0)); ed.WriteMessage("\n镜像2完成");} catch (Exception e) { throw(e); } return; } [CommandMethod("Cmove")] static public void Cmove() { ed=Application.DocumentManager.MdiActiveDocument.Editor; ed.WriteMessage("\n选择移动的实体: ");  romptSelectionResult res=ed.GetSelection(); if (res.Status!= PromptStatus.OK) return;  romptPointResult res1=ed.GetPoint("\n起点:"); if (res1.Status!= PromptStatus.OK) return;  romptPointResult res2=ed.GetPoint("\n终点:"); if (res2.Status!= PromptStatus.OK) return; SelectionSet SS = res.Value; //Ctransform(SS.GetObjectIds, Move(SS,res1.Value,res2.Value); System.GC.Collect(); return; } [CommandMethod("CRotate")] static public void Crotate() { ed=Application.DocumentManager.MdiActiveDocument.Editor; ed.WriteMessage("\n选择旋转的实体: ");  romptSelectionResult res=ed.GetSelection(); if (res.Status!= PromptStatus.OK) return;  romptPointResult res1=ed.GetPoint("\n基点:"); if (res1.Status!= PromptStatus.OK) return;  romptDoubleResult res2=ed.GetDouble("\n旋转角度(弧度): "); if (res2.Status!= PromptStatus.OK) return; SelectionSet SS = res.Value; Rotate(SS,res1.Value,res2.Value); System.GC.Collect(); return; } [CommandMethod("Cmirror")] static public void Cmirror() // This method can have any name { ed=Application.DocumentManager.MdiActiveDocument.Editor; ed.WriteMessage("\n选择要镜像的实体: ");  romptSelectionResult res=ed.GetSelection(); if (res.Status!= PromptStatus.OK) return;  romptPointResult res1=ed.GetPoint("\n镜像线第一点::"); if (res1.Status!= PromptStatus.OK) return;  romptPointResult res2=ed.GetPoint("\n镜像线第二点::"); if (res2.Status!= PromptStatus.OK) return; SelectionSet SS = res.Value; Mirror(SS,res1.Value,res2.Value); System.GC.Collect(); return; } //移动单个实体 static public void Move(Entity acadEntity,Point3d pt1,Point3d pt2) { Vector3d vect=pt2.GetVectorTo(pt1); Matrix3d tf = Matrix3d.Displacement(vect); //Transform(acadEntity.ObjectId ,tf); Transform(acadEntity,tf); return; } //实体移动复制 static public Entity Copymove(Entity acadEntity,Point3d pt1,Point3d pt2) { Vector3d vect=pt2.GetVectorTo(pt1); Matrix3d tf = Matrix3d.Displacement(vect); // if (acadEntity.IsWriteEnabled==true) // {acadEntity.TransformBy(tf);} // else // { // Transform(acadEntity.ObjectId ,tf); // } return (Ctransform(acadEntity.ObjectId,tf)); } //通过ID移动单个实体 static public void Move(ObjectId objectId,Point3d pt1,Point3d pt2) { Vector3d vect=pt2.GetVectorTo(pt1); Matrix3d tf = Matrix3d.Displacement(vect); Transform(objectId,tf); return; } //移动选择集 static public void Move(SelectionSet ss,Point3d pt1,Point3d pt2) { ObjectId[] idArray; idArray = ss.GetObjectIds(); Vector3d vect=pt2.GetVectorTo(pt1); Matrix3d tf = Matrix3d.Displacement(vect); //Transform(idArray,tf); Ctransform(idArray,tf); return; } //通过实体iD数组移动实体 static public void Move(ObjectId[] idArray,Point3d pt1,Point3d pt2) { Vector3d vect=pt2.GetVectorTo(pt1); Matrix3d tf = Matrix3d.Displacement(vect); //Transform(idArray,tf); Ctransform(idArray,tf);
return; } //XY平面内旋转Entity实体 static public void Rotate(Entity acadEntity,Point3d baspt,double Rangle) { Vector3d vect=new Vector3d(0,0,1); Matrix3d tf=Matrix3d.Rotation(Rangle,vect,baspt); Transform(acadEntity ,tf); return; } //XY平面内旋转实体Id static public void Rotate(ObjectId objectId,Point3d baspt,double Rangle) { Vector3d vect=new Vector3d(0,0,1); Matrix3d tf = Matrix3d.Rotation(Rangle,vect,baspt); Transform(objectId,tf); return; } //旋转选择集 static public void Rotate(SelectionSet ss,Point3d baspt,double Rangle) { ObjectId[] idArray; idArray = ss.GetObjectIds(); Vector3d vect=new Vector3d(0,0,1); Matrix3d tf = Matrix3d.Rotation(Rangle,vect,baspt); Transform(idArray,tf); return; } //旋转实体Id数组 static public void Rotate(ObjectId[] idArray,Point3d baspt,double Rangle) { Vector3d vect=new Vector3d(0,0,1); Matrix3d tf = Matrix3d.Rotation(Rangle,vect,baspt); Transform(idArray,tf); return; } //镜像实体 static public void Mirror(Entity acadEntity,Point3d p1,Point3d p2) {  lane Myplane=new Plane(p1,new Point3d(p2.X ,p2.Y ,-100),p2); Matrix3d tf=Matrix3d.Mirroring(Myplane); Transform(acadEntity,tf); return; } //通过Id镜像实体 static public void Mirror(ObjectId objectId,Point3d p1,Point3d p2) {  lane Myplane=new Plane(p1,new Point3d(p2.X ,p2.Y ,-100),p2); Matrix3d tf=Matrix3d.Mirroring(Myplane); Transform(objectId,tf); return; } //镜像选择集 static public void Mirror(SelectionSet ss,Point3d p1,Point3d p2) { ObjectId[] idArray; idArray = ss.GetObjectIds();  lane Myplane=new Plane(p1,new Point3d(p2.X ,p2.Y ,-100),p2); Matrix3d tf=Matrix3d.Mirroring(Myplane); Transform(idArray,tf); return; } //镜像实体Id数组 static public void Mirror(ObjectId[] idArray,Point3d p1,Point3d p2) {  lane Myplane=new Plane(p1,new Point3d(p2.X ,p2.Y ,-100),p2); Matrix3d tf=Matrix3d.Mirroring(Myplane); Transform(idArray,tf); return; } //实体缩放 static public void Scale(Entity acadEntity,Point3d basepoint,double scale) { Matrix3d tf=Matrix3d.Scaling(scale,basepoint); Transform(acadEntity,tf); return; } //实体Id缩放 static public void Scale(ObjectId objectId,Point3d basepoint,double scale) { Matrix3d tf=Matrix3d.Scaling(scale,basepoint); Transform(objectId ,tf); return; } //选择集缩放 static public void Scale(SelectionSet ss,Point3d basepoint,double scale) { ObjectId[] idArray; Matrix3d tf=Matrix3d.Scaling(scale,basepoint); idArray = ss.GetObjectIds(); Transform(idArray ,tf); return; } //实体id数组缩放 static public void Scale(ObjectId[] objectId,Point3d basepoint,double scale) { Matrix3d tf=Matrix3d.Scaling(scale,basepoint); Transform(objectId ,tf); return; } } }
|