|
这是我自已写的,原版文件在这里,如果使用中有什么问题,请与我联系
// (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..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 description for CQSClass.
///
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
//通过实体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
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
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;
}
}
}
本帖以下内容被隐藏保护;需要你回复后,才能看到! 游客,如果您要查看本帖隐藏内容请 回复 |
|