工作坐标系
附上代码:public void NCS()
{
//获得当前文档和数据库,并启动一个事务
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读的方式打开UCS表
UcsTable acUCSTbl;
acUCSTbl = acTrans.GetObject(acCurDb.UcsTableId, OpenMode.ForRead) as UcsTable;
// 以只读方式打开视图表Open the View table for read
UcsTableRecord acUCSTblRec;
//检查看“New_UCS”用户坐标系表记录是否存在
if (acUCSTbl.Has("Layout_UCS") == false)
{
acUCSTblRec = new UcsTableRecord();
acUCSTblRec.Name = "Layout_UCS";
//以写的方式打开UCSTable
acUCSTbl.UpgradeOpen();
//添加新的 UCS 表记录
acUCSTbl.Add(acUCSTblRec);
acTrans.AddNewlyCreatedDBObject(acUCSTblRec, true);
}
else
{
acUCSTblRec = acTrans.GetObject(acUCSTbl["Layout_UCS"],
OpenMode.ForWrite) as UcsTableRecord;
}
acUCSTblRec.Origin = new Point3d(100, 100, 0);
acUCSTblRec.XAxis = new Vector3d(1, 0, 0);
acUCSTblRec.YAxis = new Vector3d(0, 1, 0);
// 以只读方式打开视口表
ViewportTable acVportTbl;
acVportTbl = acTrans.GetObject(acCurDb.ViewportTableId,
OpenMode.ForRead) as ViewportTable;
// 以写的方式打开视图表
acVportTbl.UpgradeOpen();
// 遍历符号表的每一个对象
foreach (ObjectId acObjId in acVportTbl)
{
//以写方式打开对象
ViewportTableRecord acVportTblRec;
acVportTblRec = acTrans.GetObject(acObjId, OpenMode.ForWrite) as ViewportTableRecord;
// 判断是否是活动窗口,如果是就删除See if it is one of the active viewports, and if so erase it
if (acVportTblRec.Name == "*Active")
{
acVportTblRec.IconAtOrigin = true;
acVportTblRec.IconEnabled = true;
acVportTblRec.SetUcs(acUCSTblRec.ObjectId);
acDoc.Editor.UpdateTiledViewportsFromDatabase();
UcsTableRecord acUCSTblRecActive;
acUCSTblRecActive = acTrans.GetObject(acVportTblRec.UcsName,
OpenMode.ForRead) as UcsTableRecord;
CAD.CurrentEditor.WriteMessage("当前坐标系为: " + acUCSTblRecActive.Name);
Point3d center = new Point3d(0, 0, 0); //定义圆的中心
double radius = 50;
Circle circle = new Circle(center, new Vector3d(0.0, 0.0, 1.0), radius);
//获得AutoCAD块表,AutoCAD将加入到图形中的对象的信息都放在这个表中
BlockTable bt = (BlockTable)CAD.CurrentTransactionManager.GetObject(CAD.CurrentDatabase.BlockTableId, OpenMode.ForRead, false);
//获得块表记录
BlockTableRecord btr = (BlockTableRecord)
CAD.CurrentTransactionManager.GetObject(bt, OpenMode.ForWrite, false);
//向块表记录加入圆的相关信息
btr.AppendEntity(circle);
//向AutoCAD加入圆
CAD.CurrentTransactionManager.AddNewlyCreatedDBObject(circle, true);
// 保存新对象到数据库中
acTrans.Commit();
}
}
}
}
Vector3d tVec1 = acDoc.Database.Ucsorg.GetAsVector(); 或 Vector3d tVec1 =new Vector3d(100, 1000, 0);
Circle circle = new Circle(center + tVec1 , new Vector3d(0.0, 0.0, 1.0), radius);
对于实体的操作使用的应该都是世界坐标系WCS
谢谢兄弟帮忙! 本帖最后由 zzrxt 于 2011-11-5 09:49 编辑
我在建坐标系时也出现相同问题,请各位帮帮忙啊 ,刚学二次开发:
1.以wcs为基准,新建ucs1
2.以ucs1为基准,新建ucs2
3.相同代码,ucs1位置正确了,但ucs2就不对了,它的圆心位置仍然是以wcs为准了,而我想以ucs1为基准。
public static void DrawingLayout(double[] orgPt, double length, double width, double height)
{
//初始化绝对坐标系
IniLayout.TCS("ABS", new double { 0, 0, 0 });
CAD.CreateCircle(new double { 0, 0, 0, 50 });
//初始化坐标系ucs1:
IniLayout.TCS("ucs1", orgPt);
CAD.CreateCircle(new double { 0, 0, 0, 50 });
//初始化坐标系ucs2:
double[] p = new double;
p = 1000 + length / 2;
p = 1000 + width / 2;
p = 0;
IniLayout.TCS("ucs2", p);
CAD.CreateCircle(new double { 0, 0, 0, 50 });
}
/// <summary>
/// 给定原点,创建坐标系
/// </summary>
public static void TCS(String ucsName, double[] p)
{
using (DocumentLock docLock = CAD.CurrentDocument.LockDocument()) //注意此句,如果自定义对话框与CAD交互,一定要锁定文档
{
//获得当前文档和数据库,并启动一个事务,开始事务处理
using (Transaction acTrans = CAD.CurrentDatabase.TransactionManager.StartTransaction())
{
//以读的方式打开UCS表
UcsTable acUCSTbl;
acUCSTbl = acTrans.GetObject(CAD.CurrentDatabase.UcsTableId, OpenMode.ForRead) as UcsTable;
//检查看0cs用户坐标系表记录是否存在
try
{
UcsTableRecord acUCSTblRec;
if (acUCSTbl.Has(ucsName) == false)
{
acUCSTblRec = new UcsTableRecord();
acUCSTblRec.Name = ucsName;
acUCSTbl.UpgradeOpen();
//添加新的 UCS 表记录
acUCSTbl.Add(acUCSTblRec);
acTrans.AddNewlyCreatedDBObject(acUCSTblRec, true);
}
else
{
acUCSTblRec = acTrans.GetObject(acUCSTbl, OpenMode.ForWrite) as UcsTableRecord;
}
CAD.CurrentEditor.WriteMessage(p.ToString() + ">" + p.ToString() + "\n");
acUCSTblRec.Origin = new Point3d(p, p, p);
acUCSTblRec.XAxis = new Vector3d(1, 0, 0);
acUCSTblRec.YAxis = new Vector3d(0, 1, 0);
// 以只读方式打开视口表
ViewportTable acVportTbl;
acVportTbl = acTrans.GetObject(CAD.CurrentDatabase.ViewportTableId,
OpenMode.ForRead) as ViewportTable;
// 以写的方式打开视图表
acVportTbl.UpgradeOpen();
// 遍历符号表的每一个对象
foreach (ObjectId acObjId in acVportTbl)
{
//以写方式打开对象
ViewportTableRecord acVportTblRec;
acVportTblRec = acTrans.GetObject(acObjId, OpenMode.ForWrite) as ViewportTableRecord;
// 判断是否是活动窗口
if (acVportTblRec.Name == "*Active")
{
acVportTblRec.IconAtOrigin = true;
acVportTblRec.IconEnabled = true;
acVportTblRec.SetUcs(acUCSTblRec.ObjectId);
CAD.CurrentDocument.Editor.UpdateTiledViewportsFromDatabase();
UcsTableRecord acUCSTblRecActive;
acUCSTblRecActive = acTrans.GetObject(acVportTblRec.UcsName,
OpenMode.ForRead) as UcsTableRecord;
CAD.CurrentEditor.WriteMessage("\n当前坐标系为: " + acUCSTblRecActive.Name);
// 保存新对象到数据库中
acTrans.Commit();
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
}
}
public static ObjectId CreateCircle(double[] p)
{
ObjectId entId = ObjectId.Null;
using (DocumentLock docLock = CAD.CurrentDocument.LockDocument())
{
CAD.CurrentEditor.WriteMessage("\n" + p.ToString()+" "+p.ToString()+" "+ p.ToString()+" " + p.ToString()+"\n");
Point3d center = new Point3d(p, p, p);
Vector3d tVec1 = CAD.CurrentDocument.Database.Ucsorg.GetAsVector();
Circle circle = new Circle(center+tVec1, new Vector3d(0.0, 0.0, 1.0),p);
Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
//开始事务处理,也就是往CAD中加入东西
Transaction trans = CAD.CurrentTransactionManager.StartTransaction();
//获得AutoCAD块表,AutoCAD将加入到图形中的对象的信息都放在这个表中
BlockTable bt = (BlockTable)CAD.CurrentTransactionManager.GetObject(db.BlockTableId, OpenMode.ForRead, false);
//获得块表记录
BlockTableRecord btr = (BlockTableRecord)
CAD.CurrentTransactionManager.GetObject(bt, OpenMode.ForWrite, false);
//向块表记录加入圆的相关信息
entId = btr.AppendEntity(circle);
//向AutoCAD加入圆
CAD.CurrentTransactionManager.AddNewlyCreatedDBObject(circle, true);
trans.Commit();
}
return entId;
} 顶。。。求高人指点
页:
[1]