[CommandMethod("loop", CommandFlags.UsePickSet)]
public void 视角转换()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
// 获取用户的选择
PromptSelectionResult psr = ed.SelectImplied();
if (psr.Status != PromptStatus.OK)
{
psr = ed.GetSelection();
}
if (psr.Status != PromptStatus.OK)
{
ed.WriteMessage("没有选中任何对象。\n");
return;
}
SelectionSet sSet = psr.Value;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
Extents3d totalExtents = new Extents3d();
foreach (SelectedObject selObj in sSet)
{
Entity ent = (Entity)trans.GetObject(selObj.ObjectId, OpenMode.ForWrite);
totalExtents.AddExtents(ent.GeometricExtents);
}
var 范围长 = totalExtents.MaxPoint.X - totalExtents.MinPoint.X;
var 范围宽= totalExtents.MaxPoint.Y- totalExtents.MinPoint.Y;
var 中点x = totalExtents.MinPoint.X + 范围长 / 2;
var 中点y = totalExtents.MinPoint.Y + 范围宽 / 2;
//PromptPointOptions ppo = new PromptPointOptions("\n选择点: ");
//PromptPointResult ppr = ed.GetPoint(ppo);
//var ppt = ppr.Value;
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);
BlockTableRecord space = db.CurrentSpaceId.GetObject(OpenMode.ForWrite) as BlockTableRecord;
var 前一个selRes = ed.SelectCrossingWindow(totalExtents.MinPoint, totalExtents.MaxPoint);
var 块集 = new List<(BlockReference, string)>();
int 比例值 = 4;
var 左下角块 = 左下角();
var 左上角块 = 左上角();
var 右上角块 = 右上角();
var 右下角块 = 右下角();
if(左下角块!=null) 块集.Add((左下角块, "左下角"));
if (左上角块 != null) 块集.Add((左上角块, "左上角"));
if (右上角块 != null) 块集.Add((右上角块, "右上角"));
if (右下角块 != null) 块集.Add((右下角块, "右下角"));
Debug.WriteLine("块数:" + 块集.Count);
if (块集.Count == 3)
{
if (!块集.Select(item => item.Item2).ToList().Contains("左下角"))
{
Vector3d displacement = new Vector3d(0, 范围宽, 0);
Matrix3d move = Matrix3d.Displacement(displacement);
右下角块.TransformBy(move);
Vector3d displacement2 = new Vector3d(范围长, 0, 0);
Matrix3d move2 = Matrix3d.Displacement(displacement2);
左上角块.TransformBy(move2);
}
else if (!块集.Select(item => item.Item2).ToList().Contains("左上角"))
{
Vector3d displacement = new Vector3d(范围长, 0, 0);
Matrix3d move = Matrix3d.Displacement(displacement);
左下角块.TransformBy(move);
Vector3d displacement2 = new Vector3d(0, -范围宽, 0);
Matrix3d move2 = Matrix3d.Displacement(displacement2);
右上角块.TransformBy(move2);
}
else if (!块集.Select(item => item.Item2).ToList().Contains("右上角"))
{
Vector3d displacement = new Vector3d(0, -范围宽, 0);
Matrix3d move = Matrix3d.Displacement(displacement);
左上角块.TransformBy(move);
Vector3d displacement2 = new Vector3d(-范围长, 0, 0);
Matrix3d move2 = Matrix3d.Displacement(displacement2);
右下角块.TransformBy(move2);
}
else if (!块集.Select(item => item.Item2).ToList().Contains("右下角"))
{
Vector3d displacement = new Vector3d(0, 范围宽, 0);
Matrix3d move = Matrix3d.Displacement(displacement);
左下角块.TransformBy(move);
Vector3d displacement2 = new Vector3d(-范围长, 0, 0);
Matrix3d move2 = Matrix3d.Displacement(displacement2);
右上角块.TransformBy(move2);
}
}
else if (块集.Count == 2) {
if (块集.Select(item => item.Item2).ToList().Contains("左下角")
&& 块集.Select(item => item.Item2).ToList().Contains("左上角")) {
Vector3d displacement = new Vector3d(0, 范围宽, 0);
Matrix3d move = Matrix3d.Displacement(displacement);
左下角块.TransformBy(move);
}
if (块集.Select(item => item.Item2).ToList().Contains("右上角")
&& 块集.Select(item => item.Item2).ToList().Contains("左上角"))
{
Vector3d displacement2 = new Vector3d(范围长, 0, 0);
Matrix3d move2 = Matrix3d.Displacement(displacement2);
左上角块.TransformBy(move2);
}
}
创建块2(块集.Select(Item => Item.Item1).ToList());
BlockReference 左下角()
{
Point3d 左下框1 = totalExtents.MinPoint;
Point3d 左下框2 =new Point3d( totalExtents.MinPoint.X+ 范围长/ 比例值, totalExtents.MinPoint.Y+ 范围宽/ 比例值, 0);
PromptSelectionResult selRes = ed.SelectCrossingWindow(左下框1, 左下框2);
if (selRes.Status != PromptStatus.OK) return null;
Extents3d 左下框totalExtents = new Extents3d();
foreach (SelectedObject selObj in selRes.Value)
{
Entity ent = (Entity)trans.GetObject(selObj.ObjectId, OpenMode.ForWrite);
左下框totalExtents.AddExtents(ent.GeometricExtents);
}
do
{
前一个selRes = selRes;
左下框2 = new Point3d(左下框totalExtents.MaxPoint.X + 5, 左下框totalExtents.MaxPoint.Y + 5, 0);
selRes = ed.SelectCrossingWindow(左下框1, 左下框2);
左下框totalExtents = new Extents3d();
foreach (SelectedObject selObj in selRes.Value)
{
Entity ent = (Entity)trans.GetObject(selObj.ObjectId, OpenMode.ForWrite);
左下框totalExtents.AddExtents(ent.GeometricExtents);
}
} while (前一个selRes.Value.Count != selRes.Value.Count);
return 创建块(selRes);
}
BlockReference 左上角()
{
Point3d 左上框1 = new Point3d(totalExtents.MinPoint.X, totalExtents.MaxPoint.Y,0);
Point3d 左上框2 = new Point3d(totalExtents.MinPoint.X + 范围长 / 比例值, totalExtents.MaxPoint.Y - 范围宽 / 比例值, 0);
PromptSelectionResult selRes = ed.SelectCrossingWindow(左上框1, 左上框2);
Extents3d 左上框totalExtents = new Extents3d();
if (selRes.Status != PromptStatus.OK) return null;
foreach (SelectedObject selObj in selRes.Value)
{
Entity ent = (Entity)trans.GetObject(selObj.ObjectId, OpenMode.ForWrite);
左上框totalExtents.AddExtents(ent.GeometricExtents);
}
do
{
前一个selRes = selRes;
左上框2 = new Point3d(左上框totalExtents.MaxPoint.X + 5, 左上框totalExtents.MinPoint.Y - 5, 0);
selRes = ed.SelectCrossingWindow(左上框1, 左上框2);
左上框totalExtents = new Extents3d();
foreach (SelectedObject selObj in selRes.Value)
{
Entity ent = (Entity)trans.GetObject(selObj.ObjectId, OpenMode.ForWrite);
左上框totalExtents.AddExtents(ent.GeometricExtents);
}
} while (前一个selRes.Value.Count != selRes.Value.Count);
return 创建块(selRes);
}
BlockReference 右上角()
{
Point3d 右上框1 = new Point3d(totalExtents.MaxPoint.X, totalExtents.MaxPoint.Y, 0);
Point3d 右上框2 = new Point3d(totalExtents.MaxPoint.X -范围长 / 比例值, totalExtents.MaxPoint.Y - 范围宽 / 比例值, 0);
PromptSelectionResult selRes = ed.SelectCrossingWindow(右上框1, 右上框2);
Extents3d 右上框totalExtents = new Extents3d();
if (selRes.Status != PromptStatus.OK) return null;
foreach (SelectedObject selObj in selRes.Value)
{
Entity ent = (Entity)trans.GetObject(selObj.ObjectId, OpenMode.ForWrite);
右上框totalExtents.AddExtents(ent.GeometricExtents);
}
do
{
前一个selRes = selRes;
右上框2 = new Point3d(右上框totalExtents.MinPoint.X - 5, 右上框totalExtents.MinPoint.Y - 5, 0);
selRes = ed.SelectCrossingWindow(右上框1, 右上框2);
右上框totalExtents = new Extents3d();
foreach (SelectedObject selObj in selRes.Value)
{
Entity ent = (Entity)trans.GetObject(selObj.ObjectId, OpenMode.ForWrite);
右上框totalExtents.AddExtents(ent.GeometricExtents);
}
} while (前一个selRes.Value.Count != selRes.Value.Count);
return 创建块(selRes);
}
BlockReference 右下角()
{
Point3d 右下框1 = new Point3d(totalExtents.MaxPoint.X, totalExtents.MinPoint.Y, 0);
Point3d 右下框2 = new Point3d(totalExtents.MaxPoint.X - 范围长 / 比例值, totalExtents.MinPoint.Y + 范围宽 / 比例值, 0);
PromptSelectionResult selRes = ed.SelectCrossingWindow(右下框1, 右下框2);
Extents3d 右下框totalExtents = new Extents3d();
if (selRes.Status != PromptStatus.OK) return null;
foreach (SelectedObject selObj in selRes.Value)
{
Entity ent = (Entity)trans.GetObject(selObj.ObjectId, OpenMode.ForWrite);
右下框totalExtents.AddExtents(ent.GeometricExtents);
}
do
{
前一个selRes = selRes;
右下框2 = new Point3d(右下框totalExtents.MinPoint.X - 5, 右下框totalExtents.MaxPoint.Y + 5, 0);
selRes = ed.SelectCrossingWindow(右下框1, 右下框2);
右下框totalExtents = new Extents3d();
foreach (SelectedObject selObj in selRes.Value)
{
Entity ent = (Entity)trans.GetObject(selObj.ObjectId, OpenMode.ForWrite);
右下框totalExtents.AddExtents(ent.GeometricExtents);
}
} while (前一个selRes.Value.Count != selRes.Value.Count);
return 创建块(selRes);
}
BlockReference 创建块(PromptSelectionResult selRes2) {
int i = 0;
BlockTableRecord btrNewBlock = new BlockTableRecord();
btrNewBlock.Name = "MyBlock";
while (bt.Has(btrNewBlock.Name))
{
i++;
btrNewBlock.Name = "MyBlock" + i;
}
foreach (SelectedObject selObj in selRes2.Value)
{
Entity ent = (Entity)trans.GetObject(selObj.ObjectId, OpenMode.ForWrite);
ent.ColorIndex = 1;
var pCopy = (Entity)ent.Clone();
btrNewBlock.AppendEntity(pCopy);
ent.Erase();
}
bt.Add(btrNewBlock);
trans.AddNewlyCreatedDBObject(btrNewBlock, true);
BlockReference br = new BlockReference(Point3d.Origin, bt[btrNewBlock.Name]);
space.AppendEntity(br);
trans.AddNewlyCreatedDBObject(br, true);
return br;
}
BlockReference 创建块2(List<BlockReference> selRes2)
{
int i = 0;
BlockTableRecord btrNewBlock = new BlockTableRecord();
btrNewBlock.Name = "MyBlock";
while (bt.Has(btrNewBlock.Name))
{
i++;
btrNewBlock.Name = "MyBlock" + i;
}
foreach (var selObj in selRes2)
{
var pCopy = (Entity)selObj.Clone();
btrNewBlock.AppendEntity(pCopy);
selObj.Erase();
}
bt.Add(btrNewBlock);
trans.AddNewlyCreatedDBObject(btrNewBlock, true);
BlockReference br = new BlockReference(Point3d.Origin, bt[btrNewBlock.Name]);
space.AppendEntity(br);
trans.AddNewlyCreatedDBObject(br, true);
return br;
}
trans.Commit();
}
}
c# cad 视角转换
最新推荐文章于 2024-09-05 23:55:05 发布