setUCS2Object(Point3d pnt3dBase0, Point3d pnt3dBaseX, Point3d pnt3dBaseY) { Vector3d vec3dXaxis = new Vector3d(pnt3dBaseX.X - pnt3dBase0.X, pnt3dBaseX.Y - pnt3dBase0.Y, pnt3dBaseX.Z - pnt3dBase0.Z); Vector3d vec3dYaxis = new Vector3d(pnt3dBaseY.X - pnt3dBase0.X, pnt3dBaseY.Y - pnt3dBase0.Y, pnt3dBaseY.Z - pnt3dBase0.Z); try { using (Transaction tr = BaseObjs.startTransactionDb()) { // Open the UCS table for read UcsTable acUCSTbl = getUcsTable(); UcsTableRecord acUCSTblRec; // Check to see if the "New_UCS" UCS table record exists if (acUCSTbl.Has("Temp") == false) { acUCSTblRec = new UcsTableRecord(); acUCSTblRec.Name = "Temp"; // Open the UCSTable for write acUCSTbl.UpgradeOpen(); // Add the new UCS table record acUCSTbl.Add(acUCSTblRec); tr.AddNewlyCreatedDBObject(acUCSTblRec, true); } else { acUCSTblRec = tr.GetObject(acUCSTbl["Temp"], OpenMode.ForWrite) as UcsTableRecord; } acUCSTblRec.Origin = pnt3dBase0; acUCSTblRec.XAxis = vec3dXaxis; acUCSTblRec.YAxis = vec3dYaxis; // Open the active viewport ViewportTableRecord acVportTblRec; acVportTblRec = tr.GetObject(BaseObjs._editor.ActiveViewportId, OpenMode.ForWrite) as ViewportTableRecord; // Set the UCS current acVportTblRec.SetUcs(acUCSTblRec.ObjectId); BaseObjs._editor.UpdateTiledViewportsFromDatabase(); tr.Commit(); } } catch (System.Exception ex) { BaseObjs.writeDebug(ex.Message + " UCsys.cs: line: 328"); } }
/// <summary> /// 创建一个新的UCS /// </summary> /// <param name="db">数据库对象</param> /// <param name="UCSName">要创建的UCS名称</param> /// <returns>返回创建的UCS的Id</returns> public static ObjectId AddUCS(this Database db, string UCSName) { var trans = db.TransactionManager; //打开UCS表 UcsTable ut = (UcsTable)trans.GetObject(db.UcsTableId, OpenMode.ForRead); if (!ut.Has(UCSName))//如果不存在名为UCSName的UCS,则新建一个UCS { //定义一个新的UCS UcsTableRecord utr = new UcsTableRecord(); utr.Name = UCSName; //设置UCS名 ut.UpgradeOpen(); //切换UCS表的状态为写以添加新的UCS //将UCS的信息添加到UCS表中 ut.Add(utr); //把UCS添加到事务处理中 trans.AddNewlyCreatedDBObject(utr, true); ut.DowngradeOpen(); //为了安全,将UCS表的状态切换为读 } return(ut[UCSName]); //返回新添加的UCS的ObjectId }
getUcsTableRecord(string name) { try { using (Transaction tr = BaseObjs.startTransactionDb()) { UcsTable UcsT = getUcsTable(); if (UcsT.Has(name) == true) { UcsTableRecord Ucstr = (UcsTableRecord)tr.GetObject(UcsT[name], OpenMode.ForRead); return(Ucstr); } tr.Commit(); } } catch (System.Exception ex) { BaseObjs.writeDebug(ex.Message + " UCsys.cs: line: 272"); } return(null); }
public static ObjectId OpenOrCreateUserCoordinateSystem(string ucsName, bool openIfExists = true) { using (Transaction trans = Tools.StartTransaction()) { // Open the UCS table for read UcsTable ucsTable = (UcsTable)trans.GetObject(Tools.GetAcadDatabase().UcsTableId, OpenMode.ForRead); UcsTableRecord ucsTblRec; // Check to see if the "New_UCS" UCS table record exists if (ucsTable.Has(ucsName) == false) { ucsTblRec = new UcsTableRecord(); ucsTblRec.Name = ucsName; // Open the UCSTable for write ucsTable.UpgradeOpen(); // Add the new UCS table record ucsTable.Add(ucsTblRec); trans.AddNewlyCreatedDBObject(ucsTblRec, true); ucsTblRec.Dispose(); } else { if (openIfExists) { ucsTblRec = trans.GetObject(ucsTable[ucsName], OpenMode.ForWrite) as UcsTableRecord; } else { throw new ArgumentException(string.Format("\nA UCS with this name \"{0}\" alredy exists", ucsName)); } } return(ucsTblRec.Id); } }
public void CreateUCS() { Database db = HostApplicationServices.WorkingDatabase; Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; using (Transaction trans = db.TransactionManager.StartTransaction()) { UcsTable ut = (UcsTable)trans.GetObject(db.UcsTableId, OpenMode.ForWrite); String ucsName = "myUCS"; if (ut.Has(ucsName) == false) { UcsTableRecord utr = new UcsTableRecord(); utr.Name = ucsName; utr.Origin = new Point3d(0, 0, 0); utr.XAxis = new Vector3d(0, 1, 0); utr.YAxis = new Vector3d(-1, 0, 0); Matrix3d mt = Matrix3d.AlignCoordinateSystem(Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis, utr.Origin, utr.XAxis, utr.YAxis, utr.XAxis.CrossProduct(utr.YAxis)); ed.CurrentUserCoordinateSystem = mt; } trans.Commit(); } }
/// <summary> /// 将UCS表中已经存在的一个UCS设置为当前UCS /// </summary> /// <param name="db">数据库对象</param> /// <param name="UCSName">UCS名称</param> /// <returns>如果设置成功返回true,否则返回false</returns> public static bool SetCurrentUCS(this Database db, string UCSName) { var trans = db.TransactionManager; Editor ed = db.GetEditor(); //打开UCS表 UcsTable ut = (UcsTable)trans.GetObject(db.UcsTableId, OpenMode.ForRead); //如果不存在名为UCSName的UCS,则返回 if (!ut.Has(UCSName)) { return(false); } //打开当前活动的视口为写的状态 ViewportTableRecord vtr = (ViewportTableRecord)trans.GetObject(db.CurrentViewportTableRecordId(), OpenMode.ForWrite); //设置当前UCS vtr.SetUcs(ut[UCSName]); vtr.DowngradeOpen(); //更新视口 ed.UpdateTiledViewportsFromDatabase(); return(true); }
addUCS(Point3d pnt3dOrigin, Point3d pnt3dXaxis, Point3d pnt3dYaxis, string nameUCS) { Matrix3d newMatrix = new Matrix3d(); try { using (Transaction tr = BaseObjs.startTransactionDb()) { // Open the UCS table for read UcsTable ucsT = tr.GetObject(BaseObjs._db.UcsTableId, OpenMode.ForRead) as UcsTable; UcsTableRecord ucstr; Vector3d vector3dXaxis = new Vector3d(pnt3dXaxis.X - pnt3dOrigin.X, pnt3dXaxis.Y - pnt3dOrigin.Y, pnt3dXaxis.Z - pnt3dOrigin.Z); Vector3d vector3dYaxis = new Vector3d(pnt3dYaxis.X - pnt3dOrigin.X, pnt3dYaxis.Y - pnt3dOrigin.Y, pnt3dYaxis.Z - pnt3dOrigin.Z); Vector3d vector3dZ = vector3dXaxis.CrossProduct(vector3dYaxis); Vector3d vector3dY = vector3dZ.CrossProduct(vector3dXaxis); Vector3d vector3dPerpY = new Vector3d(vector3dY.X + pnt3dOrigin.X, vector3dY.Y + pnt3dOrigin.Y, vector3dY.Z + pnt3dOrigin.Z); // Check to see if the nameUCS UCS table record exists if (ucsT.Has(nameUCS) == false) { ucstr = new UcsTableRecord(); ucstr.Name = nameUCS; // Open the UCSTable for write ucsT.UpgradeOpen(); // Add the new UCS table record ucsT.Add(ucstr); tr.AddNewlyCreatedDBObject(ucstr, true); } else { ucstr = (UcsTableRecord)tr.GetObject(ucsT[nameUCS], OpenMode.ForWrite); } ucstr.Origin = pnt3dOrigin; ucstr.XAxis = vector3dXaxis; ucstr.YAxis = vector3dPerpY; // Open the active viewport ViewportTableRecord vptr; vptr = (ViewportTableRecord)tr.GetObject(BaseObjs._editor.ActiveViewportId, OpenMode.ForWrite); // Display the UCS Icon at the origin of the current viewport vptr.IconAtOrigin = true; vptr.IconEnabled = true; // Set the UCS current vptr.SetUcs(ucstr.ObjectId); BaseObjs._editor.UpdateTiledViewportsFromDatabase(); newMatrix = Matrix3d.AlignCoordinateSystem(Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis, vptr.Ucs.Origin, vptr.Ucs.Xaxis, vptr.Ucs.Yaxis, vptr.Ucs.Zaxis); tr.Commit(); } } catch (System.Exception ex) { BaseObjs.writeDebug(ex.Message + " UCsys.cs: line: 149"); } return(newMatrix); }
private static bool SwitchUCS(Point3d cornerCoords, double textAngle) { // Get the current document and database Document acDoc = Application.DocumentManager.MdiActiveDocument; Database acCurrDb = acDoc.Database; Editor acEditor = acDoc.Editor; using (Transaction acTrans = acCurrDb.TransactionManager.StartTransaction()) { try { UcsTable acUCSTbl = acTrans.GetObject(acCurrDb.UcsTableId, OpenMode.ForRead) as UcsTable; UcsTableRecord jppUCSTblRec; // Check to see if JPP App UCS table record exists and create if not if (acUCSTbl.Has("JPP_App_UCS") == false) { jppUCSTblRec = new UcsTableRecord(); jppUCSTblRec.Name = "JPP_App_UCS"; acUCSTbl.UpgradeOpen(); acUCSTbl.Add(jppUCSTblRec); acTrans.AddNewlyCreatedDBObject(jppUCSTblRec, true); } else { jppUCSTblRec = acTrans.GetObject(acUCSTbl["JPP_App_UCS"], OpenMode.ForWrite) as UcsTableRecord; } jppUCSTblRec.Origin = cornerCoords; jppUCSTblRec.XAxis = cornerCoords.GetVectorTo(new Point3d(cornerCoords.X + Math.Cos(textAngle), cornerCoords.Y + Math.Sin(textAngle), 0.0)); jppUCSTblRec.YAxis = cornerCoords.GetVectorTo(new Point3d(cornerCoords.X - Math.Sin(textAngle), cornerCoords.Y + Math.Cos(textAngle), 0.0)); // Open the active viewport ViewportTableRecord acVportTblRec = acTrans.GetObject(acEditor.ActiveViewportId, OpenMode.ForWrite) as ViewportTableRecord; // Display the UCS Icon as the origin of the current viewport // acVportTblRec.IconAtOrigin = true; // acVportTblRec.IconEnabled = true; // Set the UCS current acVportTblRec.SetUcs(jppUCSTblRec.ObjectId); acEditor.UpdateTiledViewportsFromDatabase(); // Display the name of the current UCS UcsTableRecord acUCSTblRecActive = acTrans.GetObject(acVportTblRec.UcsName, OpenMode.ForRead) as UcsTableRecord; acEditor.WriteMessage("\nThe current UCS is: {0}", acUCSTblRecActive.Name); // Save the new objects to the database acTrans.Commit(); return(true); } catch (Autodesk.AutoCAD.Runtime.Exception acException) { Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog ("The following exception was caught: \n" + acException.Message + "\nError creating UCS!"); acTrans.Abort(); return(false); } } }