【已解决】用户坐标系出现偏差如何解决?
本帖最后由 白糖 于 2012-10-11 16:42 编辑using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
namespace UCS
{
/// <summary>
/// Description of UserControl1.
/// </summary>
public partial class UserControl1 : UserControl
{
public static int i = 1;
public static List<Point3d> pts = new List<Point3d>();
public UserControl1()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
public void NewUCS()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Point3d breakPoint = new Point3d();
using (Transaction trans = db.TransactionManager.StartTransaction())
{
UcsTable ut = trans.GetObject(db.UcsTableId,OpenMode.ForRead) as UcsTable;
UcsTableRecord utr;
if (ut.Has("New_UCS") == false)
{
utr = new UcsTableRecord();
utr.Name = "New_UCS";
ut.UpgradeOpen();
ut.Add(utr);
trans.AddNewlyCreatedDBObject(utr,true);
}else
{
utr = trans.GetObject(ut["New_UCS"],OpenMode.ForWrite) as UcsTableRecord;
}
//指定原点
PromptPointOptions pPtOpts = new PromptPointOptions("\n指定原点");
PromptPointResult pPtRes = ed.GetPoint(pPtOpts);
if (pPtRes.Status == PromptStatus.OK)
{
utr.Origin = pPtRes.Value;
breakPoint = pPtRes.Value;
}
//指定Y轴
pPtOpts = new PromptPointOptions("指定Y轴方向");
pPtRes = ed.GetPoint(pPtOpts);
if (pPtRes.Status == PromptStatus.OK)
{
Vector3d vec = pPtRes.Value - utr.Origin;
utr.YAxis = vec.GetNormal();
}
//Y轴旋转90°为X轴
utr.XAxis = utr.YAxis.RotateBy(Math.PI*3/2,Vector3d.ZAxis);
Matrix3d mt = Matrix3d.AlignCoordinateSystem(Point3d.Origin,Vector3d.XAxis,Vector3d.YAxis,Vector3d.ZAxis,utr.Origin,utr.XAxis,utr.YAxis,Vector3d.ZAxis);
ed.CurrentUserCoordinateSystem = mt;
trans.Commit();
}
AddPoint();
}
// 添加点
public void AddPoint()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
Point3d pt;
PromptPointResult pPtRes;
PromptPointOptions pPtOpts = new PromptPointOptions("");
pPtOpts.Message = "\n请选择起点";
pPtRes = ed.GetPoint(pPtOpts);
while (pPtRes.Status == PromptStatus.OK | pPtRes.Status != PromptStatus.Cancel)
{
pt = pPtRes.Value;
ed.WriteMessage("选点"+pt.ToString());
Vector3d vec = db.Ucsorg.GetAsVector();
AddPoint(pt+vec);
pts.Add(pt);
pPtOpts.Message = "\n请选择下一点";
pPtOpts.UseBasePoint = true;
pPtOpts.BasePoint = pPtRes.Value;
pPtRes = ed.GetPoint(pPtOpts);
}
}
public static ObjectIdCollection AddPoint(Point3d position)
{
DBPoint ent1 = new DBPoint(position);
DBText ent2 = new DBText();
ent2.Position = new Point3d(position.X + 5, position.Y + 5, position.Z);
ent2.TextString = i.ToString();
ent2.Height = 5;
Circle ent3 = new Circle(position,Vector3d.ZAxis,5);
Entity[] ents = new Entity;
ents = ent1;
ents = ent2;
ents = ent3;
ObjectIdCollection entsId = AppendEntity(ents);
i++;
return entsId;
}
public static ObjectIdCollection AppendEntity(params Entity[] ents)
{
Database db = HostApplicationServices.WorkingDatabase;
ObjectIdCollection objIds = new ObjectIdCollection();
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt, OpenMode.ForWrite);
foreach (var ent in ents)
{
ent.ColorIndex = 210;
objIds.Add(btr.AppendEntity(ent));
trans.AddNewlyCreatedDBObject(ent, true);
}
trans.Commit();
return objIds;
}
}
}
}转换坐标解决了~
页:
[1]