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