caiqs 发表于 2007-8-25 08:55:00

C#类库源码: 实体(选择集)镜像,阵列,移动,旋转,缩放

这是我自已写的,原版文件在这里,如果使用中有什么问题,请与我联系




// (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;

//蔡全胜 QQ 361865648 实体镜像,阵列,旋转通用类库
namespace Caiqs
{
///
/// Summary description for CQSClass.
///
public class Cutility
{
static public Databasedb;
static public Editor ed;
public Cutility()
{
   //
   // TODO: Add constructor logic here
   //
}

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)
{
   Transactiontm;
   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;
   //   for(i=0;i
   return retuEnt;
}
//Id转成实体
static public Entity IdtoEntity(ObjectId objectId)
{
   Transactiontm;
   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)
{
   Transactiontm;
   db = HostApplicationServices.WorkingDatabase;
   tm = db.TransactionManager.StartTransaction();
   Entity[] retuEnt=new Entity;
   int i;
   for (i=0;i
   return;
}

static public void Cmove()
{
   ed=Application.DocumentManager.MdiActiveDocument.Editor;
   ed.WriteMessage("\n选择移动的实体: ");   
&nbspromptSelectionResult res=ed.GetSelection();
   if (res.Status!= PromptStatus.OK) return;
&nbspromptPointResult res1=ed.GetPoint("\n起点:");
   if (res1.Status!= PromptStatus.OK) return;
&nbspromptPointResult 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;
}

static public void Crotate()
{
   ed=Application.DocumentManager.MdiActiveDocument.Editor;
   ed.WriteMessage("\n选择旋转的实体: ");   
&nbspromptSelectionResult res=ed.GetSelection();
   if (res.Status!= PromptStatus.OK) return;
&nbspromptPointResult res1=ed.GetPoint("\n基点:");
   if (res1.Status!= PromptStatus.OK) return;
&nbspromptDoubleResult res2=ed.GetDouble("\n旋转角度(弧度): ");
   if (res2.Status!= PromptStatus.OK) return;
   SelectionSet SS = res.Value;
   Rotate(SS,res1.Value,res2.Value);
      System.GC.Collect();
   return;
}

static public void Cmirror() // This method can have any name
{
   ed=Application.DocumentManager.MdiActiveDocument.Editor;
   ed.WriteMessage("\n选择要镜像的实体: ");   
&nbspromptSelectionResult res=ed.GetSelection();
   if (res.Status!= PromptStatus.OK) return;
&nbspromptPointResult res1=ed.GetPoint("\n镜像线第一点::");
   if (res1.Status!= PromptStatus.OK) return;
&nbspromptPointResult 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)
{
&nbsplane 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)
{
&nbsplane 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();
&nbsplane 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)
{
&nbsplane 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;
}

}
}
**** Hidden Message *****

ZML84 发表于 2007-8-25 09:03:00

支持一下,虽然我现在还看不懂,相信以后会有用的!
谢谢!

caiqs 发表于 2007-8-25 09:05:00


编译好的在这里

JOLMO 发表于 2007-8-25 12:05:00

冰块给你顶一下,

houlinbo 发表于 2007-8-27 14:27:00

好东西谢谢

一个样 发表于 2007-8-29 23:33:00

谢谢了,用得上。

qshzhangfan 发表于 2007-8-31 10:23:00

请问 如何才能只选择1次呢, 选择的时候我只要点中的一个实体就行了

xyp1964 发表于 2007-9-27 10:44:00

太高了,看不懂,学习!

kk20021055 发表于 2007-10-15 12:23:00

ding

carrot1983 发表于 2007-12-24 16:22:00

正要学习C#,
可惜objectarx 2006的教程是用C++,难道我要改学C++。。
楼上的代码先收藏。。学习学习
页: [1]
查看完整版本: C#类库源码: 实体(选择集)镜像,阵列,移动,旋转,缩放